mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-20 22:42:12 +00:00
Merge remote-tracking branch 'develop/interfacemixins' into initphase
This commit is contained in:
commit
a5e797faed
19 files changed
+585
-150
No files matched your search
+1
-1
@@ -4,5 +4,5 @@ packaging=jar
|
||||
description=Mixin
|
||||
url=https://www.spongepowered.org
|
||||
organization=SpongePowered
|
||||
buildVersion=0.4.13
|
||||
buildVersion=0.4.14
|
||||
buildType=SNAPSHOT
|
||||
@@ -37,6 +37,7 @@ import java.util.Set;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.AnnotationValue;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
@@ -230,6 +231,11 @@ class AnnotatedMixin {
|
||||
*/
|
||||
private final TypeHandle handle;
|
||||
|
||||
/**
|
||||
* True if this is an interface mixin
|
||||
*/
|
||||
private final boolean isInterface;
|
||||
|
||||
/**
|
||||
* Specified targets
|
||||
*/
|
||||
@@ -266,11 +272,11 @@ class AnnotatedMixin {
|
||||
private final Map<ObfuscationType, Set<String>> methodMappings = new HashMap<ObfuscationType, Set<String>>();
|
||||
|
||||
public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type) {
|
||||
|
||||
this.annotation = MirrorUtils.getAnnotation(type, Mixin.class);
|
||||
this.mixins = mixins;
|
||||
this.mixin = type;
|
||||
this.handle = new TypeHandle(type);
|
||||
this.isInterface = type.getKind() == ElementKind.INTERFACE;
|
||||
this.classRef = type.getQualifiedName().toString().replace('.', '/');
|
||||
|
||||
TypeHandle primaryTarget = this.initTargets();
|
||||
@@ -376,6 +382,13 @@ class AnnotatedMixin {
|
||||
return this.handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this is an interface mixin
|
||||
*/
|
||||
public boolean isInterface() {
|
||||
return this.isInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mixin's targets
|
||||
*/
|
||||
@@ -544,6 +557,10 @@ class AnnotatedMixin {
|
||||
* @return pending error message or null if no error condition
|
||||
*/
|
||||
public Message registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) {
|
||||
if (this.isInterface) {
|
||||
this.mixins.printMessage(Kind.ERROR, "Injector in interface is unsupported", method);
|
||||
}
|
||||
|
||||
String originalReference = MirrorUtils.<String>getAnnotationValue(inject, "method");
|
||||
MemberInfo targetMember = MemberInfo.parse(originalReference);
|
||||
if (targetMember.name == null) {
|
||||
@@ -591,6 +608,10 @@ class AnnotatedMixin {
|
||||
* and process the references
|
||||
*/
|
||||
public int registerInjectionPoint(Element element, AnnotationMirror inject, AnnotationMirror at) {
|
||||
if (this.isInterface) {
|
||||
this.mixins.printMessage(Kind.ERROR, "Injector in interface is unsupported", element);
|
||||
}
|
||||
|
||||
if (!AnnotatedMixins.getRemapValue(at)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -61,10 +61,10 @@ public abstract class MixinProcessor extends AbstractProcessor {
|
||||
this.mixins.onPassStarted();
|
||||
|
||||
for (Element elem : roundEnv.getElementsAnnotatedWith(Mixin.class)) {
|
||||
if (elem.getKind() == ElementKind.CLASS) {
|
||||
if (elem.getKind() == ElementKind.CLASS || elem.getKind() == ElementKind.INTERFACE) {
|
||||
this.mixins.registerMixin((TypeElement)elem);
|
||||
} else {
|
||||
this.mixins.printMessage(Kind.ERROR, "Found an @Mixin annotation on an element which is not a class", elem);
|
||||
this.mixins.printMessage(Kind.ERROR, "Found an @Mixin annotation on an element which is not a class or interface", elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.util.Collection;
|
||||
import javax.annotation.processing.Messager;
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
@@ -67,6 +68,25 @@ public class TargetValidator extends MixinValidator {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (mixin.getKind() == ElementKind.INTERFACE) {
|
||||
this.validateInterfaceMixin(mixin, targets);
|
||||
} else {
|
||||
this.validateClassMixin(mixin, targets);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void validateInterfaceMixin(TypeElement mixin, Collection<TypeHandle> targets) {
|
||||
for (TypeHandle target : targets) {
|
||||
TypeElement targetType = target.getElement();
|
||||
if (targetType != null && !(targetType.getKind() == ElementKind.INTERFACE)) {
|
||||
this.error("Targetted type '" + target + " of " + mixin + " is not an interface", mixin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void validateClassMixin(TypeElement mixin, Collection<TypeHandle> targets) {
|
||||
TypeMirror superClass = mixin.getSuperclass();
|
||||
|
||||
for (TypeHandle target : targets) {
|
||||
@@ -75,8 +95,6 @@ public class TargetValidator extends MixinValidator {
|
||||
this.error("Superclass " + superClass + " of " + mixin + " was not found in the hierarchy of target class " + targetType, mixin);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean validateSuperClass(TypeMirror targetType, TypeMirror superClass) {
|
||||
|
||||
@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
|
||||
/**
|
||||
* Subsystem version
|
||||
*/
|
||||
public static final String VERSION = "0.4.13";
|
||||
public static final String VERSION = "0.4.14";
|
||||
|
||||
/**
|
||||
* Blackboard key where the subsystem version will be stored to indicate
|
||||
|
||||
@@ -437,11 +437,11 @@ public class MixinEnvironment implements ITokenProvider {
|
||||
|
||||
private final int classVersion;
|
||||
|
||||
private final boolean resolveMethodsInInterfaces;
|
||||
private final boolean supportsMethodsInInterfaces;
|
||||
|
||||
private CompatibilityLevel(int classVersion, boolean resolveMethodsInInterfaces) {
|
||||
this.classVersion = classVersion;
|
||||
this.resolveMethodsInInterfaces = resolveMethodsInInterfaces;
|
||||
this.supportsMethodsInInterfaces = resolveMethodsInInterfaces;
|
||||
}
|
||||
|
||||
boolean isSupported() {
|
||||
@@ -452,8 +452,8 @@ public class MixinEnvironment implements ITokenProvider {
|
||||
return this.classVersion;
|
||||
}
|
||||
|
||||
public boolean resolveMethodsInInterfaces() {
|
||||
return this.resolveMethodsInInterfaces;
|
||||
public boolean supportsMethodsInInterfaces() {
|
||||
return this.supportsMethodsInInterfaces;
|
||||
}
|
||||
|
||||
public boolean isAtLeast(CompatibilityLevel level) {
|
||||
|
||||
@@ -102,4 +102,12 @@ public interface IMixinInfo {
|
||||
* @return the phase
|
||||
*/
|
||||
public abstract Phase getPhase();
|
||||
|
||||
/**
|
||||
* Get whether this is an interface mixin or not
|
||||
*
|
||||
* @return true if this is an interface mixin
|
||||
*/
|
||||
public abstract boolean isInterfaceMixin();
|
||||
|
||||
}
|
||||
@@ -32,29 +32,41 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
public class InvalidInjectionException extends InvalidMixinException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final InjectionInfo info;
|
||||
|
||||
public InvalidInjectionException(MixinTargetContext mixin, String message) {
|
||||
super(mixin, message);
|
||||
this.info = null;
|
||||
}
|
||||
|
||||
public InvalidInjectionException(InjectionInfo info, String message) {
|
||||
super(info.getContext(), message);
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public InvalidInjectionException(MixinTargetContext mixin, Throwable cause) {
|
||||
super(mixin, cause);
|
||||
this.info = null;
|
||||
}
|
||||
|
||||
public InvalidInjectionException(InjectionInfo info, Throwable cause) {
|
||||
super(info.getContext(), cause);
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public InvalidInjectionException(MixinTargetContext mixin, String message, Throwable cause) {
|
||||
super(mixin, message, cause);
|
||||
this.info = null;
|
||||
}
|
||||
|
||||
public InvalidInjectionException(InjectionInfo info, String message, Throwable cause) {
|
||||
super(info.getContext(), message, cause);
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public InjectionInfo getInjectionInfo() {
|
||||
return this.info;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1282,7 +1282,7 @@ public class ClassInfo extends TreeInfo {
|
||||
}
|
||||
}
|
||||
|
||||
if (type == Type.METHOD && (this.isInterface || MixinEnvironment.getCompatibilityLevel().resolveMethodsInInterfaces())) {
|
||||
if (type == Type.METHOD && (this.isInterface || MixinEnvironment.getCompatibilityLevel().supportsMethodsInInterfaces())) {
|
||||
for (String implemented : this.interfaces) {
|
||||
ClassInfo iface = ClassInfo.forName(implemented);
|
||||
if (iface == null) {
|
||||
|
||||
@@ -24,10 +24,6 @@
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.transformer;
|
||||
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
|
||||
/**
|
||||
* Mixin Transformer Module interface for pre- and post-processors
|
||||
*/
|
||||
@@ -36,19 +32,15 @@ public interface IMixinTransformerModule {
|
||||
/**
|
||||
* Called before the mixins are applied
|
||||
*
|
||||
* @param transformedName Target class name
|
||||
* @param targetClass Target class tree
|
||||
* @param mixins Mixins to be applied
|
||||
* @param context Target class context
|
||||
*/
|
||||
public abstract void preApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins);
|
||||
public abstract void preApply(TargetClassContext context);
|
||||
|
||||
/**
|
||||
* Called after the mixins are applied
|
||||
*
|
||||
* @param transformedName Target class name
|
||||
* @param targetClass Target class tree
|
||||
* @param mixins Mixins which were applied
|
||||
* @param context Target class context
|
||||
*/
|
||||
public abstract void postApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins);
|
||||
public abstract void postApply(TargetClassContext context);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.transformer;
|
||||
|
||||
import org.spongepowered.asm.lib.tree.FieldNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodNode;
|
||||
import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
|
||||
/**
|
||||
* Applicator for interface mixins, mainly just disables things which aren't
|
||||
* supported for interface mixins
|
||||
*/
|
||||
public class InterfaceMixinApplicator extends MixinApplicator {
|
||||
|
||||
/**
|
||||
* ctor
|
||||
*
|
||||
* @param context applier context
|
||||
*/
|
||||
InterfaceMixinApplicator(TargetClassContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
|
||||
* #applyInterfaces(
|
||||
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
|
||||
*/
|
||||
@Override
|
||||
protected void applyInterfaces(MixinTargetContext mixin) {
|
||||
for (String interfaceName : mixin.getInterfaces()) {
|
||||
if (!this.targetClass.name.equals(interfaceName) && !this.targetClass.interfaces.contains(interfaceName)) {
|
||||
this.targetClass.interfaces.add(interfaceName);
|
||||
mixin.getTargetClassInfo().addInterface(interfaceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
|
||||
* #applyFields(
|
||||
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
|
||||
*/
|
||||
@Override
|
||||
protected void applyFields(MixinTargetContext mixin) {
|
||||
// shadow fields have no meaning for interfaces, just spam the dev with messages
|
||||
for (FieldNode shadow : mixin.getShadowFields()) {
|
||||
this.logger.error("Ignoring redundant @Shadow field {}:{} in {}", shadow.name, shadow.desc, mixin);
|
||||
}
|
||||
|
||||
this.mergeNewFields(mixin);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
|
||||
* #applyInitialisers(
|
||||
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
|
||||
*/
|
||||
@Override
|
||||
protected void applyInitialisers(MixinTargetContext mixin) {
|
||||
// disabled for interface mixins
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
|
||||
* #applyInjections(
|
||||
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
|
||||
*/
|
||||
@Override
|
||||
protected void applyInjections(MixinTargetContext mixin) {
|
||||
// disabled for interface mixins
|
||||
for (MethodNode method : this.targetClass.methods) {
|
||||
try {
|
||||
InjectionInfo injectInfo = InjectionInfo.parse(mixin, method);
|
||||
if (injectInfo != null) {
|
||||
throw new InvalidMixinException(mixin, injectInfo + " is not supported");
|
||||
}
|
||||
} catch (InvalidInjectionException ex) {
|
||||
String description = ex.getInjectionInfo() != null ? ex.getInjectionInfo().toString() : "Injection";
|
||||
throw new InvalidMixinException(mixin, description + " is not supported");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.transformer;
|
||||
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
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.transformer.ClassInfo.Method;
|
||||
|
||||
/**
|
||||
* Bytecode preprocessor for interface mixins, simply performs some additional
|
||||
* verification for things which are unsupported in interfaces
|
||||
*/
|
||||
class InterfaceMixinPreProcessor extends MixinPreProcessor {
|
||||
|
||||
/**
|
||||
* Ctor
|
||||
*
|
||||
* @param mixin Mixin info
|
||||
* @param classNode Mixin classnode
|
||||
*/
|
||||
InterfaceMixinPreProcessor(MixinInfo mixin, ClassNode classNode) {
|
||||
super(mixin, classNode);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.MixinPreProcessor
|
||||
* #prepareMethod(org.spongepowered.asm.lib.tree.MethodNode,
|
||||
* org.spongepowered.asm.mixin.transformer.ClassInfo.Method)
|
||||
*/
|
||||
@Override
|
||||
protected void prepareMethod(MethodNode mixinMethod, Method method) {
|
||||
// I have no idea how the hell you'd compile this, but we make sure anyway!
|
||||
if (!MixinApplicator.hasFlag(mixinMethod, Opcodes.ACC_PUBLIC)) {
|
||||
throw new InvalidMixinException(this.mixin, "Interface mixin contains a non-public method! Found " + method + " in " + this.mixin);
|
||||
}
|
||||
|
||||
super.prepareMethod(mixinMethod, method);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.MixinPreProcessor
|
||||
* #validateField(
|
||||
* org.spongepowered.asm.mixin.transformer.MixinTargetContext,
|
||||
* org.spongepowered.asm.lib.tree.FieldNode,
|
||||
* org.spongepowered.asm.lib.tree.AnnotationNode)
|
||||
*/
|
||||
@Override
|
||||
protected boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) {
|
||||
if (!MixinApplicator.hasFlag(field, Opcodes.ACC_STATIC)) {
|
||||
throw new InvalidMixinException(this.mixin, "Interface mixin contains an instance field! Found " + field.name + " in "
|
||||
+ this.mixin);
|
||||
}
|
||||
|
||||
return super.validateField(context, field, shadow);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -146,7 +146,7 @@ public class MixinApplicator {
|
||||
* locals are shifted so that they don't interfere with locals in the
|
||||
* receiving constructor.
|
||||
*/
|
||||
private static final int[] INITIALISER_OPCODE_BLACKLIST = {
|
||||
protected static final int[] INITIALISER_OPCODE_BLACKLIST = {
|
||||
Opcodes.RETURN, Opcodes.ILOAD, Opcodes.LLOAD, Opcodes.FLOAD, Opcodes.DLOAD, Opcodes.IALOAD, Opcodes.LALOAD, Opcodes.FALOAD, Opcodes.DALOAD,
|
||||
Opcodes.AALOAD, Opcodes.BALOAD, Opcodes.CALOAD, Opcodes.SALOAD, Opcodes.ISTORE, Opcodes.LSTORE, Opcodes.FSTORE, Opcodes.DSTORE,
|
||||
Opcodes.ASTORE, Opcodes.IASTORE, Opcodes.LASTORE, Opcodes.FASTORE, Opcodes.DASTORE, Opcodes.AASTORE, Opcodes.BASTORE, Opcodes.CASTORE,
|
||||
@@ -156,29 +156,29 @@ public class MixinApplicator {
|
||||
/**
|
||||
* Log more things
|
||||
*/
|
||||
private final Logger logger = LogManager.getLogger("mixin");
|
||||
protected final Logger logger = LogManager.getLogger("mixin");
|
||||
|
||||
/**
|
||||
* Session ID, used as a check when parsing {@link MixinMerged} annotations
|
||||
* to prevent them being applied at compile time by people trying to
|
||||
* circumvent mixin application
|
||||
*/
|
||||
private final String sessionId;
|
||||
protected final String sessionId;
|
||||
|
||||
/**
|
||||
* Target class name
|
||||
*/
|
||||
private final String targetName;
|
||||
protected final String targetName;
|
||||
|
||||
/**
|
||||
* Target class tree
|
||||
*/
|
||||
private final ClassNode targetClass;
|
||||
protected final ClassNode targetClass;
|
||||
|
||||
MixinApplicator(String sessionId, String transformedName, ClassNode targetClass) {
|
||||
this.sessionId = sessionId;
|
||||
this.targetName = transformedName;
|
||||
this.targetClass = targetClass;
|
||||
MixinApplicator(TargetClassContext context) {
|
||||
this.sessionId = context.getSessionId();
|
||||
this.targetName = context.getName();
|
||||
this.targetClass = context.getClassNode();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,7 +196,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param mixin Mixin to apply
|
||||
*/
|
||||
private void applyMixin(MixinTargetContext mixin) {
|
||||
protected final void applyMixin(MixinTargetContext mixin) {
|
||||
try {
|
||||
mixin.preApply(this.targetName, this.targetClass);
|
||||
|
||||
@@ -221,7 +221,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyInterfaces(MixinTargetContext mixin) {
|
||||
protected void applyInterfaces(MixinTargetContext mixin) {
|
||||
for (String interfaceName : mixin.getInterfaces()) {
|
||||
if (!this.targetClass.interfaces.contains(interfaceName)) {
|
||||
this.targetClass.interfaces.add(interfaceName);
|
||||
@@ -235,7 +235,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyAttributes(MixinTargetContext mixin) {
|
||||
protected void applyAttributes(MixinTargetContext mixin) {
|
||||
if (mixin.shouldSetSourceFile()) {
|
||||
this.targetClass.sourceFile = mixin.getSourceFile();
|
||||
}
|
||||
@@ -247,7 +247,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyAnnotations(MixinTargetContext mixin) {
|
||||
protected void applyAnnotations(MixinTargetContext mixin) {
|
||||
ClassNode sourceClass = mixin.getClassNode();
|
||||
this.mergeAnnotations(sourceClass, this.targetClass);
|
||||
}
|
||||
@@ -260,14 +260,21 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyFields(MixinTargetContext mixin) {
|
||||
protected void applyFields(MixinTargetContext mixin) {
|
||||
this.mergeShadowFieldAnnotations(mixin);
|
||||
this.mergeNewFields(mixin);
|
||||
}
|
||||
|
||||
protected void mergeShadowFieldAnnotations(MixinTargetContext mixin) {
|
||||
for (FieldNode shadow : mixin.getShadowFields()) {
|
||||
FieldNode target = this.findTargetField(shadow);
|
||||
if (target != null) {
|
||||
this.mergeAnnotations(shadow, target);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void mergeNewFields(MixinTargetContext mixin) {
|
||||
for (FieldNode field : mixin.getFields()) {
|
||||
FieldNode target = this.findTargetField(field);
|
||||
if (target == null) {
|
||||
@@ -282,7 +289,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyMethods(MixinTargetContext mixin) {
|
||||
protected void applyMethods(MixinTargetContext mixin) {
|
||||
for (MethodNode shadow : mixin.getShadowMethods()) {
|
||||
MethodNode target = this.findTargetMethod(shadow);
|
||||
if (target != null) {
|
||||
@@ -325,11 +332,11 @@ public class MixinApplicator {
|
||||
* @param isOverwrite true if the method is annotated with an
|
||||
* {@link Overwrite} annotation
|
||||
*/
|
||||
private void mergeMethod(MixinTargetContext mixin, MethodNode method, boolean isOverwrite) {
|
||||
protected void mergeMethod(MixinTargetContext mixin, MethodNode method, boolean isOverwrite) {
|
||||
MethodNode target = this.findTargetMethod(method);
|
||||
|
||||
if (target != null) {
|
||||
if (this.alreadyMerged(mixin, method, isOverwrite, target)) {
|
||||
if (this.isAlreadyMerged(mixin, method, isOverwrite, target)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -365,7 +372,7 @@ public class MixinApplicator {
|
||||
* @param target target method being checked
|
||||
* @return true if the target was already merged and should be skipped
|
||||
*/
|
||||
private boolean alreadyMerged(MixinTargetContext mixin, MethodNode method, boolean isOverwrite, MethodNode target) {
|
||||
protected boolean isAlreadyMerged(MixinTargetContext mixin, MethodNode method, boolean isOverwrite, MethodNode target) {
|
||||
AnnotationNode merged = ASMHelper.getVisibleAnnotation(target, MixinMerged.class);
|
||||
if (merged == null) {
|
||||
return false;
|
||||
@@ -406,7 +413,7 @@ public class MixinApplicator {
|
||||
* @return true if the intrinsic method was skipped (short-circuit further
|
||||
* merge operations)
|
||||
*/
|
||||
private boolean mergeIntrinsic(MixinTargetContext mixin, MethodNode method, boolean isOverwrite,
|
||||
protected boolean mergeIntrinsic(MixinTargetContext mixin, MethodNode method, boolean isOverwrite,
|
||||
MethodNode target, AnnotationNode intrinsic) {
|
||||
|
||||
if (isOverwrite) {
|
||||
@@ -439,7 +446,7 @@ public class MixinApplicator {
|
||||
* @param method Method being merged
|
||||
* @param target target method being checked
|
||||
*/
|
||||
private void displaceIntrinsic(MixinTargetContext mixin, MethodNode method, MethodNode target) {
|
||||
protected void displaceIntrinsic(MixinTargetContext mixin, MethodNode method, MethodNode target) {
|
||||
// Deliberately include invalid character in the method name so that
|
||||
// we guarantee no hackiness
|
||||
String proxyName = "proxy+" + target.name;
|
||||
@@ -463,7 +470,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param method
|
||||
*/
|
||||
private void appendInsns(MethodNode method) {
|
||||
protected final void appendInsns(MethodNode method) {
|
||||
if (Type.getReturnType(method.desc) != Type.VOID_TYPE) {
|
||||
throw new IllegalArgumentException("Attempted to merge insns from a method which does not return void");
|
||||
}
|
||||
@@ -498,7 +505,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyInitialisers(MixinTargetContext mixin) {
|
||||
protected void applyInitialisers(MixinTargetContext mixin) {
|
||||
// Try to find a suitable constructor, we need a constructor with line numbers in order to extract the initialiser
|
||||
MethodNode ctor = this.getConstructor(mixin);
|
||||
if (ctor == null) {
|
||||
@@ -526,7 +533,7 @@ public class MixinApplicator {
|
||||
* @param mixin mixin to search
|
||||
* @return appropriate ctor or null if none found
|
||||
*/
|
||||
private MethodNode getConstructor(MixinTargetContext mixin) {
|
||||
protected MethodNode getConstructor(MixinTargetContext mixin) {
|
||||
MethodNode ctor = null;
|
||||
|
||||
for (MethodNode mixinMethod : mixin.getMethods()) {
|
||||
@@ -598,7 +605,7 @@ public class MixinApplicator {
|
||||
* @return initialiser bytecode extracted from the supplied constructor, or
|
||||
* null if the constructor range could not be parsed
|
||||
*/
|
||||
private Deque<AbstractInsnNode> getInitialiser(MixinTargetContext mixin, MethodNode ctor) {
|
||||
protected final Deque<AbstractInsnNode> getInitialiser(MixinTargetContext mixin, MethodNode ctor) {
|
||||
// Find the range of line numbers which corresponds to the constructor body
|
||||
Range init = this.getConstructorRange(ctor);
|
||||
if (!init.isValid()) {
|
||||
@@ -669,7 +676,7 @@ public class MixinApplicator {
|
||||
* @param ctor
|
||||
* @param initialiser
|
||||
*/
|
||||
private void injectInitialiser(MixinTargetContext mixin, MethodNode ctor, Deque<AbstractInsnNode> initialiser) {
|
||||
protected final void injectInitialiser(MixinTargetContext mixin, MethodNode ctor, Deque<AbstractInsnNode> initialiser) {
|
||||
Map<LabelNode, LabelNode> labels = ASMHelper.cloneLabels(ctor.instructions);
|
||||
|
||||
for (Iterator<AbstractInsnNode> iter = ctor.instructions.iterator(0); iter.hasNext();) {
|
||||
@@ -698,7 +705,7 @@ public class MixinApplicator {
|
||||
* methods
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyInjections(MixinTargetContext mixin) {
|
||||
protected void applyInjections(MixinTargetContext mixin) {
|
||||
List<InjectionInfo> injected = new ArrayList<InjectionInfo>();
|
||||
|
||||
for (MethodNode method : this.targetClass.methods) {
|
||||
@@ -727,7 +734,7 @@ public class MixinApplicator {
|
||||
* @param mixin Mixin being applied
|
||||
* @param annotation Annotation node to check constraints
|
||||
*/
|
||||
private void checkConstraints(MixinTargetContext mixin, AnnotationNode annotation) {
|
||||
protected final void checkConstraints(MixinTargetContext mixin, AnnotationNode annotation) {
|
||||
try {
|
||||
Constraint constraint = ConstraintParser.parse(annotation);
|
||||
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
|
||||
@@ -756,7 +763,7 @@ public class MixinApplicator {
|
||||
* @param from ClassNode to merge annotations from
|
||||
* @param to ClassNode to merge annotations to
|
||||
*/
|
||||
private void mergeAnnotations(ClassNode from, ClassNode to) {
|
||||
protected final void mergeAnnotations(ClassNode from, ClassNode to) {
|
||||
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
|
||||
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
|
||||
}
|
||||
@@ -772,7 +779,7 @@ public class MixinApplicator {
|
||||
* @param from MethodNode to merge annotations from
|
||||
* @param to MethodNode to merge annotations to
|
||||
*/
|
||||
private void mergeAnnotations(MethodNode from, MethodNode to) {
|
||||
protected final void mergeAnnotations(MethodNode from, MethodNode to) {
|
||||
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
|
||||
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
|
||||
}
|
||||
@@ -788,7 +795,7 @@ public class MixinApplicator {
|
||||
* @param from FieldNode to merge annotations from
|
||||
* @param to FieldNode to merge annotations to
|
||||
*/
|
||||
private void mergeAnnotations(FieldNode from, FieldNode to) {
|
||||
protected final void mergeAnnotations(FieldNode from, FieldNode to) {
|
||||
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
|
||||
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
|
||||
}
|
||||
@@ -840,7 +847,7 @@ public class MixinApplicator {
|
||||
* @param opcode opcode to search for
|
||||
* @return found node or null if not found
|
||||
*/
|
||||
private static AbstractInsnNode findInsn(MethodNode method, int opcode) {
|
||||
protected static AbstractInsnNode findInsn(MethodNode method, int opcode) {
|
||||
Iterator<AbstractInsnNode> findReturnIter = method.instructions.iterator();
|
||||
while (findReturnIter.hasNext()) {
|
||||
AbstractInsnNode insn = findReturnIter.next();
|
||||
@@ -872,7 +879,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @return Target method matching searchFor, or null if not found
|
||||
*/
|
||||
private MethodNode findTargetMethod(MethodNode searchFor) {
|
||||
protected final MethodNode findTargetMethod(MethodNode searchFor) {
|
||||
for (MethodNode target : this.targetClass.methods) {
|
||||
if (target.name.equals(searchFor.name) && target.desc.equals(searchFor.desc)) {
|
||||
return target;
|
||||
@@ -888,7 +895,7 @@ public class MixinApplicator {
|
||||
*
|
||||
* @return Target field matching searchFor, or null if not found
|
||||
*/
|
||||
private FieldNode findTargetField(FieldNode searchFor) {
|
||||
protected final FieldNode findTargetField(FieldNode searchFor) {
|
||||
for (FieldNode target : this.targetClass.fields) {
|
||||
if (target.name.equals(searchFor.name)) {
|
||||
return target;
|
||||
@@ -905,7 +912,7 @@ public class MixinApplicator {
|
||||
* @param flag
|
||||
* @return True if the specified flag is set in this method's access flags
|
||||
*/
|
||||
static boolean hasFlag(MethodNode method, int flag) {
|
||||
protected static boolean hasFlag(MethodNode method, int flag) {
|
||||
return (method.access & flag) == flag;
|
||||
}
|
||||
|
||||
@@ -916,7 +923,7 @@ public class MixinApplicator {
|
||||
* @param flag
|
||||
* @return True if the specified flag is set in this field's access flags
|
||||
*/
|
||||
static boolean hasFlag(FieldNode field, int flag) {
|
||||
protected static boolean hasFlag(FieldNode field, int flag) {
|
||||
return (field.access & flag) == flag;
|
||||
}
|
||||
}
|
||||
@@ -234,6 +234,11 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
* Cached class info
|
||||
*/
|
||||
private final transient ClassInfo info;
|
||||
|
||||
/**
|
||||
* True if this is an interface mixin rather than a regular mixin
|
||||
*/
|
||||
private final transient boolean isInterfaceMixin;
|
||||
|
||||
/**
|
||||
* Holds state that currently is not fully initialised or validated
|
||||
@@ -267,6 +272,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
byte[] mixinBytes = this.loadMixinClass(this.className, runTransformers);
|
||||
this.uninitialisedState = new ValidationState(mixinBytes);
|
||||
this.info = this.uninitialisedState.classInfo;
|
||||
this.isInterfaceMixin = this.info.isInterface();
|
||||
|
||||
ClassNode classNode = this.uninitialisedState.getClassNode(0);
|
||||
this.priority = this.readPriority(classNode);
|
||||
@@ -284,7 +290,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
if (this.uninitialisedState instanceof ReloadedState) {
|
||||
((ReloadedState) this.uninitialisedState).validateChanges();
|
||||
} else {
|
||||
new MixinPreProcessor(this, classNode).prepare();
|
||||
this.createPreProcessor(classNode).prepare();
|
||||
}
|
||||
this.uninitialisedState.classNode = null;
|
||||
// the state is now fully initialised and validated
|
||||
@@ -316,7 +322,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
@Override
|
||||
public String apply(Type input) {
|
||||
return input.getClassName();
|
||||
};
|
||||
}
|
||||
}), suppressPlugin, false);
|
||||
}
|
||||
|
||||
@@ -335,16 +341,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
targetClassName = targetClassName.replace('/', '.');
|
||||
if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetClassName, this.className)) {
|
||||
ClassInfo targetInfo = ClassInfo.forName(targetClassName);
|
||||
if (targetInfo == null) {
|
||||
throw new RuntimeException("@Mixin target " + targetClassName + " was not found " + this);
|
||||
}
|
||||
if (targetInfo.isInterface()) {
|
||||
throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is an interface in " + this);
|
||||
}
|
||||
if (checkPublic && targetInfo.isPublic()) {
|
||||
throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is public in " + this
|
||||
+ " and must be specified in value");
|
||||
}
|
||||
this.checkTarget(targetClassName, targetInfo, checkPublic);
|
||||
if (!outTargets.contains(targetInfo)) {
|
||||
outTargets.add(targetInfo);
|
||||
targetInfo.addMixin(this);
|
||||
@@ -353,6 +350,21 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkTarget(String targetClassName, ClassInfo targetInfo, boolean checkPublic) throws InvalidMixinException {
|
||||
if (targetInfo == null) {
|
||||
throw new RuntimeException("@Mixin target " + targetClassName + " was not found " + this);
|
||||
}
|
||||
boolean targetIsInterface = targetInfo.isInterface();
|
||||
if (targetIsInterface != this.isInterfaceMixin) {
|
||||
String not = targetIsInterface ? "" : "not ";
|
||||
throw new InvalidMixinException(this, "@Mixin target type mismatch: " + targetClassName + " is " + not + "an interface in " + this);
|
||||
}
|
||||
if (checkPublic && targetInfo.isPublic()) {
|
||||
throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is public in " + this
|
||||
+ " and must be specified in value");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the priority from the {@link Mixin} annotation
|
||||
*
|
||||
@@ -370,6 +382,21 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
}
|
||||
|
||||
private boolean validateTargetClasses(ClassNode classNode) {
|
||||
if (this.isInterfaceMixin) {
|
||||
return this.validateInterfaceMixinTargets(classNode);
|
||||
}
|
||||
return this.validateClassMixinTargets(classNode);
|
||||
}
|
||||
|
||||
private boolean validateInterfaceMixinTargets(ClassNode classNode) {
|
||||
if (!"java/lang/Object".equals(classNode.superName)) {
|
||||
throw new InvalidMixinException(this, "Super class of " + this + " is invalid, found " + classNode.superName.replace('/', '.'));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean validateClassMixinTargets(ClassNode classNode) {
|
||||
boolean detached = false;
|
||||
|
||||
for (ClassInfo targetClass : this.targetClasses) {
|
||||
@@ -405,6 +432,10 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
* @param state State to validate
|
||||
*/
|
||||
private void validateMixin(ValidationState state) {
|
||||
if (this.isInterfaceMixin && !MixinEnvironment.getCompatibilityLevel().supportsMethodsInInterfaces()) {
|
||||
throw new InvalidMixinException(this, "Interface mixin not supported in current enviromnment");
|
||||
}
|
||||
|
||||
// isInner (shouldn't) return true for static inner classes
|
||||
if (!state.classInfo.isProbablyStatic()) {
|
||||
throw new InvalidMixinException(this, "Inner class mixin must be declared static");
|
||||
@@ -568,6 +599,14 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
public Phase getPhase() {
|
||||
return this.phase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this is an interface mixin or not
|
||||
*/
|
||||
@Override
|
||||
public boolean isInterfaceMixin() {
|
||||
return this.isInterfaceMixin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new tree for the class bytecode
|
||||
@@ -637,7 +676,20 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
* @return new context
|
||||
*/
|
||||
public MixinTargetContext createContextFor(ClassNode target) {
|
||||
return new MixinPreProcessor(this, this.getClassNode(ClassReader.EXPAND_FRAMES)).createContextFor(target);
|
||||
ClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES);
|
||||
return this.createPreProcessor(classNode).prepare().createContextFor(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a preprocessor based on this mixin's type
|
||||
*
|
||||
* @return new preprocessor
|
||||
*/
|
||||
private MixinPreProcessor createPreProcessor(ClassNode classNode) {
|
||||
if (this.isInterfaceMixin) {
|
||||
return new InterfaceMixinPreProcessor(this, classNode);
|
||||
}
|
||||
return new MixinPreProcessor(this, classNode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -81,12 +81,12 @@ class MixinPreProcessor {
|
||||
/**
|
||||
* The mixin
|
||||
*/
|
||||
private final MixinInfo mixin;
|
||||
protected final MixinInfo mixin;
|
||||
|
||||
/**
|
||||
* Mixin class node
|
||||
*/
|
||||
private final ClassNode classNode;
|
||||
protected final ClassNode classNode;
|
||||
|
||||
private final boolean verboseLogging;
|
||||
|
||||
@@ -103,21 +103,29 @@ class MixinPreProcessor {
|
||||
*
|
||||
* @return Prepared classnode
|
||||
*/
|
||||
ClassNode prepare() {
|
||||
MixinPreProcessor prepare() {
|
||||
if (!this.prepared) {
|
||||
this.prepared = true;
|
||||
|
||||
for (MethodNode mixinMethod : this.classNode.methods) {
|
||||
Method method = this.mixin.getClassInfo().findMethod(mixinMethod);
|
||||
this.prepareShadow(mixinMethod, method);
|
||||
this.prepareSoftImplements(mixinMethod, method);
|
||||
this.prepareMethod(mixinMethod, method);
|
||||
}
|
||||
|
||||
for (FieldNode mixinField : this.classNode.fields) {
|
||||
this.prepareField(mixinField);
|
||||
}
|
||||
}
|
||||
|
||||
return this.classNode;
|
||||
return this;
|
||||
}
|
||||
|
||||
private void prepareShadow(MethodNode mixinMethod, Method method) {
|
||||
protected void prepareMethod(MethodNode mixinMethod, Method method) {
|
||||
this.prepareShadow(mixinMethod, method);
|
||||
this.prepareSoftImplements(mixinMethod, method);
|
||||
}
|
||||
|
||||
protected void prepareShadow(MethodNode mixinMethod, Method method) {
|
||||
AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class);
|
||||
if (shadowAnnotation == null) {
|
||||
return;
|
||||
@@ -132,7 +140,7 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareSoftImplements(MethodNode mixinMethod, Method method) {
|
||||
protected void prepareSoftImplements(MethodNode mixinMethod, Method method) {
|
||||
for (InterfaceInfo iface : this.mixin.getSoftImplements()) {
|
||||
if (iface.renameMethod(mixinMethod)) {
|
||||
method.renameTo(mixinMethod.name);
|
||||
@@ -140,8 +148,11 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
protected void prepareField(FieldNode mixinField) {
|
||||
// stub
|
||||
}
|
||||
|
||||
MixinTargetContext createContextFor(ClassNode target) {
|
||||
this.prepare();
|
||||
MixinTargetContext context = new MixinTargetContext(this.mixin, this.classNode, target);
|
||||
this.attach(context);
|
||||
return context;
|
||||
@@ -167,10 +178,15 @@ class MixinPreProcessor {
|
||||
this.transform(context);
|
||||
}
|
||||
|
||||
private void attachMethods(MixinTargetContext context) {
|
||||
protected void attachMethods(MixinTargetContext context) {
|
||||
for (Iterator<MethodNode> iter = this.classNode.methods.iterator(); iter.hasNext();) {
|
||||
MethodNode mixinMethod = iter.next();
|
||||
|
||||
if (!this.validateMethod(context, mixinMethod)) {
|
||||
iter.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this.processMethod(context, mixinMethod, Shadow.class, true, true)) {
|
||||
iter.remove();
|
||||
context.addShadowMethod(mixinMethod);
|
||||
@@ -181,7 +197,11 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean processMethod(MixinTargetContext context, MethodNode mixinMethod, Class<? extends Annotation> annotationType,
|
||||
protected boolean validateMethod(MixinTargetContext context, MethodNode mixinMethod) {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean processMethod(MixinTargetContext context, MethodNode mixinMethod, Class<? extends Annotation> annotationType,
|
||||
boolean mustExist, boolean mustBePrivate) {
|
||||
AnnotationNode annotation = ASMHelper.getVisibleAnnotation(mixinMethod, annotationType);
|
||||
if (annotation == null) {
|
||||
@@ -220,7 +240,7 @@ class MixinPreProcessor {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void attachFields(MixinTargetContext context) {
|
||||
protected void attachFields(MixinTargetContext context) {
|
||||
for (Iterator<FieldNode> iter = this.classNode.fields.iterator(); iter.hasNext();) {
|
||||
FieldNode mixinField = iter.next();
|
||||
AnnotationNode shadow = ASMHelper.getVisibleAnnotation(mixinField, Shadow.class);
|
||||
@@ -282,7 +302,7 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) {
|
||||
protected boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) {
|
||||
// Public static fields will fall foul of early static binding in java, including them in a mixin is an error condition
|
||||
if (MixinApplicator.hasFlag(field, Opcodes.ACC_STATIC)
|
||||
&& !MixinApplicator.hasFlag(field, Opcodes.ACC_PRIVATE)
|
||||
@@ -316,7 +336,7 @@ class MixinPreProcessor {
|
||||
* Apply discovered method and field renames to method invocations and field
|
||||
* accesses in the mixin
|
||||
*/
|
||||
private void transform(MixinTargetContext context) {
|
||||
protected void transform(MixinTargetContext context) {
|
||||
for (MethodNode mixinMethod : this.classNode.methods) {
|
||||
for (Iterator<AbstractInsnNode> iter = mixinMethod.instructions.iterator(); iter.hasNext();) {
|
||||
AbstractInsnNode insn = iter.next();
|
||||
@@ -337,7 +357,7 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private static MethodNode findMethod(ClassNode classNode, MethodNode method, AnnotationNode annotation) {
|
||||
protected static MethodNode findMethod(ClassNode classNode, MethodNode method, AnnotationNode annotation) {
|
||||
Deque<String> aliases = new LinkedList<String>();
|
||||
aliases.add(method.name);
|
||||
if (annotation != null) {
|
||||
@@ -347,10 +367,10 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
return MixinPreProcessor.findMethod(classNode, aliases, method.desc);
|
||||
return MixinPreProcessor.findMethodRecursive(classNode, aliases, method.desc);
|
||||
}
|
||||
|
||||
private static MethodNode findRemappedMethod(ClassNode classNode, MethodNode method) {
|
||||
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)) {
|
||||
@@ -360,10 +380,10 @@ class MixinPreProcessor {
|
||||
Deque<String> aliases = new LinkedList<String>();
|
||||
aliases.add(remappedName);
|
||||
|
||||
return MixinPreProcessor.findMethod(classNode, aliases, method.desc);
|
||||
return MixinPreProcessor.findMethodRecursive(classNode, aliases, method.desc);
|
||||
}
|
||||
|
||||
private static MethodNode findMethod(ClassNode classNode, Deque<String> aliases, String desc) {
|
||||
private static MethodNode findMethodRecursive(ClassNode classNode, Deque<String> aliases, String desc) {
|
||||
String alias = aliases.poll();
|
||||
if (alias == null) {
|
||||
return null;
|
||||
@@ -375,10 +395,10 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
return MixinPreProcessor.findMethod(classNode, aliases, desc);
|
||||
return MixinPreProcessor.findMethodRecursive(classNode, aliases, desc);
|
||||
}
|
||||
|
||||
private static FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) {
|
||||
protected static FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) {
|
||||
Deque<String> aliases = new LinkedList<String>();
|
||||
aliases.add(field.name);
|
||||
if (shadow != null) {
|
||||
@@ -388,10 +408,10 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
return MixinPreProcessor.findField(classNode, aliases, field.desc);
|
||||
return MixinPreProcessor.findFieldRecursive(classNode, aliases, field.desc);
|
||||
}
|
||||
|
||||
private static FieldNode findRemappedField(ClassNode classNode, FieldNode field) {
|
||||
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)) {
|
||||
@@ -400,7 +420,7 @@ class MixinPreProcessor {
|
||||
|
||||
Deque<String> aliases = new LinkedList<String>();
|
||||
aliases.add(remappedName);
|
||||
return MixinPreProcessor.findField(classNode, aliases, field.desc);
|
||||
return MixinPreProcessor.findFieldRecursive(classNode, aliases, field.desc);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,7 +430,7 @@ class MixinPreProcessor {
|
||||
* @param desc
|
||||
* @return Target field or null if not found
|
||||
*/
|
||||
private static FieldNode findField(ClassNode classNode, Deque<String> aliases, String desc) {
|
||||
private static FieldNode findFieldRecursive(ClassNode classNode, Deque<String> aliases, String desc) {
|
||||
String alias = aliases.poll();
|
||||
if (alias == null) {
|
||||
return null;
|
||||
@@ -422,6 +442,6 @@ class MixinPreProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
return MixinPreProcessor.findField(classNode, aliases, desc);
|
||||
return MixinPreProcessor.findFieldRecursive(classNode, aliases, desc);
|
||||
}
|
||||
}
|
||||
@@ -434,7 +434,10 @@ public class MixinTransformer extends TreeTransformer {
|
||||
}
|
||||
|
||||
try {
|
||||
basicClass = this.applyMixins(transformedName, basicClass, mixins);
|
||||
// Tree for target class
|
||||
ClassNode targetClassNode = this.readClass(basicClass, true);
|
||||
TargetClassContext context = new TargetClassContext(this.sessionId, transformedName, targetClassNode, mixins);
|
||||
basicClass = this.applyMixins(context);
|
||||
} catch (InvalidMixinException th) {
|
||||
if (environment.getOption(Option.DUMP_TARGET_ON_FAILURE)) {
|
||||
this.dumpClass(transformedName.replace('.', '/') + ".target", basicClass);
|
||||
@@ -606,65 +609,52 @@ public class MixinTransformer extends TreeTransformer {
|
||||
* Apply mixins for specified target class to the class described by the
|
||||
* supplied byte array.
|
||||
*
|
||||
* @param transformedName
|
||||
* @param basicClass
|
||||
* @param mixins
|
||||
* @param context
|
||||
* @return class bytecode after application of mixins
|
||||
*/
|
||||
private byte[] applyMixins(String transformedName, byte[] basicClass, SortedSet<MixinInfo> mixins) {
|
||||
// Tree for target class
|
||||
ClassNode targetClass = this.readClass(basicClass, true);
|
||||
|
||||
this.preApply(transformedName, targetClass, mixins);
|
||||
this.apply(transformedName, targetClass, mixins);
|
||||
private byte[] applyMixins(TargetClassContext context) {
|
||||
this.preApply(context);
|
||||
this.apply(context);
|
||||
try {
|
||||
this.postApply(transformedName, targetClass, mixins);
|
||||
this.postApply(context);
|
||||
} catch (ValidationFailedException ex) {
|
||||
this.logger.info(ex.getMessage());
|
||||
// If verify is enabled and failed, write out the bytecode to allow us to inspect it
|
||||
if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT)) {
|
||||
this.writeClass(transformedName, targetClass);
|
||||
this.writeClass(context);
|
||||
}
|
||||
}
|
||||
|
||||
return this.writeClass(transformedName, targetClass);
|
||||
return this.writeClass(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process tasks before mixin application
|
||||
*
|
||||
* @param transformedName Target class transformed name
|
||||
* @param targetClass Target class
|
||||
* @param mixins Mixin which were just applied
|
||||
* @param context Target class context
|
||||
*/
|
||||
private void preApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
|
||||
private void preApply(TargetClassContext context) {
|
||||
for (IMixinTransformerModule module : this.modules) {
|
||||
module.preApply(transformedName, targetClass, mixins);
|
||||
module.preApply(context);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the mixins to the target class
|
||||
*
|
||||
* @param transformedName Target class transformed name
|
||||
* @param targetClass Target class
|
||||
* @param mixins Mixin which were just applied
|
||||
* @param context Target class context
|
||||
*/
|
||||
private void apply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
|
||||
MixinApplicator applicator = new MixinApplicator(this.sessionId, transformedName, targetClass);
|
||||
applicator.apply(mixins);
|
||||
private void apply(TargetClassContext context) {
|
||||
context.applyMixins();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process tasks after mixin application
|
||||
*
|
||||
* @param transformedName Target class transformed name
|
||||
* @param targetClass Target class
|
||||
* @param mixins Mixin which were just applied
|
||||
* @param context Target class context
|
||||
*/
|
||||
private void postApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
|
||||
private void postApply(TargetClassContext context) {
|
||||
for (IMixinTransformerModule module : this.modules) {
|
||||
module.postApply(transformedName, targetClass, mixins);
|
||||
module.postApply(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -725,6 +715,10 @@ public class MixinTransformer extends TreeTransformer {
|
||||
return Pattern.compile(this.prepareFilter(filter), Pattern.CASE_INSENSITIVE).matcher(subject).matches();
|
||||
}
|
||||
|
||||
private byte[] writeClass(TargetClassContext context) {
|
||||
return this.writeClass(context.getName(), context.getClassNode());
|
||||
}
|
||||
|
||||
private byte[] writeClass(String transformedName, ClassNode targetClass) {
|
||||
// Collapse tree to bytes
|
||||
byte[] bytes = this.writeClass(targetClass);
|
||||
|
||||
+6
-11
@@ -24,10 +24,7 @@
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.transformer;
|
||||
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.spongepowered.asm.lib.ClassWriter;
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
import org.spongepowered.asm.lib.util.CheckClassAdapter;
|
||||
|
||||
/**
|
||||
@@ -56,22 +53,20 @@ public class MixinTransformerModuleCheckClass implements IMixinTransformerModule
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
|
||||
* #preTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
|
||||
* java.util.SortedSet)
|
||||
* #preApply(org.spongepowered.asm.mixin.transformer.TargetClassContext)
|
||||
*/
|
||||
@Override
|
||||
public void preApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
|
||||
public void preApply(TargetClassContext context) {
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
|
||||
* #postTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
|
||||
* java.util.SortedSet)
|
||||
* #postApply(org.spongepowered.asm.mixin.transformer.TargetClassContext)
|
||||
*/
|
||||
@Override
|
||||
public void postApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
|
||||
public void postApply(TargetClassContext context) {
|
||||
try {
|
||||
targetClass.accept(new CheckClassAdapter(new MixinClassWriter(ClassWriter.COMPUTE_FRAMES)));
|
||||
context.getClassNode().accept(new CheckClassAdapter(new MixinClassWriter(ClassWriter.COMPUTE_FRAMES)));
|
||||
} catch (RuntimeException ex) {
|
||||
throw new ValidationFailedException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
+6
-10
@@ -32,10 +32,8 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal;
|
||||
import org.spongepowered.asm.util.PrettyPrinter;
|
||||
@@ -90,12 +88,11 @@ public class MixinTransformerModuleInterfaceChecker implements IMixinTransformer
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
|
||||
* #preTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
|
||||
* java.util.SortedSet)
|
||||
* #preApply(org.spongepowered.asm.mixin.transformer.TargetClassContext)
|
||||
*/
|
||||
@Override
|
||||
public void preApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
|
||||
ClassInfo targetClassInfo = ClassInfo.forName(targetClass.name);
|
||||
public void preApply(TargetClassContext context) {
|
||||
ClassInfo targetClassInfo = context.getClassInfo();
|
||||
for (Method m : targetClassInfo.getInterfaceMethods(false)) {
|
||||
this.interfaceMethods.put(targetClassInfo, m);
|
||||
}
|
||||
@@ -103,12 +100,11 @@ public class MixinTransformerModuleInterfaceChecker implements IMixinTransformer
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule
|
||||
* #postTransform(java.lang.String, org.objectweb.asm.tree.ClassNode,
|
||||
* java.util.SortedSet)
|
||||
* #postApply(org.spongepowered.asm.mixin.transformer.TargetClassContext)
|
||||
*/
|
||||
@Override
|
||||
public void postApply(String transformedName, ClassNode targetClass, SortedSet<MixinInfo> mixins) {
|
||||
ClassInfo targetClassInfo = ClassInfo.forName(targetClass.name);
|
||||
public void postApply(TargetClassContext context) {
|
||||
ClassInfo targetClassInfo = context.getClassInfo();
|
||||
|
||||
String className = targetClassInfo.getName().replace('/', '.');
|
||||
int missingMethodCount = 0;
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* 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.transformer;
|
||||
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
|
||||
/**
|
||||
* Struct for containing target class information during mixin application
|
||||
*/
|
||||
class TargetClassContext {
|
||||
|
||||
/**
|
||||
* Transformer session ID
|
||||
*/
|
||||
private final String sessionId;
|
||||
|
||||
/**
|
||||
* Target class name
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Target class as tree
|
||||
*/
|
||||
private final ClassNode classNode;
|
||||
|
||||
/**
|
||||
* Target class metadata
|
||||
*/
|
||||
private final ClassInfo classInfo;
|
||||
|
||||
/**
|
||||
* Mixins to apply
|
||||
*/
|
||||
private final SortedSet<MixinInfo> mixins;
|
||||
|
||||
/**
|
||||
* True once mixins have been applied to this class
|
||||
*/
|
||||
private boolean applied;
|
||||
|
||||
TargetClassContext(String sessionId, String name, ClassNode classNode, SortedSet<MixinInfo> mixins) {
|
||||
this.sessionId = sessionId;
|
||||
this.name = name;
|
||||
this.classNode = classNode;
|
||||
this.classInfo = ClassInfo.fromClassNode(classNode);
|
||||
this.mixins = mixins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transformer session ID
|
||||
*/
|
||||
public String getSessionId() {
|
||||
return this.sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class name
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class tree
|
||||
*/
|
||||
public ClassNode getClassNode() {
|
||||
return this.classNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target class metadata
|
||||
*/
|
||||
public ClassInfo getClassInfo() {
|
||||
return this.classInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mixins for this target class
|
||||
*/
|
||||
public SortedSet<MixinInfo> getMixins() {
|
||||
return this.mixins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply mixins to this class
|
||||
*
|
||||
* @param sessionId
|
||||
*/
|
||||
public void applyMixins() {
|
||||
if (this.applied) {
|
||||
throw new IllegalStateException("Mixins already applied to target class " + this.name);
|
||||
}
|
||||
this.applied = true;
|
||||
MixinApplicator applicator = this.createApplicator();
|
||||
applicator.apply(this.mixins);
|
||||
}
|
||||
|
||||
private MixinApplicator createApplicator() {
|
||||
if (this.classInfo.isInterface()) {
|
||||
return new InterfaceMixinApplicator(this);
|
||||
}
|
||||
return new MixinApplicator(this);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user