diff --git a/gradle.properties b/gradle.properties index e24d3800..75580b6d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.7.3 +buildVersion=0.7.4 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 6b14d263..8215f3e5 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -60,7 +60,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.7.3"; + public static final String VERSION = "0.7.4"; /** * Log all the things diff --git a/src/main/java/org/spongepowered/asm/mixin/Dynamic.java b/src/main/java/org/spongepowered/asm/mixin/Dynamic.java new file mode 100644 index 00000000..805daf65 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/Dynamic.java @@ -0,0 +1,107 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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; + +/** + * Decorator annotation for mixin elements whose targets are not available in + * the original class and are either fabricated or transformed at runtime. This + * annotation is purely for decoration purposes and has no semantic implications + * of any kind. + * + *

Its use on mixin elements is encouraged because it yields the following + * benefits:

+ * + *
    + *
  1. Aids mixin maintainers by providing context for seemingly-pointless + * injections. An injector or overwrite whose target does not appear to + * exist can be given context by decorating it with this annotation.
  2. + *
  3. Allowing mixin tooling such as IDE plugins to gain awareness that a + * particular target can be allowed to fail validation from static analysis. + * In the case where tooling might flag the target as invalid, this + * annotation can provide context to suppress or enhance generated warnings. + *
  4. + *
  5. Provide additional context when an injection fails. For example if an + * injector decorated with @Dynamic fails to locate a valid + * target, mixin will include the contents of the @Dynamic + * annotation with the error message, providing additional debugging + * information to end users. This might, for example, be used to identify + * when an upstream transformation is disabled or changed.
  6. + *
+ * + *

Because of the possible inclusion in error messages, it is suggested that + * the description provided via @Dynamic provides as much + * information in as terse a manner possible. Good examples of text descriptions + * to include might be: + * + *

+ * + *

In other words, the contents of the @Dynamic should try to + * provide as much useful context for the decorated method without being overly + * verbose.

+ * + *

