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 ec9b67ff..e43ca94a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java @@ -51,8 +51,9 @@ 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.Bytecode; import org.spongepowered.asm.util.Annotations; +import org.spongepowered.asm.util.Bytecode; +import org.spongepowered.asm.util.Bytecode.Visibility; import org.spongepowered.asm.util.Constants; /** @@ -312,6 +313,7 @@ class MixinPreProcessorStandard { this.checkMixinNotUnique(mixinMethod, type); } + String description = "@" + Bytecode.getSimpleName(type); Method method = this.getSpecialMethod(mixinMethod, type); MethodNode target = context.findMethod(mixinMethod, annotation); if (target == null) { @@ -320,14 +322,28 @@ class MixinPreProcessorStandard { } target = context.findRemappedMethod(mixinMethod); if (target == null) { - throw new InvalidMixinException(this.mixin, "@" + Bytecode.getSimpleName(type) + " method " + mixinMethod.name + throw new InvalidMixinException(this.mixin, description + " method " + mixinMethod.name + " in " + this.mixin + " was not located in the target class"); } mixinMethod.name = method.renameTo(target.name); } if (Constants.CTOR.equals(target.name)) { - throw new InvalidMixinException(this.mixin, "Nice try! " + mixinMethod.name + " cannot alias a constructor!"); + throw new InvalidMixinException(this.mixin, "Nice try! " + mixinMethod.name + " in " + this.mixin + " cannot alias a constructor!"); + } + + if (!Bytecode.compareFlags(mixinMethod, target, Opcodes.ACC_STATIC)) { + throw new InvalidMixinException(this.mixin, "STATIC modifier of " + description + " method " + mixinMethod.name + " in " + this.mixin + + " does not match the target"); + } + + if (overwrite) { + Visibility visTarget = Bytecode.getVisibility(target); + Visibility visMethod = Bytecode.getVisibility(mixinMethod); + if (visMethod.ordinal() < visTarget.ordinal()) { + throw new InvalidMixinException(this.mixin, visMethod + " " + description + " method " + mixinMethod.name + " in " + this.mixin + + " cannot reduce visibiliy of " + visTarget + " target method"); + } } if (!target.name.equals(mixinMethod.name)) { @@ -343,21 +359,21 @@ class MixinPreProcessorStandard { protected Method getSpecialMethod(MixinMethodNode mixinMethod, Class extends Annotation> type) { Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL); - this.checkMethodNotUnique(type, method); + this.checkMethodNotUnique(method, type); return method; } - protected void checkMethodNotUnique(Class extends Annotation> type, Method method) { + protected void checkMethodNotUnique(Method method, Class extends Annotation> type) { if (method.isUnique()) { - String annotation = "@" + Bytecode.getSimpleName(type); - throw new InvalidMixinException(this.mixin, annotation + " method " + method.getName() + " cannot be @Unique"); + String description = "@" + Bytecode.getSimpleName(type); + throw new InvalidMixinException(this.mixin, description + " method " + method.getName() + " cannot be @Unique"); } } protected void checkMixinNotUnique(MixinMethodNode mixinMethod, Class extends Annotation> type) { if (this.mixin.isUnique()) { - String annotation = "@" + Bytecode.getSimpleName(type); - throw new InvalidMixinException(this.mixin, annotation + " method " + mixinMethod.name + " found in a @Unique mixin"); + String description = "@" + Bytecode.getSimpleName(type); + throw new InvalidMixinException(this.mixin, description + " method " + mixinMethod.name + " found in a @Unique mixin"); } } @@ -434,6 +450,11 @@ class MixinPreProcessorStandard { mixinField.name = field.renameTo(target.name); } + if (!Bytecode.compareFlags(mixinField, target, Opcodes.ACC_STATIC)) { + throw new InvalidMixinException(this.mixin, "STATIC modifier of @Shadow field " + mixinField.name + " in " + this.mixin + + " does not match the target"); + } + if (field.isUnique()) { if ((mixinField.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) != 0) { String uniqueName = context.getUniqueName(mixinField); diff --git a/src/main/java/org/spongepowered/asm/util/Bytecode.java b/src/main/java/org/spongepowered/asm/util/Bytecode.java index d8fa7e8f..f531b16f 100644 --- a/src/main/java/org/spongepowered/asm/util/Bytecode.java +++ b/src/main/java/org/spongepowered/asm/util/Bytecode.java @@ -48,7 +48,39 @@ import com.google.common.primitives.Ints; * Utility methods for working with bytecode via ASM */ public final class Bytecode { - + + /** + * Ordinal member visibility level. This is used to represent visibility of + * a member in a formal way from lowest to highest. The + * {@link Bytecode#getVisibility} methods can be used to convert access + * flags to this enum. The value returned from {@link #ordinal} can then be + * used to determine whether a visibility level is higher or lower + * than any other given visibility level. + */ + public enum Visibility { + + /** + * Members decorated with {@link Opcodes#ACC_PRIVATE} + */ + PRIVATE, + + /** + * Members decorated with {@link Opcodes#ACC_PROTECTED} + */ + PROTECTED, + + /** + * Members not decorated with any access flags + */ + PACKAGE, + + /** + * Members decorated with {@link Opcodes#ACC_PUBLIC} + */ + PUBLIC + + } + /** * Integer constant opcodes */ @@ -596,6 +628,93 @@ public final class Bytecode { return (field.access & flag) == flag; } + /** + * Check whether the status of the specified flag matches on both of the + * supplied arguments. + * + * @param m1 First method + * @param m2 Second method + * @param flag flag to compare + * @return True if the flag is set to the same value on both members + */ + public static boolean compareFlags(MethodNode m1, MethodNode m2, int flag) { + return Bytecode.hasFlag(m1, flag) == Bytecode.hasFlag(m2, flag); + } + + /** + * Check whether the status of the specified flag matches on both of the + * supplied arguments. + * + * @param f1 First field + * @param f2 Second field + * @param flag flag to compare + * @return True if the flag is set to the same value on both members + */ + public static boolean compareFlags(FieldNode f1, FieldNode f2, int flag) { + return Bytecode.hasFlag(f1, flag) == Bytecode.hasFlag(f2, flag); + } + + /** + * Returns the ordinal visibility of the supplied argument where a + * higher value equals higher "visibility": + * + *