• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.xprocessing;
18 
19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
20 import static dagger.internal.codegen.xprocessing.JavaPoetExt.toParameterSpec;
21 import static javax.lang.model.element.Modifier.PROTECTED;
22 import static javax.lang.model.element.Modifier.PUBLIC;
23 
24 import androidx.room.compiler.processing.XExecutableParameterElement;
25 import androidx.room.compiler.processing.XMethodElement;
26 import androidx.room.compiler.processing.XMethodType;
27 import androidx.room.compiler.processing.XType;
28 import com.squareup.javapoet.AnnotationSpec;
29 import com.squareup.javapoet.MethodSpec;
30 
31 // TODO(bcorso): Consider moving these methods into XProcessing library.
32 /** A utility class for {@link MethodSpec} helper methods. */
33 public final class MethodSpecs {
34 
35   /** Returns a {@link MethodSpec} that overrides the given method. */
overriding(XMethodElement method, XType owner)36   public static MethodSpec.Builder overriding(XMethodElement method, XType owner) {
37     XMethodType methodType = method.asMemberOf(owner);
38     Nullability nullability = Nullability.of(method);
39     MethodSpec.Builder builder =
40         // We're overriding the method so we have to use the jvm name here.
41         MethodSpec.methodBuilder(method.getJvmName())
42             .addAnnotation(Override.class)
43             .addAnnotations(
44                 nullability.nonTypeUseNullableAnnotations().stream()
45                     .map(AnnotationSpec::builder)
46                     .map(AnnotationSpec.Builder::build)
47                     .collect(toImmutableList()))
48             .addTypeVariables(methodType.getTypeVariableNames())
49             .varargs(method.isVarArgs())
50             .returns(
51                 methodType
52                     .getReturnType()
53                     .getTypeName()
54                     .annotated(
55                         nullability.typeUseNullableAnnotations().stream()
56                             .map(AnnotationSpec::builder)
57                             .map(AnnotationSpec.Builder::build)
58                             .collect(toImmutableList())));
59     if (method.isPublic()) {
60       builder.addModifiers(PUBLIC);
61     } else if (method.isProtected()) {
62       builder.addModifiers(PROTECTED);
63     }
64     for (int i = 0; i < methodType.getParameterTypes().size(); i++) {
65       XExecutableParameterElement parameter = method.getParameters().get(i);
66       XType parameterType = methodType.getParameterTypes().get(i);
67       builder.addParameter(toParameterSpec(parameter, parameterType));
68     }
69     method.getThrownTypes().stream().map(XType::getTypeName).forEach(builder::addException);
70     return builder;
71   }
72 
MethodSpecs()73   private MethodSpecs() {}
74 }
75