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

Collapse All | Expand All

(-)ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest18.java (+419 lines)
Lines 74-79 Link Here
74
		fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
74
		fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
75
	}
75
	}
76
76
77
	@Override
77
	protected void tearDown() throws Exception {
78
	protected void tearDown() throws Exception {
78
		JavaProjectHelper.clear(fJProject1, Java18ProjectTestSetup.getDefaultClasspath());
79
		JavaProjectHelper.clear(fJProject1, Java18ProjectTestSetup.getDefaultClasspath());
79
	}
80
	}
Lines 930-935 Link Here
930
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
931
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
931
	}
932
	}
932
933
934
	// Bug 421479
935
	public void testConvertToLambda19() throws Exception {
936
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
937
		StringBuffer buf= new StringBuffer();
938
		buf.append("package test1;\n");
939
		buf.append("@FunctionalInterface\n");
940
		buf.append("interface Func2 {\n");
941
		buf.append("    enum En {\n");
942
		buf.append("        A, B;\n");
943
		buf.append("        enum COLOR {\n");
944
		buf.append("            D, E\n");
945
		buf.append("        }\n");
946
		buf.append("    }\n");
947
		buf.append("\n");
948
		buf.append("    void foo();\n");
949
		buf.append("\n");
950
		buf.append("    String NAME = \"\";\n");
951
		buf.append("\n");
952
		buf.append("    static String staticName() {\n");
953
		buf.append("        return NAME;\n");
954
		buf.append("    }\n");
955
		buf.append("}\n");
956
		buf.append("\n");
957
		buf.append("class Test2 {\n");
958
		buf.append("    void bar() {\n");
959
		buf.append("        Func2 f = new Func2() {\n");
960
		buf.append("            @Override\n");
961
		buf.append("            public void foo() {\n");
962
		buf.append("                System.out.println(NAME);\n");
963
		buf.append("                System.out.println(Func2.NAME);\n");
964
		buf.append("                System.out.println(En.A);\n");
965
		buf.append("                System.out.println(Func2.En.A);\n");
966
		buf.append("                System.out.println(En.COLOR.D);\n");
967
		buf.append("                System.out.println(Func2.En.COLOR.D);\n");
968
		buf.append("                System.out.println(Test2.this);\n");
969
		buf.append("                bar();\n");
970
		buf.append("            }\n");
971
		buf.append("        };\n");
972
		buf.append("    }\n");
973
		buf.append("}\n");
974
		ICompilationUnit cu= pack1.createCompilationUnit("Func2.java", buf.toString(), false, null);
975
976
		int offset= buf.toString().indexOf("public void");
977
		AssistContext context= getCorrectionContext(cu, offset, 0);
978
		assertNoErrors(context);
979
		List proposals= collectAssists(context, false);
980
981
		assertNumberOfProposals(proposals, 1);
982
		assertCorrectLabels(proposals);
983
984
		buf= new StringBuffer();
985
		buf.append("package test1;\n\n");
986
		buf.append("import test1.Func2.En;\n\n");
987
		buf.append("@FunctionalInterface\n");
988
		buf.append("interface Func2 {\n");
989
		buf.append("    enum En {\n");
990
		buf.append("        A, B;\n");
991
		buf.append("        enum COLOR {\n");
992
		buf.append("            D, E\n");
993
		buf.append("        }\n");
994
		buf.append("    }\n");
995
		buf.append("\n");
996
		buf.append("    void foo();\n");
997
		buf.append("\n");
998
		buf.append("    String NAME = \"\";\n");
999
		buf.append("\n");
1000
		buf.append("    static String staticName() {\n");
1001
		buf.append("        return NAME;\n");
1002
		buf.append("    }\n");
1003
		buf.append("}\n");
1004
		buf.append("\n");
1005
		buf.append("class Test2 {\n");
1006
		buf.append("    void bar() {\n");
1007
		buf.append("        Func2 f = () -> {\n");
1008
		buf.append("            System.out.println(Func2.NAME);\n");
1009
		buf.append("            System.out.println(Func2.NAME);\n");
1010
		buf.append("            System.out.println(Func2.En.A);\n");
1011
		buf.append("            System.out.println(Func2.En.A);\n");
1012
		buf.append("            System.out.println(En.COLOR.D);\n");
1013
		buf.append("            System.out.println(Func2.En.COLOR.D);\n");
1014
		buf.append("            System.out.println(Test2.this);\n");
1015
		buf.append("            bar();\n");
1016
		buf.append("        };\n");
1017
		buf.append("    }\n");
1018
		buf.append("}\n");
1019
		String expected1= buf.toString();
1020
1021
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1022
	}
