Merge remote-tracking branch 'remotes/upstream/master' into fabric-0.8

# Conflicts:
#	src/main/java/org/spongepowered/asm/util/asm/ASM.java
This commit is contained in:
Ridan Vandenbergh committed 2019-12-17 00:02:06 +01:00
commit 910d479864
23 files changed
+428 -96

No files matched your search

@@ -38,6 +38,29 @@ import org.spongepowered.asm.launch.platform.container.IContainerHandle;
*/
public interface IMixinPlatformAgent {
/**
* Result type returned from {@link IMixinPlatformAgent#accept}
*/
public enum AcceptResult {
/**
* The container was accepted
*/
ACCEPTED,
/**
* The container was rejected
*/
REJECTED,
/**
* The agent encountered an error and no further containers should be
* offered
*/
INVALID;
}
/**
* Accept and bind to a container handle. This method is called for agents
* hosted by {@link MixinContainer} and the agent should react accordingly.
@@ -47,11 +70,10 @@ public interface IMixinPlatformAgent {
*
* @param manager platform manager instance
* @param handle handle to container
* @return true if this agent can accept a container of the supplied type,
* false if the agent cannot handle the container (in which case it
* will be discarded)
* @return AcceptResult representing this agent's acceptance of the supplied
* container
*/
public abstract boolean accept(MixinPlatformManager manager, IContainerHandle handle);
public abstract AcceptResult accept(MixinPlatformManager manager, IContainerHandle handle);
/**
* Get the phase provider for this agent
@@ -28,6 +28,7 @@ import java.util.Collection;
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.IConsumer;
/**
@@ -43,7 +44,10 @@ public interface IMixinPlatformServiceAgent extends IMixinPlatformAgent {
public abstract void init();
/**
* Attempt to determine the side name from the current environment
* Attempt to determine the side name from the current environment. Return
* <tt>null</tt> if the side name cannot be determined by this agent. Return
* side name or {@link Constants#SIDE_UNKNOWN} if the agent is able to
* determine the side.
*/
public abstract String getSideName();
@@ -26,11 +26,13 @@ package org.spongepowered.asm.launch.platform;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.launch.GlobalProperties;
import org.spongepowered.asm.launch.platform.IMixinPlatformAgent.AcceptResult;
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
import org.spongepowered.asm.service.MixinService;
@@ -59,22 +61,25 @@ public class MixinContainer {
public MixinContainer(MixinPlatformManager manager, IContainerHandle handle) {
this.handle = handle;
for (String agentClass : MixinContainer.agentClasses) {
for (Iterator<String> iter = MixinContainer.agentClasses.iterator(); iter.hasNext();) {
String agentClass = iter.next();
try {
@SuppressWarnings("unchecked")
Class<IMixinPlatformAgent> clazz = (Class<IMixinPlatformAgent>)Class.forName(agentClass);
String simpleName = clazz.getSimpleName();
String acceptAction = "rejected";
MixinContainer.logger.debug("Instancing new {} for {}", simpleName, this.handle);
IMixinPlatformAgent agent = clazz.newInstance();
if (agent.accept(manager, this.handle)) {
AcceptResult acceptAction = agent.accept(manager, this.handle);
if (acceptAction == AcceptResult.ACCEPTED) {
this.agents.add(agent);
acceptAction = "accepted";
} else if (acceptAction == AcceptResult.INVALID) {
iter.remove();
continue;
}
MixinContainer.logger.debug("{} {} container {}", simpleName, acceptAction, this.handle);
MixinContainer.logger.debug("{} {} container {}", simpleName, acceptAction.name().toLowerCase(), this.handle);
} catch (InstantiationException ex) {
Throwable cause = ex.getCause();
if (cause instanceof RuntimeException) {
@@ -56,10 +56,10 @@ public abstract class MixinPlatformAgentAbstract implements IMixinPlatformAgent
}
@Override
public boolean accept(MixinPlatformManager manager, IContainerHandle handle) {
public AcceptResult accept(MixinPlatformManager manager, IContainerHandle handle) {
this.manager = manager;
this.handle = handle;
return true;
return AcceptResult.ACCEPTED;
}
@Override
@@ -33,6 +33,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.launch.GlobalProperties;
@@ -1041,7 +1042,14 @@ public final class MixinEnvironment implements ITokenProvider {
return "Unknown";
}
}
/**
* Get logging level info/debug based on verbose setting
*/
private Level getVerboseLoggingLevel() {
return this.getOption(Option.DEBUG_VERBOSE) ? Level.INFO : Level.DEBUG;
}
/**
* Get the phase for this environment
*
@@ -1119,7 +1127,7 @@ public final class MixinEnvironment implements ITokenProvider {
if (provider != null && !this.tokenProviderClasses.contains(provider.getClass().getName())) {
String providerName = provider.getClass().getName();
TokenProviderWrapper wrapper = new TokenProviderWrapper(provider, this);
MixinEnvironment.logger.info("Adding new token provider {} to {}", providerName, this);
MixinEnvironment.logger.log(this.getVerboseLoggingLevel(), "Adding new token provider {} to {}", providerName, this);
this.tokenProviders.add(wrapper);
this.tokenProviderClasses.add(providerName);
Collections.sort(this.tokenProviders);
@@ -35,6 +35,7 @@ import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
@@ -121,16 +122,12 @@ public class ModifyVariableInjector extends Injector {
protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints) {
super.sanityCheck(target, injectionPoints);
if (target.isStatic != this.isStatic) {
throw new InvalidInjectionException(this.info, "'static' of variable modifier method does not match target in " + this);
}
int ordinal = this.discriminator.getOrdinal();
if (ordinal < -1) {
throw new InvalidInjectionException(this.info, "Invalid ordinal " + ordinal + " specified in " + this);
}
if (this.discriminator.getIndex() == 0 && !this.isStatic) {
if (this.discriminator.getIndex() == 0 && !target.isStatic) {
throw new InvalidInjectionException(this.info, "Invalid index 0 specified in non-static variable modifier " + this);
}
}
@@ -149,6 +146,8 @@ public class ModifyVariableInjector extends Injector {
if (this.discriminator.printLVT()) {
this.printLocals(target, context);
}
this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
InjectorData handler = new InjectorData(target, "handler", false);
this.validateParams(handler, this.returnType, this.returnType);
@@ -72,10 +72,27 @@ import com.google.common.collect.ImmutableSet;
*/
public final class ClassInfo {
/**
* Include <tt>private</tt> members when running a member search
*/
public static final int INCLUDE_PRIVATE = Opcodes.ACC_PRIVATE;
/**
* Include <tt>static</tt> members when running a member search
*/
public static final int INCLUDE_STATIC = Opcodes.ACC_STATIC;
/**
* Include <tt>private</tt> <b>and</b> <tt>static</tt> members when running
* a member search
*/
public static final int INCLUDE_ALL = ClassInfo.INCLUDE_PRIVATE | ClassInfo.INCLUDE_STATIC;
/**
* Include instance and class initialisers when running a method search
*/
public static final int INCLUDE_INITIALISERS = 0x40000;
/**
* Search type for the findInHierarchy methods, replaces a boolean flag
* which made calling code difficult to read
@@ -682,9 +699,14 @@ public final class ClassInfo {
* Interfaces
*/
private final Set<String> interfaces;
/**
* Constructors and initialisers in this class
*/
private final Set<Method> initialisers;
/**
* Public and protected methods (instance) methods in this class
* Methods in this class
*/
private final Set<Method> methods;
@@ -754,6 +776,9 @@ public final class ClassInfo {
this.superName = null;
this.outerName = null;
this.isProbablyStatic = true;
this.initialisers = ImmutableSet.<Method>of(
new Method("<init>", "()V")
);
this.methods = ImmutableSet.<Method>of(
new Method("getClass", "()Ljava/lang/Class;"),
new Method("hashCode", "()I"),
@@ -787,6 +812,7 @@ public final class ClassInfo {
try {
this.name = classNode.name;
this.superName = classNode.superName != null ? classNode.superName : ClassInfo.JAVA_LANG_OBJECT;
this.initialisers = new HashSet<Method>();
this.methods = new HashSet<Method>();
this.fields = new HashSet<Field>();
this.isInterface = ((classNode.access & Opcodes.ACC_INTERFACE) != 0);
@@ -839,7 +865,9 @@ public final class ClassInfo {
}
private void addMethod(MethodNode method, boolean injected) {
if (!method.name.startsWith("<")) {
if (method.name.startsWith("<")) {
this.initialisers.add(new Method(method, injected));
} else {
this.methods.add(new Method(method, injected));
}
}
@@ -1740,8 +1768,8 @@ public final class ClassInfo {
* @param memberType Type of member list to search
* @return the field object or null if the field could not be resolved
*/
@SuppressWarnings("unchecked")
private <M extends Member> M findMember(String name, String desc, int flags, Type memberType) {
@SuppressWarnings("unchecked")
Set<M> members = (Set<M>)(memberType == Type.METHOD ? this.methods : this.fields);
for (M member : members) {
@@ -1749,6 +1777,14 @@ public final class ClassInfo {
return member;
}
}
if (memberType == Type.METHOD && (flags & ClassInfo.INCLUDE_INITIALISERS) != 0) {
for (Method ctor : this.initialisers) {
if (ctor.equals(name, desc) && ctor.matchesFlags(flags)) {
return (M)ctor;
}
}
}
return null;
}
@@ -37,6 +37,7 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Handle;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
@@ -44,6 +45,7 @@ import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.InnerClassNode;
import org.objectweb.asm.tree.InvokeDynamicInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Mixin;
@@ -91,6 +93,17 @@ class MixinInfo implements Comparable<MixinInfo>, IMixinInfo {
public MixinMethodNode(int access, String name, String desc, String signature, String[] exceptions) {
super(access, name, desc, signature, exceptions, MixinInfo.this);
}
@Override
public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
// Create a shallow copy of the bootstrap method args because the
// base implementation just passes the array by reference. This
// causes any changes applied to the cloned classnode to leak into
// the "master" ClassNode!
Object[] bsmArgs = new Object[bootstrapMethodArguments.length];
System.arraycopy(bootstrapMethodArguments, 0, bsmArgs, 0, bootstrapMethodArguments.length);
this.instructions.add(new InvokeDynamicInsnNode(name, descriptor, bootstrapMethodHandle, bsmArgs));
}
public boolean isInjector() {
return (this.getInjectorAnnotation() != null || this.isSurrogate());
@@ -500,17 +500,18 @@ class MixinPreProcessorStandard {
return false;
}
if (method.isSynthetic()) {
boolean synthetic = method.isSynthetic();
if (synthetic) {
context.transformDescriptor(mixinMethod);
method.remapTo(mixinMethod.desc);
}
MethodNode target = context.findMethod(mixinMethod, null);
if (target == null) {
if (target == null && !synthetic) {
return false;
}
String type = method.isSynthetic() ? "synthetic" : "@Unique";
String type = synthetic ? "synthetic" : "@Unique";
if (Bytecode.getVisibility(mixinMethod).ordinal() < Visibility.PUBLIC.ordinal()) {
if (method.isConformed()) {
@@ -524,6 +525,10 @@ class MixinPreProcessorStandard {
return false;
}
if (target == null) {
return false;
}
if (this.strictUnique) {
throw new InvalidMixinException(this.mixin, String.format("Method conflict, %s method %s in %s cannot overwrite %s%s in %s",
type, mixinMethod.name, this.mixin, target.name, target.desc, context.getTarget()));
@@ -371,13 +371,14 @@ final class TargetClassContext extends ClassContext implements ITargetClassConte
* Process {@link Debug} annotations on the class after application
*/
void processDebugTasks() {
AnnotationNode classDebugAnnotation = Annotations.getVisible(this.classNode, Debug.class);
this.forceExport = classDebugAnnotation != null && Boolean.TRUE.equals(Annotations.<Boolean>getValue(classDebugAnnotation, "export"));
if (!this.env.getOption(Option.DEBUG_VERBOSE)) {
return;
}
AnnotationNode classDebugAnnotation = Annotations.getVisible(this.classNode, Debug.class);
if (classDebugAnnotation != null) {
this.forceExport = Boolean.TRUE.equals(Annotations.<Boolean>getValue(classDebugAnnotation, "export"));
if (Boolean.TRUE.equals(Annotations.<Boolean>getValue(classDebugAnnotation, "print"))) {
Bytecode.textify(this.classNode, System.err);
}
@@ -429,10 +429,14 @@ public final class Bytecode {
out += String.format("[%s] %d", Bytecode.getOpcodeName(node), ((VarInsnNode)node).var);
} else if (node instanceof MethodInsnNode) {
MethodInsnNode mth = (MethodInsnNode)node;
out += String.format("[%s] %s %s %s", Bytecode.getOpcodeName(node), mth.owner, mth.name, mth.desc);
out += String.format("[%s] %s::%s%s", Bytecode.getOpcodeName(node), mth.owner, mth.name, mth.desc);
} else if (node instanceof FieldInsnNode) {
FieldInsnNode fld = (FieldInsnNode)node;
out += String.format("[%s] %s %s %s", Bytecode.getOpcodeName(node), fld.owner, fld.name, fld.desc);
out += String.format("[%s] %s::%s:%s", Bytecode.getOpcodeName(node), fld.owner, fld.name, fld.desc);
} else if (node instanceof InvokeDynamicInsnNode) {
InvokeDynamicInsnNode idc = (InvokeDynamicInsnNode)node;
out += String.format("[%s] %s%s { %s %s::%s%s }", Bytecode.getOpcodeName(node), idc.name, idc.desc,
Bytecode.getOpcodeName(idc.bsm.getTag(), "H_GETFIELD", 1), idc.bsm.getOwner(), idc.bsm.getName(), idc.bsm.getDesc());
} else if (node instanceof LineNumberNode) {
LineNumberNode ln = (LineNumberNode)node;
out += String.format("LINE=[%d] LABEL=[%s]", ln.line, ln.start.getLabel());
@@ -142,7 +142,7 @@ public final class Locals {
if (classInfo == null) {
throw new LVTGeneratorError("Could not load class metadata for " + classNode.name + " generating LVT for " + method.name);
}
Method methodInfo = classInfo.findMethod(method);
Method methodInfo = classInfo.findMethod(method, method.access | ClassInfo.INCLUDE_INITIALISERS);
if (methodInfo == null) {
throw new LVTGeneratorError("Could not locate method metadata for " + method.name + " generating LVT in " + classNode.name);
}
@@ -24,8 +24,9 @@
*/
package org.spongepowered.asm.util.asm;
import java.lang.reflect.Field;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
/**
* Utility methods for determining ASM version and other version-specific
@@ -33,57 +34,71 @@ import org.objectweb.asm.tree.ClassNode;
*/
public final class ASM {
@SuppressWarnings("deprecation")
private static final int[] EXPERIMENTAL_VERSIONS = { };
private static final int[] SUPPORTED_VERSIONS = { Opcodes.ASM6, Opcodes.ASM5, Opcodes.ASM7 };
private static int majorVersion = 5;
private static int minorVersion = 0;
private static String maxVersion = "FALLBACK";
/**
* The detected ASM API Version
*/
public static final int API_VERSION = ASM.detectVersion();
private static boolean experimental;
private ASM() {
}
/**
* Get the major API version
*/
public static int getApiVersionMajor() {
return ASM.majorVersion;
}
/**
* Get the minor API version
*/
public static int getApiVersionMinor() {
return ASM.minorVersion;
}
/**
* Get the ASM API version as a string (mostly for debugging and the banner)
*
* @return ASM API version as string
*/
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);
return String.format("ASM %d.%d (%s)", ASM.majorVersion, ASM.minorVersion, ASM.maxVersion);
}
private static int detectVersion() {
int expVersion = ASM.detectVersion(ASM.EXPERIMENTAL_VERSIONS);
if (expVersion > 0) {
ASM.experimental = true;
return expVersion;
}
int apiVersion = Opcodes.ASM4;
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) {
for (Field field : Opcodes.class.getDeclaredFields()) {
if (field.getType() != Integer.TYPE || !field.getName().startsWith("ASM")) {
continue;
}
try {
new ClassNode(version).hashCode();
return version;
} catch (IllegalArgumentException ex) {
// expected, this version is not supported
int version = field.getInt(null);
// int patch = version & 0xFF;
int minor = (version >> 8) & 0xFF;
int major = (version >> 16) & 0xFF;
boolean experimental = ((version >> 24) & 0xFF) != 0;
if (major >= ASM.majorVersion) {
ASM.maxVersion = field.getName();
if (!experimental) {
apiVersion = version;
ASM.majorVersion = major;
ASM.minorVersion = minor;
}
}
} catch (ReflectiveOperationException ex) {
throw new Error(ex);
}
}
return 0;
return apiVersion;
}
}