View | Details | Raw Unified | Return to bug 430573 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest18.java (+280 lines)
Lines 1304-1309 public class AssistQuickFixTest18 extends QuickFixTest { Link Here
1304
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1304
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1305
	}
1305
	}
1306
1306
1307
	public void testConvertToAnonymousClassCreation8() throws Exception {
1308
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1309
		StringBuffer buf= new StringBuffer();
1310
		buf.append("package test1;\n");
1311
		buf.append("import java.util.function.IntSupplier;\n");
1312
		buf.append("public class C {\n");
1313
		buf.append("    C() {\n");
1314
		buf.append("        IntSupplier i = () -> this.m();\n");
1315
		buf.append("    }\n");
1316
		buf.append("\n");
1317
		buf.append("    public int m() {\n");
1318
		buf.append("        return 7;\n");
1319
		buf.append("    }\n");
1320
		buf.append("}\n");
1321
		
1322
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1323
1324
		int offset= buf.toString().indexOf("->");
1325
		AssistContext context= getCorrectionContext(cu, offset, 0);
1326
		assertNoErrors(context);
1327
		List proposals= collectAssists(context, false);
1328
1329
		assertNumberOfProposals(proposals, 4);
1330
		assertCorrectLabels(proposals);
1331
1332
		buf= new StringBuffer();
1333
		buf.append("package test1;\n");
1334
		buf.append("import java.util.function.IntSupplier;\n");
1335
		buf.append("public class C {\n");
1336
		buf.append("    C() {\n");
1337
		buf.append("        IntSupplier i = new IntSupplier() {\n");
1338
		buf.append("            @Override\n");
1339
		buf.append("            public int getAsInt() {\n");
1340
		buf.append("                return C.this.m();\n");
1341
		buf.append("            }\n");
1342
		buf.append("        };\n");
1343
		buf.append("    }\n");
1344
		buf.append("\n");
1345
		buf.append("    public int m() {\n");
1346
		buf.append("        return 7;\n");
1347
		buf.append("    }\n");
1348
		buf.append("}\n");
1349
1350
1351
		String expected1= buf.toString();
1352
1353
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1354
	}
1355
1356
	public void testConvertToAnonymousClassCreation9() throws Exception {
1357
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1358
		StringBuffer buf= new StringBuffer();
1359
		buf.append("package test1;\n");
1360
		buf.append("import java.util.function.IntSupplier;\n");
1361
		buf.append("public class C {\n");
1362
		buf.append("    int varA;\n");
1363
		buf.append("    C() {\n");
1364
		buf.append("        IntSupplier i = () -> this.varA;\n");
1365
		buf.append("    }\n");
1366
		buf.append("\n");
1367
		buf.append("    public int m() {\n");
1368
		buf.append("        return 7;\n");
1369
		buf.append("    }\n");
1370
		buf.append("}\n");
1371
1372
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1373
1374
		int offset= buf.toString().indexOf("->");
1375
		AssistContext context= getCorrectionContext(cu, offset, 0);
1376
		assertNoErrors(context);
1377
		List proposals= collectAssists(context, false);
1378
1379
		assertNumberOfProposals(proposals, 4);
1380
		assertCorrectLabels(proposals);
1381
1382
		buf= new StringBuffer();
1383
		buf.append("package test1;\n");
1384
		buf.append("import java.util.function.IntSupplier;\n");
1385
		buf.append("public class C {\n");
1386
		buf.append("    int varA;\n");
1387
		buf.append("    C() {\n");
1388
		buf.append("        IntSupplier i = new IntSupplier() {\n");
1389
		buf.append("            @Override\n");
1390
		buf.append("            public int getAsInt() {\n");
1391
		buf.append("                return C.this.varA;\n");
1392
		buf.append("            }\n");
1393
		buf.append("        };\n");
1394
		buf.append("    }\n");
1395
		buf.append("\n");
1396
		buf.append("    public int m() {\n");
1397
		buf.append("        return 7;\n");
1398
		buf.append("    }\n");
1399
		buf.append("}\n");
1400
1401
1402
		String expected1= buf.toString();
1403
1404
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1405
	}
