diff options
author | Kalyan Prasad Tatavarthi | 2020-09-07 09:19:16 +0000 |
---|---|---|
committer | Kalyan Prasad Tatavarthi | 2020-09-07 11:04:30 +0000 |
commit | ad13fa01cc09ee2baa3a67fcc1148cf2c5d2874c (patch) | |
tree | 47295ca1244549551359ef6d1cb647212bc6b46e | |
parent | d853c33dd09ccb6d7cb3e62c3eef6b0d2d721bc3 (diff) | |
download | eclipse.jdt.core-ad13fa01cc09ee2baa3a67fcc1148cf2c5d2874c.tar.gz eclipse.jdt.core-ad13fa01cc09ee2baa3a67fcc1148cf2c5d2874c.tar.xz eclipse.jdt.core-ad13fa01cc09ee2baa3a67fcc1148cf2c5d2874c.zip |
Change-Id: I12a35f07ee6278fa1f353350e97ad7e23edb6e6f
Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
3 files changed, 140 insertions, 0 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java index 5d5add713a..f3a66c5d99 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java @@ -23,14 +23,18 @@ import java.util.List; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.IBinding; +import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; import org.eclipse.jdt.core.dom.MarkerAnnotation; @@ -973,4 +977,111 @@ public class ASTConverter_15Test extends ConverterTestSetup { javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); } } + + public void testRecordConstructor001() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = "record X(int lo) {\n" + + " public X {\n" + + " \n}\n" + + " public X(String str) {\n" + + " this((str != null) ? str.length() : 0);" + + " \n}\n" + + " public int abc() {\n" + + " return this.lo;\n" + + " }\n" + + "\n" + + "}\n"; + this.workingCopy = getWorkingCopy("/Converter_15/src/X.java", true/*resolve*/); + IJavaProject javaProject = this.workingCopy.getJavaProject(); + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); + ASTNode node = buildAST( + contents, + this.workingCopy); + assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType()); + CompilationUnit compilationUnit = (CompilationUnit) node; + assertProblemsSize(compilationUnit, 0); + node = ((AbstractTypeDeclaration)compilationUnit.types().get(0)); + assertEquals("Not a Type", ASTNode.RECORD_DECLARATION, node.getNodeType()); + ASTParser parser= ASTParser.newParser(getAST15()); + parser.setProject(javaProject); + IBinding[] bindings = parser.createBindings(new IJavaElement[] { this.workingCopy.findPrimaryType() }, null); + IMethodBinding methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[0]; + assertEquals("compact constructor name", "X", methodBinding.getName()); + assertTrue("not a Constructor", methodBinding.isConstructor()); + assertTrue("not a CompactConstructor", methodBinding.isCompactConstructor()); + assertTrue("not a CanonicalConstructor", methodBinding.isCanonicalConstructor()); + methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[1]; + assertEquals("constructor name", "X", methodBinding.getName()); + assertTrue("not a Constructor", methodBinding.isConstructor()); + assertFalse("Is CompactConstructor?", methodBinding.isCompactConstructor()); + assertFalse("Is CanonicalConstructor?", methodBinding.isCanonicalConstructor()); + methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[2]; + assertEquals("method name", "abc", methodBinding.getName()); + assertFalse("Is a Constructor?", methodBinding.isConstructor()); + assertFalse("Is a CompactConstructor?", methodBinding.isCompactConstructor()); + assertFalse("Is CanonicalConstructor?", methodBinding.isCanonicalConstructor()); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + public void testRecordConstructor002() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = "record X(int lo) {\n" + + " public X(int lo) {\n" + + " this.lo = lo;" + + " \n}\n" + + " public X(String str) {\n" + + " this((str != null) ? str.length() : 0);" + + " \n}\n" + + " public int abc() {\n" + + " return this.lo;\n" + + " }\n" + + "\n" + + "}\n"; + this.workingCopy = getWorkingCopy("/Converter_15/src/X.java", true/*resolve*/); + IJavaProject javaProject = this.workingCopy.getJavaProject(); + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); + ASTNode node = buildAST( + contents, + this.workingCopy); + assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType()); + CompilationUnit compilationUnit = (CompilationUnit) node; + assertProblemsSize(compilationUnit, 0); + node = ((AbstractTypeDeclaration)compilationUnit.types().get(0)); + assertEquals("Not a Type", ASTNode.RECORD_DECLARATION, node.getNodeType()); + ASTParser parser= ASTParser.newParser(getAST15()); + parser.setProject(javaProject); + IBinding[] bindings = parser.createBindings(new IJavaElement[] { this.workingCopy.findPrimaryType() }, null); + IMethodBinding methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[0]; + assertEquals("compact constructor name", "X", methodBinding.getName()); + assertTrue("not a Constructor", methodBinding.isConstructor()); + assertTrue("is a CanonicalConstructor", methodBinding.isCanonicalConstructor()); + assertFalse("is a CompactConstructor", methodBinding.isCompactConstructor()); + methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[1]; + assertEquals("constructor name", "X", methodBinding.getName()); + assertTrue("not a Constructor", methodBinding.isConstructor()); + assertFalse("Is CanonicalConstructor?", methodBinding.isCanonicalConstructor()); + assertFalse("Is CompactConstructor?", methodBinding.isCompactConstructor()); + methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[2]; + assertEquals("method name", "abc", methodBinding.getName()); + assertFalse("Is a Constructor?", methodBinding.isConstructor()); + assertFalse("Is a CanonicalConstructor?", methodBinding.isCompactConstructor()); + assertFalse("Is a CompactConstructor?", methodBinding.isCompactConstructor()); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/IMethodBinding.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/IMethodBinding.java index 7473ca9bfe..9e1967773d 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/IMethodBinding.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/IMethodBinding.java @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Stephan Herrmann - Contribution for @@ -77,6 +81,19 @@ public interface IMethodBinding extends IBinding { public boolean isCompactConstructor(); /** + * Returns whether this binding is for a canonical constructor or not. + * + * <p> + * This method returns <code>true</code> for canonical constructors + * </p> + * + * @return <code>true</code> if this is the binding for a canonical constructor + * and <code>false</code> otherwise + * @noreference + */ + public boolean isCanonicalConstructor(); + + /** * Returns whether this binding is known to be a compiler-generated * default constructor. * <p> diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java index fbc2bac346..af93037f0b 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Stephan Herrmann - Contribution for @@ -81,6 +85,14 @@ class MethodBinding implements IMethodBinding { } /** + * @see IMethodBinding#isCanonicalConstructor() + */ + @Override + public boolean isCanonicalConstructor() { + return ((this.binding.tagBits & TagBits.IsCanonicalConstructor) != 0); + } + + /** * @see IMethodBinding#isDefaultConstructor() * @since 3.0 */ |