1023
1024
	// Bug 421479
1025
	public void testConvertToLambda20() throws Exception {
1026
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1027
		StringBuffer buf= new StringBuffer();
1028
		buf.append("package test1;\n");
1029
		buf.append("interface Func<T> {\n");
1030
		buf.append("    void go();\n");
1031
		buf.append("    String NAME = Func.class.getName();\n");
1032
		buf.append("}\n");
1033
		buf.append("\n");
1034
		buf.append("public class Test {\n");
1035
		buf.append("    void foo() {\n");
1036
		buf.append("        Func<String> f= new Func<String>() {\n");
1037
		buf.append("            @Override\n");
1038
		buf.append("            public void go() {\n");
1039
		buf.append("                System.out.println(NAME);\n");
1040
		buf.append("            }\n");
1041
		buf.append("        };\n");
1042
		buf.append("    }\n");
1043
		buf.append("}\n");
1044
		ICompilationUnit cu= pack1.createCompilationUnit("Test.java", buf.toString(), false, null);
1045
1046
		int offset= buf.toString().indexOf("public void");
1047
		AssistContext context= getCorrectionContext(cu, offset, 0);
1048
		assertNoErrors(context);
1049
		List proposals= collectAssists(context, false);
1050
1051
		assertNumberOfProposals(proposals, 1);
1052
		assertCorrectLabels(proposals);
1053
1054
		buf= new StringBuffer();
1055
		buf.append("package test1;\n");
1056
		buf.append("interface Func<T> {\n");
1057
		buf.append("    void go();\n");
1058
		buf.append("    String NAME = Func.class.getName();\n");
1059
		buf.append("}\n");
1060
		buf.append("\n");
1061
		buf.append("public class Test {\n");
1062
		buf.append("    void foo() {\n");
1063
		buf.append("        Func<String> f= () -> System.out.println(Func.NAME);\n");
1064
		buf.append("    }\n");
1065
		buf.append("}\n");
1066
		String expected1= buf.toString();
1067
1068
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1069
	}
1070
1071
	// Bug 421479
