Skip to content

Commit

Permalink
fix: allow inject: with constructor class which have arguments
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jul 8, 2024
1 parent 7b0c9c6 commit 5008e6a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.acme;

public class Bean3 {

// Quarkus can inject Bean1 in the constructor without declaring the bean1 parameter with @Inject
public Bean3(Bean1 bean1) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,26 @@ public void namedWithGetterMethod() {
@Test
public void isBeanQuarkus3() throws Exception {
IJavaProject javaProject = loadMavenProject(QuteMavenProjectName.quarkus3);
IFile javaFile = javaProject.getProject()
.getFile(new Path("src/main/java/org/acme/NotBean1.java"));
ICompilationUnit cu = JDTUtilsLSImpl.getInstance().resolveCompilationUnit(javaFile.getLocationURI().toString());
// @Decorator annotated class is not a bean
assertFalse(CDIUtils.isValidBean(cu.findPrimaryType()));

cu.getAllTypes();
ICompilationUnit notBean1 = getCompilationUnit("src/main/java/org/acme/NotBean1.java", javaProject);
// @Decorator annotated class is not a bean
assertFalse(CDIUtils.isValidBean(notBean1.findPrimaryType()));

javaFile = javaProject.getProject()
.getFile(new Path("src/main/java/org/acme/NotBean2.java"));
cu = JDTUtilsLSImpl.getInstance().resolveCompilationUnit(javaFile.getLocationURI().toString());
ICompilationUnit notBean2 = getCompilationUnit("src/main/java/org/acme/NotBean2.java", javaProject);
// @Vetoed annotated class is not a bean
assertFalse(CDIUtils.isValidBean(cu.findPrimaryType()));
assertFalse(CDIUtils.isValidBean(notBean2.findPrimaryType()));

javaFile = javaProject.getProject()
.getFile(new Path("src/main/java/org/acme/Bean1.java"));
cu = JDTUtilsLSImpl.getInstance().resolveCompilationUnit(javaFile.getLocationURI().toString());
ICompilationUnit bean1 = getCompilationUnit("src/main/java/org/acme/Bean1.java", javaProject);
// Empty class is a bean
assertTrue(CDIUtils.isValidBean(cu.findPrimaryType()));
assertTrue(CDIUtils.isValidBean(bean1.findPrimaryType()));

// Class with constructor is a bean
ICompilationUnit bean3 = getCompilationUnit("src/main/java/org/acme/Bean3.java", javaProject);
assertTrue(CDIUtils.isValidBean(bean3));
}

private static ICompilationUnit getCompilationUnit(String javaFilePath, IJavaProject javaProject) {
IFile javaFile = javaProject.getProject().getFile(new Path(javaFilePath));
return JDTUtilsLSImpl.getInstance().resolveCompilationUnit(javaFile.getLocationURI().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public static boolean isValidBean(IJavaElement javaElement) {
&& !Flags.isAbstract(type.getFlags())
&& !AnnotationUtils.hasAnnotation((IAnnotatable) javaElement, JAVAX_DECORATOR_ANNOTATION, JAKARTA_DECORATOR_ANNOTATION)
&& !AnnotationUtils.hasAnnotation((IAnnotatable) javaElement, JAVAX_INJECT_VETOED_ANNOTATION, JAKARTA_INJECT_VETOED_ANNOTATION)
&& type.isClass() && hasNoArgConstructor(type));
&& type.isClass()
// In Quarkus context, all arguments are injected
// See https://github.com/redhat-developer/vscode-quarkus/issues/708
/* && hasNoArgConstructor(type)*/);
} catch (Exception e) {
return false;
}
Expand Down

0 comments on commit 5008e6a

Please sign in to comment.