1406
1407
	public void testConvertToAnonymousClassCreation10() throws Exception {
1408
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1409
		StringBuffer buf= new StringBuffer();
1410
		buf.append("package test1;\n");
1411
		buf.append("import java.util.function.IntSupplier;\n");
1412
		buf.append("public class B extends C {\n");
1413
		buf.append("    int var;\n");
1414
		buf.append("    B() {\n");
1415
		buf.append("        IntSupplier i = () -> {\n");
1416
		buf.append("            System.out.println(this.var);\n");
1417
		buf.append("            System.out.println(this.varC);\n");
1418
		buf.append("            super.o();\n");
1419
		buf.append("            return this.n();\n");
1420
		buf.append("        };\n");
1421
		buf.append("    }\n");
1422
		buf.append("\n");
1423
		buf.append("    public int n() {\n");
1424
		buf.append("        return 7;\n");
1425
		buf.append("    }\n");
1426
		buf.append("}\n");
1427
		buf.append("\n");
1428
		buf.append("class C {\n");
1429
		buf.append("    int varC;\n");
1430
		buf.append("    public void o() {}\n");
1431
		buf.append("}\n");
1432
1433
		ICompilationUnit cu= pack1.createCompilationUnit("B.java", buf.toString(), false, null);
1434
1435
		int offset= buf.toString().indexOf("->");
1436
		AssistContext context= getCorrectionContext(cu, offset, 0);
1437
		assertNoErrors(context);
1438
		List proposals= collectAssists(context, false);
1439
1440
		assertNumberOfProposals(proposals, 3);
1441
		assertCorrectLabels(proposals);
1442
1443
		buf= new StringBuffer();
1444
		buf.append("package test1;\n");
1445
		buf.append("import java.util.function.IntSupplier;\n");
1446
		buf.append("public class B extends C {\n");
1447
		buf.append("    int var;\n");
1448
		buf.append("    B() {\n");
1449
		buf.append("        IntSupplier i = new IntSupplier() {\n");
1450
		buf.append("            @Override\n");
1451
		buf.append("            public int getAsInt() {\n");
1452
		buf.append("                System.out.println(B.this.var);\n");
1453
		buf.append("                System.out.println(B.this.varC);\n");
1454
		buf.append("                B.super.o();\n");
1455
		buf.append("                return B.this.n();\n");
1456
		buf.append("            }\n");
1457
		buf.append("        };\n");
1458
		buf.append("    }\n");
1459
		buf.append("\n");
1460
		buf.append("    public int n() {\n");
1461
		buf.append("        return 7;\n");
1462
		buf.append("    }\n");
1463
		buf.append("}\n");
1464
		buf.append("\n");
1465
		buf.append("class C {\n");
1466
		buf.append("    int varC;\n");
1467
		buf.append("    public void o() {}\n");
1468
		buf.append("}\n");
1469
1470
1471
		String expected1= buf.toString();
1472
1473
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1474
	}
1475
1476
	public void testConvertToAnonymousClassCreation11() throws Exception {
1477
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1478
		StringBuffer buf= new StringBuffer();
1479
		buf.append("package test1;\n");
1480
		buf.append("import java.util.function.IntSupplier;\n");
1481
		buf.append("public class D {\n");
1482
		buf.append("    D() {\n");
1483
		buf.append("        Runnable r = new Runnable() {\n");
1484
		buf.append("            int x= 10;\n");
1485
		buf.append("            @Override\n");
1486
		buf.append("            public void run() {\n");
1487
		buf.append("                IntSupplier i = () -> this.x;\n");
1488
		buf.append("            }\n");
1489
		buf.append("        };\n");
1490
		buf.append("    }\n");
1491
		buf.append("}\n");
1492
1493
		ICompilationUnit cu= pack1.createCompilationUnit("D.java", buf.toString(), false, null);
1494
1495
		int offset= buf.toString().indexOf("->");
1496
		AssistContext context= getCorrectionContext(cu, offset, 0);
1497
		assertNoErrors(context);
1498
		List proposals= collectAssists(context, false);
1499
1500
		assertNumberOfProposals(proposals, 4);
1501
		assertCorrectLabels(proposals);
1502
1503
		buf= new StringBuffer();
1504
		buf.append("package test1;\n");
1505
		buf.append("import java.util.function.IntSupplier;\n");
1506
		buf.append("public class D {\n");
1507
		buf.append("    D() {\n");
1508
		buf.append("        Runnable r = new Runnable() {\n");
1509
		buf.append("            int x= 10;\n");
1510
		buf.append("            @Override\n");
1511
		buf.append("            public void run() {\n");
1512
		buf.append("                IntSupplier i = new IntSupplier() {\n");
1513
		buf.append("                    @Override\n");
1514
		buf.append("                    public int getAsInt() {\n");
1515
		buf.append("                        return x;\n");
1516
		buf.append("                    }\n");
1517
		buf.append("                };\n");
1518
		buf.append("            }\n");
1519
		buf.append("        };\n");
1520
		buf.append("    }\n");
1521
1522
1523
		String expected1= buf.toString();
1524
1525
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1526
	}
