Skip to content

Commit c8117ed

Browse files
authored
Fix Bug 240537 (eclipse-jdt#2211)
- add checks in CallInliner.computeRealArguments() when adding local variables to add instanceof check for the variable if it is a cast expression and the initial invocation is in a conditional with instanceof checking - add new test to InlineMethodTests - fixes eclipse-jdt#2210
1 parent 3d780e8 commit c8117ed

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/CallInliner.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.eclipse.jdt.core.dom.BodyDeclaration;
5959
import org.eclipse.jdt.core.dom.CastExpression;
6060
import org.eclipse.jdt.core.dom.CompilationUnit;
61+
import org.eclipse.jdt.core.dom.ConditionalExpression;
6162
import org.eclipse.jdt.core.dom.DoStatement;
6263
import org.eclipse.jdt.core.dom.EnhancedForStatement;
6364
import org.eclipse.jdt.core.dom.Expression;
@@ -69,6 +70,7 @@
6970
import org.eclipse.jdt.core.dom.ITypeBinding;
7071
import org.eclipse.jdt.core.dom.IVariableBinding;
7172
import org.eclipse.jdt.core.dom.IfStatement;
73+
import org.eclipse.jdt.core.dom.InstanceofExpression;
7274
import org.eclipse.jdt.core.dom.LabeledStatement;
7375
import org.eclipse.jdt.core.dom.LambdaExpression;
7476
import org.eclipse.jdt.core.dom.MethodDeclaration;
@@ -104,6 +106,7 @@
104106
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
105107
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
106108
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
109+
import org.eclipse.jdt.internal.corext.dom.AbortSearchException;
107110
import org.eclipse.jdt.internal.corext.dom.CodeScopeBuilder;
108111
import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
109112
import org.eclipse.jdt.internal.corext.dom.LocalVariableIndex;
@@ -518,6 +521,14 @@ public TextEdit getModifications() {
518521
return fRewrite.rewriteAST(fBuffer.getDocument(), fCUnit.getOptions(true));
519522
}
520523

524+
525+
private class InstanceofChecker extends ASTVisitor {
526+
@Override
527+
public boolean visit(InstanceofExpression node) {
528+
throw new AbortSearchException();
529+
}
530+
}
531+
521532
private void computeRealArguments() {
522533
List<Expression> arguments= Invocations.getArguments(fInvocation);
523534
Set<Expression> canNotInline= crossCheckArguments(arguments);
@@ -533,7 +544,36 @@ private void computeRealArguments() {
533544
} else {
534545
String name= fInvocationScope.createName(parameter.getName(), true);
535546
realArguments[i]= ast.newSimpleName(name);
536-
VariableDeclarationStatement local= createLocalDeclaration(parameter.getTypeBinding(), name, (Expression) fRewrite.createCopyTarget(expression));
547+
boolean needInstanceofCheck= false;
548+
if (expression instanceof CastExpression) {
549+
ASTNode ancestor= ASTNodes.getFirstAncestorOrNull(fInvocation, ConditionalExpression.class, Statement.class);
550+
while (ancestor instanceof ConditionalExpression condExp) {
551+
InstanceofChecker checker= new InstanceofChecker();
552+
Expression posExp= condExp.getExpression();
553+
try {
554+
posExp.accept(checker);
555+
} catch (AbortSearchException e) {
556+
needInstanceofCheck= true;
557+
break;
558+
}
559+
}
560+
}
561+
Expression newExp= null;
562+
if (needInstanceofCheck) {
563+
CastExpression castExp= (CastExpression)expression;
564+
Type t= castExp.getType();
565+
InstanceofExpression instExp= ast.newInstanceofExpression();
566+
instExp.setRightOperand((Type) fRewrite.createCopyTarget(t));
567+
instExp.setLeftOperand((Expression)fRewrite.createCopyTarget(castExp.getExpression()));
568+
ConditionalExpression condExp= ast.newConditionalExpression();
569+
condExp.setExpression(instExp);
570+
condExp.setThenExpression((Expression)fRewrite.createCopyTarget(expression));
571+
condExp.setElseExpression(ast.newNullLiteral());
572+
newExp= condExp;
573+
} else {
574+
newExp= (Expression)fRewrite.createCopyTarget(expression);
575+
}
576+
VariableDeclarationStatement local= createLocalDeclaration(parameter.getTypeBinding(), name, newExp);
537577
if (parameter.isFinal()) {
538578
local.modifiers().add(fInvocation.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD));
539579
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2210 {
4+
enum EntityType{X}
5+
static class Case extends Bug{}
6+
static class Leaf extends Bug{}
7+
static class CaseNote extends Leaf{}
8+
static class SuperView extends Leaf{}
9+
static class CaseEntity extends Leaf{
10+
EntityType getEntityType(){return null;}
11+
}
12+
public static void main(String...args){
13+
System.out.println(fetch(new Case()));
14+
}
15+
static String /*]*/getId/*[*/(final Leaf leaf){ // LINE 13
16+
return leaf instanceof CaseNote?"a":
17+
leaf instanceof CaseEntity?
18+
editorId(((CaseEntity)leaf).
19+
getEntityType()):"b";
20+
}
21+
static String editorId(final EntityType type){
22+
return""+type;
23+
}
24+
static String fetch(final Bug record){
25+
return record instanceof SuperView?"c":
26+
record instanceof Case?
27+
"DONE!":getId((Leaf)record);
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2210 {
4+
enum EntityType{X}
5+
static class Case extends Bug{}
6+
static class Leaf extends Bug{}
7+
static class CaseNote extends Leaf{}
8+
static class SuperView extends Leaf{}
9+
static class CaseEntity extends Leaf{
10+
EntityType getEntityType(){return null;}
11+
}
12+
public static void main(String...args){
13+
System.out.println(fetch(new Case()));
14+
}
15+
static String editorId(final EntityType type){
16+
return""+type;
17+
}
18+
static String fetch(final Bug record){
19+
final Leaf leaf = record instanceof Leaf ? (Leaf)record : null;
20+
return record instanceof SuperView?"c":
21+
record instanceof Case?
22+
"DONE!":leaf instanceof CaseNote?"a":
23+
leaf instanceof CaseEntity?
24+
editorId(((CaseEntity)leaf).
25+
getEntityType()):"b";
26+
}
27+
}

org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ public void test_issue_2118_3() throws Exception {
527527
performBugTest();
528528
}
529529

530+
@Test
531+
public void test_issue_2210() throws Exception {
532+
performBugTest();
533+
}
534+
530535
/* *********************** Argument Tests ******************************* */
531536

532537
private void performArgumentTest() throws Exception {

0 commit comments

Comments
 (0)