private void findAndParseTargets(RoundEnvironment roundEnv) { processInjectAnnotatedFields(roundEnv); processInjectAnnotatedMethods(roundEnv); }
private void mapTypeToMostDirectSuperTypeThatNeedsInjection(TypeElement typeElement) { TypeElement superClassWithInjectedMembers = getMostDirectSuperClassWithInjectedMembers(typeElement, true); mapTypeElementToSuperTypeElementThatNeedsInjection.put(typeElement, superClassWithInjectedMembers); }
static Iterable<? extends Processor> memberInjectorProcessors(String toothpickRegistryPackageName, List<String> toothpickRegistryChildrenPackageNameList) { return memberInjectorProcessors(toothpickRegistryPackageName, toothpickRegistryChildrenPackageNameList, "java.*,android.*"); }
private MethodInjectionTarget createMethodInjectionTarget(ExecutableElement methodElement) { TypeElement enclosingElement = (TypeElement) methodElement.getEnclosingElement(); final String methodName = methodElement.getSimpleName().toString(); boolean isOverride = isOverride(enclosingElement, methodElement); MethodInjectionTarget methodInjectionTarget = new MethodInjectionTarget(enclosingElement, methodName, isOverride); methodInjectionTarget.parameters.addAll(getParamInjectionTargetList(methodElement)); return methodInjectionTarget; }
private void processInjectAnnotatedMethod(ExecutableElement methodElement, Map<TypeElement, List<MethodInjectionTarget>> mapTypeElementToMemberInjectorTargetList) { TypeElement enclosingElement = (TypeElement) methodElement.getEnclosingElement(); // Verify common generated code restrictions. if (!isValidInjectAnnotatedMethod(methodElement)) { return; } List<MethodInjectionTarget> methodInjectionTargetList = mapTypeElementToMemberInjectorTargetList.get(enclosingElement); if (methodInjectionTargetList == null) { methodInjectionTargetList = new ArrayList<>(); mapTypeElementToMemberInjectorTargetList.put(enclosingElement, methodInjectionTargetList); } mapTypeToMostDirectSuperTypeThatNeedsInjection(enclosingElement); methodInjectionTargetList.add(createMethodInjectionTarget(methodElement)); }
private void processInjectAnnotatedField(VariableElement fieldElement, Map<TypeElement, List<FieldInjectionTarget>> mapTypeElementToMemberInjectorTargetList) { TypeElement enclosingElement = (TypeElement) fieldElement.getEnclosingElement(); // Verify common generated code restrictions. if (!isValidInjectAnnotatedFieldOrParameter(fieldElement)) { return; } List<FieldInjectionTarget> fieldInjectionTargetList = mapTypeElementToMemberInjectorTargetList.get(enclosingElement); if (fieldInjectionTargetList == null) { fieldInjectionTargetList = new ArrayList<>(); mapTypeElementToMemberInjectorTargetList.put(enclosingElement, fieldInjectionTargetList); } mapTypeToMostDirectSuperTypeThatNeedsInjection(enclosingElement); fieldInjectionTargetList.add(createFieldOrParamInjectionTarget(fieldElement)); }
protected void processInjectAnnotatedMethods(RoundEnvironment roundEnv) { for (ExecutableElement element : ElementFilter.methodsIn(roundEnv.getElementsAnnotatedWith(Inject.class))) { if (!isExcludedByFilters((TypeElement) element.getEnclosingElement())) { processInjectAnnotatedMethod(element, mapTypeElementToMethodInjectorTargetList); } } }
protected void processInjectAnnotatedFields(RoundEnvironment roundEnv) { for (VariableElement element : ElementFilter.fieldsIn(roundEnv.getElementsAnnotatedWith(Inject.class))) { if (!isExcludedByFilters((TypeElement) element.getEnclosingElement())) { processInjectAnnotatedField(element, mapTypeElementToFieldInjectorTargetList); } } }
static Iterable<? extends Processor> memberInjectorProcessors(String toothpickRegistryPackageName, List<String> toothpickRegistryChildrenPackageNameList, String toothpickExcludeFilters) { MemberInjectorProcessor memberInjectorProcessor = new MemberInjectorProcessor(); memberInjectorProcessor.setToothpickRegistryPackageName(toothpickRegistryPackageName); memberInjectorProcessor.setToothpickRegistryChildrenPackageNameList(toothpickRegistryChildrenPackageNameList); memberInjectorProcessor.setToothpickExcludeFilters(toothpickExcludeFilters); return Arrays.asList(memberInjectorProcessor); } }
static Iterable<? extends Processor> memberInjectorProcessorsFailingWhenMethodIsNotPackageVisible() { final MemberInjectorProcessor memberInjectorProcessor = new MemberInjectorProcessor(); memberInjectorProcessor.setCrashOrWarnWhenMethodIsNotPackageVisible(true); return Arrays.asList(memberInjectorProcessor); }
@Test public void testRegistry_withNoInjectors() { JavaFileObject source = classWithMemberInjection("Class0"); .processedWith(ProcessorTestUtilities.memberInjectorProcessors("toothpick", Collections.EMPTY_LIST, "java.*,android.*,test.*")) .compilesWithoutError() .and()
@Test public void testInjectedMethod_shouldFailTheBuild_whenMethodIsProtected() { JavaFileObject source = JavaFileObjects.forSourceString("test.TestWarningVisibleInjectedMethod", Joiner.on('\n').join(// "package test;", // "import javax.inject.Inject;", // "import javax.inject.Singleton;", // "public class TestWarningVisibleInjectedMethod {", // " @Inject protected void init() {}", // "}")); assert_().about(javaSource()) .that(source) .processedWith(memberInjectorProcessorsFailingWhenMethodIsNotPackageVisible()) .failsToCompile(); }
static Iterable<? extends Processor> memberInjectorProcessors() { return Arrays.asList(new MemberInjectorProcessor()); }
private Iterable<Processor> defaultMemberInjectorProcessor() { return ProcessorTestUtilities.memberInjectorProcessors("toothpick", Collections.EMPTY_LIST); } }
@Test public void testInjectedMethod_shouldFailTheBuild_whenMethodIsPublic() { JavaFileObject source = JavaFileObjects.forSourceString("test.TestWarningVisibleInjectedMethod", Joiner.on('\n').join(// "package test;", // "import javax.inject.Inject;", // "import javax.inject.Singleton;", // "public class TestWarningVisibleInjectedMethod {", // " @Inject public void init() {}", // "}")); assert_().about(javaSource()) .that(source) .processedWith(memberInjectorProcessorsFailingWhenMethodIsNotPackageVisible()) .failsToCompile(); }
static Iterable<? extends Processor> factoryAndMemberInjectorProcessors() { return Arrays.asList(new MemberInjectorProcessor(), new FactoryProcessor()); }
@Test public void testFieldInjection_shouldFail_whenFieldIsPrivate() { JavaFileObject source = JavaFileObjects.forSourceString("test.TestFieldInjection", Joiner.on('\n').join(// "package test;", // "import javax.inject.Inject;", // "public class TestFieldInjection {", // " @Inject private Foo foo;", // " public TestFieldInjection() {}", // "}", // "class Foo {}" // )); assert_().about(javaSource()) .that(source) .processedWith(memberInjectorProcessors()) .failsToCompile() .withErrorContaining("@Inject annotated fields must be non private : test.TestFieldInjection#foo"); }
@Test public void testInjectedMethod_shouldNotFailTheBuild_whenMethodIsPublicButAnnotated() { JavaFileObject source = JavaFileObjects.forSourceString("test.TestWarningVisibleInjectedMethod", Joiner.on('\n').join(// "package test;", // "import javax.inject.Inject;", // "import javax.inject.Singleton;", // "public class TestWarningVisibleInjectedMethod {", // " @SuppressWarnings(\"visible\")", // " @Inject public void init() {}", // "}")); assert_().about(javaSource()) .that(source) .processedWith(memberInjectorProcessorsFailingWhenMethodIsNotPackageVisible()) .compilesWithoutError(); }
@Test public void testSimpleMethodInjectionWithLazyOfGenericType_shouldFail_WithLazyOfGenericType() { JavaFileObject source = JavaFileObjects.forSourceString("test.TestMethodInjection", Joiner.on('\n').join(// "package test;", // "import javax.inject.Inject;", // "import toothpick.Lazy;", // "public class TestMethodInjection {", // " @Inject", // " public void m(Lazy<Foo<String>> foo) {}", // "}", // "class Foo<T> {}" // )); assert_().about(javaSource()) .that(source) .processedWith(memberInjectorProcessors()) .failsToCompile() .withErrorContaining("Lazy/Provider foo is not a valid in m. Lazy/Provider cannot be used on generic types."); }
@Test public void testInjectedMethod_shouldNotFailTheBuild_whenMethodIsProtectedButAnnotated() { JavaFileObject source = JavaFileObjects.forSourceString("test.TestWarningVisibleInjectedMethod", Joiner.on('\n').join(// "package test;", // "import javax.inject.Inject;", // "import javax.inject.Singleton;", // "public class TestWarningVisibleInjectedMethod {", // " @SuppressWarnings(\"visible\")", // " @Inject protected void init() {}", // "}")); assert_().about(javaSource()) .that(source) .processedWith(memberInjectorProcessorsFailingWhenMethodIsNotPackageVisible()) .compilesWithoutError(); } }