1 /*
2  * Copyright (C) 2014 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.internal.codegen.componentgenerator;
18 
19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
20 
21 import com.google.auto.common.MoreTypes;
22 import com.google.auto.value.AutoValue;
23 import com.google.common.base.Equivalence;
24 import com.google.common.collect.ImmutableList;
25 import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
26 import dagger.internal.codegen.langmodel.DaggerTypes;
27 import java.util.List;
28 import javax.lang.model.type.DeclaredType;
29 import javax.lang.model.type.ExecutableType;
30 import javax.lang.model.type.TypeMirror;
31 
32 /** A class that defines proper {@code equals} and {@code hashcode} for a method signature. */
33 @AutoValue
34 abstract class MethodSignature {
35 
name()36   abstract String name();
37 
parameterTypes()38   abstract ImmutableList<? extends Equivalence.Wrapper<? extends TypeMirror>> parameterTypes();
39 
thrownTypes()40   abstract ImmutableList<? extends Equivalence.Wrapper<? extends TypeMirror>> thrownTypes();
41 
forComponentMethod( ComponentMethodDescriptor componentMethod, DeclaredType componentType, DaggerTypes types)42   static MethodSignature forComponentMethod(
43       ComponentMethodDescriptor componentMethod, DeclaredType componentType, DaggerTypes types) {
44     ExecutableType methodType =
45         MoreTypes.asExecutable(types.asMemberOf(componentType, componentMethod.methodElement()));
46     return new AutoValue_MethodSignature(
47         componentMethod.methodElement().getSimpleName().toString(),
48         wrapInEquivalence(methodType.getParameterTypes()),
49         wrapInEquivalence(methodType.getThrownTypes()));
50   }
51 
52   private static ImmutableList<? extends Equivalence.Wrapper<? extends TypeMirror>>
wrapInEquivalence(List<? extends TypeMirror> types)53       wrapInEquivalence(List<? extends TypeMirror> types) {
54     return types.stream().map(MoreTypes.equivalence()::wrap).collect(toImmutableList());
55   }
56 }
57