Add an ASM detector and support backwards compatibility with ASM 5.x

This commit is contained in:
Mumfrey committed 2019-10-11 19:26:04 +01:00
1 parent 22ae7dd1f9
commit 002e5288e8
16 files changed
+124 -23

No files matched your search

@@ -25,11 +25,8 @@
package org.spongepowered.asm.launch.platform;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
@@ -54,6 +54,7 @@ import org.spongepowered.asm.util.IConsumer;
import org.spongepowered.asm.util.ITokenProvider;
import org.spongepowered.asm.util.JavaVersion;
import org.spongepowered.asm.util.PrettyPrinter;
import org.spongepowered.asm.util.asm.ASM;
import org.spongepowered.asm.util.perf.Profiler;
import com.google.common.collect.ImmutableList;
@@ -977,7 +978,8 @@ public final class MixinEnvironment implements ITokenProvider {
printer.kv("Code source", codeSource);
printer.kv("Internal Version", version);
printer.kv("Java Versions Supported", CompatibilityLevel.getSupportedVersions());
printer.kv("Current Compatibility Level", MixinEnvironment.getCompatibilityLevel()).hr();
printer.kv("Current Compatibility Level", MixinEnvironment.getCompatibilityLevel());
printer.kv("Detected ASM API Version", ASM.getApiVersionString()).hr();
printer.kv("Service Name", serviceName);
printer.kv("Mixin Service Class", this.service.getClass().getName());
printer.kv("Global Property Service Class", MixinService.getGlobalPropertyService().getClass().getName()).hr();
@@ -31,7 +31,7 @@ import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.asm.ASM;
/**
* Base class for accessor generators
@@ -70,7 +70,7 @@ public abstract class AccessorGenerator {
*/
protected final MethodNode createMethod(int maxLocals, int maxStack) {
MethodNode method = this.info.getMethod();
MethodNode accessor = new MethodNode(Bytecode.ASM_API_VERSION, (method.access & ~Opcodes.ACC_ABSTRACT) | Opcodes.ACC_SYNTHETIC, method.name,
MethodNode accessor = new MethodNode(ASM.API_VERSION, (method.access & ~Opcodes.ACC_ABSTRACT) | Opcodes.ACC_SYNTHETIC, method.name,
method.desc, null, null);
accessor.visibleAnnotations = new ArrayList<AnnotationNode>();
accessor.visibleAnnotations.add(this.info.getAnnotation());
@@ -65,6 +65,7 @@ import org.spongepowered.asm.mixin.transformer.meta.MixinMerged;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.asm.ASM;
import org.spongepowered.asm.util.asm.ElementNode;
import com.google.common.base.Strings;
@@ -466,7 +467,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
* @return new method
*/
public MethodNode addMethod(int access, String name, String desc) {
MethodNode method = new MethodNode(Bytecode.ASM_API_VERSION, access | Opcodes.ACC_SYNTHETIC, name, desc, null, null);
MethodNode method = new MethodNode(ASM.API_VERSION, access | Opcodes.ACC_SYNTHETIC, name, desc, null, null);
this.injectedMethods.add(method);
return method;
}
@@ -42,8 +42,8 @@ import org.spongepowered.asm.mixin.transformer.ext.IClassGenerator;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.service.ISyntheticClassInfo;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.IConsumer;
import org.spongepowered.asm.util.asm.ASM;
/**
* Class generator which creates unique copies of inner classes within mixins
@@ -200,7 +200,7 @@ final class InnerClassGenerator implements IClassGenerator {
private final InnerClassInfo info;
public InnerClassAdapter(ClassVisitor cv, InnerClassInfo info) {
super(Bytecode.ASM_API_VERSION, cv, info);
super(ASM.API_VERSION, cv, info);
this.info = info;
}
@@ -67,6 +67,7 @@ import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.asm.ASM;
import org.spongepowered.asm.util.perf.Profiler;
import org.spongepowered.asm.util.perf.Profiler.Section;
@@ -87,7 +88,7 @@ class MixinInfo implements Comparable<MixinInfo>, IMixinInfo {
private final String originalName;
public MixinMethodNode(int access, String name, String desc, String signature, String[] exceptions) {
super(Bytecode.ASM_API_VERSION, access, name, desc, signature, exceptions);
super(ASM.API_VERSION, access, name, desc, signature, exceptions);
this.originalName = name;
}
@@ -134,7 +135,7 @@ class MixinInfo implements Comparable<MixinInfo>, IMixinInfo {
public final List<MixinMethodNode> mixinMethods;
MixinClassNode(MixinInfo mixin) {
this(Bytecode.ASM_API_VERSION);
this(ASM.API_VERSION);
}
@SuppressWarnings("unchecked")
@@ -32,8 +32,8 @@ import org.spongepowered.asm.mixin.throwables.MixinException;
import org.spongepowered.asm.mixin.transformer.ext.Extensions;
import org.spongepowered.asm.mixin.transformer.ext.IExtensionRegistry;
import org.spongepowered.asm.transformers.TreeTransformer;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.asm.ASM;
/**
* Transformer which manages the mixin configuration and application process
@@ -203,7 +203,7 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
* You need to ask yourself why you're reading this comment
*/
private static ClassNode createEmptyClass(String name) {
ClassNode classNode = new ClassNode(Bytecode.ASM_API_VERSION);
ClassNode classNode = new ClassNode(ASM.API_VERSION);
classNode.name = name.replace('.', '/');
classNode.version = MixinEnvironment.getCompatibilityLevel().classVersion();
classNode.superName = Constants.OBJECT;
@@ -43,6 +43,7 @@ import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*;
import org.objectweb.asm.util.CheckClassAdapter;
import org.objectweb.asm.util.TraceClassVisitor;
import org.spongepowered.asm.util.asm.ASM;
import org.spongepowered.asm.util.throwables.SyntheticBridgeException;
import org.spongepowered.asm.util.throwables.SyntheticBridgeException.Problem;
@@ -143,8 +144,6 @@ public final class Bytecode {
return this.isSuper ? "super" : "this";
}
}
public static final int ASM_API_VERSION = Opcodes.ASM6;
/**
* Integer constant opcodes
@@ -1162,7 +1161,6 @@ public final class Bytecode {
dest.sourceDebug = source.sourceDebug;
dest.sourceFile = source.sourceFile;
dest.module = source.module;
dest.outerClass = source.outerClass;
dest.outerMethod = source.outerMethod;
dest.outerMethodDesc = source.outerMethodDesc;
@@ -1176,7 +1174,10 @@ public final class Bytecode {
Bytecode.<InnerClassNode>clear(dest.innerClasses);
Bytecode.<FieldNode>clear(dest.fields);
Bytecode.<MethodNode>clear(dest.methods);
if (ASM.API_VERSION >= Opcodes.ASM6) {
dest.module = source.module;
}
// TODO Java 10
// dest.nestHostClassExperimental = source.nestHostClassExperimental;
@@ -39,6 +39,7 @@ import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.signature.SignatureVisitor;
import org.objectweb.asm.signature.SignatureWriter;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.util.asm.ASM;
/**
* Represents an object-oriented view of a generic class signature. We use ASM's
@@ -660,7 +661,7 @@ public class ClassSignature {
abstract class SignatureElement extends SignatureVisitor {
public SignatureElement() {
super(Bytecode.ASM_API_VERSION);
super(ASM.API_VERSION);
}
}
@@ -897,7 +898,7 @@ public class ClassSignature {
private FormalParamElement param;
SignatureParser() {
super(Bytecode.ASM_API_VERSION);
super(ASM.API_VERSION);
}
@Override
@@ -48,6 +48,7 @@ import org.objectweb.asm.tree.analysis.Frame;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.mixin.transformer.ClassInfo.FrameData;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.util.asm.ASM;
import org.spongepowered.asm.util.asm.MixinVerifier;
import org.spongepowered.asm.util.throwables.LVTGeneratorError;
@@ -388,7 +389,7 @@ public final class Locals {
// Use Analyzer to generate the bytecode frames
Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(
new MixinVerifier(Bytecode.ASM_API_VERSION, Type.getObjectType(classNode.name), objectType, interfaces, false));
new MixinVerifier(ASM.API_VERSION, Type.getObjectType(classNode.name), objectType, interfaces, false));
try {
analyzer.analyze(classNode.name, method);
} catch (AnalyzerException ex) {
@@ -0,0 +1,89 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.util.asm;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
/**
* Utility methods for determining ASM version and other version-specific
* shenanigans
*/
public final class ASM {
@SuppressWarnings("deprecation")
private static final int[] EXPERIMENTAL_VERSIONS = { Opcodes.ASM7_EXPERIMENTAL };
private static final int[] SUPPORTED_VERSIONS = { Opcodes.ASM6, Opcodes.ASM5 };
public static final int API_VERSION = ASM.detectVersion();
private static boolean experimental;
private ASM() {
}
/**
* Get the ASM API version as a string (mostly for debugging and the banner)
*
* @return
*/
public static String getApiVersionString() {
String suffix = "";
if (ASM.experimental) {
int version = ASM.detectVersion(ASM.SUPPORTED_VERSIONS);
suffix = String.format("-EXPERIMENTAL (%d.%d)", ((0xFF0000 & version) >> 16), ((0xFF00 & version) >> 8));
}
return String.format("ASM %d.%d%s", ((0xFF0000 & ASM.API_VERSION) >> 16), ((0xFF00 & ASM.API_VERSION) >> 8), suffix);
}
private static int detectVersion() {
int expVersion = ASM.detectVersion(ASM.EXPERIMENTAL_VERSIONS);
if (expVersion > 0) {
ASM.experimental = true;
return expVersion;
}
ASM.experimental = false;
int version = ASM.detectVersion(ASM.SUPPORTED_VERSIONS);
if (version > 0) {
return version;
}
return Opcodes.ASM5;
}
private static int detectVersion(int[] versions) {
for (int version : versions) {
try {
new ClassNode(version).hashCode();
return version;
} catch (IllegalArgumentException ex) {
// expected, this version is not supported
}
}
return 0;
}
}
@@ -34,7 +34,7 @@ import org.spongepowered.asm.util.Bytecode;
public class MethodVisitorEx extends MethodVisitor {
public MethodVisitorEx(MethodVisitor mv) {
super(Bytecode.ASM_API_VERSION, mv);
super(ASM.API_VERSION, mv);
}
/**