Commit db802e6e authored by Remi Forax's avatar Remi Forax

Merge branch 'asm8-record' into 'master'

Add the support of the Record attribute

See merge request !275
parents 6eaacd4a 98817bd6
Pipeline #6787 passed with stage
in 10 minutes and 9 seconds
......@@ -36,6 +36,7 @@ import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.TypePath;
/**
......@@ -141,6 +142,20 @@ public class ClassRemapper extends ClassVisitor {
super.visitAttribute(attribute);
}
@Override
public RecordComponentVisitor visitRecordComponentExperimental(
final int access, final String name, final String descriptor, final String signature) {
RecordComponentVisitor recordComponentVisitor =
super.visitRecordComponentExperimental(
access,
remapper.mapRecordComponentNameExperimental(className, name, descriptor),
remapper.mapDesc(descriptor),
remapper.mapSignature(signature, true));
return recordComponentVisitor == null
? null
: createRecordComponentRemapper(recordComponentVisitor);
}
@Override
public FieldVisitor visitField(
final int access,
......@@ -252,4 +267,16 @@ public class ClassRemapper extends ClassVisitor {
protected ModuleVisitor createModuleRemapper(final ModuleVisitor moduleVisitor) {
return new ModuleRemapper(api, moduleVisitor, remapper);
}
/**
* Constructs a new remapper for record components. The default implementation of this method
* returns a new {@link RecordComponentRemapper}.
*
* @param recordComponentVisitor the RecordComponentVisitor the remapper must delegate to.
* @return the newly created remapper.
*/
protected RecordComponentVisitor createRecordComponentRemapper(
final RecordComponentVisitor recordComponentVisitor) {
return new RecordComponentRemapper(api, recordComponentVisitor, remapper);
}
}
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.TypePath;
/**
* A {@link RecordComponentVisitor} that remaps types with a {@link Remapper}.
*
* @author Remi Forax
*/
public class RecordComponentRemapper extends RecordComponentVisitor {
/** The remapper used to remap the types in the visited field. */
protected final Remapper remapper;
/**
* Constructs a new {@link RecordComponentRemapper}. <i>Subclasses must not use this
* constructor</i>. Instead, they must use the {@link
* #RecordComponentRemapper(int,RecordComponentVisitor,Remapper)} version.
*
* @param recordComponentVisitor the record component visitor this remapper must delegate to.
* @param remapper the remapper to use to remap the types in the visited record component.
*/
public RecordComponentRemapper(
final RecordComponentVisitor recordComponentVisitor, final Remapper remapper) {
// TODO: add 'latest api =' comment when no longer experimental.
this(Opcodes.ASM8_EXPERIMENTAL, recordComponentVisitor, remapper);
}
/**
* Constructs a new {@link RecordComponentRemapper}.
*
* @param api the ASM API version supported by this remapper. Must be {@link
* org.objectweb.asm.Opcodes#ASM8_EXPERIMENTAL}.
* @param recordComponentVisitor the record component visitor this remapper must delegate to.
* @param remapper the remapper to use to remap the types in the visited record component.
*/
protected RecordComponentRemapper(
final int api, final RecordComponentVisitor recordComponentVisitor, final Remapper remapper) {
super(api, recordComponentVisitor);
this.remapper = remapper;
}
@Override
public AnnotationVisitor visitAnnotationExperimental(
final String descriptor, final boolean visible) {
AnnotationVisitor annotationVisitor =
super.visitAnnotationExperimental(remapper.mapDesc(descriptor), visible);
return annotationVisitor == null
? null
: new AnnotationRemapper(api, annotationVisitor, remapper);
}
@Override
public AnnotationVisitor visitTypeAnnotationExperimental(
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
AnnotationVisitor annotationVisitor =
super.visitTypeAnnotationExperimental(
typeRef, typePath, remapper.mapDesc(descriptor), visible);
return annotationVisitor == null
? null
: new AnnotationRemapper(api, annotationVisitor, remapper);
}
}
......@@ -286,6 +286,22 @@ public abstract class Remapper {
return name;
}
/**
* Maps a record component name to its new name. The default implementation of this method returns
* the given name, unchanged. Subclasses can override.
*
* @param owner the internal name of the owner class of the field.
* @param name the name of the field.
* @param descriptor the descriptor of the field.
* @return the new name of the field.
* @deprecated this API is experimental.
*/
@Deprecated
public String mapRecordComponentNameExperimental(
final String owner, final String name, final String descriptor) {
return name;
}
/**
* Maps a field name to its new name. The default implementation of this method returns the given
* name, unchanged. Subclasses can override.
......
......@@ -123,7 +123,8 @@ public abstract class AsmTest {
JDK11_ALL_INSTRUCTIONS("jdk11.AllInstructions"),
JDK11_ALL_STRUCTURES("jdk11.AllStructures"),
JDK11_ALL_STRUCTURES_NESTED("jdk11.AllStructures$Nested"),
JDK14_ALL_STRUCTURES("jdk14.AllStructures");
JDK14_ALL_STRUCTURES("jdk14.AllStructures"),
JDK14_ALL_STRUCTURES_RECORD("jdk14.AllStructures$RecordSubType");
private final String name;
private byte[] bytes;
......
......@@ -496,6 +496,8 @@ public class ClassFile {
dumpNestMembersAttribute(parser, builder);
} else if (attributeName.equals("PermittedSubtypes")) {
dumpPermittedSubtypesAttribute(parser, builder);
} else if (attributeName.equals("Record")) {
dumpRecordAttribute(parser, builder);
} else if (attributeName.equals("StackMap")) {
dumpStackMapAttribute(parser, builder);
} else if (!attributeName.equals("CodeComment") && !attributeName.equals("Comment")) {
......@@ -1704,6 +1706,24 @@ public class ClassFile {
}
}
/**
* Parses and dumps a Record attribute.
*
* @param parser a class parser.
* @param builder a dump builder.
* @throws IOException if the class can't be parsed.
* @see <a href="https://openjdk.java.net/jeps/360">JEP 360</a>
*/
private static void dumpRecordAttribute(final Parser parser, final Builder builder)
throws IOException {
int numberOfComponentRecords = builder.add("number_of_component_records: ", parser.u2());
for (int i = 0; i < numberOfComponentRecords; ++i) {
builder.addCpInfo("record_component_name: ", parser.u2());
builder.addCpInfo("record_component_descriptor: ", parser.u2());
dumpAttributeList(parser, builder);
}
}
/**
* Parses and dumps a StackMap attribute.
*
......
......@@ -36,6 +36,7 @@ import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.TypePath;
/**
......@@ -135,6 +136,13 @@ public class ClassNode extends ClassVisitor {
*/
@Deprecated public List<String> permittedSubtypesExperimental;
/**
* The record components of this class. May be {@literal null}.
*
* @deprecated this API is experimental.
*/
@Deprecated public List<RecordComponentNode> recordComponentsExperimental;
/** The fields of this class. */
public List<FieldNode> fields;
......@@ -257,6 +265,15 @@ public class ClassNode extends ClassVisitor {
innerClasses.add(innerClass);
}
@Override
public RecordComponentVisitor visitRecordComponentExperimental(
final int access, final String name, final String descriptor, final String signature) {
RecordComponentNode recordComponent =
new RecordComponentNode(access, name, descriptor, signature);
recordComponentsExperimental = Util.add(recordComponentsExperimental, recordComponent);
return recordComponent;
}
@Override
public FieldVisitor visitField(
final int access,
......@@ -302,6 +319,9 @@ public class ClassNode extends ClassVisitor {
if (api != Opcodes.ASM8_EXPERIMENTAL && permittedSubtypesExperimental != null) {
throw new UnsupportedClassVersionException();
}
if (api != Opcodes.ASM8_EXPERIMENTAL && recordComponentsExperimental != null) {
throw new UnsupportedClassVersionException();
}
if (api < Opcodes.ASM7 && (nestHostClass != null || nestMembers != null)) {
throw new UnsupportedClassVersionException();
}
......@@ -337,6 +357,11 @@ public class ClassNode extends ClassVisitor {
invisibleTypeAnnotations.get(i).check(api);
}
}
if (recordComponentsExperimental != null) {
for (int i = recordComponentsExperimental.size() - 1; i >= 0; --i) {
recordComponentsExperimental.get(i).checkExperimental(api);
}
}
for (int i = fields.size() - 1; i >= 0; --i) {
fields.get(i).check(api);
}
......@@ -422,6 +447,12 @@ public class ClassNode extends ClassVisitor {
for (int i = 0, n = innerClasses.size(); i < n; ++i) {
innerClasses.get(i).accept(classVisitor);
}
// Visit the record components.
if (recordComponentsExperimental != null) {
for (int i = 0, n = recordComponentsExperimental.size(); i < n; ++i) {
recordComponentsExperimental.get(i).acceptExperimental(classVisitor);
}
}
// Visit the fields.
for (int i = 0, n = fields.size(); i < n; ++i) {
fields.get(i).accept(classVisitor);
......
......@@ -106,7 +106,7 @@ public class FieldNode extends FieldVisitor {
}
/**
* Constructs a new {@link FieldNode}. <i>Subclasses must not use this constructor</i>.
* Constructs a new {@link FieldNode}.
*
* @param api the ASM API version implemented by this visitor. Must be one of {@link Opcodes#ASM4}
* or {@link Opcodes#ASM5}.
......
......@@ -47,6 +47,7 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.TypePath;
import org.objectweb.asm.test.AsmTest;
import org.objectweb.asm.test.ClassFile;
......@@ -176,6 +177,20 @@ public class ClassNodeTest extends AsmTest {
super(api, classVisitor);
}
@Override
public ModuleVisitor visitModule(final String name, final int access, final String version) {
return null;
}
@Override
public void visitNestHost(final String nestHost) {}
@Override
public void visitNestMember(final String nestMember) {}
@Override
public void visitPermittedSubtypeExperimental(final String permittedSubtype) {}
@Override
public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
return null;
......@@ -190,6 +205,15 @@ public class ClassNodeTest extends AsmTest {
return null;
}
@Override
public void visitAttribute(final Attribute attribute) {}
@Override
public RecordComponentVisitor visitRecordComponentExperimental(
final int access, final String name, final String descriptor, final String signature) {
return null;
}
@Override
public FieldVisitor visitField(
final int access,
......@@ -209,22 +233,5 @@ public class ClassNodeTest extends AsmTest {
final String[] exceptions) {
return null;
}
@Override
public ModuleVisitor visitModule(final String name, final int access, final String version) {
return null;
}
@Override
public void visitNestHost(final String nestHost) {}
@Override
public void visitNestMember(final String nestMember) {}
@Override
public void visitPermittedSubtypeExperimental(final String permittedSubtype) {}
@Override
public void visitAttribute(final Attribute attribute) {}
}
}
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.tree;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.objectweb.asm.test.AsmTest;
/**
* Unit tests for {@link RecordComponentNode}.
*
* @author Eric Bruneton
*/
public class RecordComponentNodeTest extends AsmTest {
@Test
public void testConstructor() {
RecordComponentNode fieldNode = new RecordComponentNode(123, "component", "I", null);
assertEquals(123, fieldNode.accessExperimental);
assertEquals("component", fieldNode.nameExperimental);
assertEquals("I", fieldNode.descriptorExperimental);
}
@Test
public void testConstructor_illegalState() {
Executable constructor = () -> new RecordComponentNode(123, "component", "I", null) {};
assertThrows(IllegalStateException.class, constructor);
}
}
......@@ -205,12 +205,14 @@ public class ASMifier extends Printer {
text.add("import org.objectweb.asm.Label;\n");
text.add("import org.objectweb.asm.MethodVisitor;\n");
text.add("import org.objectweb.asm.Opcodes;\n");
text.add("import org.objectweb.asm.RecordComponentVisitor;\n");
text.add("import org.objectweb.asm.Type;\n");
text.add("import org.objectweb.asm.TypePath;\n");
text.add("public class " + simpleName + "Dump implements Opcodes {\n\n");
text.add("public static byte[] dump () throws Exception {\n\n");
text.add("ClassWriter classWriter = new ClassWriter(0);\n");
text.add("FieldVisitor fieldVisitor;\n");
text.add("RecordComponentVisitor recordComponentVisitor;\n");
text.add("MethodVisitor methodVisitor;\n");
text.add("AnnotationVisitor annotationVisitor0;\n\n");
......@@ -345,6 +347,27 @@ public class ASMifier extends Printer {
text.add(stringBuilder.toString());
}
@Override
public ASMifier visitRecordComponentExperimental(
final int access, final String name, final String descriptor, final String signature) {
stringBuilder.setLength(0);
stringBuilder.append("{\n");
stringBuilder.append("recordComponentVisitor = classWriter.visitRecordComponentExperimental(");
appendAccessFlags(access | ACCESS_FIELD);
stringBuilder.append(", ");
appendConstant(name);
stringBuilder.append(", ");
appendConstant(descriptor);
stringBuilder.append(", ");
appendConstant(signature);
stringBuilder.append(");\n");
text.add(stringBuilder.toString());
ASMifier asmifier = createASMifier("recordComponentVisitor", 0);
text.add(asmifier.getText());
text.add("}\n");
return asmifier;
}
@Override
public ASMifier visitField(
final int access,
......@@ -582,6 +605,66 @@ public class ASMifier extends Printer {
text.add(stringBuilder.toString());
}
// -----------------------------------------------------------------------------------------------
// Record components
// -----------------------------------------------------------------------------------------------
@Override
public ASMifier visitRecordComponentAnnotationExperimental(
final String descriptor, final boolean visible) {
// TODO Use this call when not experimental anymore
// return visitAnnotation(descriptor, visible);
stringBuilder.setLength(0);
stringBuilder
.append("{\n")
.append(ANNOTATION_VISITOR0)
.append(name)
.append(".visitAnnotationExperimental(");
appendConstant(descriptor);
stringBuilder.append(", ").append(visible).append(");\n");
text.add(stringBuilder.toString());
ASMifier asmifier = createASMifier(ANNOTATION_VISITOR, 0);
text.add(asmifier.getText());
text.add("}\n");
return asmifier;
}
@Override
public ASMifier visitRecordComponentTypeAnnotationExperimental(
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
// TODO Use this call when not experimental anymore
// return visitTypeAnnotation(typeRef, typePath, descriptor, visible);
return visitTypeAnnotation(
"visitTypeAnnotationExperimental", typeRef, typePath, descriptor, visible);
}
@Override
public void visitRecordComponentAttributeExperimental(final Attribute attribute) {
// TODO Use this call when not experimental anymore
// visitAttribute(attribute);
stringBuilder.setLength(0);
stringBuilder.append("// ATTRIBUTE ").append(attribute.type).append('\n');
if (attribute instanceof ASMifierSupport) {
if (labelNames == null) {
labelNames = new HashMap<>();
}
stringBuilder.append("{\n");
((ASMifierSupport) attribute).asmify(stringBuilder, "attribute", labelNames);
stringBuilder.append(name).append(".visitAttributeExperimental(attribute);\n");
stringBuilder.append("}\n");
}
text.add(stringBuilder.toString());
}
@Override
public void visitRecordComponentEndExperimental() {
stringBuilder.setLength(0);
// TODO Use this call when not experimental anymore
// stringBuilder.append(name).append(VISIT_END);
stringBuilder.append(name).append(".visitEndExperimental();\n");
text.add(stringBuilder.toString());
}
// -----------------------------------------------------------------------------------------------
// Fields
// -----------------------------------------------------------------------------------------------
......
......@@ -44,6 +44,7 @@ import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.TypePath;
import org.objectweb.asm.TypeReference;
......@@ -374,6 +375,20 @@ public class CheckClassAdapter extends ClassVisitor {
super.visitInnerClass(name, outerName, innerName, access);
}
@Override
public RecordComponentVisitor visitRecordComponentExperimental(
final int access, final String name, final String descriptor, final String signature) {
checkState();
checkAccess(access, Opcodes.ACC_DEPRECATED);
CheckMethodAdapter.checkUnqualifiedName(version, name, "record component name");
CheckMethodAdapter.checkDescriptor(version, descriptor, /* canBeVoid = */ false);
if (signature != null) {
checkFieldSignature(signature);
}
return new CheckRecordComponentAdapter(
api, super.visitRecordComponentExperimental(access, name, descriptor, signature));
}
@Override
public FieldVisitor visitField(
final int access,
......@@ -393,6 +408,7 @@ public class CheckClassAdapter extends ClassVisitor {
| Opcodes.ACC_TRANSIENT
| Opcodes.ACC_SYNTHETIC
| Opcodes.ACC_ENUM
| Opcodes.ACC_MANDATED
| Opcodes.ACC_DEPRECATED);
CheckMethodAdapter.checkUnqualifiedName(version, name, "field name");
CheckMethodAdapter.checkDescriptor(version, descriptor, /* canBeVoid = */ false);
......@@ -427,6 +443,7 @@ public class CheckClassAdapter extends ClassVisitor {
| Opcodes.ACC_ABSTRACT
| Opcodes.ACC_STRICT
| Opcodes.ACC_SYNTHETIC
| Opcodes.ACC_MANDATED
| Opcodes.ACC_DEPRECATED);
if (!"<init>".equals(name) && !"<clinit>".equals(name)) {
CheckMethodAdapter.checkMethodIdentifier(version, name, "method name");
......
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.util;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.TypePath;
import org.objectweb.asm.TypeReference;
/**
* A {@link RecordComponentVisitor} that checks that its methods are properly used.
*
* @author Eric Bruneton
* @author Remi Forax
*/
public class CheckRecordComponentAdapter extends RecordComponentVisitor {
/** Whether the {@link #visitEndExperimental()} method has been called. */
private boolean visitEndCalled;
/**
* Constructs a new {@link CheckRecordComponentAdapter}. <i>Subclasses must not use this
* constructor</i>. Instead, they must use the {@link #CheckRecordComponentAdapter(int,
* RecordComponentVisitor)} version.
*
* @param recordComponentVisitor the record component visitor to which this adapter must delegate
* calls.
* @throws IllegalStateException If a subclass calls this constructor.
*/
public CheckRecordComponentAdapter(final RecordComponentVisitor recordComponentVisitor) {
// TODO: add 'latest api =' comment when no longer experimental.
this(Opcodes.ASM8_EXPERIMENTAL, recordComponentVisitor);
if (getClass() != CheckRecordComponentAdapter.class) {
throw new IllegalStateException();
}
}
/**
* Constructs a new {@link CheckRecordComponentAdapter}.
*
* @param api the ASM API version implemented by this visitor. Must be {@link
* Opcodes#ASM8_EXPERIMENTAL}.
* @param recordComponentVisitor the record component visitor to which this adapter must delegate
* calls.
*/
protected CheckRecordComponentAdapter(
final int api, final RecordComponentVisitor recordComponentVisitor) {
super(api, recordComponentVisitor);
}
@Override
public AnnotationVisitor visitAnnotationExperimental(
final String descriptor, final boolean visible) {
checkVisitEndNotCalled();
// Annotations can only appear in V1_5 or more classes.
CheckMethodAdapter.checkDescriptor(Opcodes.V1_5, descriptor, false);
return new CheckAnnotationAdapter(super.visitAnnotationExperimental(descriptor, visible));
}
@Override
public AnnotationVisitor visitTypeAnnotationExperimental(
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
checkVisitEndNotCalled();
int sort = new TypeReference(typeRef).getSort();
if (sort != TypeReference.FIELD) {
throw new IllegalArgumentException(
"Invalid type reference sort 0x" + Integer.toHexString(sort));
}
CheckClassAdapter.checkTypeRef(typeRef);
CheckMethodAdapter.checkDescriptor(Opcodes.V1_5, descriptor, false);
return new CheckAnnotationAdapter(
super.visitTypeAnnotationExperimental(typeRef, typePath, descriptor, visible));
}
@Override
public void visitAttributeExperimental(final Attribute attribute) {
checkVisitEndNotCalled();
if (attribute == null) {