From 77a8187efade4da99289e7b85a335358a3c25d08 Mon Sep 17 00:00:00 2001 From: Jay Arthanareeswaran Date: Thu, 20 Feb 2020 14:47:49 +0530 Subject: Bug 560260 - Add implementation for RecordComponentElement#getAccessor() Change-Id: Ie7dabc68f0ca8bef769d2d2b80eedb1d5a3d944e Signed-off-by: Jay Arthanareeswaran --- .../lib/apttestprocessors8.jar | Bin 280972 -> 282238 bytes .../elements/Java14ElementProcessor.java | 50 +++++++++++++++++++-- .../compiler/apt/tests/Java14ElementsTests.java | 16 +++++++ .../jdt/internal/compiler/apt/model/Factory.java | 4 -- .../apt/model/RecordComponentElementImpl.java | 10 ++++- 5 files changed, 72 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar index 0df7843e78..2112b322ea 100644 Binary files a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar and b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar differ diff --git a/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java14ElementProcessor.java b/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java14ElementProcessor.java index 4bc52c12d3..9b934a9e58 100644 --- a/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java14ElementProcessor.java +++ b/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java14ElementProcessor.java @@ -130,6 +130,12 @@ public class Java14ElementProcessor extends BaseProcessor { public void testAll() throws AssertionFailedError, IOException { testPreviewFlagTrue(); + testRecords1(); + testRecords2(); + testRecords3(); + testRecords4(); + testRecords5(); + testRecords6(); } public void testPreviewFlagTrue() throws IOException { @@ -218,9 +224,6 @@ public class Java14ElementProcessor extends BaseProcessor { verifyAnnotations(recordComponent, new String[]{"@MyAnnot()"}); } - /* - * Test for getAccessor of a record component - */ public void testRecords5() { Map expRecComps = new HashMap<>(); expRecComps.put("x", TypeKind.INT); @@ -275,6 +278,47 @@ public class Java14ElementProcessor extends BaseProcessor { "actual : " + actualMethodNames); } } + public void testRecords6() { + TypeElement recordElement = _elementUtils.getTypeElement("records.Record2"); + final List members = _elementUtils.getAllMembers(recordElement); + final List enclosedElements = recordElement.getEnclosedElements(); + + final HashSet enclosedElementsSet = new HashSet(recordElement.getEnclosedElements()); + + List constructors = ElementFilter.constructorsIn(enclosedElements); + List methods = ElementFilter.methodsIn(enclosedElements); + List fields = ElementFilter.fieldsIn(enclosedElements); + + Set constructorsSet = ElementFilter.constructorsIn(enclosedElementsSet); + Set methodsSet = ElementFilter.methodsIn(enclosedElementsSet); + Set fieldsSet = ElementFilter.fieldsIn(enclosedElementsSet); + + assertTrue("Constructors must be within all members", members.containsAll(constructors)); + assertTrue("Constructors must be within enclosed elements", enclosedElements.containsAll(constructors)); + assertEquals("Overloaded versions of ElementFilter.constructorsIn() must return equal results", + new HashSet(constructors), constructorsSet); + + assertTrue("Methods must be within all members", members.containsAll(methods)); + assertTrue("Methods must be within enclosed elements", enclosedElements.containsAll(methods)); + assertEquals("Overloaded versions of ElementFilter.methodsIn() must return equal results", + new HashSet(methods), methodsSet); + + assertTrue("Fields must be within all members", members.containsAll(fields)); + assertTrue("Fields must be within enclosed elements", enclosedElements.containsAll(fields)); + assertEquals("Overloaded versions of ElementFilter.fieldsIn() must return equal results", new HashSet(fields), fieldsSet); + } + public void testRecords7() { + TypeElement recordElement = _elementUtils.getTypeElement("records.Record2"); + final List members = _elementUtils.getAllMembers(recordElement); + final List enclosedElements = recordElement.getEnclosedElements(); + List records = ElementFilter.recordComponentsIn(enclosedElements); + for (RecordComponentElement record : records) { + ExecutableElement method = record.getAccessor(); + assertTrue("Accessor method not found", members.contains(method)); + assertTrue("Accessor method not found", enclosedElements.contains(method)); + assertEquals("Accessor method name incorrect", record.getSimpleName().toString(), method.getSimpleName().toString()); + } + } @Override public void reportError(String msg) { diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java14ElementsTests.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java14ElementsTests.java index 2ddcfac44e..aa5a15730c 100644 --- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java14ElementsTests.java +++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java14ElementsTests.java @@ -78,6 +78,22 @@ public class Java14ElementsTests extends TestCase { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); internalTestWithPreview(compiler, MODULE_PROC, "14", "testRecords5", null, "records", true); } + public void testRecords6() throws IOException { + JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); + internalTestWithPreview(compiler, MODULE_PROC, "14", "testRecords6", null, "records", true); + } + public void testRecords6Javac() throws IOException { + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + internalTestWithPreview(compiler, MODULE_PROC, "14", "testRecords6", null, "records", true); + } + public void testRecords7() throws IOException { + JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); + internalTestWithPreview(compiler, MODULE_PROC, "14", "testRecords7", null, "records", true); + } + public void testRecords7Javac() throws IOException { + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + internalTestWithPreview(compiler, MODULE_PROC, "14", "testRecords7", null, "records", true); + } protected void internalTestWithPreview(JavaCompiler compiler, String processor, String compliance, String testMethod, String testClass, String resourceArea, boolean preview) throws IOException { diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java index 23720dc569..4e837153bf 100644 --- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java +++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java @@ -51,7 +51,6 @@ import org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding; import org.eclipse.jdt.internal.compiler.lookup.Binding; import org.eclipse.jdt.internal.compiler.lookup.ElementValuePair; import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers; -import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.ModuleBinding; import org.eclipse.jdt.internal.compiler.lookup.PackageBinding; @@ -353,9 +352,6 @@ public class Factory { return null; switch (binding.kind()) { case Binding.FIELD: - if (((FieldBinding) binding).isRecordComponent()) { - return new RecordComponentElementImpl(_env, (FieldBinding) binding); - } case Binding.LOCAL: case Binding.VARIABLE: return new VariableElementImpl(_env, (VariableBinding) binding); diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/RecordComponentElementImpl.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/RecordComponentElementImpl.java index 2aade6ff4e..01ebb8ee9f 100644 --- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/RecordComponentElementImpl.java +++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/RecordComponentElementImpl.java @@ -24,6 +24,9 @@ import javax.lang.model.element.RecordComponentElement; import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl; import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; +import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; +import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; +import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding; public class RecordComponentElementImpl extends VariableElementImpl implements RecordComponentElement { @@ -38,7 +41,12 @@ public class RecordComponentElementImpl extends VariableElementImpl implements R @Override public ExecutableElement getAccessor() { - // TODO: Looks like no direct way of accessing the synthetic directly from the field binding. + FieldBinding field = (FieldBinding) this._binding; + ReferenceBinding binding = field.declaringClass; + if (binding instanceof SourceTypeBinding) { + MethodBinding accessor = ((SourceTypeBinding) binding).getRecordComponentAccessor(field.name); + return new ExecutableElementImpl(_env, accessor); + } return null; } } -- cgit v1.2.3