Skip to content

Commit 074c7dc

Browse files
authored
Fix illegal name exception when inlining qualified field name (#1718)
- fix InlineTempRefactoring.getAlternativeQualification() to not calculate qualified alternatives if not dealing with static reference - fix InlineTempRefactoring.checkClashes() to handle when there are qualified alternatives - add new test to InlineTempTests - fixes #1705
1 parent c2ceaa2 commit 074c7dc

File tree

4 files changed

+123
-76
lines changed

4 files changed

+123
-76
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,18 @@ private RefactoringStatus checkClashes(VariableDeclaration declaration) {
262262
for (Name name : initializerNames) {
263263
if (clashesWithNewVariables(newVariables, name)) {
264264
List<Expression> alternativeQualifications= getAlternativeQualifications(reference, name);
265-
boolean inlinable= false;
266-
for (Expression alternative : alternativeQualifications) {
267-
if (!clashesWithNewVariables(newVariables, alternative)) {
268-
inlinable= true;
269-
break;
265+
if (alternativeQualifications.size() > 0) {
266+
boolean inlinable= false;
267+
for (Expression alternative : alternativeQualifications) {
268+
if (!clashesWithNewVariables(newVariables, alternative)) {
269+
inlinable= true;
270+
break;
271+
}
272+
}
273+
if (!inlinable) {
274+
return RefactoringStatus.createFatalErrorStatus(
275+
Messages.format(RefactoringCoreMessages.InlineTemRefactoring_error_message_inliningClashes, name));
270276
}
271-
}
272-
if (!inlinable) {
273-
return RefactoringStatus.createFatalErrorStatus(
274-
Messages.format(RefactoringCoreMessages.InlineTemRefactoring_error_message_inliningClashes, name));
275277
}
276278
}
277279
}
@@ -652,13 +654,19 @@ private List<Expression> getAlternativeQualifications(SimpleName reference, Name
652654
}
653655
} else if (initializerName instanceof QualifiedName){
654656
QualifiedName initializerQualifiedName = (QualifiedName) initializerName;
655-
SimpleName simpleName= initializerQualifiedName.getName();
656-
int i= findCommonDeclaringClassIndex(reference, initializerQualifiedName);
657-
ITypeBinding initializerQualifiedNameTypeBinding= initializerQualifiedName.getQualifier().resolveTypeBinding();
658-
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, false));
659-
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, i, false));
660-
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, true));
661-
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, i, true));
657+
IBinding resolveBinding= initializerQualifiedName.resolveBinding();
658+
if (resolveBinding instanceof IVariableBinding resolvedVariableBinding) {
659+
boolean isStatic= Modifier.isStatic(resolvedVariableBinding.getModifiers());
660+
if (isStatic) {
661+
SimpleName simpleName= initializerQualifiedName.getName();
662+
int i= findCommonDeclaringClassIndex(reference, initializerQualifiedName);
663+
ITypeBinding initializerQualifiedNameTypeBinding= initializerQualifiedName.getQualifier().resolveTypeBinding();
664+
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, false));
665+
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, i, false));
666+
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, true));
667+
ans.add(createFullyQualifiedName(simpleName, initializerQualifiedNameTypeBinding, i, true));
668+
}
669+
}
662670
}
663671
return ans;
664672
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package p;
2+
3+
public class A {
4+
public static void main(String[] args) {
5+
String[] a= new String[0];
6+
int length= a.length;
7+
if (length > 0) {
8+
System.out.println("Longer"); //$NON-NLS-1$
9+
}
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package p;
2+
3+
public class A {
4+
public static void main(String[] args) {
5+
String[] a= new String[0];
6+
if (a.length > 0) {
7+
System.out.println("Longer"); //$NON-NLS-1$
8+
}
9+
}
10+
}

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

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -347,66 +347,6 @@ public void test40() throws Exception{
347347
helper1(5, 43, 5, 46);
348348
}
349349

350-
public void test62() throws Exception {
351-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
352-
helper1(14, 13, 14, 14);
353-
}
354-
355-
public void test63() throws Exception {
356-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
357-
helper1(11, 13, 11, 14);
358-
}
359-
360-
public void test64() throws Exception {
361-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
362-
helper1(7, 13, 7, 14);
363-
}
364-
365-
public void test53() throws Exception {
366-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
367-
helper1(12, 13, 12, 14);
368-
}
369-
370-
public void test54() throws Exception {
371-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
372-
helper1(7, 13, 7, 14);
373-
}
374-
375-
public void test55() throws Exception {
376-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
377-
helper1(11, 13, 11, 14);
378-
}
379-
380-
public void test56() throws Exception {
381-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
382-
helper1(12, 13, 12, 14);
383-
}
384-
385-
public void test57() throws Exception {
386-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
387-
helper1(9, 26, 9, 27);
388-
}
389-
390-
public void test58() throws Exception {
391-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
392-
helper1(9, 26, 9, 27);
393-
}
394-
395-
public void test59() throws Exception {
396-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
397-
helper1(11, 11, 11, 12);
398-
}
399-
400-
public void test60() throws Exception {
401-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
402-
helper1(7, 13, 7, 14);
403-
}
404-
405-
public void test61() throws Exception {
406-
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
407-
helper1(9, 13, 9, 14);
408-
}
409-
410350
@Test
411351
public void test41() throws Exception {
412352
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
@@ -479,6 +419,84 @@ public void test52() throws Exception {
479419
helper1(4, 14, 4, 15);
480420
}
481421

422+
@Test
423+
public void test53() throws Exception {
424+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
425+
helper1(12, 13, 12, 14);
426+
}
427+
428+
@Test
429+
public void test54() throws Exception {
430+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
431+
helper1(7, 13, 7, 14);
432+
}
433+
434+
@Test
435+
public void test55() throws Exception {
436+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
437+
helper1(11, 13, 11, 14);
438+
}
439+
440+
@Test
441+
public void test56() throws Exception {
442+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
443+
helper1(12, 13, 12, 14);
444+
}
445+
446+
@Test
447+
public void test57() throws Exception {
448+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
449+
helper1(9, 26, 9, 27);
450+
}
451+
452+
@Test
453+
public void test58() throws Exception {
454+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
455+
helper1(9, 26, 9, 27);
456+
}
457+
458+
@Test
459+
public void test59() throws Exception {
460+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
461+
helper1(11, 11, 11, 12);
462+
}
463+
464+
@Test
465+
public void test60() throws Exception {
466+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
467+
helper1(7, 13, 7, 14);
468+
}
469+
470+
@Test
471+
public void test61() throws Exception {
472+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
473+
helper1(9, 13, 9, 14);
474+
}
475+
476+
@Test
477+
public void test62() throws Exception {
478+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
479+
helper1(14, 13, 14, 14);
480+
}
481+
482+
@Test
483+
public void test63() throws Exception {
484+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
485+
helper1(11, 13, 11, 14);
486+
}
487+
488+
@Test
489+
public void test64() throws Exception {
490+
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=367536
491+
helper1(7, 13, 7, 14);
492+
}
493+
494+
@Test
495+
public void test65() throws Exception {
496+
//https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/1705
497+
helper1(6, 13, 6, 19);
498+
}
499+
482500
//------
483501

484502
@Ignore("compile errors are ok now")

0 commit comments

Comments
 (0)