1072
	public void testConvertToLambda21() throws Exception {
1073
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1074
		StringBuffer buf= new StringBuffer();
1075
		buf.append("package test1;\n");
1076
		buf.append("import test1.Func2.En.COLOR;\n");
1077
		buf.append("@FunctionalInterface\n");
1078
		buf.append("interface Func2 {\n");
1079
		buf.append("    enum En {\n");
1080
		buf.append("        A, B;\n");
1081
		buf.append("        enum COLOR {\n");
1082
		buf.append("            D, E\n");
1083
		buf.append("        }\n");
1084
		buf.append("    }\n");
1085
		buf.append("\n");
1086
		buf.append("    void foo();\n");
1087
		buf.append("\n");
1088
		buf.append("    String NAME = \"\";\n");
1089
		buf.append("\n");
1090
		buf.append("    static String staticName() {\n");
1091
		buf.append("        return NAME;\n");
1092
		buf.append("    }\n");
1093
		buf.append("}\n");
1094
		buf.append("\n");
1095
		buf.append("class Test2 {\n");
1096
		buf.append("    void bar() {\n");
1097
		buf.append("        Func2 f = new Func2() {\n");
1098
		buf.append("            @Override\n");
1099
		buf.append("            public void foo() {\n");
1100
		buf.append("                System.out.println(COLOR.D);\n");
1101
		buf.append("            }\n");
1102
		buf.append("        };\n");
1103
		buf.append("    }\n");
1104
		buf.append("}\n");
1105
		ICompilationUnit cu= pack1.createCompilationUnit("Func2.java", buf.toString(), false, null);
1106
1107
		int offset= buf.toString().indexOf("public void");
1108
		AssistContext context= getCorrectionContext(cu, offset, 0);
1109
		assertNoErrors(context);
1110
		List proposals= collectAssists(context, false);
1111
1112
		assertNumberOfProposals(proposals, 1);
1113
		assertCorrectLabels(proposals);
1114
1115
		buf= new StringBuffer();
1116
		buf.append("package test1;\n");
1117
		buf.append("import test1.Func2.En.COLOR;\n");
1118
		buf.append("@FunctionalInterface\n");
1119
		buf.append("interface Func2 {\n");
1120
		buf.append("    enum En {\n");
1121
		buf.append("        A, B;\n");
1122
		buf.append("        enum COLOR {\n");
1123
		buf.append("            D, E\n");
1124
		buf.append("        }\n");
1125
		buf.append("    }\n");
1126
		buf.append("\n");
1127
		buf.append("    void foo();\n");
1128
		buf.append("\n");
1129
		buf.append("    String NAME = \"\";\n");
1130
		buf.append("\n");
1131
		buf.append("    static String staticName() {\n");
1132
		buf.append("        return NAME;\n");
1133
		buf.append("    }\n");
1134
		buf.append("}\n");
1135
		buf.append("\n");
1136
		buf.append("class Test2 {\n");
1137
		buf.append("    void bar() {\n");
1138
		buf.append("        Func2 f = () -> System.out.println(COLOR.D);\n");
1139
		buf.append("    }\n");
1140
		buf.append("}\n");
1141
		String expected1= buf.toString();
1142
1143
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1144
	}
1145
1146
	// Bug 421479
1147
	public void testConvertToLambda22() throws Exception {
1148
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1149
		StringBuffer buf= new StringBuffer();
1150
		buf.append("package test1;\n");
1151
		buf.append("@FunctionalInterface\n");
1152
		buf.append("interface Func {\n");
1153
		buf.append("    class C {\n");
1154
		buf.append("        static  class CIn { static int i= 0; }        \n");
1155
		buf.append("        static String NAME = \"\";\n");
1156
		buf.append("    }\n");
1157
		buf.append("    void foo();\n");
1158
		buf.append("}\n");
1159
		buf.append("\n");
1160
		buf.append("class Test {\n");
1161
		buf.append("    static class C {\n");
1162
		buf.append("        static  class CIn { static int i= 1; }        \n");
1163
		buf.append("        static String NAME = \"\";\n");
1164
		buf.append("    }\n");
1165
		buf.append("    void bar() {\n");
1166
		buf.append("        Func f = new Func() {\n");
1167
		buf.append("            @Override\n");
1168
		buf.append("            public void foo() {\n");
1169
		buf.append("                System.out.println(C.CIn.i);\n");
1170
		buf.append("                System.out.println(C.NAME);\n");
1171
		buf.append("            }\n");
1172
		buf.append("        };\n");
1173
		buf.append("    }\n");
1174
		buf.append("}\n");
1175
		ICompilationUnit cu= pack1.createCompilationUnit("Func.java", buf.toString(), false, null);
1176
1177
		int offset= buf.toString().indexOf("public void");
1178
		AssistContext context= getCorrectionContext(cu, offset, 0);
1179
		assertNoErrors(context);
1180
		List proposals= collectAssists(context, false);
1181
1182
		assertNumberOfProposals(proposals, 1);
1183
		assertCorrectLabels(proposals);
1184
1185
		buf= new StringBuffer();
1186
		buf.append("package test1;\n");
1187
		buf.append("@FunctionalInterface\n");
1188
		buf.append("interface Func {\n");
1189
		buf.append("    class C {\n");
1190
		buf.append("        static  class CIn { static int i= 0; }        \n");
1191
		buf.append("        static String NAME = \"\";\n");
1192
		buf.append("    }\n");
1193
		buf.append("    void foo();\n");
1194
		buf.append("}\n");
1195
		buf.append("\n");
1196
		buf.append("class Test {\n");
1197
		buf.append("    static class C {\n");
1198
		buf.append("        static  class CIn { static int i= 1; }        \n");
1199
		buf.append("        static String NAME = \"\";\n");
1200
		buf.append("    }\n");
1201
		buf.append("    void bar() {\n");
1202
		buf.append("        Func f = () -> {\n");
1203
		buf.append("            System.out.println(test1.Func.C.CIn.i);\n");
1204
		buf.append("            System.out.println(Func.C.NAME);\n");
1205
		buf.append("        };\n");
1206
		buf.append("    }\n");
1207
		buf.append("}\n");
1208
		String expected1= buf.toString();
1209
1210
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1211
	}
