diff --git a/toothpick-compiler/src/main/java/toothpick/compiler/factory/FactoryProcessor.java b/toothpick-compiler/src/main/java/toothpick/compiler/factory/FactoryProcessor.java index 94ed8c29..a6def8a7 100644 --- a/toothpick-compiler/src/main/java/toothpick/compiler/factory/FactoryProcessor.java +++ b/toothpick-compiler/src/main/java/toothpick/compiler/factory/FactoryProcessor.java @@ -280,8 +280,11 @@ private ConstructorInjectionTarget createConstructorInjectionTarget(TypeElement for (ExecutableElement constructorElement : constructorElements) { if (constructorElement.getParameters().isEmpty()) { if (constructorElement.getModifiers().contains(Modifier.PRIVATE)) { - warning(constructorElement, "The class %s has a private default constructor, Toothpick can't create a factory for it.", - typeElement.getQualifiedName().toString()); + if (!isInjectableWarningSuppressed(typeElement)) { + warning(constructorElement, + "The class %s has a private default constructor, Toothpick can't create a factory for it.", + typeElement.getQualifiedName().toString()); + } return null; } @@ -291,8 +294,12 @@ private ConstructorInjectionTarget createConstructorInjectionTarget(TypeElement } } - warning(typeElement, "The class %s has injected fields but has no injected constructor, and no public default constructor." - + " Toothpick can't create a factory for it.", typeElement.getQualifiedName().toString()); + if (!isInjectableWarningSuppressed(typeElement)) { + warning(typeElement, + "The class %s has injected fields but has no injected constructor, and no public default constructor." + + " Toothpick can't create a factory for it.", + typeElement.getQualifiedName().toString()); + } return null; } @@ -318,6 +325,25 @@ private String getScopeName(TypeElement typeElement) { return scopeName; } + /** + * Checks if the injectable warning is suppressed for the TypeElement, + * through the usage of @SuppressWarning("Injectable"). + * + * @param typeElement the element to check if the warning is suppressed. + * @return true is the injectable warning is suppressed, false otherwise. + */ + private boolean isInjectableWarningSuppressed(TypeElement typeElement) { + SuppressWarnings suppressWarnings = typeElement.getAnnotation(SuppressWarnings.class); + if (suppressWarnings != null) { + for (String value : suppressWarnings.value()) { + if (value.equals("Injectable")) { + return true; + } + } + } + return false; + } + private boolean canTypeHaveAFactory(TypeElement typeElement) { boolean isAbstract = typeElement.getModifiers().contains(Modifier.ABSTRACT); boolean isPrivate = typeElement.getModifiers().contains(Modifier.PRIVATE); diff --git a/toothpick-sample/src/main/java/toothpick/sample/SecondEntryPoint.java b/toothpick-sample/src/main/java/toothpick/sample/SecondEntryPoint.java new file mode 100644 index 00000000..ede5758c --- /dev/null +++ b/toothpick-sample/src/main/java/toothpick/sample/SecondEntryPoint.java @@ -0,0 +1,17 @@ +package toothpick.sample; + +import javax.inject.Inject; +import toothpick.Scope; +import toothpick.Toothpick; + +@SuppressWarnings("Injectable") +public class SecondEntryPoint { + + @Inject Computer computer; + @Inject Computer2 computer2; + + private SecondEntryPoint() { + Scope scope = Toothpick.openScope("SecondEntryPoint"); + Toothpick.inject(this, scope); + } +}