1527
1528
	public void testConvertToAnonymousClassCreation12() throws Exception {
1529
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1530
		StringBuffer buf= new StringBuffer();
1531
		buf.append("package test1;\n");
1532
		buf.append("import java.util.function.IntSupplier;\n");
1533
		buf.append("public class D {\n");
1534
		buf.append("    D() {\n");
1535
		buf.append("        Runnable r = new Runnable() {\n");
1536
		buf.append("            @Override\n");
1537
		buf.append("            public void run() {\n");
1538
		buf.append("                IntSupplier i = () -> D.this.n();\n");
1539
		buf.append("            }\n");
1540
		buf.append("        };\n");
1541
		buf.append("    }\n");
1542
		buf.append("\n");
1543
		buf.append("    public int n() {\n");
1544
		buf.append("        return 7;\n");
1545
		buf.append("    }\n");
1546
		buf.append("}\n");
1547
1548
		ICompilationUnit cu= pack1.createCompilationUnit("D.java", buf.toString(), false, null);
1549
1550
		int offset= buf.toString().indexOf("->");
1551
		AssistContext context= getCorrectionContext(cu, offset, 0);
1552
		assertNoErrors(context);
1553
		List proposals= collectAssists(context, false);
1554
1555
		assertNumberOfProposals(proposals, 4);
1556
		assertCorrectLabels(proposals);
1557
1558
		buf= new StringBuffer();
1559
		buf.append("package test1;\n");
1560
		buf.append("import java.util.function.IntSupplier;\n");
1561
		buf.append("public class D {\n");
1562
		buf.append("    D() {\n");
1563
		buf.append("        Runnable r = new Runnable() {\n");
1564
		buf.append("            @Override\n");
1565
		buf.append("            public void run() {\n");
1566
		buf.append("                IntSupplier i = new IntSupplier() {\n");
1567
		buf.append("                    @Override\n");
1568
		buf.append("                    public int getAsInt() {\n");
1569
		buf.append("                        return D.this.n();\n");
1570
		buf.append("                    }\n");
1571
		buf.append("                };\n");
1572
		buf.append("            }\n");
1573
		buf.append("        };\n");
1574
		buf.append("    }\n");
1575
		buf.append("\n");
1576
		buf.append("    public int n() {\n");
1577
		buf.append("        return 7;\n");
1578
		buf.append("    }\n");
1579
		buf.append("}\n");
1580
1581
1582
		String expected1= buf.toString();
1583
1584
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1585
	}
1586
1307
	public void testConvertToAnonymousClassCreationWithParameterName() throws Exception {
1587
	public void testConvertToAnonymousClassCreationWithParameterName() throws Exception {
1308
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1588
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1309
		StringBuffer buf= new StringBuffer();
1589
		StringBuffer buf= new StringBuffer();
(-)a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/LambdaExpressionsFix.java (-1 / +71 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Jerome Cambon <jerome.cambon@oracle.com> - [1.8][clean up][quick assist] Convert lambda to anonymous must qualify references to 'this'/'super' - https://bugs.eclipse.org/430573
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.corext.fix;
12
package org.eclipse.jdt.internal.corext.fix;
12
13
Lines 33-44 import org.eclipse.jdt.core.dom.ClassInstanceCreation; Link Here
33
import org.eclipse.jdt.core.dom.CompilationUnit;
34
import org.eclipse.jdt.core.dom.CompilationUnit;
34
import org.eclipse.jdt.core.dom.Expression;
35
import org.eclipse.jdt.core.dom.Expression;
35
import org.eclipse.jdt.core.dom.ExpressionStatement;
36
import org.eclipse.jdt.core.dom.ExpressionStatement;
37
import org.eclipse.jdt.core.dom.FieldAccess;
36
import org.eclipse.jdt.core.dom.IBinding;
38
import org.eclipse.jdt.core.dom.IBinding;
37
import org.eclipse.jdt.core.dom.IMethodBinding;
39
import org.eclipse.jdt.core.dom.IMethodBinding;
38
import org.eclipse.jdt.core.dom.ITypeBinding;
40
import org.eclipse.jdt.core.dom.ITypeBinding;
39
import org.eclipse.jdt.core.dom.LambdaExpression;
41
import org.eclipse.jdt.core.dom.LambdaExpression;
40
import org.eclipse.jdt.core.dom.MethodDeclaration;
42
import org.eclipse.jdt.core.dom.MethodDeclaration;
41
import org.eclipse.jdt.core.dom.MethodInvocation;
43
import org.eclipse.jdt.core.dom.MethodInvocation;
44
import org.eclipse.jdt.core.dom.Name;
42
import org.eclipse.jdt.core.dom.ReturnStatement;
45
import org.eclipse.jdt.core.dom.ReturnStatement;
43
import org.eclipse.jdt.core.dom.SimpleName;
46
import org.eclipse.jdt.core.dom.SimpleName;
44
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
47
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
Lines 110-115 public class LambdaExpressionsFix extends CompilationUnitRewriteOperationsFix { Link Here
110
			return true;
113
			return true;
111
		}
114
		}
112
	}
115
	}
116
	
