diff options
author | Kalyan Prasad Tatavarthi | 2020-06-19 06:11:56 +0000 |
---|---|---|
committer | Kalyan Prasad Tatavarthi | 2020-06-24 14:39:59 +0000 |
commit | 146cb65b1914537e39f41cc8788466be3a58b585 (patch) | |
tree | ddde21979caae5569c9e8efaeb2170a535b868a0 | |
parent | 475591df71dc3e97c1ebea9d583779b10c2f1505 (diff) | |
download | eclipse.jdt.core-146cb65b1914537e39f41cc8788466be3a58b585.tar.gz eclipse.jdt.core-146cb65b1914537e39f41cc8788466be3a58b585.tar.xz eclipse.jdt.core-146cb65b1914537e39f41cc8788466be3a58b585.zip |
Bug 563698 - [15] [DOM] [AST] provide an API on IMethodBinding for Compact ConstructorI20200624-1800
1. Used ASTNode.Bit24 bit which is not used on methods.
2. Updated javadoc for isCompactConstructor to indicate that this function works only for source files and not on class files as there is not enough info to deduce this.
3. Added tests
Change-Id: Iec32eec76d459790193f0c3fb063b0429e7b5378
Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
6 files changed, 101 insertions, 3 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter14Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter14Test.java index 4baf681369..82deb0ed68 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter14Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter14Test.java @@ -16,6 +16,7 @@ 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; @@ -29,6 +30,7 @@ import org.eclipse.jdt.core.dom.BreakStatement; 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; @@ -927,4 +929,55 @@ public class ASTConverter14Test extends ConverterTestSetup { } } + public void testRecordConstructor001() throws CoreException { + if (!isJRE14) { + System.err.println("Test "+getName()+" requires a JRE 14"); + return; + } + String contents = "record X(int lo) {\n" + + " public X {\n" + + " \n}\n" + + " public X(String str) {\n" + + " this.lo = (str != null) ? str.length() : 0;\n" + + " \n}\n" + + " public int abc() {\n" + + " return this.lo;\n" + + " }\n" + + "\n" + + "}\n"; + this.workingCopy = getWorkingCopy("/Converter14/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(getAST14()); + 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()); + methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[1]; + assertEquals("constructor name", "X", methodBinding.getName()); + assertTrue("not a Constructor", methodBinding.isConstructor()); + 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 CompactConstructor?", methodBinding.isCompactConstructor()); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ExtraCompilerModifiers.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ExtraCompilerModifiers.java index 97329ccf62..e142a5896e 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ExtraCompilerModifiers.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ExtraCompilerModifiers.java @@ -31,6 +31,7 @@ public interface ExtraCompilerModifiers { // modifier constant final int AccJustFlag = 0xFFFF;// 16 lower bits final int AccDefaultMethod = ASTNode.Bit17; + final int AccCompactConstructor = ASTNode.Bit24; // bit18 - use by ClassFileConstants.AccAnnotationDefault final int AccRestrictedAccess = ASTNode.Bit19; final int AccFromClassFile = ASTNode.Bit20; diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java index 20b3cf7ec4..6f86e09694 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -803,6 +803,12 @@ public final boolean isConstructor() { return this.selector == TypeConstants.INIT; } +/* Answer true if the receiver is a compact constructor +*/ +public final boolean isCompactConstructor() { + return (this.modifiers & ExtraCompilerModifiers.AccCompactConstructor) != 0; +} + /* Answer true if the receiver has default visibility */ public final boolean isDefault() { diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java index ccc537b3ba..17de30f6a8 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java @@ -3103,6 +3103,7 @@ private void populateCompactConstructor(CompactConstructorDeclaration ccd) { //modifiers ccd.declarationSourceStart = this.intStack[this.intPtr--]; ccd.modifiers = this.intStack[this.intPtr--]; + ccd.modifiers |= ExtraCompilerModifiers.AccCompactConstructor; // consume annotations int length; if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) { 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 3b01fb8d30..0851564818 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -48,6 +48,35 @@ public interface IMethodBinding extends IBinding { public boolean isConstructor(); /** + * Returns whether this binding is for a compact constructor or not. + * + * <p> + * This method returns <code>true</code> for: + * <ul> + * <li>compact constructors where the binding + * information was obtained from a Java source file containing a compact constructor + * declaration</li> + * </ul> + * + * </p> + * <p> + * This method returns <code>false</code> for: + * <ul> + * <li>methods</li> + * <li>constructors</li> + * <li>constructors where the binding information was obtained from a Java class file (it + * is not possible to determine from a class file whether a constructor is a + * compact constructor or not</li> + * </ul> + * <p> + * + * @return <code>true</code> if this is the binding for a compact constructor + * in a source file and and <code>false</code> otherwise + * @noreference + */ + public boolean isCompactConstructor(); + + /** * 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 16b33c0552..fbc2bac346 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -73,6 +73,14 @@ class MethodBinding implements IMethodBinding { } /** + * @see IMethodBinding#isCompactConstructor() + */ + @Override + public boolean isCompactConstructor() { + return this.binding.isCompactConstructor(); + } + + /** * @see IMethodBinding#isDefaultConstructor() * @since 3.0 */ |