From b1b64fcb51875a1a354135ef635cb5b1add486da Mon Sep 17 00:00:00 2001 From: Noopur Gupta Date: Wed, 30 Apr 2014 22:28:25 +0200 Subject: [PATCH] Bug 424745: [1.8][inline] Error after inlining constant with static method reference as initializer --- .../test1004/in/TestInlineMethodRef_Enum.java | 20 +++ .../out/TestInlineMethodRef_Enum.java | 18 ++ .../refactoring/InlineConstantTests18.java | 164 +++++++++--------- .../code/InlineConstantRefactoring.java | 11 ++ 4 files changed, 131 insertions(+), 82 deletions(-) create mode 100644 org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/in/TestInlineMethodRef_Enum.java create mode 100644 org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/out/TestInlineMethodRef_Enum.java diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/in/TestInlineMethodRef_Enum.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/in/TestInlineMethodRef_Enum.java new file mode 100644 index 0000000000..0473a374b4 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/in/TestInlineMethodRef_Enum.java @@ -0,0 +1,20 @@ +// 5, 28 -> 5, 30 replaceAll = true, removeDeclaration = true +package p; + +class Test { + public static final FI f1 = Test::m; + + static int m(int x) { + return x--; + } +} + +enum E { + E_C1(Test.f1); // [1] + E(FI fi) {} +} + +@FunctionalInterface +interface FI { + int foo(int x); +} \ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/out/TestInlineMethodRef_Enum.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/out/TestInlineMethodRef_Enum.java new file mode 100644 index 0000000000..f4d7123eb6 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline18/test1004/out/TestInlineMethodRef_Enum.java @@ -0,0 +1,18 @@ +// 5, 28 -> 5, 30 replaceAll = true, removeDeclaration = true +package p; + +class Test { + static int m(int x) { + return x--; + } +} + +enum E { + E_C1(Test::m); // [1] + E(FI fi) {} +} + +@FunctionalInterface +interface FI { + int foo(int x); +} \ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests18.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests18.java index c845b6ef83..8803cabab2 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests18.java +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests18.java @@ -1,82 +1,82 @@ -/******************************************************************************* - * Copyright (c) 2013 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * This is an implementation of an early-draft specification developed under the Java - * Community Process (JCP) and is made available for testing and evaluation purposes - * only. The code is not compatible with any specification of the JCP. - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.jdt.ui.tests.refactoring; - -import junit.framework.Test; - -public class InlineConstantTests18 extends InlineConstantTests { - private static final Class clazz= InlineConstantTests18.class; - - public InlineConstantTests18(String name) { - super(name); - } - - @Override - protected String successPath() { - return toSucceed ? "/canInline18/" : "/cannotInline18/"; - } - - public static Test suite() { - return new Java18Setup(new NoSuperTestsSuite(clazz)); - } - - public static Test setUpTest(Test someTest) { - return new Java18Setup(someTest); - } - - //--- Test lambda expressions - - public void test0() throws Exception { - helper1("p.TestInlineLambda", 5, 28, 5, 30, true, true); - } - - public void test1() throws Exception { - helper1("p.TestInlineLambda_Cast", 5, 28, 5, 30, true, true); - } - - public void test2() throws Exception { - helper1("p.TestInlineLambdaArray", 5, 30, 5, 35, true, true); - } - - public void test3() throws Exception { - helper1("p.TestInlineLambda_Ambiguous", 5, 28, 5, 30, true, true); - } - - public void test4() throws Exception { - helper1("p.TestInlineLambda_Ambiguous", 5, 28, 5, 30, true, true); - } - - public void test5() throws Exception { - helper1("p.TestInlineLambda_Cast", 15, 30, 15, 36, true, true); - } - - //--- Test method references - - public void test1000() throws Exception { - helper1("p.TestInlineMethodRef", 5, 28, 5, 30, true, true); - } - - public void test1001() throws Exception { - helper1("p.TestInlineMethodRef_Cast", 5, 28, 5, 30, true, true); - } - - public void test1002() throws Exception { - helper1("p.TestInlineMethodRefArray", 5, 30, 5, 35, true, true); - } - - public void test1003() throws Exception { - helper1("p.TestInlineMethodRef_Ambiguous", 5, 28, 5, 30, true, true); - } -} +/******************************************************************************* + * Copyright (c) 2013, 2014 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.refactoring; + +import junit.framework.Test; + +public class InlineConstantTests18 extends InlineConstantTests { + private static final Class clazz= InlineConstantTests18.class; + + public InlineConstantTests18(String name) { + super(name); + } + + @Override + protected String successPath() { + return toSucceed ? "/canInline18/" : "/cannotInline18/"; + } + + public static Test suite() { + return new Java18Setup(new NoSuperTestsSuite(clazz)); + } + + public static Test setUpTest(Test someTest) { + return new Java18Setup(someTest); + } + + //--- Test lambda expressions + + public void test0() throws Exception { + helper1("p.TestInlineLambda", 5, 28, 5, 30, true, true); + } + + public void test1() throws Exception { + helper1("p.TestInlineLambda_Cast", 5, 28, 5, 30, true, true); + } + + public void test2() throws Exception { + helper1("p.TestInlineLambdaArray", 5, 30, 5, 35, true, true); + } + + public void test3() throws Exception { + helper1("p.TestInlineLambda_Ambiguous", 5, 28, 5, 30, true, true); + } + + public void test4() throws Exception { + helper1("p.TestInlineLambda_Ambiguous", 5, 28, 5, 30, true, true); + } + + public void test5() throws Exception { + helper1("p.TestInlineLambda_Cast", 15, 30, 15, 36, true, true); + } + + //--- Test method references + + public void test1000() throws Exception { + helper1("p.TestInlineMethodRef", 5, 28, 5, 30, true, true); + } + + public void test1001() throws Exception { + helper1("p.TestInlineMethodRef_Cast", 5, 28, 5, 30, true, true); + } + + public void test1002() throws Exception { + helper1("p.TestInlineMethodRefArray", 5, 30, 5, 35, true, true); + } + + public void test1003() throws Exception { + helper1("p.TestInlineMethodRef_Ambiguous", 5, 28, 5, 30, true, true); + } + + public void test1004() throws Exception { + helper1("p.TestInlineMethodRef_Enum", 5, 28, 5, 30, true, true); + } +} diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java index f7d98143da..f4a1efdbef 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java @@ -62,6 +62,7 @@ import org.eclipse.jdt.core.dom.CastExpression; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.ExpressionMethodReference; import org.eclipse.jdt.core.dom.FieldAccess; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.IBinding; @@ -77,8 +78,11 @@ import org.eclipse.jdt.core.dom.QualifiedName; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; +import org.eclipse.jdt.core.dom.SuperMethodReference; import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.TypeDeclarationStatement; +import org.eclipse.jdt.core.dom.TypeMethodReference; import org.eclipse.jdt.core.dom.VariableDeclaration; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; @@ -309,6 +313,13 @@ public boolean visit(MethodInvocation invocation) { @Override public boolean visit(Name name) { + StructuralPropertyDescriptor locationInParent= name.getLocationInParent(); + if (locationInParent == ExpressionMethodReference.NAME_PROPERTY + || locationInParent == TypeMethodReference.NAME_PROPERTY + || locationInParent == SuperMethodReference.NAME_PROPERTY) { + return false; + } + SimpleName leftmost= getLeftmost(name); IBinding leftmostBinding= leftmost.resolveBinding();