1212
1213
	// Bug 421479
1214
	public void testConvertToLambda23() throws Exception {
1215
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1216
		StringBuffer buf= new StringBuffer();
1217
		buf.append("package test1;\n");
1218
		buf.append("@FunctionalInterface\n");
1219
		buf.append("interface Func {\n");
1220
		buf.append("    class C {\n");
1221
		buf.append("        static  class CIn { static int i= 0; }        \n");
1222
		buf.append("    }\n");
1223
		buf.append("    void foo();\n");
1224
		buf.append("}\n");
1225
		buf.append("\n");
1226
		buf.append("class Test extends TT {\n");
1227
		buf.append("    static class C {\n");
1228
		buf.append("        static  class CIn { static int i= 1; }        \n");
1229
		buf.append("    }\n");
1230
		buf.append("    void bar() {\n");
1231
		buf.append("        test1.Func f = new test1.Func() {\n");
1232
		buf.append("            @Override\n");
1233
		buf.append("            public void foo() {\n");
1234
		buf.append("                System.out.println(C.CIn.i); // [1]\n");
1235
		buf.append("            }\n");
1236
		buf.append("        };\n");
1237
		buf.append("    }\n");
1238
		buf.append("}\n");
1239
		buf.append("\n");
1240
		buf.append("class TT {\n");
1241
		buf.append("    class Func {\n");
1242
		buf.append("    }\n");
1243
		buf.append("}\n");
1244
		ICompilationUnit cu= pack1.createCompilationUnit("Func.java", buf.toString(), false, null);
1245
1246
		int offset= buf.toString().indexOf("public void");
1247
		AssistContext context= getCorrectionContext(cu, offset, 0);
1248
		assertNoErrors(context);
1249
		List proposals= collectAssists(context, false);
1250
1251
		assertNumberOfProposals(proposals, 1);
1252
		assertCorrectLabels(proposals);
1253
1254
		buf= new StringBuffer();
1255
		buf.append("package test1;\n");
1256
		buf.append("@FunctionalInterface\n");
1257
		buf.append("interface Func {\n");
1258
		buf.append("    class C {\n");
1259
		buf.append("        static  class CIn { static int i= 0; }        \n");
1260
		buf.append("    }\n");
1261
		buf.append("    void foo();\n");
1262
		buf.append("}\n");
1263
		buf.append("\n");
1264
		buf.append("class Test extends TT {\n");
1265
		buf.append("    static class C {\n");
1266
		buf.append("        static  class CIn { static int i= 1; }        \n");
1267
		buf.append("    }\n");
1268
		buf.append("    void bar() {\n");
1269
		buf.append("        test1.Func f = () -> System.out.println(test1.Func.C.CIn.i);\n");
1270
		buf.append("    }\n");
1271
		buf.append("}\n");
1272
		buf.append("\n");
1273
		buf.append("class TT {\n");
1274
		buf.append("    class Func {\n");
1275
		buf.append("    }\n");
1276
		buf.append("}\n");
1277
		String expected1= buf.toString();
1278
1279
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1280
	}
