diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java index 27102baa4b3..7bd57392809 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java @@ -8199,7 +8199,7 @@ public void test0249() throws JavaModelException { expression = fragment.getInitializer(); assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression.getNodeType()); methodInvocation = (MethodInvocation) expression; - assertFalse("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType()); + assertTrue("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType()); } //https://bugs.eclipse.org/bugs/show_bug.cgi?id=174436 diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java index 8941eca2e4a..02c785cf086 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java @@ -8196,7 +8196,7 @@ public void test0249() throws JavaModelException { expression = fragment.getInitializer(); assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression.getNodeType()); methodInvocation = (MethodInvocation) expression; - assertFalse("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType()); + assertTrue("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType()); } //https://bugs.eclipse.org/bugs/show_bug.cgi?id=174436 diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java index 3cd67dc44b4..49d5a71e682 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java @@ -8235,7 +8235,7 @@ public void test0249() throws JavaModelException { expression = fragment.getInitializer(); assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression.getNodeType()); methodInvocation = (MethodInvocation) expression; - assertFalse("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType()); + assertTrue("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType()); } //https://bugs.eclipse.org/bugs/show_bug.cgi?id=174436 diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTestJLS8.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTestJLS8.java index 6c3c08a6279..a0b2134c3e5 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTestJLS8.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTestJLS8.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2016 IBM Corporation and others. + * Copyright (c) 2011, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -21,7 +21,13 @@ import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; import org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteFlattener; import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore; @@ -1113,4 +1119,49 @@ public void testGH1376() throws CoreException, IOException { deleteProject("P"); } } +public void testGH2275() throws CoreException { + try { + createJavaProject("P", new String[] { "" }, new String[] { "CONVERTER_JCL_LIB" }, "", "1.8", true); + createFolder("P/p"); + createFile("P/p/A.java", + """ + package p; + import java.util.Collections; + import java.util.Map; + + class A { + public A(Map map) { + Map emptyMap= Collections.emptyMap(); // return type inferred from the target type + Map emptyMap2= foo(A.class); // inference used, but not influencing the return type + } + Map foo(Class clazz) { + return Collections.emptyMap(); + } + } + """ + ); + ICompilationUnit cuA = getCompilationUnit("P/p/A.java"); + ASTParser parser = createASTParser(); + parser.setResolveBindings(true); + parser.setSource(cuA); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); + + TypeDeclaration classA = (TypeDeclaration) cu.types().get(0); + MethodDeclaration constructor = classA.getMethods()[0]; + assertTrue(constructor.isConstructor()); + List statements = constructor.getBody().statements(); + + VariableDeclarationStatement local = (VariableDeclarationStatement) statements.get(0); + MethodInvocation invocation = (MethodInvocation) ((VariableDeclarationFragment) local.fragments().get(0)).getInitializer(); + assertEquals("emptyMap", invocation.getName().getIdentifier()); + assertTrue(invocation.isResolvedTypeInferredFromExpectedType()); + + VariableDeclarationStatement local2 = (VariableDeclarationStatement) statements.get(1); + MethodInvocation invocation2 = (MethodInvocation) ((VariableDeclarationFragment) local2.fragments().get(0)).getInitializer(); + assertEquals("foo", invocation2.getName().getIdentifier()); + assertFalse(invocation2.isResolvedTypeInferredFromExpectedType()); + } finally { + deleteProject("P"); + } +} } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java index 29ce757fd96..a26879c82f4 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2021 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -574,7 +574,10 @@ boolean isResolvedTypeInferredFromExpectedType(MethodInvocation methodInvocation org.eclipse.jdt.internal.compiler.lookup.MethodBinding methodBinding = messageSend.binding; if (methodBinding instanceof ParameterizedGenericMethodBinding) { ParameterizedGenericMethodBinding genericMethodBinding = (ParameterizedGenericMethodBinding) methodBinding; - return genericMethodBinding.inferredReturnType; + if (genericMethodBinding.wasInferred && messageSend.typeArguments == null) { + return org.eclipse.jdt.internal.compiler.lookup.TypeBinding.notEquals( + genericMethodBinding.original().returnType, genericMethodBinding.returnType); + } } } return false;