diff --git a/compiler/src/main/java/dagger/internal/codegen/ProcessorJavadocs.java b/compiler/src/main/java/dagger/internal/codegen/AdapterJavadocs.java similarity index 96% rename from compiler/src/main/java/dagger/internal/codegen/ProcessorJavadocs.java rename to compiler/src/main/java/dagger/internal/codegen/AdapterJavadocs.java index 51bc2283554..3312f1e52d2 100644 --- a/compiler/src/main/java/dagger/internal/codegen/ProcessorJavadocs.java +++ b/compiler/src/main/java/dagger/internal/codegen/AdapterJavadocs.java @@ -16,10 +16,10 @@ package dagger.internal.codegen; /** - * Utility class providing some commonly used boilerplate between {@code InjectProcessor} - * and {@code ProvidesProcessor}. + * Utility class providing some commonly used boilerplate between {@code InjectAdapterProcessor} + * and {@code ModuleAdapterProcessor}. */ -public final class ProcessorJavadocs { +public final class AdapterJavadocs { static final String GENERATED_BY_DAGGER = "Code generated by dagger-compiler. Do not edit."; static final String MEMBERS_INJECT_METHOD = "" + "Injects any {@code @Inject} annotated fields in the given instance,\n" diff --git a/compiler/src/main/java/dagger/internal/codegen/GeneratorKeys.java b/compiler/src/main/java/dagger/internal/codegen/GeneratorKeys.java index 75c9e7fbfc9..838fa9cb2c3 100644 --- a/compiler/src/main/java/dagger/internal/codegen/GeneratorKeys.java +++ b/compiler/src/main/java/dagger/internal/codegen/GeneratorKeys.java @@ -25,6 +25,9 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; +import static dagger.internal.codegen.TypeUtils.rawTypeToString; +import static dagger.internal.codegen.TypeUtils.typeToString; + /** * Creates keys using javac's mirror APIs. Unlike {@code Keys}, this class uses * APIs not available on Android. @@ -41,13 +44,13 @@ private GeneratorKeys() { * generated code. */ public static String rawMembersKey(TypeMirror type) { - return "members/" + CodeGen.rawTypeToString(type, '$'); + return "members/" + rawTypeToString(type, '$'); } /** Returns the provider key for {@code type}. */ public static String get(TypeMirror type) { StringBuilder result = new StringBuilder(); - CodeGen.typeToString(type, result, '$'); + typeToString(type, result, '$'); return result.toString(); } @@ -58,7 +61,7 @@ public static String get(ExecutableElement method) { if (qualifier != null) { qualifierToString(qualifier, result); } - CodeGen.typeToString(method.getReturnType(), result, '$'); + typeToString(method.getReturnType(), result, '$'); return result.toString(); } @@ -70,7 +73,7 @@ public static String getSetKey(ExecutableElement method) { qualifierToString(qualifier, result); } result.append(SET_PREFIX); - CodeGen.typeToString(method.getReturnType(), result, '$'); + typeToString(method.getReturnType(), result, '$'); result.append(">"); return result.toString(); } @@ -82,14 +85,14 @@ public static String get(VariableElement variable) { if (qualifier != null) { qualifierToString(qualifier, result); } - CodeGen.typeToString(variable.asType(), result, '$'); + typeToString(variable.asType(), result, '$'); return result.toString(); } private static void qualifierToString(AnnotationMirror qualifier, StringBuilder result) { // TODO: guarantee that element values are sorted by name (if there are multiple) result.append('@'); - CodeGen.typeToString(qualifier.getAnnotationType(), result, '$'); + typeToString(qualifier.getAnnotationType(), result, '$'); result.append('('); for (Map.Entry entry : qualifier.getElementValues().entrySet()) { diff --git a/compiler/src/main/java/dagger/internal/codegen/ReportingErrorHandler.java b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisErrorHandler.java similarity index 89% rename from compiler/src/main/java/dagger/internal/codegen/ReportingErrorHandler.java rename to compiler/src/main/java/dagger/internal/codegen/GraphAnalysisErrorHandler.java index 74b98fbd74e..121bbaebdf7 100644 --- a/compiler/src/main/java/dagger/internal/codegen/ReportingErrorHandler.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisErrorHandler.java @@ -25,11 +25,11 @@ * A {@code Linker.ErrorHandler} which gathers errors and reports them via a processing * environment. */ -final class ReportingErrorHandler implements Linker.ErrorHandler { +final class GraphAnalysisErrorHandler implements Linker.ErrorHandler { private final ProcessingEnvironment processingEnv; private final String moduleName; - ReportingErrorHandler(ProcessingEnvironment processingEnv, String moduleName) { + GraphAnalysisErrorHandler(ProcessingEnvironment processingEnv, String moduleName) { this.processingEnv = processingEnv; this.moduleName = moduleName; } diff --git a/compiler/src/main/java/dagger/internal/codegen/AtInjectBinding.java b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisInjectBinding.java similarity index 88% rename from compiler/src/main/java/dagger/internal/codegen/AtInjectBinding.java rename to compiler/src/main/java/dagger/internal/codegen/GraphAnalysisInjectBinding.java index 40488d3df1c..d4e073fea39 100644 --- a/compiler/src/main/java/dagger/internal/codegen/AtInjectBinding.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisInjectBinding.java @@ -30,17 +30,18 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; +import static dagger.internal.codegen.TypeUtils.getApplicationSupertype; + /** * A build time binding that injects the constructor and fields of a class. */ -final class AtInjectBinding extends Binding { +final class GraphAnalysisInjectBinding extends Binding { private final TypeElement type; private final List keys; private final Binding[] bindings; private final String supertypeKey; - private Binding supertypeBinding; - private AtInjectBinding(String provideKey, String membersKey, + private GraphAnalysisInjectBinding(String provideKey, String membersKey, TypeElement type, List keys, String supertypeKey) { super(provideKey, membersKey, type.getAnnotation(Singleton.class) != null, type.getQualifiedName().toString()); @@ -50,7 +51,7 @@ private AtInjectBinding(String provideKey, String membersKey, this.supertypeKey = supertypeKey; } - static AtInjectBinding create(TypeElement type, boolean mustHaveInjections) { + static GraphAnalysisInjectBinding create(TypeElement type, boolean mustHaveInjections) { List requiredKeys = new ArrayList(); boolean hasInjectConstructor = false; boolean hasNoArgsConstructor = false; @@ -94,7 +95,7 @@ static AtInjectBinding create(TypeElement type, boolean mustHaveInjections) { } // Attach the supertype. - TypeMirror supertype = CodeGen.getApplicationSupertype(type); + TypeMirror supertype = getApplicationSupertype(type); String supertypeKey = supertype != null ? GeneratorKeys.rawMembersKey(supertype) : null; @@ -103,7 +104,7 @@ static AtInjectBinding create(TypeElement type, boolean mustHaveInjections) { ? GeneratorKeys.get(type.asType()) : null; String membersKey = GeneratorKeys.rawMembersKey(type.asType()); - return new AtInjectBinding(provideKey, membersKey, type, requiredKeys, supertypeKey); + return new GraphAnalysisInjectBinding(provideKey, membersKey, type, requiredKeys, supertypeKey); } private static boolean hasAtInject(Element enclosed) { @@ -117,8 +118,8 @@ private static boolean hasAtInject(Element enclosed) { getClass().getClassLoader()); } if (supertypeKey != null) { - supertypeBinding = linker.requestBinding(supertypeKey, requiredBy, - getClass().getClassLoader(), false, true); + // Force the binding lookup. + linker.requestBinding(supertypeKey, requiredBy, getClass().getClassLoader(), false, true); } } diff --git a/compiler/src/main/java/dagger/internal/codegen/CompileTimeLoader.java b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisLoader.java similarity index 92% rename from compiler/src/main/java/dagger/internal/codegen/CompileTimeLoader.java rename to compiler/src/main/java/dagger/internal/codegen/GraphAnalysisLoader.java index a7b40582e47..e35e84d161b 100644 --- a/compiler/src/main/java/dagger/internal/codegen/CompileTimeLoader.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisLoader.java @@ -29,11 +29,11 @@ * {@link Binding#get} or {@link Binding#injectMembers} methods. They are only suitable * for graph analysis and error detection. */ -public final class CompileTimeLoader implements Loader { +public final class GraphAnalysisLoader implements Loader { private final ProcessingEnvironment processingEnv; - public CompileTimeLoader(ProcessingEnvironment processingEnv) { + public GraphAnalysisLoader(ProcessingEnvironment processingEnv) { this.processingEnv = processingEnv; } @@ -51,7 +51,7 @@ public CompileTimeLoader(ProcessingEnvironment processingEnv) { if (type.getKind() == ElementKind.INTERFACE) { return null; } - return AtInjectBinding.create(type, mustHaveInjections); + return GraphAnalysisInjectBinding.create(type, mustHaveInjections); } @Override public ModuleAdapter getModuleAdapter(Class moduleClass, T module) { diff --git a/compiler/src/main/java/dagger/internal/codegen/FullGraphProcessor.java b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisProcessor.java similarity index 90% rename from compiler/src/main/java/dagger/internal/codegen/FullGraphProcessor.java rename to compiler/src/main/java/dagger/internal/codegen/GraphAnalysisProcessor.java index 512e75fbada..0c70f965e86 100644 --- a/compiler/src/main/java/dagger/internal/codegen/FullGraphProcessor.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisProcessor.java @@ -51,11 +51,16 @@ import javax.tools.JavaFileManager; import javax.tools.StandardLocation; +import static dagger.internal.codegen.TypeUtils.getAnnotation; +import static dagger.internal.codegen.TypeUtils.getPackage; +import static dagger.internal.codegen.TypeUtils.isInterface; +import static dagger.internal.codegen.TypeUtils.methodName; + /** * Performs full graph analysis on a module. */ @SupportedAnnotationTypes("dagger.Module") -public final class FullGraphProcessor extends AbstractProcessor { +public final class GraphAnalysisProcessor extends AbstractProcessor { private final Set delayedModuleNames = new LinkedHashSet(); @Override public SourceVersion getSupportedSourceVersion() { @@ -86,7 +91,7 @@ public final class FullGraphProcessor extends AbstractProcessor { } for (Element element : modules) { - Map annotation = CodeGen.getAnnotation(Module.class, element); + Map annotation = getAnnotation(Module.class, element); TypeElement moduleType = (TypeElement) element; if (annotation.get("complete").equals(Boolean.TRUE)) { @@ -132,11 +137,12 @@ private Map> processCompleteModule(TypeElement rootModule, boolean ignoreCompletenessErrors) { Map allModules = new LinkedHashMap(); collectIncludesRecursively(rootModule, allModules, new LinkedList()); - ArrayList staticInjections = new ArrayList(); + ArrayList staticInjections = + new ArrayList(); Linker.ErrorHandler errorHandler = ignoreCompletenessErrors ? Linker.ErrorHandler.NULL - : new ReportingErrorHandler(processingEnv, rootModule.getQualifiedName().toString()); - Linker linker = new Linker(null, new CompileTimeLoader(processingEnv), errorHandler); + : new GraphAnalysisErrorHandler(processingEnv, rootModule.getQualifiedName().toString()); + Linker linker = new Linker(null, new GraphAnalysisLoader(processingEnv), errorHandler); // Linker requires synchronization for calls to requestBinding and linkAll. // We know statically that we're single threaded, but we synchronize anyway // to make the linker happy. @@ -144,7 +150,7 @@ private Map> processCompleteModule(TypeElement rootModule, Map> baseBindings = new LinkedHashMap>(); Map> overrideBindings = new LinkedHashMap>(); for (TypeElement module : allModules.values()) { - Map annotation = CodeGen.getAnnotation(Module.class, module); + Map annotation = getAnnotation(Module.class, module); boolean overrides = (Boolean) annotation.get("overrides"); boolean library = (Boolean) annotation.get("library"); Map> addTo = overrides ? overrideBindings : baseBindings; @@ -152,7 +158,7 @@ private Map> processCompleteModule(TypeElement rootModule, // Gather the injectable types from the annotation. for (Object injectableTypeObject : (Object[]) annotation.get("injects")) { TypeMirror injectableType = (TypeMirror) injectableTypeObject; - String key = CodeGen.isInterface(injectableType) + String key = isInterface(injectableType) ? GeneratorKeys.get(injectableType) : GeneratorKeys.rawMembersKey(injectableType); linker.requestBinding(key, module.getQualifiedName().toString(), @@ -163,7 +169,7 @@ private Map> processCompleteModule(TypeElement rootModule, for (Object staticInjection : (Object[]) annotation.get("staticInjections")) { TypeMirror staticInjectionTypeMirror = (TypeMirror) staticInjection; Element element = processingEnv.getTypeUtils().asElement(staticInjectionTypeMirror); - staticInjections.add(new CodeGenStaticInjection(element)); + staticInjections.add(new GraphAnalysisStaticInjection(element)); } // Gather the enclosed @Provides methods. @@ -208,7 +214,7 @@ private Map> processCompleteModule(TypeElement rootModule, linker.installBindings(baseBindings); linker.installBindings(overrideBindings); - for (CodeGenStaticInjection staticInjection : staticInjections) { + for (GraphAnalysisStaticInjection staticInjection : staticInjections) { staticInjection.attach(linker); } @@ -225,7 +231,7 @@ private String shortMethodName(ExecutableElement method) { void collectIncludesRecursively( TypeElement module, Map result, Deque path) { - Map annotation = CodeGen.getAnnotation(Module.class, module); + Map annotation = getAnnotation(Module.class, module); if (annotation == null) { // TODO(tbroyer): pass annotation information throw new ModuleValidationException("No @Module on " + module, module); @@ -253,7 +259,7 @@ void collectIncludesRecursively( result.put(name, module); // Recurse for each included module. - Types typeUtils = processingEnv.getTypeUtils(); + Types types = processingEnv.getTypeUtils(); List seedModules = new ArrayList(); seedModules.addAll(Arrays.asList((Object[]) annotation.get("includes"))); if (!annotation.get("addsTo").equals(Void.class)) seedModules.add(annotation.get("addsTo")); @@ -264,7 +270,7 @@ void collectIncludesRecursively( "Unexpected value for include: " + include + " in " + module, module); continue; } - TypeElement includedModule = (TypeElement) typeUtils.asElement((TypeMirror) include); + TypeElement includedModule = (TypeElement) types.asElement((TypeMirror) include); path.push(name); collectIncludesRecursively(includedModule, result, path); path.pop(); @@ -276,8 +282,7 @@ static class ProviderMethodBinding extends Binding { private final Binding[] parameters; protected ProviderMethodBinding(String provideKey, ExecutableElement method, boolean library) { - super(provideKey, null, method.getAnnotation(Singleton.class) != null, - CodeGen.methodName(method)); + super(provideKey, null, method.getAnnotation(Singleton.class) != null, methodName(method)); this.method = method; this.parameters = new Binding[method.getParameters().size()]; setLibrary(library); @@ -307,12 +312,12 @@ protected ProviderMethodBinding(String provideKey, ExecutableElement method, boo void writeDotFile(TypeElement module, Map> bindings) throws IOException { JavaFileManager.Location location = StandardLocation.SOURCE_OUTPUT; - String path = CodeGen.getPackage(module).getQualifiedName().toString(); + String path = getPackage(module).getQualifiedName().toString(); String file = module.getQualifiedName().toString().substring(path.length() + 1) + ".dot"; FileObject resource = processingEnv.getFiler().createResource(location, path, file, module); Writer writer = resource.openWriter(); - DotWriter dotWriter = new DotWriter(writer); + GraphVizWriter dotWriter = new GraphVizWriter(writer); new GraphVisualizer().write(bindings, dotWriter); dotWriter.close(); } diff --git a/compiler/src/main/java/dagger/internal/codegen/CodeGenStaticInjection.java b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisStaticInjection.java similarity index 83% rename from compiler/src/main/java/dagger/internal/codegen/CodeGenStaticInjection.java rename to compiler/src/main/java/dagger/internal/codegen/GraphAnalysisStaticInjection.java index 8615319b04d..5f4b96a8586 100644 --- a/compiler/src/main/java/dagger/internal/codegen/CodeGenStaticInjection.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphAnalysisStaticInjection.java @@ -20,17 +20,19 @@ import javax.inject.Inject; import javax.lang.model.element.Element; -public final class CodeGenStaticInjection extends StaticInjection { +import static dagger.internal.codegen.TypeUtils.isStatic; + +public final class GraphAnalysisStaticInjection extends StaticInjection { private final Element enclosingClass; - public CodeGenStaticInjection(Element enclosingClass) { + public GraphAnalysisStaticInjection(Element enclosingClass) { this.enclosingClass = enclosingClass; } @Override public void attach(Linker linker) { for (Element enclosedElement : enclosingClass.getEnclosedElements()) { - if (enclosedElement.getKind().isField() && CodeGen.isStatic(enclosedElement)) { + if (enclosedElement.getKind().isField() && isStatic(enclosedElement)) { Inject injectAnnotation = enclosedElement.getAnnotation(Inject.class); if (injectAnnotation != null) { String key = GeneratorKeys.get(enclosedElement.asType()); diff --git a/compiler/src/main/java/dagger/internal/codegen/GraphVisualizer.java b/compiler/src/main/java/dagger/internal/codegen/GraphVisualizer.java index 8d8070bd34a..a9a90fe8eda 100644 --- a/compiler/src/main/java/dagger/internal/codegen/GraphVisualizer.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphVisualizer.java @@ -42,7 +42,7 @@ public final class GraphVisualizer { + "((\\[\\])*)" // Arrays. Group 4. + ""); - public void write(Map> bindings, DotWriter writer) throws IOException { + public void write(Map> bindings, GraphVizWriter writer) throws IOException { Map, String> namesIndex = buildNamesIndex(bindings); writer.beginGraph("concentrate", "true"); diff --git a/compiler/src/main/java/dagger/internal/codegen/DotWriter.java b/compiler/src/main/java/dagger/internal/codegen/GraphVizWriter.java similarity index 97% rename from compiler/src/main/java/dagger/internal/codegen/DotWriter.java rename to compiler/src/main/java/dagger/internal/codegen/GraphVizWriter.java index 51b7978f73e..519309e76d8 100644 --- a/compiler/src/main/java/dagger/internal/codegen/DotWriter.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphVizWriter.java @@ -25,7 +25,7 @@ /** * Emits dot graphs. */ -public final class DotWriter implements Closeable { +public final class GraphVizWriter implements Closeable { private static final String INDENT = " "; private final Writer out; private int indent = 0; @@ -36,7 +36,7 @@ public final class DotWriter implements Closeable { * @param out the stream to which dot data will be written. This should be a * buffered stream. */ - public DotWriter(Writer out) { + public GraphVizWriter(Writer out) { this.out = out; } diff --git a/compiler/src/main/java/dagger/internal/codegen/InjectProcessor.java b/compiler/src/main/java/dagger/internal/codegen/InjectAdapterProcessor.java similarity index 88% rename from compiler/src/main/java/dagger/internal/codegen/InjectProcessor.java rename to compiler/src/main/java/dagger/internal/codegen/InjectAdapterProcessor.java index 4ab1a8d9e97..1c61996d2d0 100644 --- a/compiler/src/main/java/dagger/internal/codegen/InjectProcessor.java +++ b/compiler/src/main/java/dagger/internal/codegen/InjectAdapterProcessor.java @@ -22,6 +22,7 @@ import dagger.internal.StaticInjection; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashSet; @@ -45,8 +46,14 @@ import javax.tools.Diagnostic; import javax.tools.JavaFileObject; -import static dagger.internal.codegen.CodeGen.typeToString; -import static dagger.internal.codegen.ProcessorJavadocs.binderTypeDocs; +import static dagger.internal.codegen.AdapterJavadocs.binderTypeDocs; +import static dagger.internal.codegen.TypeUtils.adapterName; +import static dagger.internal.codegen.TypeUtils.getApplicationSupertype; +import static dagger.internal.codegen.TypeUtils.getNoArgsConstructor; +import static dagger.internal.codegen.TypeUtils.getPackage; +import static dagger.internal.codegen.TypeUtils.isCallableConstructor; +import static dagger.internal.codegen.TypeUtils.rawTypeToString; +import static dagger.internal.codegen.TypeUtils.typeToString; import static dagger.internal.loaders.generated.GeneratedAdapterLoader.INJECT_ADAPTER_SUFFIX; import static dagger.internal.loaders.generated.GeneratedAdapterLoader.STATIC_INJECTION_SUFFIX; import static java.lang.reflect.Modifier.FINAL; @@ -58,7 +65,7 @@ * {@literal @}{@code Inject}-annotated members of a class. */ @SupportedAnnotationTypes("javax.inject.Inject") -public final class InjectProcessor extends AbstractProcessor { +public final class InjectAdapterProcessor extends AbstractProcessor { private final Set remainingTypeNames = new LinkedHashSet(); @Override public SourceVersion getSupportedSourceVersion() { @@ -120,7 +127,7 @@ private Set getInjectedClassNames(RoundEnvironment env) { if (!validateInjectable(element)) { continue; } - injectedTypeNames.add(CodeGen.rawTypeToString(element.getEnclosingElement().asType(), '.')); + injectedTypeNames.add(rawTypeToString(element.getEnclosingElement().asType(), '.')); } return injectedTypeNames; } @@ -196,8 +203,8 @@ private InjectedClass getInjectedClass(String injectedClassName) { } if (constructor == null && !isAbstract) { - constructor = CodeGen.getNoArgsConstructor(type); - if (constructor != null && !CodeGen.isCallableConstructor(constructor)) { + constructor = getNoArgsConstructor(type); + if (constructor != null && !isCallableConstructor(constructor)) { constructor = null; } } @@ -218,10 +225,10 @@ private void error(String msg, Element element) { */ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, List fields) throws IOException { - String packageName = CodeGen.getPackage(type).getQualifiedName().toString(); + String packageName = getPackage(type).getQualifiedName().toString(); String strippedTypeName = strippedTypeName(type.getQualifiedName().toString(), packageName); - TypeMirror supertype = CodeGen.getApplicationSupertype(type); - String adapterName = CodeGen.adapterName(type, INJECT_ADAPTER_SUFFIX); + TypeMirror supertype = getApplicationSupertype(type); + String adapterName = adapterName(type, INJECT_ADAPTER_SUFFIX); JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(adapterName, type); JavaWriter writer = new JavaWriter(sourceFile.openWriter()); boolean isAbstract = type.getModifiers().contains(Modifier.ABSTRACT); @@ -232,7 +239,7 @@ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, boolean dependent = injectMembers || ((constructor != null) && !constructor.getParameters().isEmpty()); - writer.emitEndOfLineComment(ProcessorJavadocs.GENERATED_BY_DAGGER); + writer.emitEndOfLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); writer.emitPackage(packageName); writer.emitEmptyLine(); writer.emitImports(getImports(dependent, injectMembers, constructor != null)); @@ -246,18 +253,18 @@ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, if (constructor != null) { for (VariableElement parameter : constructor.getParameters()) { writer.emitField(JavaWriter.type(Binding.class, - CodeGen.typeToString(parameter.asType())), + typeToString(parameter.asType())), parameterName(disambiguateFields, parameter), PRIVATE); } } for (Element field : fields) { writer.emitField(JavaWriter.type(Binding.class, - CodeGen.typeToString(field.asType())), + typeToString(field.asType())), fieldName(disambiguateFields, field), PRIVATE); } if (supertype != null) { writer.emitField(JavaWriter.type(Binding.class, - CodeGen.rawTypeToString(supertype, '.')), "supertype", PRIVATE); + rawTypeToString(supertype, '.')), "supertype", PRIVATE); } writer.emitEmptyLine(); @@ -272,7 +279,7 @@ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, writer.endMethod(); if (dependent) { writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.ATTACH_METHOD); + writer.emitJavadoc(AdapterJavadocs.ATTACH_METHOD); writer.emitAnnotation(Override.class); writer.emitAnnotation(SuppressWarnings.class, JavaWriter.stringLiteral("unchecked")); writer.beginMethod("void", "attach", PUBLIC, Linker.class.getCanonicalName(), "linker"); @@ -299,15 +306,14 @@ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, "%s = (%s) linker.requestBinding(%s, %s.class, getClass().getClassLoader()" + ", false, true)", // Yep. This is a dumb line-length violation otherwise. "supertype", - writer.compressType(JavaWriter.type(Binding.class, - CodeGen.rawTypeToString(supertype, '.'))), + writer.compressType(JavaWriter.type(Binding.class, rawTypeToString(supertype, '.'))), JavaWriter.stringLiteral(GeneratorKeys.rawMembersKey(supertype)), strippedTypeName); } writer.endMethod(); writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.GET_DEPENDENCIES_METHOD); + writer.emitJavadoc(AdapterJavadocs.GET_DEPENDENCIES_METHOD); writer.emitAnnotation(Override.class); String setOfBindings = JavaWriter.type(Set.class, "Binding"); writer.beginMethod("void", "getDependencies", PUBLIC, setOfBindings, "getBindings", @@ -328,7 +334,7 @@ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, if (constructor != null) { writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.GET_METHOD, strippedTypeName); + writer.emitJavadoc(AdapterJavadocs.GET_METHOD, strippedTypeName); writer.emitAnnotation(Override.class); writer.beginMethod(strippedTypeName, "get", PUBLIC); StringBuilder newInstance = new StringBuilder(); @@ -351,7 +357,7 @@ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, if (injectMembers) { writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.MEMBERS_INJECT_METHOD, strippedTypeName); + writer.emitJavadoc(AdapterJavadocs.MEMBERS_INJECT_METHOD, strippedTypeName); writer.emitAnnotation(Override.class); writer.beginMethod("void", "injectMembers", PUBLIC, strippedTypeName, "object"); for (Element field : fields) { @@ -400,33 +406,32 @@ private String strippedTypeName(String type, String packageName) { */ private void writeStaticInjection(TypeElement type, List fields) throws IOException { String typeName = type.getQualifiedName().toString(); - String adapterName = CodeGen.adapterName(type, STATIC_INJECTION_SUFFIX); + String adapterName = adapterName(type, STATIC_INJECTION_SUFFIX); JavaFileObject sourceFile = processingEnv.getFiler() .createSourceFile(adapterName, type); JavaWriter writer = new JavaWriter(sourceFile.openWriter()); - writer.emitEndOfLineComment(ProcessorJavadocs.GENERATED_BY_DAGGER); - writer.emitPackage(CodeGen.getPackage(type).getQualifiedName().toString()); + writer.emitEndOfLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); + writer.emitPackage(getPackage(type).getQualifiedName().toString()); writer.emitEmptyLine(); - writer.emitImports(CodeGen.setOf( + writer.emitImports(Arrays.asList( StaticInjection.class.getName(), Binding.class.getName(), Linker.class.getName())); writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.STATIC_INJECTION_TYPE, type.getSimpleName()); + writer.emitJavadoc(AdapterJavadocs.STATIC_INJECTION_TYPE, type.getSimpleName()); writer.beginType(adapterName, "class", PUBLIC | FINAL, StaticInjection.class.getSimpleName()); for (Element field : fields) { - writer.emitField(JavaWriter.type(Binding.class, - CodeGen.typeToString(field.asType())), + writer.emitField(JavaWriter.type(Binding.class, typeToString(field.asType())), fieldName(false, field), PRIVATE); } writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.ATTACH_METHOD); + writer.emitJavadoc(AdapterJavadocs.ATTACH_METHOD); writer.emitAnnotation(Override.class); writer.beginMethod("void", "attach", PUBLIC, Linker.class.getName(), "linker"); for (Element field : fields) { @@ -440,7 +445,7 @@ private void writeStaticInjection(TypeElement type, List fields) throws writer.endMethod(); writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.STATIC_INJECT_METHOD); + writer.emitJavadoc(AdapterJavadocs.STATIC_INJECT_METHOD); writer.emitAnnotation(Override.class); writer.beginMethod("void", "inject", PUBLIC); for (Element field : fields) { @@ -477,4 +482,5 @@ static class InjectedClass { this.fields = fields; } } + } diff --git a/compiler/src/main/java/dagger/internal/codegen/ProvidesProcessor.java b/compiler/src/main/java/dagger/internal/codegen/ModuleAdapterProcessor.java similarity index 89% rename from compiler/src/main/java/dagger/internal/codegen/ProvidesProcessor.java rename to compiler/src/main/java/dagger/internal/codegen/ModuleAdapterProcessor.java index 32a9386f87e..bafbe5f4e8c 100644 --- a/compiler/src/main/java/dagger/internal/codegen/ProvidesProcessor.java +++ b/compiler/src/main/java/dagger/internal/codegen/ModuleAdapterProcessor.java @@ -50,7 +50,14 @@ import javax.tools.Diagnostic; import javax.tools.JavaFileObject; -import static dagger.internal.codegen.ProcessorJavadocs.binderTypeDocs; +import static dagger.internal.codegen.AdapterJavadocs.binderTypeDocs; +import static dagger.internal.codegen.TypeUtils.adapterName; +import static dagger.internal.codegen.TypeUtils.getAnnotation; +import static dagger.internal.codegen.TypeUtils.getNoArgsConstructor; +import static dagger.internal.codegen.TypeUtils.getPackage; +import static dagger.internal.codegen.TypeUtils.isCallableConstructor; +import static dagger.internal.codegen.TypeUtils.isInterface; +import static dagger.internal.codegen.TypeUtils.typeToString; import static dagger.internal.loaders.generated.GeneratedAdapterLoader.MODULE_ADAPTER_SUFFIX; import static java.lang.reflect.Modifier.FINAL; import static java.lang.reflect.Modifier.PRIVATE; @@ -63,7 +70,7 @@ * for each {@code @Provides} method of a target class. */ @SupportedAnnotationTypes({ "dagger.Provides", "dagger.Module" }) -public final class ProvidesProcessor extends AbstractProcessor { +public final class ModuleAdapterProcessor extends AbstractProcessor { private final LinkedHashMap> remainingTypes = new LinkedHashMap>(); private static final String BINDINGS_MAP = JavaWriter.type( @@ -82,7 +89,7 @@ public final class ProvidesProcessor extends AbstractProcessor { try { // Attempt to get the annotation. If types are missing, this will throw // IllegalStateException. - Map parsedAnnotation = CodeGen.getAnnotation(Module.class, type); + Map parsedAnnotation = getAnnotation(Module.class, type); try { writeModuleAdapter(type, parsedAnnotation, providesTypes); } catch (IOException e) { @@ -109,12 +116,12 @@ private void error(String msg, Element element) { */ private Map> providerMethodsByClass(RoundEnvironment env) { Elements elementUtils = processingEnv.getElementUtils(); - Types typeUtils = processingEnv.getTypeUtils(); + Types types = processingEnv.getTypeUtils(); TypeElement providerElement = elementUtils.getTypeElement("javax.inject.Provider"); - TypeMirror providerType = typeUtils.erasure(providerElement.asType()); + TypeMirror providerType = types.erasure(providerElement.asType()); TypeElement lazyElement = elementUtils.getTypeElement("dagger.Lazy"); - TypeMirror lazyType = typeUtils.erasure(lazyElement.asType()); + TypeMirror lazyType = types.erasure(lazyElement.asType()); Map> result = new HashMap>(); for (Element providerMethod : providesMethods(env)) { @@ -151,15 +158,15 @@ private Map> providerMethodsByClass(RoundEnviron continue; } - TypeMirror returnType = typeUtils.erasure(providerMethodAsExecutable.getReturnType()); - if (typeUtils.isSameType(returnType, providerType)) { + TypeMirror returnType = types.erasure(providerMethodAsExecutable.getReturnType()); + if (types.isSameType(returnType, providerType)) { error("@Provides method must not return Provider directly: " + type.getQualifiedName() + "." + providerMethod, providerMethod); continue; } - if (typeUtils.isSameType(returnType, lazyType)) { + if (types.isSameType(returnType, lazyType)) { error("@Provides method must not return Lazy directly: " + type.getQualifiedName() + "." @@ -224,7 +231,7 @@ private void writeModuleAdapter(TypeElement type, Map module, boolean complete = (Boolean) module.get("complete"); boolean library = (Boolean) module.get("library"); - String adapterName = CodeGen.adapterName(type, MODULE_ADAPTER_SUFFIX); + String adapterName = adapterName(type, MODULE_ADAPTER_SUFFIX); JavaFileObject sourceFile = processingEnv.getFiler() .createSourceFile(adapterName, type); JavaWriter writer = new JavaWriter(sourceFile.openWriter()); @@ -232,22 +239,22 @@ private void writeModuleAdapter(TypeElement type, Map module, boolean multibindings = checkForMultibindings(providerMethods); boolean providerMethodDependencies = checkForDependencies(providerMethods); - writer.emitEndOfLineComment(ProcessorJavadocs.GENERATED_BY_DAGGER); - writer.emitPackage(CodeGen.getPackage(type).getQualifiedName().toString()); + writer.emitEndOfLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); + writer.emitPackage(getPackage(type).getQualifiedName().toString()); writer.emitEmptyLine(); writer.emitImports( getImports(multibindings, !providerMethods.isEmpty(), providerMethodDependencies)); String typeName = type.getQualifiedName().toString(); writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.MODULE_TYPE); + writer.emitJavadoc(AdapterJavadocs.MODULE_TYPE); writer.beginType(adapterName, "class", PUBLIC | FINAL, JavaWriter.type(ModuleAdapter.class, typeName)); StringBuilder injectsField = new StringBuilder().append("{ "); for (Object injectableType : injects) { TypeMirror typeMirror = (TypeMirror) injectableType; - String key = CodeGen.isInterface(typeMirror) + String key = isInterface(typeMirror) ? GeneratorKeys.get(typeMirror) : GeneratorKeys.rawMembersKey(typeMirror); injectsField.append(JavaWriter.stringLiteral(key)).append(", "); @@ -259,7 +266,7 @@ private void writeModuleAdapter(TypeElement type, Map module, StringBuilder staticInjectionsField = new StringBuilder().append("{ "); for (Object staticInjection : staticInjections) { TypeMirror typeMirror = (TypeMirror) staticInjection; - staticInjectionsField.append(CodeGen.typeToString(typeMirror)).append(".class, "); + staticInjectionsField.append(typeToString(typeMirror)).append(".class, "); } staticInjectionsField.append("}"); writer.emitField("Class[]", "STATIC_INJECTIONS", PRIVATE | STATIC | FINAL, @@ -274,7 +281,7 @@ private void writeModuleAdapter(TypeElement type, Map module, continue; } TypeMirror typeMirror = (TypeMirror) include; - includesField.append(CodeGen.typeToString(typeMirror)).append(".class, "); + includesField.append(typeToString(typeMirror)).append(".class, "); } includesField.append("}"); writer.emitField("Class[]", "INCLUDES", PRIVATE | STATIC | FINAL, includesField.toString()); @@ -285,8 +292,8 @@ private void writeModuleAdapter(TypeElement type, Map module, + "INCLUDES, %s /*complete*/, %s /*library*/)", overrides, complete, library); writer.endMethod(); - ExecutableElement noArgsConstructor = CodeGen.getNoArgsConstructor(type); - if (noArgsConstructor != null && CodeGen.isCallableConstructor(noArgsConstructor)) { + ExecutableElement noArgsConstructor = getNoArgsConstructor(type); + if (noArgsConstructor != null && isCallableConstructor(noArgsConstructor)) { writer.emitEmptyLine(); writer.emitAnnotation(Override.class); writer.beginMethod(typeName, "newModule", PROTECTED); @@ -300,7 +307,7 @@ private void writeModuleAdapter(TypeElement type, Map module, if (!providerMethods.isEmpty()) { writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.GET_DEPENDENCIES_METHOD); + writer.emitJavadoc(AdapterJavadocs.GET_DEPENDENCIES_METHOD); writer.emitAnnotation(Override.class); writer.beginMethod("void", "getBindings", PUBLIC, BINDINGS_MAP, "map"); @@ -399,9 +406,9 @@ private void writeProvidesAdapter(JavaWriter writer, ExecutableElement providerM Map methodNameToNextId, boolean library) throws IOException { String methodName = providerMethod.getSimpleName().toString(); - String moduleType = CodeGen.typeToString(providerMethod.getEnclosingElement().asType()); + String moduleType = typeToString(providerMethod.getEnclosingElement().asType()); String className = bindingClassName(providerMethod, methodToClassName, methodNameToNextId); - String returnType = CodeGen.typeToString(providerMethod.getReturnType()); + String returnType = typeToString(providerMethod.getReturnType()); List parameters = providerMethod.getParameters(); boolean dependent = !parameters.isEmpty(); @@ -413,8 +420,7 @@ private void writeProvidesAdapter(JavaWriter writer, ExecutableElement providerM writer.emitField(moduleType, "module", PRIVATE | FINAL); for (Element parameter : parameters) { TypeMirror parameterType = parameter.asType(); - writer.emitField(JavaWriter.type(Binding.class, - CodeGen.typeToString(parameterType)), + writer.emitField(JavaWriter.type(Binding.class, typeToString(parameterType)), parameterName(parameter), PRIVATE); } @@ -432,7 +438,7 @@ private void writeProvidesAdapter(JavaWriter writer, ExecutableElement providerM if (dependent) { writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.ATTACH_METHOD); + writer.emitJavadoc(AdapterJavadocs.ATTACH_METHOD); writer.emitAnnotation(Override.class); writer.emitAnnotation(SuppressWarnings.class, JavaWriter.stringLiteral("unchecked")); writer.beginMethod("void", "attach", PUBLIC, Linker.class.getCanonicalName(), "linker"); @@ -441,15 +447,14 @@ private void writeProvidesAdapter(JavaWriter writer, ExecutableElement providerM writer.emitStatement( "%s = (%s) linker.requestBinding(%s, %s.class, getClass().getClassLoader())", parameterName(parameter), - writer.compressType(JavaWriter.type(Binding.class, - CodeGen.typeToString(parameter.asType()))), + writer.compressType(JavaWriter.type(Binding.class, typeToString(parameter.asType()))), JavaWriter.stringLiteral(parameterKey), writer.compressType(moduleType)); } writer.endMethod(); writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.GET_DEPENDENCIES_METHOD); + writer.emitJavadoc(AdapterJavadocs.GET_DEPENDENCIES_METHOD); writer.emitAnnotation(Override.class); String setOfBindings = JavaWriter.type(Set.class, "Binding"); writer.beginMethod("void", "getDependencies", PUBLIC, setOfBindings, "getBindings", @@ -461,7 +466,7 @@ private void writeProvidesAdapter(JavaWriter writer, ExecutableElement providerM } writer.emitEmptyLine(); - writer.emitJavadoc(ProcessorJavadocs.GET_METHOD, returnType); + writer.emitJavadoc(AdapterJavadocs.GET_METHOD, returnType); writer.emitAnnotation(Override.class); writer.beginMethod(returnType, "get", PUBLIC); StringBuilder args = new StringBuilder(); diff --git a/compiler/src/main/java/dagger/internal/codegen/CodeGen.java b/compiler/src/main/java/dagger/internal/codegen/TypeUtils.java similarity index 96% rename from compiler/src/main/java/dagger/internal/codegen/CodeGen.java rename to compiler/src/main/java/dagger/internal/codegen/TypeUtils.java index d765e80e06f..39e61917b8a 100644 --- a/compiler/src/main/java/dagger/internal/codegen/CodeGen.java +++ b/compiler/src/main/java/dagger/internal/codegen/TypeUtils.java @@ -19,10 +19,8 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.Set; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.AnnotationValueVisitor; @@ -41,10 +39,10 @@ import javax.lang.model.util.SimpleTypeVisitor6; /** - * Support for annotation processors. + * Utilities for handling types in annotation processors */ -final class CodeGen { - private CodeGen() { +final class TypeUtils { + private TypeUtils() { } public static PackageElement getPackage(Element type) { @@ -160,7 +158,7 @@ public static void typeToString(final TypeMirror type, final StringBuilder resul */ public static Map getAnnotation(Class annotationType, Element element) { for (AnnotationMirror annotation : element.getAnnotationMirrors()) { - if (!CodeGen.rawTypeToString(annotation.getAnnotationType(), '$') + if (!rawTypeToString(annotation.getAnnotationType(), '$') .equals(annotationType.getName())) { continue; } @@ -279,15 +277,6 @@ public static boolean isCallableConstructor(ExecutableElement constructor) { || type.getModifiers().contains(Modifier.STATIC); } - /** - * Returns a set comprised of the given items - */ - public static Set setOf(T ... items) { - Set set = new LinkedHashSet(); - set.addAll(Arrays.asList(items)); - return set; - } - /** * Returns a user-presentable string like {@code * coffee.CoffeeModule#provideHeater()}. diff --git a/compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor index db389355b1b..dce5a84cd53 100644 --- a/compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor +++ b/compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -1,3 +1,3 @@ -dagger.internal.codegen.InjectProcessor -dagger.internal.codegen.ProvidesProcessor -dagger.internal.codegen.FullGraphProcessor \ No newline at end of file +dagger.internal.codegen.InjectAdapterProcessor +dagger.internal.codegen.ModuleAdapterProcessor +dagger.internal.codegen.GraphAnalysisProcessor \ No newline at end of file diff --git a/compiler/src/test/java/dagger/internal/codegen/DotWriterTest.java b/compiler/src/test/java/dagger/internal/codegen/DotWriterTest.java index 69a5be47544..e1ce8e261ed 100644 --- a/compiler/src/test/java/dagger/internal/codegen/DotWriterTest.java +++ b/compiler/src/test/java/dagger/internal/codegen/DotWriterTest.java @@ -26,7 +26,7 @@ @RunWith(JUnit4.class) public final class DotWriterTest { private final StringWriter stringWriter = new StringWriter(); - private final DotWriter dotWriter = new DotWriter(stringWriter); + private final GraphVizWriter dotWriter = new GraphVizWriter(stringWriter); @Test public void graphWithAttributes() throws IOException { dotWriter.beginGraph();