Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 578244 - [compiler] Unhandled event loop exception in AST while (#27
)

hovering over method (ClassCastException)

Change-Id: Ia1299f660cd99e6f3b126586abc474919132c988
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
  • Loading branch information
jarthana committed Apr 19, 2022
1 parent 08e12ad commit 22b4e47
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 33 deletions.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2021 IBM Corporation and others.
* Copyright (c) 2020, 2022 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -1087,7 +1087,7 @@ public void testBug561132_033() throws CoreException {
int length = selection.length();

IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
assertTrue(elements.length ==1);
assertEquals("incorrect size of elements", 1, elements.length);
assertTrue((elements[0] instanceof LocalVariable));

}
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* Copyright (c) 2021, 2022 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -612,6 +612,7 @@ public void testBug574870_4() throws CoreException {
int start = str.indexOf(selection);
int length = selection.length();
IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
assertEquals("incorrect number of elements", 1, elements.length);
ILocalVariable local = (ILocalVariable) elements[0];
search(local, DECLARATIONS, EXACT_RULE);
assertSearchResults("src/X.java void X.method(Object).xvar [xvar] EXACT_MATCH");
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 IBM Corporation and others.
* Copyright (c) 2019, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -25,7 +25,7 @@ public class ResolveTests12To15 extends AbstractJavaModelTests {
ICompilationUnit wc = null;

static {
// TESTS_NAMES = new String[] { "test018" };
// TESTS_NAMES = new String[] { "testBug577508_3" };
// TESTS_NUMBERS = new int[] { 124 };
// TESTS_RANGE = new int[] { 16, -1 };
}
Expand Down Expand Up @@ -803,4 +803,85 @@ public void testBugDiamond() throws JavaModelException {
"s2 [in <anonymous #1> [in test() [in Test2 [in [Working copy] Test2.java [in <default> [in src [in Resolve]]]]]]]",
elements);
}
public void testBug577508_1() throws JavaModelException {
this.wc = getWorkingCopy("/Resolve15/src/X.java",
"public class X {\n"
+ " public X () {\n"
+ " new Runnable() {\n"
+ " public void run () {\n"
+ " Object object = null;\n"
+ " if (object instanceof Thread thread) thread.start();\n"
+ " tryToOpenDeclarationOnThisMethod();\n"
+ " }\n"
+ " };\n"
+ " }\n"
+ " public void tryToOpenDeclarationOnThisMethod () {\n"
+ " }\n"
+ "}");
String str = this.wc.getSource();
String selection = "tryToOpenDeclarationOnThisMethod";
int start = str.indexOf(selection);
int length = selection.length();
IJavaElement[] elements = this.wc.codeSelect(start, length);
assertElementsEqual(
"Unexpected elements",
"tryToOpenDeclarationOnThisMethod() [in X [in [Working copy] X.java [in <default> [in src [in Resolve15]]]]]",
elements
);
}
public void testBug577508_2() throws JavaModelException {
this.wc = getWorkingCopy("/Resolve15/src/X.java",
"public class X {\n"
+ " public X () {\n"
+ " for (Object object : new Object[] {\"test\"}) {\n"
+ " if (object instanceof String string) {\n"
+ " System.out.println(string);\n"
+ " tryToOpenDeclarationOnThisMethod();\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " static public void tryToOpenDeclarationOnThisMethod () {\n"
+ " }\n"
+ "}");
String str = this.wc.getSource();
String selection = "tryToOpenDeclarationOnThisMethod";
int start = str.indexOf(selection);
int length = selection.length();
IJavaElement[] elements = this.wc.codeSelect(start, length);
assertElementsEqual(
"Unexpected elements",
"tryToOpenDeclarationOnThisMethod() [in X [in [Working copy] X.java [in <default> [in src [in Resolve15]]]]]",
elements
);
}
public void testBug577508_3() throws JavaModelException {
this.wc = getWorkingCopy("/Resolve15/src/X.java",
"public class X {\n"
+ " public static void main(String[] args) {\n"
+ " Set<Foo> foos = Set.of(new Foo(), new Bar());\n"
+ " for (Foo foo : foos) {\n"
+ " String string;\n"
+ " if (foo instanceof Bar bar) {\n"
+ " string = \"__\";\n"
+ " }\n"
+ " }\n"
+ " String[] names = new String[] {};\n"
+ " for (String name : names) {\n"
+ " int size = name.length();\n"
+ " }\n"
+ " }\n"
+ " static class Foo {}\n"
+ " static class Bar extends Foo {}\n"
+ "}");
String str = this.wc.getSource();
String selection = "length";
int start = str.indexOf(selection);
int length = "length".length();
IJavaElement[] elements = this.wc.codeSelect(start, length);
assertElementsEqual(
"Unexpected elements",
"length() [in String [in String.class [in java.lang [in "+ getExternalPath() + "jclMin14.jar]]]]",
elements
);
}
}
Expand Up @@ -49,6 +49,7 @@
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.SuperReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypePattern;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
Expand Down Expand Up @@ -372,6 +373,41 @@ public RecoveredElement buildInitialRecoveryState(){
this.lastCheckPoint = importRef.declarationSourceEnd + 1;
}
}
// This is copy of the code that processes astStack early in this method
for (int i = 0; i <= this.patternPtr; i++, lastNode = node) {
node = this.patternStack[i];
/* check for intermediate block creation, so recovery can properly close them afterwards */
int nodeStart = node.sourceStart;
for (int j = blockIndex; j <= this.realBlockPtr; j++){
if (this.blockStarts[j] >= 0) {
if (this.blockStarts[j] > nodeStart){
blockIndex = j; // shift the index to the new block
break;
}
if (this.blockStarts[j] != lastStart){ // avoid multiple block if at same position
block = new Block(0);
block.sourceStart = lastStart = this.blockStarts[j];
element = element.add(block, 1);
}
} else {
if (-this.blockStarts[j] > nodeStart){
blockIndex = j; // shift the index to the new block
break;
}
block = new Block(0);
block.sourceStart = lastStart = -this.blockStarts[j];
element = element.add(block, 1);
}
blockIndex = j+1; // shift the index to the new block
}

if (node instanceof TypePattern){
TypePattern pattern = (TypePattern) node;
LocalDeclaration local = pattern.getPatternVariableIntroduced();
element = element.add(local, 0);
continue;
}
}
if (this.currentToken == TokenNameRBRACE) {
if (isIndirectlyInsideLambdaExpression())
this.ignoreNextClosingBrace = true;
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -28,7 +28,6 @@
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.codeassist.impl.AssistParser;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration;
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
Expand All @@ -43,7 +42,6 @@
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
import org.eclipse.jdt.internal.compiler.ast.GuardedPattern;
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression;
import org.eclipse.jdt.internal.compiler.ast.LambdaExpression;
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
Expand Down Expand Up @@ -819,30 +817,10 @@ protected void consumeInsideCastExpressionWithQualifiedGenerics() {
pushOnElementStack(K_CAST_STATEMENT);
}
@Override
protected void consumeInstanceOfExpression() {
if (indexOfAssistIdentifier() < 0) {
super.consumeInstanceOfExpression();
int length = this.expressionLengthPtr >= 0 ?
this.expressionLengthStack[this.expressionLengthPtr] : 0;
if (length > 0) {
Expression exp = this.expressionStack[this.expressionPtr];
LocalDeclaration local = null;
if (exp instanceof InstanceOfExpression) {
local = ((InstanceOfExpression) exp).elementVariable;
} else if (exp instanceof AND_AND_Expression) {
InstanceOfExpression insExpr = (InstanceOfExpression) ((AND_AND_Expression) exp).left;
local = insExpr.elementVariable;
}
if (local != null) {
pushOnAstStack(local);
}
}
} else {
getTypeReference(this.intStack[this.intPtr--]);
this.isOrphanCompletionNode = true;
this.restartRecovery = true;
this.lastIgnoredToken = -1;
}
protected Expression consumePatternInsideInstanceof(Pattern pattern) {
Expression exp = super.consumePatternInsideInstanceof(pattern);
pushOnPatternStack(pattern); // Push it back again.
return exp;
}
@Override
protected void consumeInstanceOfExpressionWithName() {
Expand Down
Expand Up @@ -4600,7 +4600,7 @@ protected void consumeInstanceOfExpression() {
exp.sourceEnd = this.scanner.startPosition - 1;
}
}
private Expression consumePatternInsideInstanceof(Pattern pattern) {
protected Expression consumePatternInsideInstanceof(Pattern pattern) {
Expression exp;
if (pattern instanceof GuardedPattern) {
// This is a workaround as InstanceOfExpression doesn't handle a guarded pattern
Expand Down

0 comments on commit 22b4e47

Please sign in to comment.