diff options
author | Kalyan Prasad Tatavarthi | 2020-11-06 06:55:38 +0000 |
---|---|---|
committer | Sarika Sinha | 2020-11-06 11:12:14 +0000 |
commit | acf690f325624478930157b8b486f6ac453e42e2 (patch) | |
tree | e45d5f3e2481c516180bae8152df0aa06f776e33 | |
parent | 78bd16f4aec4e40ee9607069cfbb22ffbfd89050 (diff) | |
download | eclipse.jdt.core-acf690f325624478930157b8b486f6ac453e42e2.tar.gz eclipse.jdt.core-acf690f325624478930157b8b486f6ac453e42e2.tar.xz eclipse.jdt.core-acf690f325624478930157b8b486f6ac453e42e2.zip |
Bug 566853 - [15] record - generate Delegate Methods does not give any outputI20201108-1800I20201107-1800I20201107-0750I20201106-1800I20201106-0710
Added an API in JDT core to find out if a method is a synthetic record method or not.
Change-Id: I80ef0aa44c347f0f1075518f57e81b1d873dce35
Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
3 files changed, 69 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 c374727aa5..c38256a95a 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 @@ -555,6 +555,54 @@ public class ASTConverter_15Test extends ConverterTestSetup { } } + public void testRecord012() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = + "public record X(int myComp) {\n" + + " public void foo() {\n" + + " System.out.println(\"no error\");\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); + List<AbstractTypeDeclaration> types = compilationUnit.types(); + assertEquals("No. of Types is not 1", types.size(), 1); + AbstractTypeDeclaration type = types.get(0); + assertTrue("type not a Record", type instanceof RecordDeclaration); + RecordDeclaration recDecl = (RecordDeclaration)type; + MethodDeclaration[] methods = recDecl.getMethods(); + assertEquals("No. of methods is not 1", methods.length, 1); + ITypeBinding typeBinding = type.resolveBinding(); + assertNotNull("typeBinding is null", typeBinding); + IMethodBinding[] mBindings = typeBinding.getDeclaredMethods(); + assertEquals("No. of declared methods is not 6", mBindings.length, 6); + for (IMethodBinding mBinding : mBindings) { + if (mBinding.getName().equals("X") || mBinding.getName().equals("foo")) { + assertFalse("foo is not a synthetic method", mBinding.isSyntheticRecordMethod()); + } else { + assertTrue("expected a synthetic method", mBinding.isSyntheticRecordMethod()); + } + } + + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + public void testClass001() throws CoreException { if (!isJRE15) { System.err.println("Test "+getName()+" requires a JRE 15"); 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 f5f8f7113d..d6ed09e3bc 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 @@ -438,4 +438,18 @@ public interface IMethodBinding extends IBinding { */ public IVariableBinding[] getSyntheticOuterLocals(); + /** + * Returns if this is a compiler generated equals(), hashCode(), toString() or any accessor + * method of a Record or not. + * Methods equals(), hashCode() and toString() and accessor methods of a Record do not have + * AccSynthetic flag set for them even if they are compiler generated methods. To differentiate + * between these above compiler generated methods and user created methods equals(), hashCode() + * and toString() or accessor methods in a Record, this function can be used. + * + * @return <code>true</code> for compiler generated equals(), hashCode() and toString() or any + * accessor method of a Record, else it returns <code>false</code>. + * @noreference This method is not intended to be referenced by clients. + */ + public boolean isSyntheticRecordMethod(); + } 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 7d938bf654..585f650ef2 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 @@ -24,6 +24,7 @@ import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment; import org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding; import org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding; import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; +import org.eclipse.jdt.internal.compiler.lookup.SyntheticMethodBinding; import org.eclipse.jdt.internal.compiler.lookup.TagBits; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding; @@ -607,4 +608,10 @@ class MethodBinding implements IMethodBinding { public IVariableBinding[] getSyntheticOuterLocals() { return NO_VARIABLE_BINDINGS; } + + @Override + public boolean isSyntheticRecordMethod() { + return ((getDeclaringClass().isRecord()) && + (this.binding instanceof SyntheticMethodBinding)); + } }
\ No newline at end of file |