Add Unique, relax target checks, closes #130, fixes #134, bump to 0.5.7

This commit is contained in:
Mumfrey committed 2016-06-14 23:11:30 +01:00
1 parent 76856fb41e
commit 053bc174de
17 files changed
+459 -134

No files matched your search

@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.5.6";
public static final String VERSION = "0.5.7";
/**
* Blackboard key where the subsystem version will be stored to indicate
@@ -233,6 +233,7 @@ public class MixinTweaker implements ITweaker {
*
* @param config config resource name, does not require a leading /
*/
@SuppressWarnings("deprecation")
static void addConfig(String config) {
if (config.endsWith(".json")) {
MixinTweaker.logger.debug("Registering mixin config: {}", config);
@@ -53,4 +53,12 @@ public @interface Interface {
*/
public String prefix();
/**
* If set to <tt>true</tt>, all methods implementing this interface are
* treated as if they were individually decorated with {@link Unique}
*
* @return true to mark all implementing methods as unique
*/
public boolean unique() default false;
}
@@ -276,7 +276,9 @@ public final class MixinEnvironment implements ITokenProvider {
},
/**
* Run the CheckClassAdapter on all classes after mixins are applied
* Run the CheckClassAdapter on all classes after mixins are applied,
* also enables stricter checks on mixins for use at dev-time, promotes
* some warning-level messages to exceptions
*/
DEBUG_VERIFY(Option.DEBUG_ALL, "verify"),
@@ -292,6 +294,28 @@ public final class MixinEnvironment implements ITokenProvider {
*/
DEBUG_INJECTORS(Option.DEBUG_ALL, "countInjections"),
/**
* Enable strict checks
*/
DEBUG_STRICT(Option.DEBUG_ALL, "strict") {
@Override
boolean getBooleanValue() {
return Booleans.parseBoolean(System.getProperty(this.property), false);
}
},
/**
* If false (default), {@link Unique} public methods merely raise a
* warning when encountered and are not merged into the target. If true,
* an exception is thrown instead
*/
DEBUG_UNIQUE(Option.DEBUG_STRICT, "unique"),
/**
* Enable strict checking for mixin targets
*/
DEBUG_TARGETS(Option.DEBUG_STRICT, "targets"),
/**
* Disable the injector handler remapper
*/
@@ -802,6 +826,7 @@ public final class MixinEnvironment implements ITokenProvider {
*/
@Deprecated
public MixinEnvironment addConfiguration(String config) {
MixinEnvironment.logger.warn("MixinEnvironment::addConfiguration is deprecated and will be removed. Use Mixins::addConfiguration instead!");
Mixins.addConfiguration(config, this);
return this;
}
@@ -1199,6 +1224,8 @@ public final class MixinEnvironment implements ITokenProvider {
*/
@Deprecated
public static void setCompatibilityLevel(CompatibilityLevel level) throws IllegalArgumentException {
MixinEnvironment.logger.warn("MixinEnvironment::setCompatibilityLevel is deprecated and will be removed. Set level via config instead!");
if (level != MixinEnvironment.compatibility && level.isAtLeast(MixinEnvironment.compatibility)) {
if (!level.isSupported()) {
throw new IllegalArgumentException("The requested compatibility level " + level + " could not be set. Level is not supported");
@@ -0,0 +1,71 @@
/*
* 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.mixin;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation, when applied to a member method in a mixin, indicates that
* the method <b>should never</b> overwrite a matching method in the target
* class. This indicates that the method differs from the normal "overlay-like"
* behaviour of mixins in general, and should only ever be <em>added</em> to the
* target.
*
* <p>Typical usage of this annotation would be to decorate a utility method in
* a mixin, or mark an interface-implementing method which must not overwrite a
* target if it exists (consider appropriate use of {@link Intrinsic} in these
* situations).</p>
*
* <p>Because of the mixed usage, this annotation has different implications for
* methods with differing visibility:</p>
*
* <dl>
* <dt>public methods</dt>
* <dd>public methods marked with this annotation are <b>discarded</b> if a
* matching target exists. A <tt>warning</tt>-level message is generated.</dd>
* <dt>private and protected methods</dt>
* <dd>non-public methods are <b>renamed</b> if a matching target method is
* found, this allows utility methods to be safely assigned meaningful names
* in code, but renamed if a conflict occurs when a mixin is applied.</dd>
* </dl>
*
* <p><strong>Notes</strong></p>
*
* <ul>
* <li>To mark all methods in a mixin as unique, apply the annotation to the
* mixin itself</li>
* <li>Uniqueness can be defined on a per-interface basis by using an
* {@link Implements} annotation with <tt>unique</tt> set to <tt>true</tt>
* </li>
* </ul>
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Unique {
}
@@ -259,8 +259,8 @@ public class LocalVariableDiscriminator {
}
/**
* If the user specifies no values for {@link #ordinal}, {@link #index} or
* {@link #names} then we are considered to be operating in "implicit mode"
* If the user specifies no values for <tt>ordinal</tt>, <tt>index</tt> or
* <tt>names</tt> then we are considered to be operating in "implicit mode"
* where only a single local variable of the specified type is expected to
* exist.
*
@@ -141,7 +141,7 @@ public abstract class InjectionInfo {
this.mixin = mixin;
this.method = method;
this.annotation = annotation;
this.classNode = mixin.getTargetClass();
this.classNode = mixin.getTargetClassNode();
this.isStatic = ASMHelper.methodIsStatic(method);
this.readAnnotation();
}
@@ -46,8 +46,10 @@ import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Member.Type;
import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode;
import org.spongepowered.asm.util.ASMHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@@ -59,8 +61,8 @@ import com.google.common.collect.ImmutableSet;
*/
public class ClassInfo extends TreeInfo {
public static final int INCLUDE_PRIVATE = 0x0002;
public static final int INCLUDE_STATIC = 0x0008;
public static final int INCLUDE_PRIVATE = Opcodes.ACC_PRIVATE;
public static final int INCLUDE_STATIC = Opcodes.ACC_STATIC;
public static final int INCLUDE_ALL = ClassInfo.INCLUDE_PRIVATE | ClassInfo.INCLUDE_STATIC;
/**
@@ -334,10 +336,13 @@ public class ClassInfo extends TreeInfo {
public class Method extends Member {
private final List<FrameData> frames;
private boolean unique;
public Method(Member member) {
super(member);
this.frames = member instanceof Method ? ((Method)member).frames : null;
this.unique = member instanceof Method ? ((Method)member).unique : false;
}
public Method(MethodNode method) {
@@ -347,21 +352,25 @@ public class ClassInfo extends TreeInfo {
public Method(MethodNode method, boolean injected) {
super(Type.METHOD, method.name, method.desc, method.access, injected);
this.frames = this.gatherFrames(method);
this.unique = ASMHelper.getVisibleAnnotation(method, Unique.class) != null;
}
public Method(String name, String desc) {
super(Type.METHOD, name, desc, Opcodes.ACC_PUBLIC, false);
this.frames = null;
this.unique = false;
}
public Method(String name, String desc, int access) {
super(Type.METHOD, name, desc, access, false);
this.frames = null;
this.unique = false;
}
public Method(String name, String desc, int access, boolean injected) {
super(Type.METHOD, name, desc, access, injected);
this.frames = null;
this.unique = false;
}
private List<FrameData> gatherFrames(MethodNode method) {
@@ -378,6 +387,14 @@ public class ClassInfo extends TreeInfo {
public List<FrameData> getFrames() {
return this.frames;
}
public boolean isUnique() {
return this.unique;
}
public void setUnique(boolean unique) {
this.unique = unique;
}
@Override
public ClassInfo getOwner() {
@@ -31,15 +31,22 @@ import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.AnnotationNode;
import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.util.ASMHelper;
import com.sun.xml.internal.ws.org.objectweb.asm.Opcodes;
/**
* Information about an interface being runtime-patched onto a mixin target
* class, see {@link org.spongepowered.asm.mixin.Implements Implements}
*/
/**
* @author adam
*
*/
public class InterfaceInfo extends TreeInfo {
/**
@@ -58,6 +65,12 @@ public class InterfaceInfo extends TreeInfo {
*/
private final Type iface;
/**
* True if all methods implementing this interface should be treated as
* unique
*/
private final boolean unique;
/**
* Method signatures in the interface, lazy loaded
*/
@@ -70,7 +83,7 @@ public class InterfaceInfo extends TreeInfo {
* @param prefix Method prefix
* @param iface Interface to load
*/
private InterfaceInfo(MixinInfo mixin, String prefix, Type iface) {
private InterfaceInfo(MixinInfo mixin, String prefix, Type iface, boolean unique) {
if (prefix == null || prefix.length() < 2 || !prefix.endsWith("$")) {
throw new InvalidMixinException(mixin, String.format("Prefix %s for iface %s is not valid", prefix, iface.toString()));
}
@@ -78,6 +91,7 @@ public class InterfaceInfo extends TreeInfo {
this.mixin = mixin;
this.prefix = prefix;
this.iface = iface;
this.unique = unique;
}
/**
@@ -123,6 +137,15 @@ public class InterfaceInfo extends TreeInfo {
public Type getIface() {
return this.iface;
}
/**
* Get the internal name of the interface
*
* @return the internal name for the interface
*/
public String getName() {
return this.iface.getClassName();
}
/**
* Get the internal name of the interface
@@ -132,6 +155,15 @@ public class InterfaceInfo extends TreeInfo {
public String getInternalName() {
return this.iface.getInternalName();
}
/**
* Get whether all methods for this interface should be treated as unique
*
* @return true to treat all member methods as unique
*/
public boolean isUnique() {
return this.unique;
}
/**
* Processes a method node in the mixin and renames it if necessary. If the
@@ -148,20 +180,46 @@ public class InterfaceInfo extends TreeInfo {
}
if (!method.name.startsWith(this.prefix)) {
if (this.methods.contains(method.name + method.desc)) {
this.decorateUniqueMethod(method);
}
return false;
}
String realName = method.name.substring(this.prefix.length());
String signature = realName + method.desc;
if (!this.methods.contains(signature)) {
throw new InvalidMixinException(this.mixin, String.format("%s does not exist in target interface %s", realName, this.iface.toString()));
throw new InvalidMixinException(this.mixin, String.format("%s does not exist in target interface %s", realName, this.getName()));
}
if ((method.access & Opcodes.ACC_PUBLIC) == 0) {
throw new InvalidMixinException(this.mixin, String.format("%s cannot implement %s because it is not visible", realName, this.getName()));
}
ASMHelper.setVisibleAnnotation(method, MixinRenamed.class, "originalName", method.name, "isInterfaceMember", true);
this.decorateUniqueMethod(method);
method.name = realName;
return true;
}
/**
* Decorate the target method with {@link Unique} if the interface is marked
* as unique
*
* @param method method to decorate
*/
private void decorateUniqueMethod(MethodNode method) {
if (!this.unique) {
return;
}
if (ASMHelper.getVisibleAnnotation(method, Unique.class) == null) {
ASMHelper.setVisibleAnnotation(method, Unique.class);
this.mixin.getClassInfo().findMethod(method).setUnique(true);
}
}
/**
* Convert an {@link Interface} annotation node into an
* {@link InterfaceInfo}
@@ -173,12 +231,13 @@ public class InterfaceInfo extends TreeInfo {
static InterfaceInfo fromAnnotation(MixinInfo mixin, AnnotationNode node) {
String prefix = ASMHelper.<String>getAnnotationValue(node, "prefix");
Type iface = ASMHelper.<Type>getAnnotationValue(node, "iface");
Boolean unique = ASMHelper.<Boolean>getAnnotationValue(node, "unique");
if (prefix == null || iface == null) {
throw new InvalidMixinException(mixin, String.format("@Interface annotation on %s is missing a required parameter", mixin));
}
return new InterfaceInfo(mixin, prefix, iface);
return new InterfaceInfo(mixin, prefix, iface, unique != null ? unique.booleanValue() : false);
}
@Override
@@ -756,7 +756,7 @@ class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
*
* @param configFile
* @param outer fallback environment
* @return
* @return new Config
*/
static Config create(String configFile, MixinEnvironment outer) {
try {
@@ -47,9 +47,11 @@ import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.extensibility.IMixinConfig;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
@@ -109,6 +111,11 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
*/
private boolean detachedSuper;
/**
* True if this mixin is decorated with {@link Unique}
*/
private boolean unique;
/**
* All interfaces implemented by this mixin, including soft
* implementations
@@ -137,8 +144,16 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
State(byte[] mixinBytes, ClassInfo classInfo) {
this.mixinBytes = mixinBytes;
this.connect();
this.classInfo = classInfo != null ? classInfo : ClassInfo.fromClassNode(this.getClassNode());
}
private void connect() {
this.classNode = this.createClassNode(0);
this.classInfo = classInfo != null ? classInfo : ClassInfo.fromClassNode(this.classNode);
}
private void complete() {
this.classNode = null;
}
ClassInfo getClassInfo() {
@@ -156,6 +171,10 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
boolean isDetachedSuper() {
return this.detachedSuper;
}
boolean isUnique() {
return this.unique;
}
List<? extends InterfaceInfo> getSoftImplements() {
return this.softImplements;
@@ -192,6 +211,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
type.validate(this, targetClasses);
this.detachedSuper = type.isDetachedSuper();
this.unique = ASMHelper.getVisibleAnnotation(this.getClassNode(), Unique.class) != null;
// Pre-flight checks
this.validateInner();
@@ -206,7 +226,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
this.validateChanges(type, targetClasses);
// Null out the validation classnode
this.classNode = null;
this.complete();
}
private void validateInner() {
@@ -380,7 +400,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
* Get whether this mixin is detached super, must call {@link #validate}
* first
*
* @return
* @return true if super is detached
*/
boolean isDetachedSuper() {
return this.detached;
@@ -547,6 +567,11 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
* Mixin type
*/
private final transient SubType type;
/**
* Strict target checks enabled
*/
private final transient boolean strict;
/**
* Holds state that currently is not fully initialised or validated
@@ -566,18 +591,19 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
* @param runTransformers
* @param plugin
* @param suppressPlugin
* @throws ClassNotFoundException
*/
MixinInfo(MixinConfig parent, String mixinName, boolean runTransformers, IMixinConfigPlugin plugin, boolean suppressPlugin) {
this.parent = parent;
this.name = mixinName;
this.className = parent.getMixinPackage() + mixinName;
this.plugin = plugin;
this.phase = MixinEnvironment.getCurrentEnvironment().getPhase();
this.phase = parent.getEnvironment().getPhase();
this.strict = parent.getEnvironment().getOption(Option.DEBUG_TARGETS);
// Read the class bytes and transform
try {
this.pendingState = new State(this.loadMixinClass(this.className, runTransformers));
byte[] mixinBytes = this.loadMixinClass(this.className, runTransformers);
this.pendingState = new State(mixinBytes);
this.info = this.pendingState.getClassInfo();
this.type = this.initType();
} catch (Exception ex) {
@@ -627,7 +653,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
*
* @param classNode
* @param suppressPlugin
* @return
* @return target class list read from classNode
*/
protected List<ClassInfo> readTargetClasses(ClassNode classNode, boolean suppressPlugin) {
if (classNode == null) {
@@ -672,7 +698,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
targetName = targetName.replace('/', '.');
if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetName, this.className)) {
ClassInfo targetInfo = this.getTarget(targetName, checkPublic);
if (!outTargets.contains(targetInfo)) {
if (targetInfo != null && !outTargets.contains(targetInfo)) {
outTargets.add(targetInfo);
targetInfo.addMixin(this);
}
@@ -683,21 +709,29 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
private ClassInfo getTarget(String targetName, boolean checkPublic) throws InvalidMixinException {
ClassInfo targetInfo = ClassInfo.forName(targetName);
if (targetInfo == null) {
throw new InvalidMixinException(this, "@Mixin target " + targetName + " was not found " + this);
this.handleTargetError("@Mixin target " + targetName + " was not found " + this);
return null;
}
this.type.validateTarget(targetName, targetInfo);
if (checkPublic && targetInfo.isPublic()) {
throw new InvalidMixinException(this, "@Mixin target " + targetName + " is public in " + this
+ " and must be specified in value");
this.handleTargetError("@Mixin target " + targetName + " is public in " + this + " and should be specified in value");
}
return targetInfo;
}
private void handleTargetError(String message) {
if (this.strict) {
this.logger.error(message);
throw new InvalidMixinException(this, message);
}
this.logger.warn(message);
}
/**
* Read the priority from the {@link Mixin} annotation
*
* @param classNode
* @return
* @return priority read from classNode
*/
protected int readPriority(ClassNode classNode) {
if (classNode == null) {
@@ -792,6 +826,13 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
return this.getState().isDetachedSuper();
}
/**
* True if this mixin is decorated with {@link Unique}
*/
public boolean isUnique() {
return this.getState().isUnique();
}
/**
* Get the logging level for this mixin
*/
@@ -958,4 +999,5 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
}
return null;
}
}
@@ -25,10 +25,7 @@
package org.spongepowered.asm.mixin.transformer;
import java.lang.annotation.Annotation;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -52,7 +49,6 @@ import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.obfuscation.RemapperChain;
import org.spongepowered.asm.util.ASMHelper;
import org.spongepowered.asm.util.Constants;
@@ -92,14 +88,16 @@ class MixinPreProcessorStandard {
*/
protected final ClassNode classNode;
private final boolean verboseLogging;
private final boolean verboseLogging, strictUnique;
private boolean prepared, attached;
MixinPreProcessorStandard(MixinInfo mixin, ClassNode classNode) {
this.mixin = mixin;
this.classNode = classNode;
this.verboseLogging = mixin.getParent().getEnvironment().getOption(Option.DEBUG_VERBOSE);
MixinEnvironment env = mixin.getParent().getEnvironment();
this.verboseLogging = env.getOption(Option.DEBUG_VERBOSE);
this.strictUnique = env.getOption(Option.DEBUG_UNIQUE);
}
/**
@@ -195,13 +193,18 @@ class MixinPreProcessorStandard {
continue;
}
if (this.processMemberMethod(context, mixinMethod, Shadow.class, true, true)) {
if (this.processShadowMethod(context, mixinMethod)) {
iter.remove();
context.addShadowMethod(mixinMethod);
continue;
}
if (this.processMemberMethod(context, mixinMethod, Overwrite.class, false, false)) {
if (this.processOverwriteMethod(context, mixinMethod)) {
continue;
}
if (this.processUniqueMethod(context, mixinMethod)) {
iter.remove();
continue;
}
@@ -222,28 +225,50 @@ class MixinPreProcessorStandard {
String handlerName = context.getHandlerName(annotation, mixinMethod, surrogate);
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
if (method.isUnique()) {
MixinPreProcessorStandard.logger.warn("Redundant @Unique on injector method {} in {}. Injectors are implicitly unique", method, context);
}
method.renameTo(handlerName);
mixinMethod.name = handlerName;
return true;
}
protected boolean processMemberMethod(MixinTargetContext context, MethodNode mixinMethod, Class<? extends Annotation> annotationType,
boolean mustExist, boolean mustBePrivate) {
AnnotationNode annotation = ASMHelper.getVisibleAnnotation(mixinMethod, annotationType);
protected boolean processShadowMethod(MixinTargetContext context, MethodNode mixinMethod) {
return this.processSpecialMethod(context, mixinMethod, Shadow.class, false);
}
protected boolean processOverwriteMethod(MixinTargetContext context, MethodNode mixinMethod) {
return this.processSpecialMethod(context, mixinMethod, Overwrite.class, true);
}
protected boolean processSpecialMethod(MixinTargetContext context, MethodNode mixinMethod, Class<? extends Annotation> type, boolean overwrite) {
AnnotationNode annotation = ASMHelper.getVisibleAnnotation(mixinMethod, type);
if (annotation == null) {
return false;
}
if (this.mixin.isUnique() && overwrite) {
throw new InvalidMixinException(this.mixin, "@Overwrite method " + mixinMethod.name + " found in a @Unique mixin");
}
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
MethodNode target = MixinPreProcessorStandard.findMethod(context.getTargetClass(), mixinMethod, annotation);
MethodNode target = context.findMethod(mixinMethod, annotation);
if (method.isUnique()) {
throw new InvalidMixinException(this.mixin, ASMHelper.getSimpleName(type) + " method " + mixinMethod.name + " cannot be @Unique");
}
if (target == null) {
if (!mustExist) {
if (overwrite) {
return false;
}
target = MixinPreProcessorStandard.findRemappedMethod(context.getTargetClass(), mixinMethod);
target = context.findRemappedMethod(mixinMethod);
if (target == null) {
throw new InvalidMixinException(this.mixin, annotationType.getSimpleName() + " method " + mixinMethod.name
throw new InvalidMixinException(this.mixin, ASMHelper.getSimpleName(type) + " method " + mixinMethod.name
+ " was not located in the target class");
}
mixinMethod.name = target.name;
@@ -255,7 +280,7 @@ class MixinPreProcessorStandard {
}
if (!target.name.equals(mixinMethod.name)) {
if (mustBePrivate && (target.access & Opcodes.ACC_PRIVATE) == 0) {
if (!overwrite && (target.access & Opcodes.ACC_PRIVATE) == 0) {
throw new InvalidMixinException(this.mixin, "Non-private method cannot be aliased. Found " + target.name);
}
@@ -266,6 +291,37 @@ class MixinPreProcessorStandard {
return true;
}
protected boolean processUniqueMethod(MixinTargetContext context, MethodNode mixinMethod) {
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
if (method == null || (!method.isUnique() && !this.mixin.isUnique())) {
return false;
}
MethodNode target = context.findMethod(mixinMethod, null);
if (target == null) {
return false;
}
if ((mixinMethod.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) != 0) {
String uniqueName = context.getUniqueName(mixinMethod);
MixinPreProcessorStandard.logger.log(this.mixin.getLoggingLevel(), "Renaming @Unique method {}{} to {} in {}",
mixinMethod.name, mixinMethod.desc, uniqueName, this.mixin);
mixinMethod.name = uniqueName;
method.renameTo(uniqueName);
return false;
}
if (this.strictUnique) {
throw new InvalidMixinException(this.mixin, "Method conflict, @Unique method " + mixinMethod.name + " in " + this.mixin
+ " cannot overwrite " + target.name + target.desc + " in " + context.getTarget());
}
MixinPreProcessorStandard.logger.warn("Discarding @Unique public method {} in {} because it already exists in {}", mixinMethod.name,
this.mixin, context.getTarget());
return true;
}
protected void processMethod(MethodNode mixinMethod) {
Method method = this.mixin.getClassInfo().findMethod(mixinMethod);
if (method == null) {
@@ -293,12 +349,12 @@ class MixinPreProcessorStandard {
context.transformDescriptor(mixinField);
Field field = this.mixin.getClassInfo().findField(mixinField);
FieldNode target = MixinPreProcessorStandard.findField(context.getTargetClass(), mixinField, shadow);
FieldNode target = context.findField(mixinField, shadow);
if (target == null) {
if (shadow == null) {
continue;
}
target = MixinPreProcessorStandard.findRemappedField(context.getTargetClass(), mixinField);
target = context.findRemappedField(mixinField);
if (target == null) {
// If this field is a shadow field but is NOT found in the target class, that's bad, mmkay
throw new InvalidMixinException(this.mixin, "Shadow field " + mixinField.name + " was not located in the target class");
@@ -400,91 +456,4 @@ class MixinPreProcessorStandard {
}
}
protected static MethodNode findMethod(ClassNode classNode, MethodNode method, AnnotationNode annotation) {
Deque<String> aliases = new LinkedList<String>();
aliases.add(method.name);
if (annotation != null) {
List<String> aka = ASMHelper.<List<String>>getAnnotationValue(annotation, "aliases");
if (aka != null) {
aliases.addAll(aka);
}
}
return MixinPreProcessorStandard.findMethodRecursive(classNode, aliases, method.desc);
}
protected static MethodNode findRemappedMethod(ClassNode classNode, MethodNode method) {
RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers();
String remappedName = remapperChain.mapMethodName(classNode.name, method.name, method.desc);
if (remappedName.equals(method.name)) {
return null;
}
Deque<String> aliases = new LinkedList<String>();
aliases.add(remappedName);
return MixinPreProcessorStandard.findMethodRecursive(classNode, aliases, method.desc);
}
private static MethodNode findMethodRecursive(ClassNode classNode, Deque<String> aliases, String desc) {
String alias = aliases.poll();
if (alias == null) {
return null;
}
for (MethodNode target : classNode.methods) {
if (target.name.equals(alias) && target.desc.equals(desc)) {
return target;
}
}
return MixinPreProcessorStandard.findMethodRecursive(classNode, aliases, desc);
}
protected static FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) {
Deque<String> aliases = new LinkedList<String>();
aliases.add(field.name);
if (shadow != null) {
List<String> aka = ASMHelper.<List<String>>getAnnotationValue(shadow, "aliases");
if (aka != null) {
aliases.addAll(aka);
}
}
return MixinPreProcessorStandard.findFieldRecursive(classNode, aliases, field.desc);
}
protected static FieldNode findRemappedField(ClassNode classNode, FieldNode field) {
RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers();
String remappedName = remapperChain.mapFieldName(classNode.name, field.name, field.desc);
if (remappedName.equals(field.name)) {
return null;
}
Deque<String> aliases = new LinkedList<String>();
aliases.add(remappedName);
return MixinPreProcessorStandard.findFieldRecursive(classNode, aliases, field.desc);
}
/**
* Finds a field in the target class
*
* @param aliases
* @param desc
* @return Target field or null if not found
*/
private static FieldNode findFieldRecursive(ClassNode classNode, Deque<String> aliases, String desc) {
String alias = aliases.poll();
if (alias == null) {
return null;
}
for (FieldNode target : classNode.fields) {
if (target.name.equals(alias) && target.desc.equals(desc)) {
return target;
}
}
return MixinPreProcessorStandard.findFieldRecursive(classNode, aliases, desc);
}
}
@@ -26,8 +26,10 @@ package org.spongepowered.asm.mixin.transformer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -58,6 +60,7 @@ import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal;
import org.spongepowered.asm.mixin.transformer.meta.MixinMerged;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError;
import org.spongepowered.asm.obfuscation.RemapperChain;
import org.spongepowered.asm.util.ASMHelper;
import org.spongepowered.asm.util.Constants;
@@ -148,7 +151,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
*
* @param mixin Mixin information
* @param classNode Mixin classnode
* @param target target class
* @param context target class
*/
MixinTargetContext(MixinInfo mixin, ClassNode classNode, TargetClassContext context) {
this.mixin = mixin;
@@ -233,6 +236,15 @@ public class MixinTargetContext implements IReferenceMapperContext {
public String getClassRef() {
return this.mixin.getClassRef();
}
/**
* Get the target class context
*
* @return the target class context
*/
public TargetClassContext getTarget() {
return this.targetClass;
}
/**
* Get the target class reference
@@ -249,7 +261,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
*
* @return the target class
*/
public ClassNode getTargetClass() {
public ClassNode getTargetClassNode() {
return this.targetClass.getClassNode();
}
@@ -755,6 +767,57 @@ public class MixinTargetContext implements IReferenceMapperContext {
return this.targetClass.getTargetMethod(method);
}
MethodNode findMethod(MethodNode method, AnnotationNode annotation) {
Deque<String> aliases = new LinkedList<String>();
aliases.add(method.name);
if (annotation != null) {
List<String> aka = ASMHelper.<List<String>>getAnnotationValue(annotation, "aliases");
if (aka != null) {
aliases.addAll(aka);
}
}
return this.targetClass.findAliasedMethod(aliases, method.desc);
}
MethodNode findRemappedMethod(MethodNode method) {
RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers();
String remappedName = remapperChain.mapMethodName(this.targetClass.getName(), method.name, method.desc);
if (remappedName.equals(method.name)) {
return null;
}
Deque<String> aliases = new LinkedList<String>();
aliases.add(remappedName);
return this.targetClass.findAliasedMethod(aliases, method.desc);
}
FieldNode findField(FieldNode field, AnnotationNode shadow) {
Deque<String> aliases = new LinkedList<String>();
aliases.add(field.name);
if (shadow != null) {
List<String> aka = ASMHelper.<List<String>>getAnnotationValue(shadow, "aliases");
if (aka != null) {
aliases.addAll(aka);
}
}
return this.targetClass.findAliasedField(aliases, field.desc);
}
FieldNode findRemappedField(FieldNode field) {
RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers();
String remappedName = remapperChain.mapFieldName(this.targetClass.getName(), field.name, field.desc);
if (remappedName.equals(field.name)) {
return null;
}
Deque<String> aliases = new LinkedList<String>();
aliases.add(remappedName);
return this.targetClass.findAliasedField( aliases, field.desc);
}
/**
* Mark this mixin as requiring the specified class version in the context
* of the current target
@@ -907,11 +970,15 @@ public class MixinTargetContext implements IReferenceMapperContext {
this.mixin.postApply(transformedName, targetClass);
}
public String getHandlerName(AnnotationNode annotation, MethodNode method, boolean surrogate) {
return this.targetClass.getHandlerName(annotation, method, surrogate);
}
public String getUniqueName(MethodNode method) {
return this.targetClass.getUniqueName(method);
}
/**
* Scans the target class for injector methods and prepares discovered
* injectors
@@ -24,6 +24,7 @@
*/
package org.spongepowered.asm.mixin.transformer;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -31,6 +32,7 @@ import java.util.SortedSet;
import org.spongepowered.asm.lib.tree.AnnotationNode;
import org.spongepowered.asm.lib.tree.ClassNode;
import org.spongepowered.asm.lib.tree.FieldNode;
import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.MixinEnvironment;
@@ -91,6 +93,11 @@ class TargetClassContext {
* Do not remap injector handler methods (debug option)
*/
private final boolean disableHandlerRemap;
/**
* Unique method index
*/
private int nextUniqueMethodIndex = 0;
/**
* True once mixins have been applied to this class
@@ -112,6 +119,11 @@ class TargetClassContext {
this.disableHandlerRemap = MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_DISABLE_HANDLER_REMAP);
}
@Override
public String toString() {
return this.className;
}
public boolean isApplied() {
return this.applied;
}
@@ -154,6 +166,13 @@ class TargetClassContext {
public List<MethodNode> getMethods() {
return this.classNode.methods;
}
/**
* Get the class fields (from the tree)
*/
public List<FieldNode> getFields() {
return this.classNode.fields;
}
/**
* Get the target class metadata
@@ -168,7 +187,44 @@ class TargetClassContext {
public SortedSet<MixinInfo> getMixins() {
return this.mixins;
}
MethodNode findAliasedMethod(Deque<String> aliases, String desc) {
String alias = aliases.poll();
if (alias == null) {
return null;
}
for (MethodNode target : this.classNode.methods) {
if (target.name.equals(alias) && target.desc.equals(desc)) {
return target;
}
}
return this.findAliasedMethod(aliases, desc);
}
/**
* Finds a field in the target class
*
* @param aliases
* @param desc
* @return Target field or null if not found
*/
FieldNode findAliasedField(Deque<String> aliases, String desc) {
String alias = aliases.poll();
if (alias == null) {
return null;
}
for (FieldNode target : this.classNode.fields) {
if (target.name.equals(alias) && target.desc.equals(desc)) {
return target;
}
}
return this.findAliasedField(aliases, desc);
}
/**
* Get a target method handle from the target class
*
@@ -204,6 +260,10 @@ class TargetClassContext {
}
return String.format("%s$%s$%d", InjectionInfo.getInjectorPrefix(annotation), method.name, id.value);
}
public String getUniqueName(MethodNode method) {
return String.format("md%s$%s$%d", this.sessionId.substring(30), method.name, this.nextUniqueMethodIndex++);
}
/**
* Apply mixins to this class
@@ -226,7 +286,7 @@ class TargetClassContext {
/**
* Process {@link Debug) annotations on the class after application
* Process {@link Debug} annotations on the class after application
*/
public void processDebugTasks() {
if (!MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERBOSE)) {
@@ -1025,6 +1025,10 @@ public class ASMHelper {
return arg == null ? "" : arg.toString();
}
public static String getSimpleName(Class<? extends Annotation> annotationType) {
return String.format("@%s", annotationType.getSimpleName());
}
public static String getSimpleName(AnnotationNode annotation) {
return ASMHelper.getSimpleName(annotation.desc);
}