Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
mezarin committed Feb 8, 2024
1 parent f78be9b commit 3693b75
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ && containsAnnotation(type, field.getAnnotations(), INJECT_FQ_NAME)) {
// For performance purposes, process this validation only when necessary. That is when we have a single valid
// constructor annotated with @Inject. Note that the previous validation will make sure there is a single
// constructor annotated with @Inject.
System.out.println("@ed: processing injection point checks.");
if (injectedConstructors.size() == 1) {
System.out.println("@ed: Found a single injection point.");
IMethod constructor = injectedConstructors.get(0);
if (constructor.getNumberOfParameters() > 0) {
System.out.println("@ed: Found parameters to check for injection point validity.");
ILocalVariable[] params = constructor.getParameters();
for (int i = 0; i < params.length; i++) {
ILocalVariable param = params[i];
Expand Down Expand Up @@ -180,6 +183,11 @@ private boolean containsAnnotation(IType type, IAnnotation[] annotations, String
*/
public static void getInjectionPointDiagnostics(List<Diagnostic> diagnostics, JavaDiagnosticsContext context, String uri, ILocalVariable variable) {
try {
System.out.println("@ed: Adding injection point diagnostics.");

// Note: Although, these checks apply to all managed bean parameters that are injections points,
// some of these checks may not apply to other non-managed beans that are injectable.
// Further consideration is required.
Range range = PositionUtils.toNameRange(variable,
context.getUtils());
IType variableType = ManagedBean.variableSignatureToType(variable);
Expand Down Expand Up @@ -223,6 +231,14 @@ public static void getInjectionPointDiagnostics(List<Diagnostic> diagnostics, Ja
ErrorCode.InjectionPointInvalidVetoedClassBean,
DiagnosticSeverity.Warning));
}

// Check if the type does not have a constructor with no parameters or the class declares a constructor that is not annotated @Inject.
if (!ManagedBean.containsValidConstructor(variableType)) {
String msg = Messages.getMessage("InjectionPointInvalidConstructorBean");
diagnostics.add(context.createDiagnostic(uri, msg, range, Constants.DIAGNOSTIC_SOURCE,
ErrorCode.InjectionPointInvalidConstructorBean,
DiagnosticSeverity.Warning));
}
} catch (JavaModelException jme) {
JakartaCorePlugin.logException("Cannot obtain injection point diagnostics for variable: " + variable + " in file: " + uri, jme);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,28 @@ public void vetoedPackageConstructorParam() throws Exception {

assertJavaDiagnostics(diagnosticsParams, IJDT_UTILS, d1);
}

/**
* Tests diagnostic issued for a bean constructor parameter injection point that does not define
* a class with a valid constructor. A valid constructor is one that has no parameters or one that
* is annotated with @Inject.
*
* @throws Exception
*/
@Test
public void invalidConstructorParam() throws Exception {
IJavaProject javaProject = loadJavaProject("jakarta-sample", "");
IFile javaFile = javaProject.getProject().getFile(new Path("src/main/java/io/openliberty/sample/jakarta/di/InvalidConstructorInjectionPointUser.java"));
String uri = javaFile.getLocation().toFile().toURI().toString();

JakartaJavaDiagnosticsParams diagnosticsParams = new JakartaJavaDiagnosticsParams();
diagnosticsParams.setUris(Arrays.asList(uri));

// Test expected diagnostic
Diagnostic d1 = d(22, 72, 75,
"Invalid parameter injection point. The parameter should define a constructor with no parameters or a constructor annotated with @Inject.",
DiagnosticSeverity.Warning, "jakarta-di", "InjectionPointInvalidConstructorBean");

assertJavaDiagnostics(diagnosticsParams, IJDT_UTILS, d1);
}
}

0 comments on commit 3693b75

Please sign in to comment.