Commit db802e6e authored by Remi Forax's avatar Remi Forax
Browse files

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}.
......
// 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 java.util.List;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.TypePath;
/**
* A node that represents a record component.
*
* @author Remi Forax
* @deprecated this API is experimental.
*/
@Deprecated
public class RecordComponentNode extends RecordComponentVisitor {
/**
* The record component access flags (see {@link org.objectweb.asm.Opcodes}). The only valid value
* is {@link Opcodes#ACC_DEPRECATED}.
*
* @deprecated this API is experimental.
*/
public int accessExperimental;
/**
* The record component name.
*
* @deprecated this API is experimental.
*/
public String nameExperimental;
/**
* The record component descriptor (see {@link org.objectweb.asm.Type}).
*
* @deprecated this API is experimental.
*/
public String descriptorExperimental;
/**
* The record component signature. May be {@literal null}.
*
* @deprecated this API is experimental.
*/
public String signatureExperimental;
/**
* The runtime visible annotations of this record component. May be {@literal null}.
*
* @deprecated this API is experimental.
*/
public List<AnnotationNode> visibleAnnotationsExperimental;
/**
* The runtime invisible annotations of this record component. May be {@literal null}.
*
* @deprecated this API is experimental.
*/
public List<AnnotationNode> invisibleAnnotationsExperimental;
/**
* The runtime visible type annotations of this record component. May be {@literal null}.
*
* @deprecated this API is experimental.
*/
public List<TypeAnnotationNode> visibleTypeAnnotationsExperimental;
/**
* The runtime invisible type annotations of this record component. May be {@literal null}.
*
* @deprecated this API is experimental.
*/
public List<TypeAnnotationNode> invisibleTypeAnnotationsExperimental;
/**
* The non standard attributes of this record component. * May be {@literal null}.
*
* @deprecated this API is experimental.
*/
public List<Attribute> attrsExperimental;
/**
* Constructs a new {@link RecordComponentNode}. <i>Subclasses must not use this constructor</i>.
* Instead, they must use the {@link #RecordComponentNode(int, int, String, String, String)}
* version.
*
* @param access the record component access flags (see {@link org.objectweb.asm.Opcodes}). The
* only valid value is {@link Opcodes#ACC_DEPRECATED}.
* @param name the record component name.
* @param descriptor the record component descriptor (see {@link org.objectweb.asm.Type}).
* @param signature the record component signature.
* @throws IllegalStateException If a subclass calls this constructor.
* @deprecated this API is experimental.
*/
@Deprecated
public RecordComponentNode(
final int access, final String name, final String descriptor, final String signature) {
this(/* latest api = */ Opcodes.ASM7, access, name, descriptor, signature);
if (getClass() != RecordComponentNode.class) {
throw new IllegalStateException();
}
}
/**
* Constructs a new {@link RecordComponentNode}.
*
* @param api the ASM API version implemented by this visitor. Must be {@link
* Opcodes#ASM8_EXPERIMENTAL}.
* @param access the record component access flags (see {@link org.objectweb.asm.Opcodes}). The
* only valid value is {@link Opcodes#ACC_DEPRECATED}.
* @param name the record component name.
* @param descriptor the record component descriptor (see {@link org.objectweb.asm.Type}).
* @param signature the record component signature.
* @deprecated this API is experimental.
*/
@Deprecated
public RecordComponentNode(
final int api,
final int access,
final String name,
final String descriptor,
final String signature) {
super(api);
this.accessExperimental = access;
this.nameExperimental = name;
this.descriptorExperimental = descriptor;
this.signatureExperimental = signature;
}
// -----------------------------------------------------------------------------------------------
// Implementation of the FieldVisitor abstract class
// -----------------------------------------------------------------------------------------------
@Override
public AnnotationVisitor visitAnnotationExperimental(
final String descriptor, final boolean visible) {
AnnotationNode annotation = new AnnotationNode(descriptor);
if (visible) {
visibleAnnotationsExperimental = Util.add(visibleAnnotationsExperimental, annotation);
} else {
invisibleAnnotationsExperimental = Util.add(invisibleAnnotationsExperimental, annotation);
}
return annotation;
}
@Override
public AnnotationVisitor visitTypeAnnotationExperimental(
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
TypeAnnotationNode typeAnnotation = new TypeAnnotationNode(typeRef, typePath, descriptor);
if (visible) {
visibleTypeAnnotationsExperimental =
Util.add(visibleTypeAnnotationsExperimental, typeAnnotation);
} else {
invisibleTypeAnnotationsExperimental =
Util.add(invisibleTypeAnnotationsExperimental, typeAnnotation);
}
return typeAnnotation;
}
@Override
public void visitAttributeExperimental(final Attribute attribute) {
attrsExperimental = Util.add(attrsExperimental, attribute);
}
@Override
public void visitEndExperimental() {
// Nothing to do.
}
// -----------------------------------------------------------------------------------------------
// Accept methods
// -----------------------------------------------------------------------------------------------
/**
* Checks that this record component node is compatible with the given ASM API version. This
* method checks that this node, and all its children recursively, do not contain elements that
* were introduced in more recent versions of the ASM API than the given version.
*
* @param api an ASM API version. Must be {@link Opcodes#ASM8_EXPERIMENTAL}.
* @deprecated this API is experimental.
*/
public void checkExperimental(final int api) {
if (api != Opcodes.ASM8_EXPERIMENTAL) {
throw new UnsupportedClassVersionException();
}
}
/**
* Makes the given class visitor visit this record component.
*
* @param classVisitor a class visitor.
* @deprecated this API is experimental.
*/
public void acceptExperimental(final ClassVisitor classVisitor) {
RecordComponentVisitor recordComponentVisitor =
classVisitor.visitRecordComponentExperimental(
accessExperimental, nameExperimental, descriptorExperimental, signatureExperimental);
if (recordComponentVisitor == null) {
return;
}
// Visit the annotations.
if (visibleAnnotationsExperimental != null) {
for (int i = 0, n = visibleAnnotationsExperimental.size(); i < n; ++i) {
AnnotationNode annotation = visibleAnnotationsExperimental.get(i);
annotation.accept(
recordComponentVisitor.visitAnnotationExperimental(annotation.desc, true));
}
}
if (invisibleAnnotationsExperimental != null) {
for (int i = 0, n = invisibleAnnotationsExperimental.size(); i < n; ++i) {
AnnotationNode annotation = invisibleAnnotationsExperimental.get(i);
annotation.accept(
recordComponentVisitor.visitAnnotationExperimental(annotation.desc, false));
}
}
if (visibleTypeAnnotationsExperimental != null) {
for (int i = 0, n = visibleTypeAnnotationsExperimental.size(); i < n; ++i) {
TypeAnnotationNode typeAnnotation = visibleTypeAnnotationsExperimental.get(i);
typeAnnotation.accept(
recordComponentVisitor.visitTypeAnnotationExperimental(
typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, true));
}
}
if (invisibleTypeAnnotationsExperimental != null) {
for (int i = 0, n = invisibleTypeAnnotationsExperimental.size(); i < n; ++i) {
TypeAnnotationNode typeAnnotation = invisibleTypeAnnotationsExperimental.get(i);
typeAnnotation.accept(
recordComponentVisitor.visitTypeAnnotationExperimental(
typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, false));
}
}
// Visit the non standard attributes.
if (attrsExperimental != null) {
for (int i = 0, n = attrsExperimental.size(); i < n; ++i) {
recordComponentVisitor.visitAttributeExperimental(attrsExperimental.get(i));
}
}
recordComponentVisitor.visitEndExperimental();
}
}
......@@ -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