From bc3bfa5cb57f056f8789f39b9f87046a73c75977 Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 4 Jul 2024 11:45:58 +0200 Subject: [PATCH] fix: allow inject: with constructor class which have arguments Signed-off-by: azerr --- .../src/main/java/org/acme/Bean3.java | 9 ++++++ .../redhat/qute/jdt/utils/CDIUtilsTest.java | 31 ++++++++++--------- .../com/redhat/qute/jdt/utils/CDIUtils.java | 5 ++- 3 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 qute.jdt/com.redhat.qute.jdt.test/projects/maven/quarkus3/src/main/java/org/acme/Bean3.java diff --git a/qute.jdt/com.redhat.qute.jdt.test/projects/maven/quarkus3/src/main/java/org/acme/Bean3.java b/qute.jdt/com.redhat.qute.jdt.test/projects/maven/quarkus3/src/main/java/org/acme/Bean3.java new file mode 100644 index 000000000..40e99b326 --- /dev/null +++ b/qute.jdt/com.redhat.qute.jdt.test/projects/maven/quarkus3/src/main/java/org/acme/Bean3.java @@ -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) { + + } +} diff --git a/qute.jdt/com.redhat.qute.jdt.test/src/main/java/com/redhat/qute/jdt/utils/CDIUtilsTest.java b/qute.jdt/com.redhat.qute.jdt.test/src/main/java/com/redhat/qute/jdt/utils/CDIUtilsTest.java index 54a86b10c..4ac50f8fd 100644 --- a/qute.jdt/com.redhat.qute.jdt.test/src/main/java/com/redhat/qute/jdt/utils/CDIUtilsTest.java +++ b/qute.jdt/com.redhat.qute.jdt.test/src/main/java/com/redhat/qute/jdt/utils/CDIUtilsTest.java @@ -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; @@ -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(); } } diff --git a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/utils/CDIUtils.java b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/utils/CDIUtils.java index 2b9ddd42d..af89358aa 100644 --- a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/utils/CDIUtils.java +++ b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/utils/CDIUtils.java @@ -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; }