1281
1282
	// Bug 421479
1283
	public void testConvertToLambda24() throws Exception {
1284
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1285
		StringBuffer buf= new StringBuffer();
1286
		buf.append("package test1;\n");
1287
		buf.append("@FunctionalInterface\n");
1288
		buf.append("interface Func2 {\n");
1289
		buf.append("    enum En {\n");
1290
		buf.append("        A, B\n");
1291
		buf.append("    }\n");
1292
		buf.append("\n");
1293
		buf.append("    void foo();\n");
1294
		buf.append("\n");
1295
		buf.append("    String NAME = \"\";\n");
1296
		buf.append("\n");
1297
		buf.append("    static String staticName() {\n");
1298
		buf.append("        return NAME;\n");
1299
		buf.append("    }\n");
1300
		buf.append("}\n");
1301
		buf.append("\n");
1302
		buf.append("class Test2 {\n");
1303
		buf.append("    void bar() {\n");
1304
		buf.append("        Func2 f = new Func2() {\n");
1305
		buf.append("            @Override\n");
1306
		buf.append("            public void foo() {\n");
1307
		buf.append("                foo();\n");
1308
		buf.append("            }\n");
1309
		buf.append("        };\n");
1310
		buf.append("    }\n");
1311
		buf.append("}\n");
1312
		ICompilationUnit cu= pack1.createCompilationUnit("Func2.java", buf.toString(), false, null);
1313
1314
		int offset= buf.toString().indexOf("public void");
1315
		AssistContext context= getCorrectionContext(cu, offset, 0);
1316
		assertNoErrors(context);
1317
		List proposals= collectAssists(context, false);
1318
1319
		assertNumberOfProposals(proposals, 0);
1320
	}
1321
1322
	// Bug 421479
1323
	public void testConvertToLambda25() throws Exception {
1324
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1325
		StringBuffer buf= new StringBuffer();
1326
		buf.append("package test1;\n");
1327
		buf.append("import java.util.function.Consumer;\n");
1328
		buf.append("\n");
1329
		buf.append("public class Test {\n");
1330
		buf.append("    static void run(Consumer<Integer> consumer) {\n");
1331
		buf.append("        System.out.println(\"consumer\");\n");
1332
		buf.append("    }\n");
1333
		buf.append("    static {\n");
1334
		buf.append("        run(new Consumer<Integer>() {\n");
1335
		buf.append("            @Override\n");
1336
		buf.append("            public void accept(Integer integer) {\n");
1337
		buf.append("                System.out.println(andThen(null));\n");
1338
		buf.append("            }\n");
1339
		buf.append("        });\n");
1340
		buf.append("    }\n");
1341
		buf.append("}\n");
1342
		ICompilationUnit cu= pack1.createCompilationUnit("Test.java", buf.toString(), false, null);
1343
1344
		int offset= buf.toString().indexOf("public void");
1345
		AssistContext context= getCorrectionContext(cu, offset, 0);
1346
		assertNoErrors(context);
1347
		List proposals= collectAssists(context, false);
1348
1349
		assertNumberOfProposals(proposals, 0);
1350
	}
