mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
IDEA-117252 Extract variable bug
This commit is contained in:
@@ -17,6 +17,7 @@ package com.intellij.codeInsight;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.PsiDiamondTypeElementImpl;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
@@ -25,8 +26,12 @@ public class JavaPsiEquivalenceUtil {
|
||||
return PsiEquivalenceUtil.areElementsEquivalent(expr1, expr2, new Comparator<PsiElement>() {
|
||||
@Override
|
||||
public int compare(PsiElement o1, PsiElement o2) {
|
||||
if (o1 instanceof PsiParameter && o2 instanceof PsiParameter && ((PsiParameter)o1).getDeclarationScope() instanceof PsiMethod) {
|
||||
return ((PsiParameter)o1).getName().compareTo(((PsiParameter)o2).getName());
|
||||
if (o1 instanceof PsiParameter && o2 instanceof PsiParameter) {
|
||||
final PsiElement scope1 = ((PsiParameter)o1).getDeclarationScope();
|
||||
final PsiElement scope2 = ((PsiParameter)o2).getDeclarationScope();
|
||||
if (scope1 instanceof PsiMethod && scope2 instanceof PsiMethod && !scope1.getTextRange().intersects(scope2.getTextRange())) {
|
||||
return ((PsiParameter)o1).getName().compareTo(((PsiParameter)o2).getName());
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import java.io.PrintStream;
|
||||
class Foo {
|
||||
|
||||
public static void bazz(int i) {
|
||||
Foo j = new Foo(new IBar() {
|
||||
public void doSomething(PrintStream out) {
|
||||
out.println("hello");
|
||||
}
|
||||
});
|
||||
final Foo foo = i != 0 ? j : j;
|
||||
foo.bla();
|
||||
}
|
||||
|
||||
private final IBar bar;
|
||||
|
||||
public Foo(IBar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public void bla() {
|
||||
bar.doSomething(System.out);
|
||||
}
|
||||
|
||||
public interface IBar {
|
||||
void doSomething(PrintStream out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import java.io.PrintStream;
|
||||
class Foo {
|
||||
|
||||
public static void bazz(int i) {
|
||||
final Foo foo = i != 0 ? <selection>new Foo(new IBar() {
|
||||
public void doSomething(PrintStream out) {
|
||||
out.println("hello");
|
||||
}
|
||||
})</selection> : new Foo(new IBar() {
|
||||
public void doSomething(PrintStream out) {
|
||||
out.println("hello");
|
||||
}
|
||||
});
|
||||
foo.bla();
|
||||
}
|
||||
|
||||
private final IBar bar;
|
||||
|
||||
public Foo(IBar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public void bla() {
|
||||
bar.doSomething(System.out);
|
||||
}
|
||||
|
||||
public interface IBar {
|
||||
void doSomething(PrintStream out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class ExtractVariableSample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Collection<String> strings = new ArrayList<>();
|
||||
new Object() {
|
||||
public void foo(String s) {
|
||||
System.out.println( s.hashCode());
|
||||
}
|
||||
};
|
||||
|
||||
for (String s : strings) {
|
||||
int j = s.hashCode();
|
||||
System.out.println(j);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class ExtractVariableSample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Collection<String> strings = new ArrayList<>();
|
||||
new Object() {
|
||||
public void foo(String s) {
|
||||
System.out.println( s.hashCode());
|
||||
}
|
||||
};
|
||||
|
||||
for (String s : strings) {
|
||||
System.out.println(<selection>s.hashCode()</selection>);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class ExtractVariableSample {
|
||||
|
||||
public static void main(String[] args, String s) {
|
||||
Collection<String> strings = new ArrayList<>();
|
||||
new Object() {
|
||||
public void foo(String s) {
|
||||
System.out.println( s.hashCode());
|
||||
}
|
||||
};
|
||||
|
||||
int j = s.hashCode();
|
||||
System.out.println(j);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class ExtractVariableSample {
|
||||
|
||||
public static void main(String[] args, String s) {
|
||||
Collection<String> strings = new ArrayList<>();
|
||||
new Object() {
|
||||
public void foo(String s) {
|
||||
System.out.println( s.hashCode());
|
||||
}
|
||||
};
|
||||
|
||||
System.out.println(<selection>s.hashCode()</selection>);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -78,6 +78,18 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
|
||||
doTest(new MockIntroduceVariableHandler("j", true, false, false, "int"));
|
||||
}
|
||||
|
||||
public void testAnonymousClass3() throws Exception {
|
||||
doTest(new MockIntroduceVariableHandler("j", true, false, false, "Foo"));
|
||||
}
|
||||
|
||||
public void testAnonymousClass4() throws Exception {
|
||||
doTest(new MockIntroduceVariableHandler("j", true, false, false, "int"));
|
||||
}
|
||||
|
||||
public void testAnonymousClass5() throws Exception {
|
||||
doTest(new MockIntroduceVariableHandler("j", true, false, false, "int"));
|
||||
}
|
||||
|
||||
public void testParenthized() throws Exception {
|
||||
doTest(new MockIntroduceVariableHandler("temp", true, false, false, "int"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user