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

Collapse All | Expand All

(-)ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest18.java (+50 lines)
Lines 672-677 Link Here
672
		assertProposalDoesNotExist(proposals, FixMessages.LambdaExpressionsFix_convert_to_lambda_expression);
672
		assertProposalDoesNotExist(proposals, FixMessages.LambdaExpressionsFix_convert_to_lambda_expression);
673
	}
673
	}
674
674
675
	public void testConvertToLambda14() throws Exception {
676
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
677
		StringBuffer buf= new StringBuffer();
678
		buf.append("package test1;\n");
679
		buf.append("import java.util.List;\n");
680
		buf.append("\n");
681
		buf.append("interface I<T> {\n");
682
		buf.append("    void goo(List<T> i);\n");
683
		buf.append("}\n");
684
		buf.append("\n");
685
		buf.append("public class X {\n");
686
		buf.append("    public static void main(String[] args) {\n");
687
		buf.append("        I i = new I<String>() {\n");
688
		buf.append("            @Override\n");
689
		buf.append("            public void goo(List<String> ls) {\n");
690
		buf.append("                String s = ls.get(0);\n");
691
		buf.append("            }\n");
692
		buf.append("        };\n");
693
		buf.append("    }\n");
694
		buf.append("}\n");
695
		ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
696
697
		int offset= buf.toString().indexOf("I<String>()");
698
		AssistContext context= getCorrectionContext(cu, offset, 0);
699
		assertNoErrors(context);
700
		List proposals= collectAssists(context, false);
701
702
		assertNumberOfProposals(proposals, 2);
703
		assertCorrectLabels(proposals);
704
705
		buf= new StringBuffer();
706
		buf.append("package test1;\n");
707
		buf.append("import java.util.List;\n");
708
		buf.append("\n");
709
		buf.append("interface I<T> {\n");
710
		buf.append("    void goo(List<T> i);\n");
711
		buf.append("}\n");
712
		buf.append("\n");
713
		buf.append("public class X {\n");
714
		buf.append("    public static void main(String[] args) {\n");
715
		buf.append("        I i = (I<String>) ls -> {\n");
716
		buf.append("            String s = ls.get(0);\n");
717
		buf.append("        };\n");
718
		buf.append("    }\n");
719
		buf.append("}\n");
720
		String expected1= buf.toString();
721
722
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
723
	}
724
675
	public void testConvertToLambdaAmbiguousOverridden() throws Exception {
725
	public void testConvertToLambdaAmbiguousOverridden() throws Exception {
676
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
726
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
677
		StringBuffer buf= new StringBuffer();
727
		StringBuffer buf= new StringBuffer();
(-)ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest18.java (-3 / +29 lines)
Lines 182-188 Link Here
182
		disable(CleanUpConstants.USE_ANONYMOUS_CLASS_CREATION);
182
		disable(CleanUpConstants.USE_ANONYMOUS_CLASS_CREATION);
183
		enable(CleanUpConstants.USE_LAMBDA);
183
		enable(CleanUpConstants.USE_LAMBDA);
184
		
184
		
185
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { original });
185
		buf= new StringBuffer();
186
		buf.append("package test;\n");
187
		buf.append("import java.util.*;\n");
188
		buf.append("public class E {\n");
189
		buf.append("    void foo(Integer[] ints){\n");
190
		buf.append("        Arrays.sort(ints, (Comparator<Integer>) (i1, i2) -> i1 - i2);\n");
191
		buf.append("        Comparator<?> cw = (Comparator<Object>) (w1, w2) -> 0;\n");
192
		buf.append("        Comparator cr = (r1, r2) -> 0;\n");
193
		buf.append("        Comparator<? extends Number> ce = (Comparator<Number>) (n1, n2) -> -0;\n");
194
		buf.append("    };\n");
195
		buf.append("}\n");
196
		String expected2= buf.toString();
197
198
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected2 });
186
	}
199
	}
187
200
188
	public void testConvertToAnonymous_andBack_WithWildcards1() throws Exception {
201
	public void testConvertToAnonymous_andBack_WithWildcards1() throws Exception {
Lines 226-232 Link Here
226
		disable(CleanUpConstants.USE_ANONYMOUS_CLASS_CREATION);
239
		disable(CleanUpConstants.USE_ANONYMOUS_CLASS_CREATION);
227
		enable(CleanUpConstants.USE_LAMBDA);
240
		enable(CleanUpConstants.USE_LAMBDA);
228
241
229
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { original });
242
		buf= new StringBuffer();
243
		buf.append("package test;\n");
244
		buf.append("\n");
245
		buf.append("interface I<M> {\n");
246
		buf.append("    M run(M x);\n");
247
		buf.append("}\n");
248
		buf.append("\n");
249
		buf.append("class Test {\n");
250
		buf.append("    I<?> li = (I<Object>) s -> null;\n");
251
		buf.append("}\n");
252
		String expected2= buf.toString();
253
254
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected2 });
230
	}