1351
933
	public void testConvertToLambdaAmbiguousOverridden() throws Exception {
1352
	public void testConvertToLambdaAmbiguousOverridden() throws Exception {
934
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1353
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
935
		StringBuffer buf= new StringBuffer();
1354
		StringBuffer buf= new StringBuffer();
(-)core extension/org/eclipse/jdt/internal/corext/fix/LambdaExpressionsFix.java (-7 / +122 lines)
Lines 21-26 Link Here
21
21
22
import org.eclipse.text.edits.TextEditGroup;
22
import org.eclipse.text.edits.TextEditGroup;
23
23
24
import org.eclipse.jdt.core.ICompilationUnit;
25
import org.eclipse.jdt.core.IImportDeclaration;
26
import org.eclipse.jdt.core.JavaModelException;
24
import org.eclipse.jdt.core.dom.AST;
27
import org.eclipse.jdt.core.dom.AST;
25
import org.eclipse.jdt.core.dom.ASTNode;
28
import org.eclipse.jdt.core.dom.ASTNode;
26
import org.eclipse.jdt.core.dom.ASTVisitor;
29
import org.eclipse.jdt.core.dom.ASTVisitor;
Lines 36-46 Link Here
36
import org.eclipse.jdt.core.dom.IBinding;
39
import org.eclipse.jdt.core.dom.IBinding;
37
import org.eclipse.jdt.core.dom.IMethodBinding;
40
import org.eclipse.jdt.core.dom.IMethodBinding;
38
import org.eclipse.jdt.core.dom.ITypeBinding;
41
import org.eclipse.jdt.core.dom.ITypeBinding;
42
import org.eclipse.jdt.core.dom.IVariableBinding;
39
import org.eclipse.jdt.core.dom.LambdaExpression;
43
import org.eclipse.jdt.core.dom.LambdaExpression;
40
import org.eclipse.jdt.core.dom.MethodDeclaration;
44
import org.eclipse.jdt.core.dom.MethodDeclaration;
41
import org.eclipse.jdt.core.dom.MethodInvocation;
45
import org.eclipse.jdt.core.dom.MethodInvocation;
46
import org.eclipse.jdt.core.dom.Name;
47
import org.eclipse.jdt.core.dom.QualifiedName;
42
import org.eclipse.jdt.core.dom.ReturnStatement;
48
import org.eclipse.jdt.core.dom.ReturnStatement;
43
import org.eclipse.jdt.core.dom.SimpleName;
49
import org.eclipse.jdt.core.dom.SimpleName;
50
import org.eclipse.jdt.core.dom.SimpleType;
44
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
51
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
45
import org.eclipse.jdt.core.dom.Statement;
52
import org.eclipse.jdt.core.dom.Statement;
46
import org.eclipse.jdt.core.dom.SuperFieldAccess;
53
import org.eclipse.jdt.core.dom.SuperFieldAccess;
Lines 71-76 Link Here
71
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
78
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
72
79
73
public class LambdaExpressionsFix extends CompilationUnitRewriteOperationsFix {
80
public class LambdaExpressionsFix extends CompilationUnitRewriteOperationsFix {
81
82
	private static final class InterfaceAccessQualifier extends HierarchicalASTVisitor {
83
84
		List<Name> nameNodes;
85
86
		private ITypeBinding typeBinding;
87
88
		public InterfaceAccessQualifier(ITypeBinding typeBinding) {
89
			this.typeBinding= typeBinding;
90
			nameNodes= new ArrayList<Name>();
91
		}
92
93
		@Override
94
		public boolean visit(SimpleName node) {
95
			IBinding resolveBinding= node.resolveBinding();
96
			ITypeBinding declaringClass= null;
97
			if (resolveBinding instanceof IVariableBinding) {
98
				declaringClass= ((IVariableBinding) resolveBinding).getDeclaringClass();
99
			} else if (resolveBinding instanceof ITypeBinding) {
100
				declaringClass= ((ITypeBinding) resolveBinding).getDeclaringClass();
101
			}
102
			if (declaringClass != null && Bindings.equals(typeBinding, declaringClass)) {
103
				nameNodes.add(node);
104
			}
105
			return true;
106
		}
107
108
		@Override
109
		public boolean visit(QualifiedName node) {
110
			Name qualifier= node.getQualifier();
111
			ITypeBinding qualifierTypeBinding= qualifier.resolveTypeBinding();
112
			// Ignore if already qualified with the functional interface
113
			if (Bindings.equals(typeBinding, qualifierTypeBinding)) {
114
				return false;
115
			}
116
			qualifierTypeBinding= ASTNodes.getLeftMostSimpleName(qualifier).resolveTypeBinding();
117
			if (qualifierTypeBinding == null || Bindings.equals(typeBinding, qualifierTypeBinding)) {
118
				return false;
119
			}
120
			IBinding resolveBinding= node.resolveBinding();
121
			ITypeBinding declaringClass= null;
122
			if (resolveBinding instanceof IVariableBinding) {
123
				declaringClass= ((IVariableBinding) resolveBinding).getDeclaringClass();
124
			} else if (resolveBinding instanceof ITypeBinding) {
125
				declaringClass= ((ITypeBinding) resolveBinding).getDeclaringClass();
126
			}
127
			while (declaringClass != null) {
128
				if (Bindings.equals(typeBinding, declaringClass)) {
129
					nameNodes.add(qualifier);
130
					return false;
131
				}
132
				declaringClass= declaringClass.getDeclaringClass();
133
			}
134
			return false;
135
		}
136
	}
74
137
75
	private static final class FunctionalAnonymousClassesFinder extends ASTVisitor {
138
	private static final class FunctionalAnonymousClassesFinder extends ASTVisitor {
76
139
Lines 176-183 Link Here
176
		@Override
239
		@Override
177
		public boolean visit(MethodInvocation node) {
240
		public boolean visit(MethodInvocation node) {
178
			IMethodBinding binding= node.resolveMethodBinding();
241
			IMethodBinding binding= node.resolveMethodBinding();
179
			if (binding != null && !JdtFlags.isStatic(binding) && node.getExpression() == null
242
			if (binding != null
180
					&& Bindings.isSuperType(binding.getDeclaringClass(), fFunctionalInterface, false))
243
					&& ((!JdtFlags.isStatic(binding) && node.getExpression() == null && Bindings.isSuperType(binding.getDeclaringClass(), fFunctionalInterface, true))
244
					|| (Bindings.equals(binding, fMethodDeclaration.resolveBinding()))))
181
				throw new AbortSearchException();
245
				throw new AbortSearchException();
182
			return true;
246
			return true;
183
		}
247
		}
Lines 250-258 Link Here
250
						}
314
						}
251
					}
315
					}
252
				}
316
				}
