1 /* 2 * Copyright (C) 2023 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.hilt.processor.internal; 18 19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; 20 import static java.util.stream.Collectors.joining; 21 22 import androidx.room.compiler.processing.XExecutableElement; 23 import androidx.room.compiler.processing.XMethodElement; 24 import androidx.room.compiler.processing.XMethodType; 25 import androidx.room.compiler.processing.XType; 26 import com.google.auto.value.AutoValue; 27 import com.google.common.collect.ImmutableList; 28 import com.squareup.javapoet.MethodSpec; 29 import com.squareup.javapoet.TypeName; 30 import dagger.internal.codegen.xprocessing.XElements; 31 32 /** Represents the method signature needed to uniquely identify a method. */ 33 @AutoValue 34 public abstract class MethodSignature { MethodSignature()35 MethodSignature() {} 36 name()37 abstract String name(); 38 parameters()39 abstract ImmutableList<TypeName> parameters(); 40 41 /** Creates a {@link MethodSignature} from a method name and parameter {@link TypeNames} */ of(String methodName, TypeName... typeNames)42 public static MethodSignature of(String methodName, TypeName... typeNames) { 43 return new AutoValue_MethodSignature(methodName, ImmutableList.copyOf(typeNames)); 44 } 45 46 /** Creates a {@link MethodSignature} from a {@link MethodSpec} */ of(MethodSpec method)47 public static MethodSignature of(MethodSpec method) { 48 return new AutoValue_MethodSignature( 49 method.name, method.parameters.stream().map(p -> p.type).collect(toImmutableList())); 50 } 51 52 /** Creates a {@link MethodSignature} from an {@link XExecutableElement} */ of(XExecutableElement executableElement)53 public static MethodSignature of(XExecutableElement executableElement) { 54 return new AutoValue_MethodSignature( 55 XElements.getSimpleName(executableElement), 56 executableElement.getParameters().stream() 57 .map(p -> p.getType().getTypeName()) 58 .collect(toImmutableList())); 59 } 60 61 /** 62 * Creates a {@link MethodSignature} from an {@link XMethodElement}. 63 * 64 * <p>This version will resolve type parameters as declared by {@code enclosing}. 65 */ ofDeclaredType(XMethodElement method, XType enclosing)66 static MethodSignature ofDeclaredType(XMethodElement method, XType enclosing) { 67 XMethodType executableType = method.asMemberOf(enclosing); 68 return new AutoValue_MethodSignature( 69 XElements.getSimpleName(method), 70 executableType.getParameterTypes().stream() 71 .map(XType::getTypeName) 72 .collect(toImmutableList())); 73 } 74 75 /** Returns a string in the format: METHOD_NAME(PARAM_TYPE1,PARAM_TYEP2,...) */ 76 @Override toString()77 public final String toString() { 78 return String.format( 79 "%s(%s)", name(), parameters().stream().map(Object::toString).collect(joining(","))); 80 } 81 } 82