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 <azerr@redhat.com>
  • Loading branch information
angelozerr authored and fbricon committed Jul 9, 2024
1 parent 7b0c9c6 commit 51aa262
Show file tree
Hide file tree
Showing 3 changed files with 30 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 @@ -20,6 +20,7 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -85,24 +86,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();
IType notBean1 = getCompilationUnit("src/main/java/org/acme/NotBean1.java", javaProject);
// @Decorator annotated class is not a bean
assertFalse(CDIUtils.isValidBean(notBean1));

javaFile = javaProject.getProject()
.getFile(new Path("src/main/java/org/acme/NotBean2.java"));
cu = JDTUtilsLSImpl.getInstance().resolveCompilationUnit(javaFile.getLocationURI().toString());
IType 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));

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

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

private static IType getCompilationUnit(String javaFilePath, IJavaProject javaProject) {
IFile javaFile = javaProject.getProject().getFile(new Path(javaFilePath));
return JDTUtilsLSImpl.getInstance().resolveCompilationUnit(javaFile.getLocationURI().toString()).findPrimaryType();
}
}
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 51aa262

Please sign in to comment.