Skip to content

Commit

Permalink
missing pieces in the AST: parser seems to skip broken annotation
Browse files Browse the repository at this point in the history
altogether when on method parameters

recover incomplete annotation on argument

fixes eclipse-jdt#3155
  • Loading branch information
stephan-herrmann committed Oct 25, 2024
1 parent 185d23b commit 342939f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalanceValu
*/
@Override
public RecoveredElement add(FieldDeclaration fieldDeclaration, int bracketBalanceValue) {
if (recoverAsArgument(fieldDeclaration)) {
resetPendingModifiers();
return this;
}
resetPendingModifiers();

/* local variables inside method can only be final and non void */
Expand Down Expand Up @@ -133,6 +137,27 @@ public RecoveredElement add(FieldDeclaration fieldDeclaration, int bracketBalanc
// still inside method, treat as local variable
return this; // ignore
}

private boolean recoverAsArgument(FieldDeclaration fieldDeclaration) {
if (!this.foundOpeningBrace
&& this.methodDeclaration.declarationSourceEnd == 0
&& this.methodDeclaration.arguments == null) { // misparsed parameter?
long position = ((long) fieldDeclaration.sourceStart << 32)+fieldDeclaration.sourceEnd;
Argument arg = new Argument(fieldDeclaration.name, position, fieldDeclaration.type, fieldDeclaration.modifiers);
this.methodDeclaration.arguments = new Argument[] { arg };
int annotCount = this.pendingAnnotationCount;
if (this.pendingAnnotations != null) {
arg.annotations = new Annotation[annotCount];
for (int i = 0; i < this.pendingAnnotationCount; i++) {
arg.annotations[i] = this.pendingAnnotations[i].annotation;
if (i == 0)
arg.declarationSourceStart = arg.annotations[i].sourceStart;
}
}
return true;
}
return false;
}
/*
* Record a local declaration - regular method should have been created a block body
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1085,4 +1085,39 @@ public void test0021() throws JavaModelException {
List statements = block.statements();
assertEquals("wrong size", 0, statements.size()); //$NON-NLS-1$
}

public void testGH3155() throws JavaModelException {
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy(
"/Converter18/src/test/TestDependsOnClass.java",
"""
package test;
public class TestDependsOnClass {
public TestDependsOnClass(@Qualifier(value= ) Object mybean) { }
}
""");
ASTNode result = runConversion(getJLS8(), this.workingCopies[0], true, true);

assertASTNodeEquals(
"""
package test;
public class TestDependsOnClass {
public TestDependsOnClass( @Qualifier(value=$missing$) Object mybean){
}
}
""",
result);

ASTNode node = getASTNode((CompilationUnit) result, 0, 0);
assertNotNull(node);
assertTrue("Not a method declaration", node.getNodeType() == ASTNode.METHOD_DECLARATION); //$NON-NLS-1$
MethodDeclaration methodDeclaration = (MethodDeclaration) node;
assertTrue(methodDeclaration.isConstructor());
assertEquals(1, methodDeclaration.parameters().size());
SingleVariableDeclaration param = (SingleVariableDeclaration) methodDeclaration.parameters().get(0);
assertEquals(1, param.modifiers().size());
IExtendedModifier mod = (IExtendedModifier) param.modifiers().get(0);
assertTrue(mod.isAnnotation());
assertEquals("Qualifier", ((Annotation) mod).getTypeName().toString());
}
}

0 comments on commit 342939f

Please sign in to comment.