From 7bfca4ba788c9ca8d4aac91f015409427c052d7d Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Thu, 16 Jun 2016 16:37:05 +0100 Subject: [PATCH] Minor adjustments for sanity --- .../transformer/MixinApplicatorStandard.java | 17 ++++++++ .../asm/mixin/transformer/MixinInfo.java | 26 +++++++----- .../MixinPreProcessorInterface.java | 8 ++-- .../MixinPreProcessorStandard.java | 42 ++++++++----------- .../MixinTargetAlreadyLoadedException.java | 16 ++++--- 5 files changed, 64 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java index b261f9db..504dd07d 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java @@ -379,6 +379,7 @@ public class MixinApplicatorStandard { mixin.transformMethod(mixinMethod); if (!mixinMethod.name.startsWith("<")) { + this.checkMethodVisibility(mixin, mixinMethod); this.checkMethodConstraints(mixin, mixinMethod); this.mergeMethod(mixin, mixinMethod); } else if (Constants.CLINIT.equals(mixinMethod.name)) { @@ -793,6 +794,22 @@ public class MixinApplicatorStandard { mixin.applyInjections(); } + /** + * Check visibility before merging a mixin method + * + * @param mixin + * @param mixinMethod + */ + protected void checkMethodVisibility(MixinTargetContext mixin, MethodNode mixinMethod) { + if (ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_STATIC) + && !ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE) + && !ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC) + && !(ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null)) { + throw new InvalidMixinException(mixin, + String.format("Mixin %s contains non-private static method %s", mixin, mixinMethod)); + } + } + /** * Check constraints in annotations on the specified mixin method * diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index f1d41c96..4c4a0877 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -75,10 +75,10 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { /** * A MethodNode in a mixin */ - public class MixinMethodNode extends MethodNode { + class MixinMethodNode extends MethodNode { private final String originalName; - + public MixinMethodNode(int access, String name, String desc, String signature, String[] exceptions) { super(Opcodes.ASM5, access, name, desc, signature, exceptions); this.originalName = name; @@ -98,14 +98,18 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { /** * ClassNode for a MixinInfo */ - public class MixinClassNode extends ClassNode { + class MixinClassNode extends ClassNode { + + public final List mixinMethods; public MixinClassNode(MixinInfo mixin) { this(Opcodes.ASM5); } + @SuppressWarnings("unchecked") public MixinClassNode(int api) { super(api); + this.mixinMethods = (List)(Object)this.methods; } public MixinInfo getMixin() { @@ -167,7 +171,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * Initial ClassNode created for mixin validation, not used for actual * application */ - protected ClassNode classNode; + protected MixinClassNode classNode; State(byte[] mixinBytes) { this(mixinBytes, null); @@ -225,7 +229,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @param flags Flags passed into classReader * @return Tree representing the bytecode */ - ClassNode createClassNode(int flags) { + MixinClassNode createClassNode(int flags) { MixinClassNode classNode = new MixinClassNode(MixinInfo.this); ClassReader classReader = new ClassReader(this.mixinBytes); classReader.accept(classNode, flags); @@ -454,7 +458,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { abstract void validate(State state, List targetClasses); - abstract MixinPreProcessorStandard createPreProcessor(ClassNode classNode); + abstract MixinPreProcessorStandard createPreProcessor(MixinClassNode classNode); /** * A standard mixin @@ -496,7 +500,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } @Override - MixinPreProcessorStandard createPreProcessor(ClassNode classNode) { + MixinPreProcessorStandard createPreProcessor(MixinClassNode classNode) { return new MixinPreProcessorStandard(this.mixin, classNode); } } @@ -525,7 +529,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } @Override - MixinPreProcessorStandard createPreProcessor(ClassNode classNode) { + MixinPreProcessorStandard createPreProcessor(MixinClassNode classNode) { return new MixinPreProcessorInterface(this.mixin, classNode); } } @@ -737,7 +741,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { if (MixinInfo.classLoaderUtil.isClassLoaded(targetName)) { String message = String.format("Critical problem: %s target %s was already transformed.", this, targetName); if (this.parent.isRequired()) { - throw new MixinTargetAlreadyLoadedException(this, message); + throw new MixinTargetAlreadyLoadedException(this, message, targetName); } this.logger.error(message); } @@ -898,7 +902,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * Get a new tree for the class bytecode */ @Override - public ClassNode getClassNode(int flags) { + public MixinClassNode getClassNode(int flags) { return this.getState().createClassNode(flags); } @@ -947,7 +951,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @return new context */ MixinTargetContext createContextFor(TargetClassContext target) { - ClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES); + MixinClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES); return this.type.createPreProcessor(classNode).prepare().createContextFor(target); } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorInterface.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorInterface.java index 2119b485..d9fbf4e2 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorInterface.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorInterface.java @@ -26,10 +26,10 @@ 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; +import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode; +import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinMethodNode; import org.spongepowered.asm.mixin.transformer.throwables.InvalidInterfaceMixinException; import org.spongepowered.asm.util.ASMHelper; @@ -45,7 +45,7 @@ class MixinPreProcessorInterface extends MixinPreProcessorStandard { * @param mixin Mixin info * @param classNode Mixin classnode */ - MixinPreProcessorInterface(MixinInfo mixin, ClassNode classNode) { + MixinPreProcessorInterface(MixinInfo mixin, MixinClassNode classNode) { super(mixin, classNode); } @@ -55,7 +55,7 @@ class MixinPreProcessorInterface extends MixinPreProcessorStandard { * org.spongepowered.asm.mixin.transformer.ClassInfo.Method) */ @Override - protected void prepareMethod(MethodNode mixinMethod, Method method) { + protected void prepareMethod(MixinMethodNode mixinMethod, Method method) { // I have no idea how the hell you'd compile this, but we make sure anyway! if (!ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_PUBLIC)) { throw new InvalidInterfaceMixinException(this.mixin, "Interface mixin contains a non-public method! Found " + method + " in " diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java index 32044029..8ae7a238 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java @@ -32,7 +32,6 @@ import org.apache.logging.log4j.Logger; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.AnnotationNode; -import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.lib.tree.FieldInsnNode; import org.spongepowered.asm.lib.tree.FieldNode; import org.spongepowered.asm.lib.tree.MethodInsnNode; @@ -47,6 +46,8 @@ import org.spongepowered.asm.mixin.injection.Surrogate; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.transformer.ClassInfo.Field; import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; +import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode; +import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinMethodNode; import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.util.ASMHelper; @@ -86,13 +87,13 @@ class MixinPreProcessorStandard { /** * Mixin class node */ - protected final ClassNode classNode; + protected final MixinClassNode classNode; private final boolean verboseLogging, strictUnique; private boolean prepared, attached; - MixinPreProcessorStandard(MixinInfo mixin, ClassNode classNode) { + MixinPreProcessorStandard(MixinInfo mixin, MixinClassNode classNode) { this.mixin = mixin; this.classNode = classNode; MixinEnvironment env = mixin.getParent().getEnvironment(); @@ -109,7 +110,7 @@ class MixinPreProcessorStandard { if (!this.prepared) { this.prepared = true; - for (MethodNode mixinMethod : this.classNode.methods) { + for (MixinMethodNode mixinMethod : this.classNode.mixinMethods) { Method method = this.mixin.getClassInfo().findMethod(mixinMethod); this.prepareMethod(mixinMethod, method); } @@ -122,12 +123,12 @@ class MixinPreProcessorStandard { return this; } - protected void prepareMethod(MethodNode mixinMethod, Method method) { + protected void prepareMethod(MixinMethodNode mixinMethod, Method method) { this.prepareShadow(mixinMethod, method); this.prepareSoftImplements(mixinMethod, method); } - protected void prepareShadow(MethodNode mixinMethod, Method method) { + protected void prepareShadow(MixinMethodNode mixinMethod, Method method) { AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); if (shadowAnnotation == null) { return; @@ -142,7 +143,7 @@ class MixinPreProcessorStandard { } } - protected void prepareSoftImplements(MethodNode mixinMethod, Method method) { + protected void prepareSoftImplements(MixinMethodNode mixinMethod, Method method) { for (InterfaceInfo iface : this.mixin.getSoftImplements()) { if (iface.renameMethod(mixinMethod)) { method.renameTo(mixinMethod.name); @@ -181,8 +182,8 @@ class MixinPreProcessorStandard { } protected void attachMethods(MixinTargetContext context) { - for (Iterator iter = this.classNode.methods.iterator(); iter.hasNext();) { - MethodNode mixinMethod = iter.next(); + for (Iterator iter = this.classNode.mixinMethods.iterator(); iter.hasNext();) { + MixinMethodNode mixinMethod = iter.next(); if (!this.validateMethod(context, mixinMethod)) { iter.remove(); @@ -212,19 +213,11 @@ class MixinPreProcessorStandard { } } - protected boolean validateMethod(MixinTargetContext context, MethodNode mixinMethod) { - if (ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_STATIC) - && !ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE) - && !ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC) - && !(ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null)) { - throw new InvalidMixinException(context, - String.format("Mixin %s contains non-private static method %s", context, mixinMethod)); - } - + protected boolean validateMethod(MixinTargetContext context, MixinMethodNode mixinMethod) { return true; } - protected boolean processInjectorMethod(MixinTargetContext context, MethodNode mixinMethod) { + protected boolean processInjectorMethod(MixinTargetContext context, MixinMethodNode mixinMethod) { AnnotationNode annotation = InjectionInfo.getInjectorAnnotation(context, mixinMethod); boolean surrogate = ASMHelper.getVisibleAnnotation(mixinMethod, Surrogate.class) != null; if (annotation == null && !surrogate) { @@ -244,15 +237,16 @@ class MixinPreProcessorStandard { return true; } - protected boolean processShadowMethod(MixinTargetContext context, MethodNode mixinMethod) { + protected boolean processShadowMethod(MixinTargetContext context, MixinMethodNode mixinMethod) { return this.processSpecialMethod(context, mixinMethod, Shadow.class, false); } - protected boolean processOverwriteMethod(MixinTargetContext context, MethodNode mixinMethod) { + protected boolean processOverwriteMethod(MixinTargetContext context, MixinMethodNode mixinMethod) { return this.processSpecialMethod(context, mixinMethod, Overwrite.class, true); } - protected boolean processSpecialMethod(MixinTargetContext context, MethodNode mixinMethod, Class type, boolean overwrite) { + protected boolean processSpecialMethod(MixinTargetContext context, MixinMethodNode mixinMethod, Class type, + boolean overwrite) { AnnotationNode annotation = ASMHelper.getVisibleAnnotation(mixinMethod, type); if (annotation == null) { @@ -299,7 +293,7 @@ class MixinPreProcessorStandard { return true; } - protected boolean processUniqueMethod(MixinTargetContext context, MethodNode mixinMethod) { + protected boolean processUniqueMethod(MixinTargetContext context, MixinMethodNode mixinMethod) { Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL); if (method == null || (!method.isUnique() && !this.mixin.isUnique())) { return false; @@ -330,7 +324,7 @@ class MixinPreProcessorStandard { return true; } - protected void processMethod(MethodNode mixinMethod) { + protected void processMethod(MixinMethodNode mixinMethod) { Method method = this.mixin.getClassInfo().findMethod(mixinMethod); if (method == null) { return; diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/throwables/MixinTargetAlreadyLoadedException.java b/src/main/java/org/spongepowered/asm/mixin/transformer/throwables/MixinTargetAlreadyLoadedException.java index 70780f29..613ad74c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/throwables/MixinTargetAlreadyLoadedException.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/throwables/MixinTargetAlreadyLoadedException.java @@ -32,17 +32,21 @@ import org.spongepowered.asm.mixin.extensibility.IMixinInfo; public class MixinTargetAlreadyLoadedException extends InvalidMixinException { private static final long serialVersionUID = 1L; + + private final String target; - public MixinTargetAlreadyLoadedException(IMixinInfo mixin, String message) { + public MixinTargetAlreadyLoadedException(IMixinInfo mixin, String message, String target) { super(mixin, message); + this.target = target; } - public MixinTargetAlreadyLoadedException(IMixinInfo mixin, Throwable cause) { - super(mixin, cause); - } - - public MixinTargetAlreadyLoadedException(IMixinInfo mixin, String message, Throwable cause) { + public MixinTargetAlreadyLoadedException(IMixinInfo mixin, String message, String target, Throwable cause) { super(mixin, message, cause); + this.target = target; + } + + public String getTarget() { + return this.target; } }