mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Handle references with different qualifiers when extracting duplicate code (IDEA-193287)
This commit is contained in:
+12
-4
@@ -593,16 +593,24 @@ public class ParametrizedDuplicates {
|
||||
if (pattern == copy) return;
|
||||
if (pattern instanceof PsiExpression && copy instanceof PsiExpression && replaceablePatterns.contains(pattern)) {
|
||||
expressions.put((PsiExpression)pattern, (PsiExpression)copy);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pattern instanceof PsiReferenceExpression && copy instanceof PsiReferenceExpression) {
|
||||
PsiElement resolvedPattern = ((PsiReferenceExpression)pattern).resolve();
|
||||
PsiElement resolvedCopy = ((PsiReferenceExpression)copy).resolve();
|
||||
if (pattern instanceof PsiJavaCodeReferenceElement && copy instanceof PsiJavaCodeReferenceElement) {
|
||||
PsiElement resolvedPattern = ((PsiJavaCodeReferenceElement)pattern).resolve();
|
||||
PsiElement resolvedCopy = ((PsiJavaCodeReferenceElement)copy).resolve();
|
||||
if (resolvedPattern != resolvedCopy && resolvedPattern instanceof PsiVariable && resolvedCopy instanceof PsiVariable) {
|
||||
variables.put((PsiVariable)resolvedPattern, (PsiVariable)resolvedCopy);
|
||||
}
|
||||
PsiElement patternQualifier = ((PsiJavaCodeReferenceElement)pattern).getQualifier();
|
||||
PsiElement copyQualifier = ((PsiJavaCodeReferenceElement)copy).getQualifier();
|
||||
if (patternQualifier != null && copyQualifier != null) {
|
||||
collectCopyMapping(patternQualifier, copyQualifier, replaceablePatterns, expressions, variables);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (pattern instanceof PsiVariable && copy instanceof PsiVariable) {
|
||||
|
||||
if (pattern instanceof PsiVariable && copy instanceof PsiVariable) {
|
||||
variables.put((PsiVariable)pattern, (PsiVariable)copy);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
public class FullyQualifiedType {
|
||||
void foo(String s) {
|
||||
try {<selection>
|
||||
java.net.URL url = parse(s, java.net.URL.class);
|
||||
System.out.println(url);
|
||||
if (url != null) {
|
||||
System.out.println(url.toExternalForm());
|
||||
System.out.println(url.getProtocol());
|
||||
}</selection>
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
void bar(String s) {
|
||||
try {
|
||||
java.net.URL url = parse(s, java.net.URL.class);
|
||||
System.out.println(url);
|
||||
if (url != null) {
|
||||
System.out.println(url.toExternalForm());
|
||||
System.out.println(url.getProtocol());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T parse(String s, Class<T> aClass) throws Exception {
|
||||
return aClass.getDeclaredConstructor(String.class).newInstance(s);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
public class FullyQualifiedType {
|
||||
void foo(String s) {
|
||||
try {
|
||||
newMethod(s);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void newMethod(String s) throws Exception {
|
||||
java.net.URL url = parse(s, java.net.URL.class);
|
||||
System.out.println(url);
|
||||
if (url != null) {
|
||||
System.out.println(url.toExternalForm());
|
||||
System.out.println(url.getProtocol());
|
||||
}
|
||||
}
|
||||
|
||||
void bar(String s) {
|
||||
try {
|
||||
newMethod(s);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T parse(String s, Class<T> aClass) throws Exception {
|
||||
return aClass.getDeclaredConstructor(String.class).newInstance(s);
|
||||
}
|
||||
}
|
||||
@@ -299,6 +299,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
public void testDuplicatesFullyQualifiedType() throws Exception {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
public void testCodeDuplicatesWithContinueNoReturn() throws Exception {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user