diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index b16c8bf4..a37dc776 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -373,7 +373,7 @@ public abstract class InjectionPoint { */ public static InjectionPoint parse(MixinTargetContext mixin, MethodNode method, AnnotationNode parent, String at, At.Shift shift, int by, List args, String target, int ordinal, int opcode) { - InjectionPointData data = new InjectionPointData(mixin, args, target, ordinal, opcode); + InjectionPointData data = new InjectionPointData(mixin, method, parent, at, args, target, ordinal, opcode); InjectionPoint point = null; if (BeforeFieldAccess.CODE.equals(at)) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionPointException.java b/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionPointException.java new file mode 100644 index 00000000..23a0b59d --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionPointException.java @@ -0,0 +1,53 @@ +/* + * 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.injection; + +import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; + +/** + * Thrown when an injection point cannot be parsed due to invalid data + */ +public class InvalidInjectionPointException extends InvalidInjectionException { + + private static final long serialVersionUID = 1L; + + public InvalidInjectionPointException(MixinTargetContext mixin, String format, Object... args) { + super(mixin, String.format(format, args)); + } + + public InvalidInjectionPointException(InjectionInfo info, String format, Object... args) { + super(info, String.format(format, args)); + } + + public InvalidInjectionPointException(MixinTargetContext mixin, Throwable cause, String format, Object... args) { + super(mixin, String.format(format, args), cause); + } + + public InvalidInjectionPointException(InjectionInfo info, Throwable cause, String format, Object... args) { + super(info, String.format(format, args), cause); + } + +} 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 1d67a8b1..0de2641b 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 @@ -63,8 +63,6 @@ public abstract class InjectionInfo { protected final AnnotationNode annotation; - protected final String annotationType; - /** * Class */ @@ -142,7 +140,6 @@ public abstract class InjectionInfo { this.mixin = mixin; this.method = method; this.annotation = annotation; - this.annotationType = annotation.desc.substring(annotation.desc.lastIndexOf('/') + 1).replace(";", ""); this.classNode = mixin.getTargetClass(); this.isStatic = ASMHelper.methodIsStatic(method); this.readAnnotation(); @@ -157,7 +154,7 @@ public abstract class InjectionInfo { return; } - String type = "@" + this.annotation.desc.substring(this.annotation.desc.lastIndexOf('/') + 1, this.annotation.desc.length() - 1); + String type = "@" + ASMHelper.getSimpleName(this.annotation); String method = ASMHelper.getAnnotationValue(this.annotation, "method"); if (method == null) { @@ -175,7 +172,13 @@ public abstract class InjectionInfo { throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing 'at' value(s)"); } - MemberInfo targetMember = MemberInfo.parseAndValidate(method, this.mixin); + MemberInfo targetMember; + try { + targetMember = MemberInfo.parseAndValidate(method, this.mixin); + } catch (InvalidMemberDescriptorException ex) { + throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + ", has invalid target descriptor: \"" + + method + "\""); + } if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) { throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " specifies a target class '" @@ -276,7 +279,7 @@ public abstract class InjectionInfo { @Override public String toString() { - return String.format("%s->@%s::%s%s", this.mixin.toString(), this.annotationType, this.method.name, this.method.desc); + return InjectionInfo.describeInjector(this.mixin, this.annotation, this.method); } /** @@ -445,4 +448,8 @@ public abstract class InjectionInfo { return "handler"; } + static String describeInjector(MixinTargetContext mixin, AnnotationNode annotation, MethodNode method) { + return String.format("%s->@%s::%s%s", mixin.toString(), ASMHelper.getSimpleName(annotation), method.name, method.desc); + } + } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java index 667f53b1..bd5130f9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java @@ -28,8 +28,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.spongepowered.asm.lib.tree.AnnotationNode; +import org.spongepowered.asm.lib.tree.MethodNode; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.transformer.IReferenceMapperContext; +import org.spongepowered.asm.mixin.injection.InvalidInjectionPointException; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; /** * Data read from an {@link org.spongepowered.asm.mixin.injection.At} annotation @@ -45,8 +48,23 @@ public class InjectionPointData { /** * Mixin */ - private final IReferenceMapperContext mixin; + private final MixinTargetContext mixin; + /** + * Injector callback + */ + private MethodNode method; + + /** + * Parent annotation + */ + private AnnotationNode parent; + + /** + * At arg + */ + private String at; + /** * Target */ @@ -62,8 +80,12 @@ public class InjectionPointData { */ private final int opcode; - public InjectionPointData(IReferenceMapperContext mixin, List args, String target, int ordinal, int opcode) { + public InjectionPointData(MixinTargetContext mixin, MethodNode method, AnnotationNode parent, String at, List args, String target, + int ordinal, int opcode) { this.mixin = mixin; + this.method = method; + this.parent = parent; + this.at = at; this.target = target; this.ordinal = Math.max(-1, ordinal); this.opcode = opcode; @@ -87,6 +109,18 @@ public class InjectionPointData { } } } + + public MixinTargetContext getMixin() { + return this.mixin; + } + + public MethodNode getMethod() { + return this.method; + } + + public AnnotationNode getParent() { + return this.parent; + } public String get(String key, String defaultValue) { String value = this.args.get(key); @@ -102,7 +136,12 @@ public class InjectionPointData { } public MemberInfo get(String key) { - return MemberInfo.parseAndValidate(this.get(key, ""), this.mixin); + try { + return MemberInfo.parseAndValidate(this.get(key, ""), this.mixin); + } catch (InvalidMemberDescriptorException ex) { + throw new InvalidInjectionPointException(this.mixin, "Failed parsing @At(\"%s\").%s descriptor \"%s\" on %s", + this.at, key, this.target, InjectionInfo.describeInjector(this.mixin, this.parent, this.method)); + } } private int parseInt(String string, int defaultValue) { @@ -122,7 +161,12 @@ public class InjectionPointData { } public MemberInfo getTarget() { - return MemberInfo.parseAndValidate(this.target, this.mixin); + try { + return MemberInfo.parseAndValidate(this.target, this.mixin); + } catch (InvalidMemberDescriptorException ex) { + throw new InvalidInjectionPointException(this.mixin, "Failed parsing @At(\"%s\") descriptor \"%s\" on %s", + this.at, this.target, InjectionInfo.describeInjector(this.mixin, this.parent, this.method)); + } } public int getOrdinal() { diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index de2bfcab..3c5bc534 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -934,4 +934,12 @@ public class ASMHelper { return arg == null ? "" : arg.toString(); } + public static String getSimpleName(AnnotationNode annotation) { + return ASMHelper.getSimpleName(annotation.desc); + } + + public static String getSimpleName(String desc) { + return desc.substring(desc.lastIndexOf('/') + 1).replace(";", ""); + } + }