255
	}
231
256
232
	public void testConvertToAnonymous_andBack_WithJoinedSAM() throws Exception {
257
	public void testConvertToAnonymous_andBack_WithJoinedSAM() throws Exception {
Lines 302-311 Link Here
302
		
327
		
303
		buf= new StringBuffer();
328
		buf= new StringBuffer();
304
		buf.append("package test;\n");
329
		buf.append("package test;\n");
330
		buf.append("import java.util.concurrent.Callable;\n");
305
		buf.append("import java.util.concurrent.Executors;\n");
331
		buf.append("import java.util.concurrent.Executors;\n");
306
		buf.append("public class E {\n");
332
		buf.append("public class E {\n");
307
		buf.append("    void foo() {\n");
333
		buf.append("    void foo() {\n");
308
		buf.append("        new Thread(() -> Executors.newSingleThreadExecutor().submit(() -> \"hi\"));\n");
334
		buf.append("        new Thread(() -> Executors.newSingleThreadExecutor().submit((Callable<String>) () -> \"hi\"));\n");
309
		buf.append("    }\n");
335
		buf.append("    }\n");
310
		buf.append("}\n");
336
		buf.append("}\n");
311
		String expected1= buf.toString();
337
		String expected1= buf.toString();
(-)core extension/org/eclipse/jdt/internal/corext/dom/ASTNodes.java (-1 / +4 lines)
Lines 581-586 Link Here
581
	public static ITypeBinding getExplicitCast(Expression initializer, Expression reference) {
581
	public static ITypeBinding getExplicitCast(Expression initializer, Expression reference) {
582
		ITypeBinding initializerType= initializer.resolveTypeBinding();
582
		ITypeBinding initializerType= initializer.resolveTypeBinding();
583
		ITypeBinding referenceType= reference.resolveTypeBinding();
583
		ITypeBinding referenceType= reference.resolveTypeBinding();
584
		if (reference instanceof ClassInstanceCreation) {
585
			referenceType= ((ClassInstanceCreation) reference).getType().resolveBinding();
586
		}
584
		if (initializerType == null || referenceType == null)
587
		if (initializerType == null || referenceType == null)
585
			return null;
588
			return null;
586
		
589
		
Lines 607-613 Link Here
607
				return referenceType;
610
				return referenceType;
608
			} else {
611
			} else {
609
				ITypeBinding targetType= getTargetType(reference);
612
				ITypeBinding targetType= getTargetType(reference);
610
				if (targetType == null || targetType != referenceType) {
613
				if (!referenceType.isEqualTo(targetType)) { // includes targetType == null
611
					return referenceType;
614
					return referenceType;
612
				}
615
				}
613
			}
616
			}
(-)core extension/org/eclipse/jdt/internal/corext/fix/LambdaExpressionsFix.java (-2 / +4 lines)
Lines 240-251 Link Here
240
				
240
				
241
				lambdaExpression.setBody(rewrite.createCopyTarget(lambdaBody));
241
				lambdaExpression.setBody(rewrite.createCopyTarget(lambdaBody));
242
				Expression replacement= lambdaExpression;
242
				Expression replacement= lambdaExpression;
243
				if (ASTNodes.isTargetAmbiguous(classInstanceCreation)) {
243
				ITypeBinding cicTypeBinding= classInstanceCreation.getType().resolveBinding();
244
				ITypeBinding targetTypeBinding= ASTNodes.getTargetType(classInstanceCreation);
245
				if (cicTypeBinding != null && (ASTNodes.isTargetAmbiguous(classInstanceCreation) || !cicTypeBinding.isEqualTo(targetTypeBinding))) {
244
					CastExpression cast= ast.newCastExpression();
246
					CastExpression cast= ast.newCastExpression();
245
					cast.setExpression(lambdaExpression);
247
					cast.setExpression(lambdaExpression);
246
					ImportRewrite importRewrite= cuRewrite.getImportRewrite();
248
					ImportRewrite importRewrite= cuRewrite.getImportRewrite();
247
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(classInstanceCreation, importRewrite);
249
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(classInstanceCreation, importRewrite);
248
					Type castType= importRewrite.addImport(classInstanceCreation.getType().resolveBinding(), ast, importRewriteContext);
250
					Type castType= importRewrite.addImport(cicTypeBinding, ast, importRewriteContext);
249
					cast.setType(castType);
251
					cast.setType(castType);
250
					importRemover.registerAddedImports(castType);
252
					importRemover.registerAddedImports(castType);
251
					replacement= cast;
253
					replacement= cast;

Return to bug 425412