In the case where the target method or field is added by an upstream + * mixin, and the mixin in question is on the classpath of the project, it is + * also possible to use the {@link #mixin} value to specify the mixin which + * contributes the target member. This provides both a useful navigable link in + * IDEs, and useful context for mixin tooling such as IDE plugins.

+ * + *

If the target member is contributed by an upstream mixin but the mixin is + * not on the classpath of the current project, using the fully- + * qualified name of the upstream mixin as the {@link #value} is a useful + * fallback, since developers reading the source code can still use the string + * value as a search term in their IDE or on Github.

+ */ +@Target({ ElementType.METHOD, ElementType.FIELD }) +@Retention(RetentionPolicy.CLASS) +public @interface Dynamic { + + /** + * Description of this @Dynamic member. See the notes above. + * Can be omitted if {@link #mixin} is specified instead. + * + * @return description of the member + */ + public String value() default ""; + + /** + * If the target member is added by an upstream mixin, and the mixin in + * question is on the classpath, specify the mixin class here in order to + * provide useful context for the annotated member. + * + * @return upstream mixin reference + */ + public Class mixin() default void.class; + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java index 0fa2a37b..179c3ee7 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java @@ -36,8 +36,10 @@ import java.util.Map.Entry; import java.util.Set; import org.spongepowered.asm.lib.Opcodes; +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.Dynamic; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; @@ -64,6 +66,8 @@ import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Bytecode; +import com.google.common.base.Strings; + /** * Contructs information about an injection from an {@link Inject} annotation * and allows the injection to be processed. @@ -174,7 +178,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC protected Set parseTargets(String type) { List methods = Annotations.getValue(this.annotation, "method", false); if (methods == null) { - throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing method name"); + throw new InvalidInjectionException(this, String.format("%s annotation on %s is missing method name", type, this.method.name)); } Set members = new LinkedHashSet(); @@ -182,13 +186,14 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC try { MemberInfo targetMember = MemberInfo.parseAndValidate(method, this.mixin); if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) { - throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " specifies a target class '" - + targetMember.owner + "', which is not supported"); + throw new InvalidInjectionException(this, + String.format("%s annotation on %s specifies a target class '%s', which is not supported", type, this.method.name, + targetMember.owner)); } members.add(targetMember); } catch (InvalidMemberDescriptorException ex) { - throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + ", has invalid target descriptor: \"" - + method + "\". " + this.mixin.getReferenceMapper().getStatus()); + throw new InvalidInjectionException(this, String.format("%s annotation on %s, has invalid target descriptor: \"%s\". %s", type, + this.method.name, method, this.mixin.getReferenceMapper().getStatus())); } } return members; @@ -197,7 +202,8 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC protected List readInjectionPoints(String type) { List ats = Annotations.getValue(this.annotation, this.atKey, false); if (ats == null) { - throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing '" + this.atKey + "' value(s)"); + throw new InvalidInjectionException(this, String.format("%s annotation on %s is missing '%s' value(s)", type, this.method.name, + this.atKey)); } return ats; } @@ -271,19 +277,23 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC this.classNode.methods.add(method); } + String description = this.getDescription(); String refMapStatus = this.mixin.getReferenceMapper().getStatus(); + String dynamicInfo = this.getDynamicInfo(); if ((this.mixin.getEnvironment().getOption(Option.DEBUG_INJECTORS) && this.injectedCallbackCount < this.expectedCallbackCount)) { throw new InvalidInjectionException(this, - String.format("Injection validation failed: %s %s%s in %s expected %d invocation(s) but %d succeeded. %s", this.getDescription(), - this.method.name, this.method.desc, this.mixin, this.expectedCallbackCount, this.injectedCallbackCount, refMapStatus)); + String.format("Injection validation failed: %s %s%s in %s expected %d invocation(s) but %d succeeded. %s%s", description, + this.method.name, this.method.desc, this.mixin, this.expectedCallbackCount, this.injectedCallbackCount, refMapStatus, + dynamicInfo)); } else if (this.injectedCallbackCount < this.requiredCallbackCount) { throw new InjectionError( - String.format("Critical injection failure: %s %s%s in %s failed injection check, (%d/%d) succeeded. %s", this.getDescription(), - this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.requiredCallbackCount, refMapStatus)); + String.format("Critical injection failure: %s %s%s in %s failed injection check, (%d/%d) succeeded. %s%s", description, + this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.requiredCallbackCount, refMapStatus, + dynamicInfo)); } else if (this.injectedCallbackCount > this.maxCallbackCount) { throw new InjectionError( - String.format("Critical injection failure: %s %s%s in %s failed injection check, %d succeeded of %d allowed", - this.getDescription(), this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.maxCallbackCount)); + String.format("Critical injection failure: %s %s%s in %s failed injection check, %d succeeded of %d allowed.%s", + description, this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.maxCallbackCount, dynamicInfo)); } } @@ -407,8 +417,10 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } if (this.targets.size() == 0) { - throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " could not find any targets matching " - + InjectionInfo.namesOf(searchFor) + ". " + this.mixin.getReferenceMapper().getStatus()); + throw new InvalidInjectionException(this, + String.format("%s annotation on %s could not find any targets matching %s in the target class %s. %s%s", + type, this.method.name, InjectionInfo.namesOf(searchFor), this.mixin.getTarget(), + this.mixin.getReferenceMapper().getStatus(), this.getDynamicInfo())); } } @@ -422,16 +434,32 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC int priority = Annotations.getValue(merged, "priority"); if (priority >= this.mixin.getPriority() && !owner.equals(this.mixin.getClassName())) { - throw new InvalidInjectionException(this, this + " cannot inject into " + this.classNode.name + "::" + target.name + target.desc - + " merged by " + owner + " with priority " + priority); + throw new InvalidInjectionException(this, String.format("%s cannot inject into %s::%s%s merged by %s with priority %d", this, + this.classNode.name, target.name, target.desc, owner, priority)); } if (Annotations.getVisible(target, Final.class) != null) { - throw new InvalidInjectionException(this, this + " cannot inject into @Final method " + this.classNode.name + "::" + target.name - + target.desc + " merged by " + owner); + throw new InvalidInjectionException(this, String.format("%s cannot inject into @Final method %s::%s%s merged by %s", this, + this.classNode.name, target.name, target.desc, owner)); } } + /** + * Get info from a decorating {@link Dynamic} annotation. If the annotation + * is present, a descriptive string suitable for inclusion in an error + * message is returned. If the annotation is not present then an empty + * string is returned. + */ + protected String getDynamicInfo() { + AnnotationNode annotation = Annotations.getInvisible(this.method, Dynamic.class); + String description = Strings.nullToEmpty(Annotations.getValue(annotation)); + Type upstream = Annotations.getValue(annotation, "mixin"); + if (upstream != null) { + description = String.format("{%s} %s", upstream.getClassName(), description).trim(); + } + return description.length() > 0 ? String.format(" Method is @Dynamic(%s)", description) : ""; + } + /** * Parse an injector from the specified method (if an injector annotation is * present). If no injector annotation is present then null is @@ -487,8 +515,8 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC ModifyConstant.class ); } catch (IllegalArgumentException ex) { - throw new InvalidMixinException(mixin, "Error parsing annotations on " + method.name + " in " + mixin.getClassName() + ": " - + ex.getMessage()); + throw new InvalidMixinException(mixin, String.format("Error parsing annotations on %s in %s: %s", method.name, mixin.getClassName(), + ex.getMessage())); } return annotation; } 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 bc97c423..3e47d35a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java @@ -30,12 +30,14 @@ import java.util.Iterator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.lib.Opcodes; +import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.AnnotationNode; import org.spongepowered.asm.lib.tree.FieldInsnNode; import org.spongepowered.asm.lib.tree.FieldNode; import org.spongepowered.asm.lib.tree.MethodInsnNode; import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.mixin.Dynamic; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel; import org.spongepowered.asm.mixin.MixinEnvironment.Option; @@ -60,6 +62,8 @@ import org.spongepowered.asm.util.perf.Profiler; import org.spongepowered.asm.util.perf.Profiler.Section; import org.spongepowered.asm.util.throwables.SyntheticBridgeException; +import com.google.common.base.Strings; + /** *

Mixin bytecode pre-processor. This class is responsible for bytecode pre- * processing tasks required to be performed on mixin bytecode before the mixin @@ -379,19 +383,21 @@ class MixinPreProcessorStandard { } target = context.findRemappedMethod(mixinMethod); if (target == null) { - throw new InvalidMixinException(this.mixin, type + " method " + mixinMethod.name + " in " + this.mixin - + " was not located in the target class." + context.getReferenceMapper().getStatus()); + throw new InvalidMixinException(this.mixin, + String.format("%s method %s in %s was not located in the target class %s. %s%s", type, mixinMethod.name, this.mixin, + context.getTarget(), context.getReferenceMapper().getStatus(), + MixinPreProcessorStandard.getDynamicInfo(mixinMethod))); } mixinMethod.name = method.renameTo(target.name); } if (Constants.CTOR.equals(target.name)) { - throw new InvalidMixinException(this.mixin, "Nice try! " + mixinMethod.name + " in " + this.mixin + " cannot alias a constructor!"); + throw new InvalidMixinException(this.mixin, String.format("Nice try! %s in %s cannot alias a constructor", mixinMethod.name, this.mixin)); } if (!Bytecode.compareFlags(mixinMethod, target, Opcodes.ACC_STATIC)) { - throw new InvalidMixinException(this.mixin, "STATIC modifier of " + type + " method " + mixinMethod.name + " in " + this.mixin - + " does not match the target"); + throw new InvalidMixinException(this.mixin, String.format("STATIC modifier of %s method %s in %s does not match the target", type, + mixinMethod.name, this.mixin)); } this.conformVisibility(context, mixinMethod, type, target); @@ -441,13 +447,14 @@ class MixinPreProcessorStandard { protected void checkMethodNotUnique(Method method, SpecialMethod type) { if (method.isUnique()) { - throw new InvalidMixinException(this.mixin, type + " method " + method.getName() + " cannot be @Unique"); + throw new InvalidMixinException(this.mixin, String.format("%s method %s in %s cannot be @Unique", type, method.getName(), this.mixin)); } } protected void checkMixinNotUnique(MixinMethodNode mixinMethod, SpecialMethod type) { if (this.mixin.isUnique()) { - throw new InvalidMixinException(this.mixin, type + " method " + mixinMethod.name + " found in a @Unique mixin"); + throw new InvalidMixinException(this.mixin, String.format("%s method %s found in a @Unique mixin %s", type, mixinMethod.name, + this.mixin)); } } @@ -478,8 +485,8 @@ class MixinPreProcessorStandard { } if (this.strictUnique) { - throw new InvalidMixinException(this.mixin, "Method conflict, " + type + " method " + mixinMethod.name + " in " + this.mixin - + " cannot overwrite " + target.name + target.desc + " in " + context.getTarget()); + throw new InvalidMixinException(this.mixin, String.format("Method conflict, %s method %s in %s cannot overwrite %s%s in %s", + type, mixinMethod.name, this.mixin, target.name, target.desc, context.getTarget())); } AnnotationNode unique = Annotations.getVisible(mixinMethod, Unique.class); @@ -542,7 +549,7 @@ class MixinPreProcessorStandard { field.remapTo(mixinField.desc); if (field.isUnique() && isShadow) { - throw new InvalidMixinException(this.mixin, "@Shadow field " + mixinField.name + " cannot be @Unique"); + throw new InvalidMixinException(this.mixin, String.format("@Shadow field %s cannot be @Unique", mixinField.name)); } FieldNode target = context.findField(mixinField, shadow); @@ -553,14 +560,16 @@ class MixinPreProcessorStandard { 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"); + throw new InvalidMixinException(this.mixin, String.format("Shadow field %s was not located in the target class %s. %s%s", + mixinField.name, context.getTarget(), context.getReferenceMapper().getStatus(), + MixinPreProcessorStandard.getDynamicInfo(mixinField))); } 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"); + throw new InvalidMixinException(this.mixin, String.format("STATIC modifier of @Shadow field %s in %s does not match the target", + mixinField.name, this.mixin)); } if (field.isUnique()) { @@ -573,8 +582,8 @@ class MixinPreProcessorStandard { } if (this.strictUnique) { - throw new InvalidMixinException(this.mixin, "Field conflict, @Unique field " + mixinField.name + " in " + this.mixin - + " cannot overwrite " + target.name + target.desc + " in " + context.getTarget()); + throw new InvalidMixinException(this.mixin, String.format("Field conflict, @Unique field %s in %s cannot overwrite %s%s in %s", + mixinField.name, this.mixin, target.name, target.desc, context.getTarget())); } MixinPreProcessorStandard.logger.warn("Discarding @Unique public field {} in {} because it already exists in {}. " @@ -586,7 +595,8 @@ class MixinPreProcessorStandard { // Check that the shadow field has a matching descriptor if (!target.desc.equals(mixinField.desc)) { - throw new InvalidMixinException(this.mixin, "The field " + mixinField.name + " in the target class has a conflicting signature"); + throw new InvalidMixinException(this.mixin, String.format("The field %s in the target class has a conflicting signature", + mixinField.name)); } if (!target.name.equals(mixinField.name)) { @@ -634,12 +644,13 @@ class MixinPreProcessorStandard { // Imaginary super fields get stripped from the class, but first we validate them if (Constants.IMAGINARY_SUPER.equals(field.name)) { if (field.access != Opcodes.ACC_PRIVATE) { - throw new InvalidMixinException(this.mixin, "Imaginary super field " + context + "." + field.name - + " must be private and non-final"); + throw new InvalidMixinException(this.mixin, String.format("Imaginary super field %s.%s must be private and non-final", context, + field.name)); } if (!field.desc.equals("L" + this.mixin.getClassRef() + ";")) { - throw new InvalidMixinException(this.mixin, "Imaginary super field " + context + "." + field.name - + " must have the same type as the parent mixin"); + throw new InvalidMixinException(this.mixin, + String.format("Imaginary super field %s.%s must have the same type as the parent mixin (%s)", context, field.name, + this.mixin.getClassName())); } return false; } @@ -693,5 +704,40 @@ class MixinPreProcessorStandard { fieldNode.name = field.getName(); } } + + /** + * Get info from a decorating {@link Dynamic} annotation. If the annotation + * is present, a descriptive string suitable for inclusion in an error + * message is returned. If the annotation is not present then an empty + * string is returned. + * + * @param method method to inspect + * @return dynamic text in parentheses or empty string + */ + protected static String getDynamicInfo(MethodNode method) { + return MixinPreProcessorStandard.getDynamicInfo("Method", Annotations.getInvisible(method, Dynamic.class)); + } + + /** + * Get info from a decorating {@link Dynamic} annotation. If the annotation + * is present, a descriptive string suitable for inclusion in an error + * message is returned. If the annotation is not present then an empty + * string is returned. + * + * @param method method to inspect + * @return dynamic text in parentheses or empty string + */ + protected static String getDynamicInfo(FieldNode method) { + return MixinPreProcessorStandard.getDynamicInfo("Field", Annotations.getInvisible(method, Dynamic.class)); + } + + private static String getDynamicInfo(String targetType, AnnotationNode annotation) { + String description = Strings.nullToEmpty(Annotations.getValue(annotation)); + Type upstream = Annotations.getValue(annotation, "mixin"); + if (upstream != null) { + description = String.format("{%s} %s", upstream.getClassName(), description).trim(); + } + return description.length() > 0 ? String.format(" %s is @Dynamic(%s)", targetType, description) : ""; + } }