253
				//TODO: Bug 421479: [1.8][clean up][quick assist] convert anonymous to lambda must consider lost scope of interface
317
				ITypeBinding typeBinding= classInstanceCreation.getType().resolveBinding();
254
//				lambdaBody.accept(new InterfaceAccessQualifier(rewrite, classInstanceCreation.getType().resolveBinding())); //TODO: maybe need a separate ASTRewrite and string placeholder
318
				InterfaceAccessQualifier visitor= new InterfaceAccessQualifier(typeBinding);
255
				
319
				lambdaBody.accept(visitor);
320
				qualifyNameNodes(cuRewrite, classInstanceCreation, visitor.nameNodes, group);
256
				lambdaExpression.setBody(rewrite.createCopyTarget(lambdaBody));
321
				lambdaExpression.setBody(rewrite.createCopyTarget(lambdaBody));
257
				Expression replacement= lambdaExpression;
322
				Expression replacement= lambdaExpression;
258
				if (ASTNodes.isTargetAmbiguous(classInstanceCreation, lambdaParameters.isEmpty())) {
323
				if (ASTNodes.isTargetAmbiguous(classInstanceCreation, lambdaParameters.isEmpty())) {
Lines 260-266 Link Here
260
					cast.setExpression(lambdaExpression);
325
					cast.setExpression(lambdaExpression);
261
					ImportRewrite importRewrite= cuRewrite.getImportRewrite();
326
					ImportRewrite importRewrite= cuRewrite.getImportRewrite();
262
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(classInstanceCreation, importRewrite);
327
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(classInstanceCreation, importRewrite);
263
					Type castType= importRewrite.addImport(classInstanceCreation.getType().resolveBinding(), ast, importRewriteContext);
328
					Type castType= importRewrite.addImport(typeBinding, ast, importRewriteContext);
264
					cast.setType(castType);
329
					cast.setType(castType);
265
					importRemover.registerAddedImports(castType);
330
					importRemover.registerAddedImports(castType);
266
					replacement= cast;
331
					replacement= cast;
Lines 270-275 Link Here
270
				importRemover.registerRemovedNode(classInstanceCreation);
335
				importRemover.registerRemovedNode(classInstanceCreation);
271
				importRemover.registerRetainedNode(lambdaBody);
336
				importRemover.registerRetainedNode(lambdaBody);
272
			}
337
			}
