Skip to content

Commit

Permalink
Fix issue 261. Handle not supported types in a better way
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanenicolas committed Oct 15, 2017
1 parent 30eb7ea commit 886f2a7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,8 @@
//Continue from top

## Release version 1.1.2 (To be released)
* Fix issue #261. Better handling of non supported injected types (like primitives).
Thx to Alexey Ershov for the detailed bug report.

## Release version 1.1.1 (Oct. 13th, 2017)
* Fix issue #258, #256, #253: remove dependency to JSR 250 annotations, fix missing overrides.
Expand Down
Expand Up @@ -308,24 +308,34 @@ protected boolean isValidInjectedType(VariableElement injectedTypeElement) {

private boolean isValidInjectedElementKind(VariableElement injectedTypeElement) {
Element typeElement = typeUtils.asElement(injectedTypeElement.asType());
if (typeElement.getKind() != ElementKind.CLASS //
//typeElement can be null for primitives. https://github.com/stephanenicolas/toothpick/issues/261
if (typeElement == null //
|| typeElement.getKind() != ElementKind.CLASS //
&& typeElement.getKind() != ElementKind.INTERFACE //
&& typeElement.getKind() != ElementKind.ENUM) {

//find the class containing the element
//the element can be a field or a parameter
Element enclosingElement = injectedTypeElement.getEnclosingElement();
final String typeName;
if (typeElement != null) {
typeName = typeElement.toString();
} else {
typeName = injectedTypeElement.asType().toString();
}
if (enclosingElement instanceof TypeElement) {
error(injectedTypeElement, "Field %s#%s is of type %s which is not supported by Toothpick.",
((TypeElement) enclosingElement).getQualifiedName(), injectedTypeElement.getSimpleName(), typeElement);
((TypeElement) enclosingElement).getQualifiedName(),
injectedTypeElement.getSimpleName(), typeName);
} else {
Element methodOrConstructorElement = enclosingElement;
enclosingElement = enclosingElement.getEnclosingElement();
error(injectedTypeElement, "Parameter %s in method/constructor %s#%s is of type %s which is not supported by Toothpick.",
error(injectedTypeElement,
"Parameter %s in method/constructor %s#%s is of type %s which is not supported by Toothpick.",
injectedTypeElement.getSimpleName(), //
((TypeElement) enclosingElement).getQualifiedName(), //
methodOrConstructorElement.getSimpleName(), //
typeElement);
typeName);
}
return false;
}
Expand Down
Expand Up @@ -1146,4 +1146,21 @@ public void testAClassWithProvidesSingletonAnnotation_shouldHaveAFactoryThatSays
.and()
.generatesSources(expectedSource);
}

@Test
public void testInjectedConstructor_withPrimitiveParam() {
JavaFileObject source = JavaFileObjects.forSourceString("test.TestPrimitiveConstructor", Joiner.on('\n').join(//
"package test;", //
"import javax.inject.Inject;", //
"public class TestPrimitiveConstructor {", //
" @Inject TestPrimitiveConstructor(int n) {}", //
"}" //
));

assert_().about(javaSource())
.that(source)
.processedWith(ProcessorTestUtilities.factoryProcessors())
.failsToCompile()
.withErrorContaining("Parameter n in method/constructor test.TestPrimitiveConstructor#<init> is of type int which is not supported by Toothpick.");
}
}
Expand Up @@ -646,4 +646,22 @@ public void testMemberInjection_shouldInjectAsAnInstanceOfSuperClass_whenSuperCl
.and()
.generatesSources(expectedSource);
}

@Test
public void testFieldInjection_shouldFail_WhenFieldIsPrimitive() {
JavaFileObject source = JavaFileObjects.forSourceString("test.TestFieldInjection", Joiner.on('\n').join(//
"package test;", //
"import javax.inject.Inject;", //
"public class TestFieldInjection {", //
" @Inject int foo;", //
" public TestFieldInjection() {}", //
"}" //
));

assert_().about(javaSource())
.that(source)
.processedWith(memberInjectorProcessors())
.failsToCompile()
.withErrorContaining("Field test.TestFieldInjection#foo is of type int which is not supported by Toothpick.");
}
}

0 comments on commit 886f2a7

Please sign in to comment.