Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Arthanareeswaran2020-02-20 09:17:49 +0000
committerJay Arthanareeswaran2020-02-20 09:39:56 +0000
commit77a8187efade4da99289e7b85a335358a3c25d08 (patch)
treef90f09665aed2c1687dc002ab12d9e6a8c36e890
parenta29dbe04febdc3cab76b9c88e8ecd30003e154cd (diff)
downloadeclipse.jdt.core-BETA_JAVA14.tar.gz
eclipse.jdt.core-BETA_JAVA14.tar.xz
eclipse.jdt.core-BETA_JAVA14.zip
Bug 560260 - Add implementation for RecordComponentElement#getAccessor()BETA_JAVA14
Change-Id: Ie7dabc68f0ca8bef769d2d2b80eedb1d5a3d944e Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
-rw-r--r--org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jarbin280972 -> 282238 bytes
-rw-r--r--org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java14ElementProcessor.java50
-rw-r--r--org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java14ElementsTests.java16
-rw-r--r--org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java4
-rw-r--r--org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/RecordComponentElementImpl.java10
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
--- a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar
+++ b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar
Binary files 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<String, TypeKind> 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<? extends Element> members = _elementUtils.getAllMembers(recordElement);
+ final List<? extends Element> enclosedElements = recordElement.getEnclosedElements();
+
+ final HashSet<? extends Element> enclosedElementsSet = new HashSet<Element>(recordElement.getEnclosedElements());
+
+ List<ExecutableElement> constructors = ElementFilter.constructorsIn(enclosedElements);
+ List<ExecutableElement> methods = ElementFilter.methodsIn(enclosedElements);
+ List<VariableElement> fields = ElementFilter.fieldsIn(enclosedElements);
+
+ Set<ExecutableElement> constructorsSet = ElementFilter.constructorsIn(enclosedElementsSet);
+ Set<ExecutableElement> methodsSet = ElementFilter.methodsIn(enclosedElementsSet);
+ Set<VariableElement> 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<Element>(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<Element>(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<Element>(fields), fieldsSet);
+ }
+ public void testRecords7() {
+ TypeElement recordElement = _elementUtils.getTypeElement("records.Record2");
+ final List<? extends Element> members = _elementUtils.getAllMembers(recordElement);
+ final List<? extends Element> enclosedElements = recordElement.getEnclosedElements();
+ List<RecordComponentElement> 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;
}
}

Back to the top