117
	private static final class ThisOrSuperQualifier extends ASTVisitor {
118
119
		private String fClassName;
120
		private ASTRewrite fRewrite;
121
122
		public static void perform(ASTNode node, String className, ASTRewrite rewrite) {
123
			ThisOrSuperQualifier qualifier= new ThisOrSuperQualifier();
124
			qualifier.fClassName= className;
125
			qualifier.fRewrite= rewrite;
126
			node.accept(qualifier);
127
		}
128
129
		@Override
130
		public boolean visit(ThisExpression node) {
131
			if (isValidClassName() && ((node.getQualifier() == null))) {
132
				ThisExpression newThisExpression= (ThisExpression) ASTNode.copySubtree(fRewrite.getAST(), node);
133
				newThisExpression.setQualifier(getNewDeclaringClass());
134
				fRewrite.replace(node, newThisExpression, null);
135
			}
136
			return true;
137
		}
138
139
		@Override
140
		public boolean visit(SuperFieldAccess node) {
141
			if (isValidClassName() && ((node.getQualifier() == null))) {
142
				SuperFieldAccess newSuperFieldAccess= (SuperFieldAccess) ASTNode.copySubtree(fRewrite.getAST(), node);
143
				newSuperFieldAccess.setQualifier(getNewDeclaringClass());
144
				fRewrite.replace(node, newSuperFieldAccess, null);
145
			}
146
			return true;
147
		}
148
149
		@Override
150
		public boolean visit(SuperMethodInvocation node) {
151
			if (isValidClassName() && ((node.getQualifier() == null))) {
152
				SuperMethodInvocation newSuperMethodInvocation= (SuperMethodInvocation) ASTNode.copySubtree(fRewrite.getAST(), node);
153
				newSuperMethodInvocation.setQualifier(getNewDeclaringClass());
154
				fRewrite.replace(node, newSuperMethodInvocation, null);
155
			}
156
			return true;
157
		}
158
159
		@Override
160
		public boolean visit(FieldAccess node) {
161
			// In case of className not defined (e.g lambda in an anonymous), we need to remove the 'this' qualifier
162
			// !! Issue in some cases (see AssistQuickFixTest18.testConvertToAnonymousClassCreation11)
163
			if (!isValidClassName() && (node.getExpression() instanceof ThisExpression)) {
164
				node.getParent();
165
				SimpleName simpleName= (SimpleName) ASTNode.copySubtree(fRewrite.getAST(), node.getName());
166
				fRewrite.replace(node, simpleName, null);
167
			}
168
			return true;
169
		}
170
171
		private Name getNewDeclaringClass() {
172
			return fRewrite.getAST().newSimpleName(fClassName);
173
		}
174
175
		private boolean isValidClassName() {
176
			return (fClassName != null) && (fClassName.length() > 0);
177
		}
178
	}
113
179
114
	private static class AbortSearchException extends RuntimeException {
180
	private static class AbortSearchException extends RuntimeException {
115
		private static final long serialVersionUID= 1L;
181
		private static final long serialVersionUID= 1L;
Lines 395-400 public class LambdaExpressionsFix extends CompilationUnitRewriteOperationsFix { Link Here
395
				MethodDeclaration methodDeclaration= StubUtility2.createImplementationStub(cuRewrite.getCu(), rewrite, importRewrite, importContext,
461
				MethodDeclaration methodDeclaration= StubUtility2.createImplementationStub(cuRewrite.getCu(), rewrite, importRewrite, importContext,
396
						methodBinding, parameterNames, lambdaTypeBinding.getName(), settings, false);
462
						methodBinding, parameterNames, lambdaTypeBinding.getName(), settings, false);
397
463
464
				// Qualify reference to this or super
465
				ITypeBinding typeBinding= lambdaExpression.resolveMethodBinding().getDeclaringClass();
466
				ThisOrSuperQualifier.perform(lambdaExpression, typeBinding.getName(), rewrite);
467
398
				Block block;
468
				Block block;
399
				ASTNode lambdaBody= lambdaExpression.getBody();
469
				ASTNode lambdaBody= lambdaExpression.getBody();
400
				if (lambdaBody instanceof Block) {
470
				if (lambdaBody instanceof Block) {
Lines 547-553 public class LambdaExpressionsFix extends CompilationUnitRewriteOperationsFix { Link Here
547
			return methodBinding.getReturnType().getFunctionalInterfaceMethod() != null;
617
			return methodBinding.getReturnType().getFunctionalInterfaceMethod() != null;
548
		}
618
		}
549
		
619
		
550
		//TODO: should also check whether variable is of a functional type 
620
		//TODO: should also check whether variable is of a functional type
551
		return locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
621
		return locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
552
				|| locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
622
				|| locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
553
				|| locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY
623
				|| locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY

Return to bug 430573