Improve error message for invalid injector descriptors, closes #115

This commit is contained in:
Mumfrey committed 2016-04-20 14:44:56 +01:00
1 parent 2060164ec8
commit 3ca55a3a5c
5 files changed
+124 -12

No files matched your search

@@ -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<String> 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)) {
@@ -0,0 +1,53 @@
/*
* 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.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);
}
}
@@ -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.<String>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);
}
}
@@ -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<String> args, String target, int ordinal, int opcode) {
public InjectionPointData(MixinTargetContext mixin, MethodNode method, AnnotationNode parent, String at, List<String> 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() {
@@ -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(";", "");
}
}