Commit 2c689cbc authored by Eric Bruneton's avatar Eric Bruneton
Browse files

Proposed API changes for ASM8 (in addition to whatever will be added for the...

Proposed API changes for ASM8 (in addition to whatever will be added for the next Java class file format).
parent 6574c38c
Pipeline #4630 passed with stage
in 7 minutes and 23 seconds
......@@ -17,4 +17,4 @@ build:
- $GRADLE build
- $GRADLE test jacocoTestCoverageVerification
- $GRADLE uploadArchives
- '$GRADLE sonarqube -Dsonar.host.url=$SONAR'
......@@ -211,9 +211,10 @@ final class MethodNodeBuilder {
static ClassFile buildClassWithMethod(final MethodNode methodNode) {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "C", null, "java/lang/Object", null);
classWriter.visit(
Opcodes.V1_5, Opcodes.ACC_PUBLIC, "C", null, "java/lang/Object", Opcodes.NO_INTERFACES);
MethodVisitor methodVisitor =
classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, Opcodes.NO_EXCEPTIONS);
methodVisitor.visitCode();
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitMethodInsn(
......
......@@ -31,8 +31,10 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Array;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
import org.objectweb.asm.IntArray;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
......@@ -493,7 +495,13 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
final String name,
final String descriptor,
final Handle bootstrapMethodHandle,
final Object... bootstrapMethodArguments) {
final Array<Object> bootstrapMethodArguments) {
if (api < Opcodes.ASM8 && bootstrapMethodArguments.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitInvokeDynamicInsn(
name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
return;
}
super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
doVisitMethodInsn(Opcodes.INVOKEDYNAMIC, descriptor);
}
......@@ -535,7 +543,14 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
}
@Override
public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
public void visitLookupSwitchInsn(
final Label dflt, final IntArray keys, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitLookupSwitchInsn(dflt, keys, labels);
return;
}
super.visitLookupSwitchInsn(dflt, keys, labels);
if (isConstructor && !superClassConstructorCalled) {
popValue();
......@@ -545,7 +560,13 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitTableSwitchInsn(
final int min, final int max, final Label dflt, final Label... labels) {
final int min, final int max, final Label dflt, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitTableSwitchInsn(min, max, dflt, labels);
return;
}
super.visitTableSwitchInsn(min, max, dflt, labels);
if (isConstructor && !superClassConstructorCalled) {
popValue();
......@@ -572,10 +593,10 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
}
}
private void addForwardJumps(final Label dflt, final Label[] labels) {
private void addForwardJumps(final Label dflt, final Array<Label> labels) {
addForwardJump(dflt);
for (Label label : labels) {
addForwardJump(label);
for (int i = 0; i < labels.size(); ++i) {
addForwardJump(labels.get(i));
}
}
......
......@@ -31,8 +31,10 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Array;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
import org.objectweb.asm.IntArray;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
......@@ -116,7 +118,7 @@ public class AnalyzerAdapter extends MethodVisitor {
final String name,
final String descriptor,
final MethodVisitor methodVisitor) {
this(Opcodes.ASM7, owner, access, name, descriptor, methodVisitor);
this(Opcodes.ASM8, owner, access, name, descriptor, methodVisitor);
if (getClass() != AnalyzerAdapter.class) {
throw new IllegalStateException();
}
......@@ -191,14 +193,19 @@ public class AnalyzerAdapter extends MethodVisitor {
public void visitFrame(
final int type,
final int numLocal,
final Object[] local,
final Array<Object> local,
final int numStack,
final Object[] stack) {
final Array<Object> stack) {
if (api < Opcodes.ASM8 && stack.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitFrame(type, numLocal, local, numStack, stack);
return;
}
if (type != Opcodes.F_NEW) { // Uncompressed frame.
throw new IllegalArgumentException(
"AnalyzerAdapter only accepts expanded frames (see ClassReader.EXPAND_FRAMES)");
}
super.visitFrame(type, numLocal, local, numStack, stack);
if (this.locals != null) {
......@@ -215,9 +222,9 @@ public class AnalyzerAdapter extends MethodVisitor {
}
private static void visitFrameTypes(
final int numTypes, final Object[] frameTypes, final List<Object> result) {
final int numTypes, final Array<Object> frameTypes, final List<Object> result) {
for (int i = 0; i < numTypes; ++i) {
Object frameType = frameTypes[i];
Object frameType = frameTypes.get(i);
result.add(frameType);
if (frameType == Opcodes.LONG || frameType == Opcodes.DOUBLE) {
result.add(Opcodes.TOP);
......@@ -329,7 +336,14 @@ public class AnalyzerAdapter extends MethodVisitor {
final String name,
final String descriptor,
final Handle bootstrapMethodHandle,
final Object... bootstrapMethodArguments) {
final Array<Object> bootstrapMethodArguments) {
if (api < Opcodes.ASM8 && bootstrapMethodArguments.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitInvokeDynamicInsn(
name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
return;
}
super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
if (this.locals == null) {
labels = null;
......@@ -406,7 +420,13 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitTableSwitchInsn(
final int min, final int max, final Label dflt, final Label... labels) {
final int min, final int max, final Label dflt, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitTableSwitchInsn(min, max, dflt, labels);
return;
}
super.visitTableSwitchInsn(min, max, dflt, labels);
execute(Opcodes.TABLESWITCH, 0, null);
this.locals = null;
......@@ -414,7 +434,14 @@ public class AnalyzerAdapter extends MethodVisitor {
}
@Override
public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
public void visitLookupSwitchInsn(
final Label dflt, final IntArray keys, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitLookupSwitchInsn(dflt, keys, labels);
return;
}
super.visitLookupSwitchInsn(dflt, keys, labels);
execute(Opcodes.LOOKUPSWITCH, 0, null);
this.locals = null;
......
......@@ -49,7 +49,7 @@ public class AnnotationRemapper extends AnnotationVisitor {
* @param remapper the remapper to use to remap the types in the visited annotation.
*/
public AnnotationRemapper(final AnnotationVisitor annotationVisitor, final Remapper remapper) {
this(Opcodes.ASM7, annotationVisitor, remapper);
this(Opcodes.ASM8, annotationVisitor, remapper);
}
/**
......
......@@ -30,6 +30,7 @@ package org.objectweb.asm.commons;
import java.util.List;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Array;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
......@@ -59,7 +60,7 @@ public class ClassRemapper extends ClassVisitor {
* @param remapper the remapper to use to remap the types in the visited class.
*/
public ClassRemapper(final ClassVisitor classVisitor, final Remapper remapper) {
this(Opcodes.ASM7, classVisitor, remapper);
this(Opcodes.ASM8, classVisitor, remapper);
}
/**
......@@ -83,7 +84,13 @@ public class ClassRemapper extends ClassVisitor {
final String name,
final String signature,
final String superName,
final String[] interfaces) {
final Array<String> interfaces) {
if (api < Opcodes.ASM8 && interfaces.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visit(version, access, name, signature, superName, interfaces);
return;
}
this.className = name;
super.visit(
version,
......@@ -91,7 +98,7 @@ public class ClassRemapper extends ClassVisitor {
remapper.mapType(name),
remapper.mapSignature(signature, false),
remapper.mapType(superName),
interfaces == null ? null : remapper.mapTypes(interfaces));
remapper.mapTypes(interfaces));
}
@Override
......@@ -150,7 +157,12 @@ public class ClassRemapper extends ClassVisitor {
final String name,
final String descriptor,
final String signature,
final String[] exceptions) {
final Array<String> exceptions) {
if (api < Opcodes.ASM8 && exceptions.isPublic()) {
// Redirect the call to the deprecated version of this method.
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
String remappedDescriptor = remapper.mapMethodDesc(descriptor);
MethodVisitor methodVisitor =
super.visitMethod(
......@@ -158,7 +170,7 @@ public class ClassRemapper extends ClassVisitor {
remapper.mapMethodName(className, name, descriptor),
remappedDescriptor,
remapper.mapSignature(signature, false),
exceptions == null ? null : remapper.mapTypes(exceptions));
remapper.mapTypes(exceptions));
return methodVisitor == null ? null : createMethodRemapper(methodVisitor);
}
......
......@@ -27,8 +27,10 @@
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import org.objectweb.asm.Array;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
import org.objectweb.asm.IntArray;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
......@@ -47,7 +49,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
private int maxSize;
public CodeSizeEvaluator(final MethodVisitor methodVisitor) {
this(Opcodes.ASM7, methodVisitor);
this(Opcodes.ASM8, methodVisitor);
}
protected CodeSizeEvaluator(final int api, final MethodVisitor methodVisitor) {
......@@ -140,7 +142,14 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
final String name,
final String descriptor,
final Handle bootstrapMethodHandle,
final Object... bootstrapMethodArguments) {
final Array<Object> bootstrapMethodArguments) {
if (api < Opcodes.ASM8 && bootstrapMethodArguments.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitInvokeDynamicInsn(
name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
return;
}
minSize += 5;
maxSize += 5;
super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
......@@ -185,16 +194,29 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
@Override
public void visitTableSwitchInsn(
final int min, final int max, final Label dflt, final Label... labels) {
minSize += 13 + labels.length * 4;
maxSize += 16 + labels.length * 4;
final int min, final int max, final Label dflt, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitTableSwitchInsn(min, max, dflt, labels);
return;
}
minSize += 13 + labels.size() * 4;
maxSize += 16 + labels.size() * 4;
super.visitTableSwitchInsn(min, max, dflt, labels);
}
@Override
public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
minSize += 9 + keys.length * 8;
maxSize += 12 + keys.length * 8;
public void visitLookupSwitchInsn(
final Label dflt, final IntArray keys, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitLookupSwitchInsn(dflt, keys, labels);
return;
}
minSize += 9 + keys.size() * 8;
maxSize += 12 + keys.size() * 8;
super.visitLookupSwitchInsn(dflt, keys, labels);
}
......
......@@ -51,7 +51,7 @@ public class FieldRemapper extends FieldVisitor {
* @param remapper the remapper to use to remap the types in the visited field.
*/
public FieldRemapper(final FieldVisitor fieldVisitor, final Remapper remapper) {
this(Opcodes.ASM7, fieldVisitor, remapper);
this(Opcodes.ASM8, fieldVisitor, remapper);
}
/**
......
......@@ -28,11 +28,12 @@
package org.objectweb.asm.commons;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.objectweb.asm.Array;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
import org.objectweb.asm.IntArray;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
......@@ -201,7 +202,7 @@ public class GeneratorAdapter extends LocalVariablesSorter {
final int access,
final String name,
final String descriptor) {
this(Opcodes.ASM7, methodVisitor, access, name, descriptor);
this(Opcodes.ASM8, methodVisitor, access, name, descriptor);
if (getClass() != GeneratorAdapter.class) {
throw new IllegalStateException();
}
......@@ -269,7 +270,7 @@ public class GeneratorAdapter extends LocalVariablesSorter {
method.getName(),
method.getDescriptor(),
signature,
exceptions == null ? null : getInternalNames(exceptions)));
exceptions == null ? Opcodes.NO_EXCEPTIONS : getInternalNames(exceptions)));
}
/**
......@@ -278,12 +279,12 @@ public class GeneratorAdapter extends LocalVariablesSorter {
* @param types a set of types.
* @return the internal names of the given types.
*/
private static String[] getInternalNames(final Type[] types) {
String[] names = new String[types.length];
for (int i = 0; i < names.length; ++i) {
names[i] = types[i].getInternalName();
private static Array<String> getInternalNames(final Type[] types) {
Array.Builder<String> names = Array.newStrings(types.length);
for (int i = 0; i < types.length; ++i) {
names.set(i, types[i].getInternalName());
}
return names;
return names.build();
}
public int getAccess() {
......@@ -1073,27 +1074,31 @@ public class GeneratorAdapter extends LocalVariablesSorter {
int min = keys[0];
int max = keys[numKeys - 1];
int range = max - min + 1;
Label[] labels = new Label[range];
Arrays.fill(labels, defaultLabel);
Array.Builder<Label> labelsBuilder = Array.newLabels(range);
for (int i = 0; i < range; ++i) {
labelsBuilder.set(i, defaultLabel);
}
for (int i = 0; i < numKeys; ++i) {
labels[keys[i] - min] = newLabel();
labelsBuilder.set(keys[i] - min, newLabel());
}
Array<Label> labels = labelsBuilder.build();
mv.visitTableSwitchInsn(min, max, defaultLabel, labels);
for (int i = 0; i < range; ++i) {
Label label = labels[i];
Label label = labels.get(i);
if (label != defaultLabel) {
mark(label);
generator.generateCase(i + min, endLabel);
}
}
} else {
Label[] labels = new Label[numKeys];
Array.Builder<Label> labelsBuilder = Array.newLabels(numKeys);
for (int i = 0; i < numKeys; ++i) {
labels[i] = newLabel();
labelsBuilder.set(i, newLabel());
}
mv.visitLookupSwitchInsn(defaultLabel, keys, labels);
Array<Label> labels = labelsBuilder.build();
mv.visitLookupSwitchInsn(defaultLabel, IntArray.of(keys), labels);
for (int i = 0; i < numKeys; ++i) {
mark(labels[i]);
mark(labels.get(i));
generator.generateCase(keys[i], endLabel);
}
}
......@@ -1243,7 +1248,8 @@ public class GeneratorAdapter extends LocalVariablesSorter {
final String descriptor,
final Handle bootstrapMethodHandle,
final Object... bootstrapMethodArguments) {
mv.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
mv.visitInvokeDynamicInsn(
name, descriptor, bootstrapMethodHandle, Array.of(bootstrapMethodArguments));
}
// -----------------------------------------------------------------------------------------------
......
......@@ -28,8 +28,10 @@
package org.objectweb.asm.commons;
import org.objectweb.asm.Array;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
import org.objectweb.asm.IntArray;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
......@@ -53,7 +55,7 @@ public class InstructionAdapter extends MethodVisitor {
* @throws IllegalStateException If a subclass calls this constructor.
*/
public InstructionAdapter(final MethodVisitor methodVisitor) {
this(Opcodes.ASM7, methodVisitor);
this(Opcodes.ASM8, methodVisitor);
if (getClass() != InstructionAdapter.class) {
throw new IllegalStateException();
}
......@@ -543,8 +545,15 @@ public class InstructionAdapter extends MethodVisitor {
final String name,
final String descriptor,
final Handle bootstrapMethodHandle,
final Object... bootstrapMethodArguments) {
invokedynamic(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
final Array<Object> bootstrapMethodArguments) {
if (api < Opcodes.ASM8 && bootstrapMethodArguments.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitInvokeDynamicInsn(
name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
return;
}
invokedynamic(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments.toArray());
}
@Override
......@@ -621,7 +630,7 @@ public class InstructionAdapter extends MethodVisitor {
|| (value instanceof Type && ((Type) value).getSort() == Type.METHOD))) {
throw new UnsupportedOperationException("This feature requires ASM5");
}
if (api != Opcodes.ASM7 && value instanceof ConstantDynamic) {
if (api < Opcodes.ASM7 && value instanceof ConstantDynamic) {
throw new UnsupportedOperationException("This feature requires ASM7");
}
if (value instanceof Integer) {
......@@ -660,13 +669,26 @@ public class InstructionAdapter extends MethodVisitor {
@Override
public void visitTableSwitchInsn(
final int min, final int max, final Label dflt, final Label... labels) {
tableswitch(min, max, dflt, labels);
final int min, final int max, final Label dflt, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitTableSwitchInsn(min, max, dflt, labels);
return;
}
tableswitch(min, max, dflt, labels.toArray());
}
@Override
public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
lookupswitch(dflt, keys, labels);
public void visitLookupSwitchInsn(
final Label dflt, final IntArray keys, final Array<Label> labels) {
if (api < Opcodes.ASM8 && labels.isPublic()) {
// Redirect the call to the deprecated version of this method.
super.visitLookupSwitchInsn(dflt, keys, labels);
return;
}
lookupswitch(dflt, keys.toArray(), labels.toArray());
}
@Override
......@@ -1033,11 +1055,11 @@ public class InstructionAdapter extends MethodVisitor {
}
public void tableswitch(final int min, final int max, final Label dflt, final Label... labels) {
mv.visitTableSwitchInsn(min, max, dflt, labels);
mv.visitTableSwitchInsn(min, max, dflt, Array.of(labels));
}
public void lookupswitch(final Label dflt, final int[] keys, final Label[] labels) {
mv.visitLookupSwitchInsn(dflt, keys, labels);
mv.visitLookupSwitchInsn(dflt, IntArray.of(keys), Array.of(labels));
}
public void areturn(final Type type) {
......@@ -1074,7 +1096,7 @@ public class InstructionAdapter extends MethodVisitor {
invokevirtual(owner, name, descriptor, false);
return;
}
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, descriptor);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, descriptor, false);
}
/**
......@@ -1202,7 +1224,8 @@ public class InstructionAdapter extends MethodVisitor {
final String descriptor,
final Handle bootstrapMethodHandle,
final Object[] bootstrapMethodArguments) {
mv.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
mv.visitInvokeDynamicInsn(
name, descriptor, bootstrapMethodHandle, Array.of(bootstrapMethodArguments));
}
public void anew(final Type type) {
......
......@@ -98,7 +98,7 @@ public class JSRInlinerAdapter extends MethodNode implements Opcodes {
final String descriptor,
final String signature,
final String[] exceptions) {
this(Opcodes.ASM7, methodVisitor, access, name, descriptor, signature, exceptions);
this(Opcodes.ASM8, methodVisitor, access, name, descriptor, signature, exceptions);
if (getClass() != JSRInlinerAdapter.class) {
throw new IllegalStateException();
}
......