Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow inject: with constructor class which have arguments #964

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading