/* * Copyright (C) 2014 The Dagger Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dagger.internal.codegen; import static com.squareup.javapoet.MethodSpec.constructorBuilder; import static com.squareup.javapoet.MethodSpec.methodBuilder; import static com.squareup.javapoet.TypeSpec.classBuilder; import static dagger.internal.codegen.AnnotationExpression.createMethodName; import static dagger.internal.codegen.AnnotationExpression.getAnnotationCreatorClassName; import static dagger.internal.codegen.javapoet.CodeBlocks.makeParametersCodeBlock; import static javax.lang.model.element.Modifier.FINAL; import static javax.lang.model.element.Modifier.PRIVATE; import static javax.lang.model.element.Modifier.PUBLIC; import static javax.lang.model.element.Modifier.STATIC; import static javax.lang.model.util.ElementFilter.methodsIn; import com.google.auto.common.MoreTypes; import com.google.common.collect.ImmutableList; import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.MethodSpec; import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import dagger.internal.codegen.langmodel.DaggerElements; import java.util.LinkedHashSet; import java.util.Optional; import java.util.Set; import javax.annotation.processing.Filer; import javax.inject.Inject; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.util.SimpleTypeVisitor6; /** * Generates classes that create annotation instances for an annotation type. The generated class * will have a private empty constructor, a static method that creates the annotation type itself, * and a static method that creates each annotation type that is nested in the top-level annotation * type. * *
So for an example annotation: * *
* {@literal @interface} Foo {
* String s();
* int i();
* Bar bar(); // an annotation defined elsewhere
* }
*
*
* the generated class will look like:
*
*
* public final class FooCreator {
* private FooCreator() {}
*
* public static Foo createFoo(String s, int i, Bar bar) { … }
* public static Bar createBar(…) { … }
* }
*
*/
class AnnotationCreatorGenerator extends SourceFileGenerator