Skip to content

Commit

Permalink
Adding support for the SuppressWarnings annotation to remove missing …
Browse files Browse the repository at this point in the history
…contructor warning.
  • Loading branch information
dlemures committed Sep 20, 2016
1 parent 95e8371 commit 244787d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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);
Expand Down
@@ -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);
}
}

0 comments on commit 244787d

Please sign in to comment.