338
		}
339
340
		private void qualifyNameNodes(CompilationUnitRewrite cuRewrite, ClassInstanceCreation classInstanceCreation, List<Name> nameNodes, TextEditGroup group) {
341
			AST ast= cuRewrite.getAST();
342
			IImportDeclaration[] availableImports= null;
343
			ICompilationUnit cu= cuRewrite.getCu();
344
			try {
345
				availableImports= cu.getImports();
346
			} catch (JavaModelException e) {
347
				// ignore
348
			}
349
			ITypeBinding typeBinding= classInstanceCreation.getType().resolveBinding();
350
			for (Name name : nameNodes) {
351
				Type addImport= null;
352
				if (name instanceof QualifiedName) {
353
					ITypeBinding resolveTypeBinding= ASTNodes.getLeftMostSimpleName(name).resolveTypeBinding();
354
					if (resolveTypeBinding != null) {
355
						ImportRewrite importRewrite= cuRewrite.getImportRewrite();
356
						ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(classInstanceCreation, importRewrite);
357
						addImport= importRewrite.addImport(resolveTypeBinding, ast, importRewriteContext);
358
						if (addImport instanceof SimpleType) {
359
							Name qualifiedName= ((SimpleType) addImport).getName();
360
							if (qualifiedName instanceof QualifiedName) {
361
								ASTRewrite rewrite= cuRewrite.getASTRewrite();
362
								rewrite.set(name, QualifiedName.QUALIFIER_PROPERTY, qualifiedName, group);
363
							} else {
364
								ImportRemover importRemover= cuRewrite.getImportRemover();
365
								importRemover.registerAddedImports(addImport);
366
							}
367
						}
368
					}
369
				} else if (name instanceof SimpleName) {
370
					ITypeBinding resolvedTypeBinding= name.resolveTypeBinding();
371
					if (!isTypeImported(availableImports, resolvedTypeBinding.getQualifiedName())) {
372
						ASTRewrite rewrite= cuRewrite.getASTRewrite();
373
						String qualifier= typeBinding.getErasure().getName();
374
						SimpleName simpleName= ast.newSimpleName(((SimpleName) name).getIdentifier());
375
						rewrite.set(name, SimpleName.IDENTIFIER_PROPERTY, ast.newQualifiedName(ast.newName(qualifier), simpleName).getFullyQualifiedName(), group);
376
					}
377
				}
378
			}
379
		}
380
381
		private boolean isTypeImported(IImportDeclaration[] availableImports, String qualifiedName) {
382
			for (IImportDeclaration iImportDeclaration : availableImports) {
383
				if (qualifiedName.equals(iImportDeclaration.getElementName())) {
384
					return true;
385
				}
386
			}
387
			return false;
273
		}
388
		}
274
389
275
		private HashSet<String> makeNamesUnique(HashSet<String> excludedNames, MethodDeclaration methodDeclaration, ASTRewrite rewrite, TextEditGroup group) {
390
		private HashSet<String> makeNamesUnique(HashSet<String> excludedNames, MethodDeclaration methodDeclaration, ASTRewrite rewrite, TextEditGroup group) {
Lines 547-553 Link Here
547
			return methodBinding.getReturnType().getFunctionalInterfaceMethod() != null;
662
			return methodBinding.getReturnType().getFunctionalInterfaceMethod() != null;
548
		}
663
		}
549
		
664
		
550
		//TODO: should also check whether variable is of a functional type 
665
		//TODO: should also check whether variable is of a functional type
551
		return locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
666
		return locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
552
				|| locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
667
				|| locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
553
				|| locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY
668
				|| locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY

Return to bug 421479