WIP but mostly working Desc selector functionality

This commit is contained in:
Mumfrey committed 2021-01-31 23:00:25 +00:00
1 parent 2ef2417de6
commit 21881482ae
27 files changed
+998 -42

No files matched your search

@@ -165,6 +165,12 @@ public @interface At {
*/
public String target() default "";
/**
* Target descriptor used in place of string-based descriptors for
* {@link #target}
*/
public Desc desc() default @Desc("");
/**
* Ordinal offset. Many InjectionPoints will return every opcode matching
* their criteria, specifying <em>ordinal</em> allows a particular opcode to
@@ -0,0 +1,51 @@
/*
* 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 java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* TODO Javadoc
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Descriptors.class)
public @interface Desc {
public String id() default "";
public Class<?> owner() default Void.class;
public String value();
public Class<?> ret() default Void.class;
public Class<?>[] args() default { };
}
@@ -0,0 +1,45 @@
/*
* 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 java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Collection type for {@link Desc} to allow {@link Repeatable} to work.
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Descriptors {
/**
* Desc annotations for element
*/
Desc[] value();
}
@@ -122,7 +122,15 @@ public @interface Inject {
*
* @return target method(s) for this injector
*/
public String[] method();
public String[] method() default {};
/**
* Literal representation of one or more {@link Desc &#064;Desc} annotations
* which identify the target methods.
*
* @return target method(s) for this injector as descriptors
*/
public Desc[] target() default {};
/**
* Array of {@link Slice} annotations which describe the method bisections
@@ -80,7 +80,15 @@ public @interface ModifyArg {
*
* @return target method(s) for this injector
*/
public String[] method();
public String[] method() default {};
/**
* Literal representation of one or more {@link Desc &#064;Desc} annotations
* which identify the target methods.
*
* @return target method(s) for this injector as descriptors
*/
public Desc[] target() default {};
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -85,7 +85,15 @@ public @interface ModifyArgs {
*
* @return target method(s) for this injector
*/
public String[] method();
public String[] method() default {};
/**
* Literal representation of one or more {@link Desc &#064;Desc} annotations
* which identify the target methods.
*
* @return target method(s) for this injector as descriptors
*/
public Desc[] target() default {};
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -67,7 +67,15 @@ public @interface ModifyConstant {
*
* @return target method(s) for this injector
*/
public String[] method();
public String[] method() default {};
/**
* Literal representation of one or more {@link Desc &#064;Desc} annotations
* which identify the target methods.
*
* @return target method(s) for this injector as descriptors
*/
public Desc[] target() default {};
/**
* Array of {@link Slice} annotations which describe the method bisections
@@ -79,7 +79,15 @@ public @interface ModifyVariable {
*
* @return target method(s) for this injector
*/
public String[] method();
public String[] method() default {};
/**
* Literal representation of one or more {@link Desc &#064;Desc} annotations
* which identify the target methods.
*
* @return target method(s) for this injector as descriptors
*/
public Desc[] target() default {};
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -326,7 +326,15 @@ public @interface Redirect {
*
* @return target method(s) for this injector
*/
public String[] method();
public String[] method() default {};
/**
* Literal representation of one or more {@link Desc &#064;Desc} annotations
* which identify the target methods.
*
* @return target method(s) for this injector as descriptors
*/
public Desc[] target() default {};
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -66,8 +66,11 @@ public interface ISelectorContext {
* Get the coordinate for this selector. This is the local coordinate of the
* selector without any parent parts. Parent parts are prepended by
* consumers during the resolve process.
*
* @param leaf True if getting the coordinate when this element is a leaf,
* false if getting the coordinate when it is a parent
*/
public abstract String getSelectorCoordinate();
public abstract String getSelectorCoordinate(boolean leaf);
/**
* Remap a reference in the context of this selector, usually via the local
@@ -38,6 +38,7 @@ import java.util.regex.Pattern;
import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorDynamic.SelectorId;
import org.spongepowered.asm.mixin.injection.selectors.dynamic.DynamicSelectorDesc;
import org.spongepowered.asm.mixin.injection.struct.InvalidMemberDescriptorException;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.mixin.injection.struct.MemberMatcher;
@@ -140,6 +141,7 @@ public final class TargetSelector {
throw (MixinException)cause;
}
Throwable ex = cause != null ? cause : itex;
ex.printStackTrace();
throw new MixinError("Error parsing dynamic target selector [" + this.type.getName() + "] for " + context, ex);
}
}
@@ -155,6 +157,10 @@ public final class TargetSelector {
*/
private static Map<String, DynamicSelectorEntry> dynamicSelectors = new LinkedHashMap<String, DynamicSelectorEntry>();
static {
TargetSelector.registerBuiltIn(DynamicSelectorDesc.class);
}
private TargetSelector() {
}
@@ -265,7 +271,7 @@ public final class TargetSelector {
}
try {
return TargetSelector.dynamicSelectors.get(selectorId).parse(Strings.nullToEmpty(dynamic.group(4)), context);
return TargetSelector.dynamicSelectors.get(selectorId).parse(Strings.nullToEmpty(dynamic.group(4)).trim(), context);
} catch (ReflectiveOperationException ex) {
return new InvalidSelector(ex.getCause(), string);
} catch (Exception ex) {
@@ -0,0 +1,245 @@
/*
* 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.selectors.dynamic;
import java.lang.annotation.Annotation;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.injection.Desc;
import org.spongepowered.asm.mixin.injection.Descriptors;
import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext;
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.asm.IAnnotatedElement;
import org.spongepowered.asm.util.asm.IAnnotationHandle;
import com.google.common.base.Strings;
/**
* Utility class which contains the logic for resolving descriptors
* ({@link Desc} annotations) starting from an element and recursing up the tree
* of parent elements looking for a matching descriptor
*/
public final class DescriptorResolver {
/**
* Resolution result
*/
public static final class Result {
/**
* The set of coordinates which were searched, stored here so they can
* be included in error messages if resolution fails
*/
private final Set<String> searched;
/**
* The resolved descriptor
*/
private final IAnnotationHandle desc;
Result(Set<String> searched, IAnnotationHandle desc) {
this.searched = searched;
this.desc = desc;
}
/**
* True if the resolution process was successful
*/
public boolean isResolved() {
return this.desc != null;
}
/**
* Gets the resolved descriptor annotation if successful, otherwise
* returns null
*/
public IAnnotationHandle getAnnotation() {
return this.desc;
}
/**
* Gets the set of coordinates which were searched during resolution
*/
public Set<String> getSearched() {
return this.searched;
}
}
// No
private DescriptorResolver() {
}
/**
* Resolve the specified descriptor id starting at the specified context
*
* @param id Id to resolve, can be empty if resolving by coordinate
* @param context Selector context, usually from the source annotation
* @return Resolution result
*/
public static Result resolve(String id, ISelectorContext context) {
Set<String> searched = new LinkedHashSet<String>();
if (!Strings.isNullOrEmpty(id)) {
searched.add(id);
}
return DescriptorResolver.resolve(id, context, searched, context.getSelectorCoordinate(true));
}
/**
* Recursive function which checks an element and then recurses to parent
*/
private static Result resolve(String id, ISelectorContext context, Set<String> searched, String coordinate) {
searched.add(coordinate);
IAnnotationHandle annotation = Annotations.handleOf(context.getSelectorAnnotation());
// First check if there's a "desc" value on the current annotation
Result resolved = DescriptorResolver.resolve(id, context, searched, coordinate, annotation.getAnnotationList("desc"));
if (resolved != null) {
return resolved;
}
// Next check with the current coordinates on the owning method
resolved = DescriptorResolver.resolve(id, context, searched, coordinate, context.getMethod());
if (resolved != null) {
return resolved;
}
ISelectorContext root = DescriptorResolver.getRoot(context);
String rootCoordinate = root.getSelectorCoordinate(false);
String mixinCoordinate = !rootCoordinate.equals(coordinate) ? rootCoordinate + "." + coordinate : coordinate;
// Next check with the current coordinates on the mixin
resolved = DescriptorResolver.resolve(id, context, searched, mixinCoordinate, context.getMixin());
if (resolved != null) {
return resolved;
}
// Otherwise compute the parent coordinates and then recurse into the parent
ISelectorContext parent = context.getParent();
if (parent != null) {
String parentCoordinate = parent.getSelectorCoordinate(false) + "." + coordinate;
return DescriptorResolver.resolve(id, parent, searched, parentCoordinate);
}
// If all else fails, return failure
return new Result(searched, null);
}
/**
* Attempt to resolve on an element
*/
private static Result resolve(String id, ISelectorContext context, Set<String> searched, String coordinate, Object element) {
searched.add(coordinate);
IAnnotationHandle descriptors = DescriptorResolver.getVisibleAnnotation(element, Descriptors.class);
if (descriptors != null) {
Result resolved = DescriptorResolver.resolve(id, context, searched, coordinate, descriptors.getAnnotationList("value"));
if (resolved != null) {
return resolved;
}
}
IAnnotationHandle descriptor = DescriptorResolver.getVisibleAnnotation(element, Desc.class);
if (descriptor != null) {
Result resolved = DescriptorResolver.resolve(id, context, searched, coordinate, descriptor);
if (resolved != null) {
return resolved;
}
}
return null;
}
/**
* Attempt resolution using annotations from a particular element
*/
private static Result resolve(String id, ISelectorContext context, Set<String> searched, String coordinate,
List<IAnnotationHandle> availableDescriptors) {
if (availableDescriptors != null) {
searched.add(coordinate);
for (IAnnotationHandle desc : availableDescriptors) {
Result resolved = DescriptorResolver.resolve(id, context, searched, coordinate, desc);
if (resolved != null) {
return resolved;
}
}
}
return null;
}
/**
* Attempt resolution using an actual annotation
*/
private static Result resolve(String id, ISelectorContext context, Set<String> searched, String coordinate, IAnnotationHandle desc) {
if (desc != null) {
String descriptorId = desc.getValue("id", coordinate);
boolean implicit = Strings.isNullOrEmpty(id);
if ((implicit && descriptorId.equalsIgnoreCase(coordinate)) || (!implicit && descriptorId.equalsIgnoreCase(id))) {
return new Result(searched, desc);
}
}
return null;
}
/**
* Utility function, gets an annotation handle from the candidate elements
* we are able to process
*
* @param element Element to fetch annotations for
* @param annotationClass Annotation we're searching for
* @return Annotation handle or null if the source is null
* @throws IllegalStateException if element is of an unsupported type
*/
private static IAnnotationHandle getVisibleAnnotation(Object element, Class<? extends Annotation> annotationClass) {
if (element instanceof MethodNode) {
return Annotations.handleOf(Annotations.getVisible((MethodNode)element, annotationClass));
} else if (element instanceof ClassNode) {
return Annotations.handleOf(Annotations.getVisible((ClassNode)element, annotationClass));
} else if (element instanceof MixinTargetContext) {
return Annotations.handleOf(Annotations.getVisible(((MixinTargetContext)element).getClassNode(), annotationClass));
} else if (element instanceof IAnnotatedElement) {
return ((IAnnotatedElement)element).getAnnotation(annotationClass);
} else if (element == null) {
return null;
}
throw new IllegalStateException("Cannot read visible annotations from element with unknown type: " + element.getClass().getName());
}
private static ISelectorContext getRoot(ISelectorContext context) {
ISelectorContext parent = context.getParent();
while (parent != null) {
context = parent;
parent = context.getParent();
}
return context;
}
}
@@ -0,0 +1,266 @@
/*
* 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.selectors.dynamic;
import java.util.List;
import java.util.Set;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.spongepowered.asm.mixin.injection.Desc;
import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorDynamic.SelectorId;
import org.spongepowered.asm.mixin.injection.selectors.InvalidSelectorException;
import org.spongepowered.asm.mixin.injection.selectors.MatchResult;
import org.spongepowered.asm.mixin.injection.selectors.dynamic.DescriptorResolver.Result;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.asm.ElementNode;
import org.spongepowered.asm.util.asm.IAnnotationHandle;
/**
* A {@link ITargetSelector Target Selector} which matches candidates using
* descriptors contained in {@link Desc} annotations. The descriptor annotations
* may be local, or may be applied to the method or even the owning mixin using
* user-specified ids, or implicit coordinates.
*
* <p>TODO complete javadoc</p>
*/
@SelectorId("Desc")
public final class DynamicSelectorDesc extends DynamicSelectorAbstract {
/**
* The input string, only stored if the descriptor is invalid so we can emit
* it in the error message
*/
private final String input;
/**
* Searched coordinates, only stored if the descriptor is invalid so we can
* emit it in the error message
*/
private final Set<String> searched;
/**
* Resolved id
*/
private final String id;
/**
* The owner specified in the resolved {@link Desc} annotation, or null if
* resolution failed
*/
private final Type owner;
/**
* The name specified in the resolved {@link Desc} annotation, or null if
* resolution failed
*/
private final String name;
/**
* The arguments specified in the resolved {@link Desc} annotation, or null
* if resolution failed
*/
private final Type[] args;
/**
* The return type specified in the resolved {@link Desc} annotation, or
* null if resolution failed
*/
private final Type returnType;
/**
* Method descriptor, used for matching against methods. Computed from
* argument types and return type
*/
private final String methodDesc;
private DynamicSelectorDesc(String id, Type owner, String name, Type[] args, Type returnType) {
this(null, null, id, owner, name, args, returnType, Bytecode.getDescriptor(returnType, args));
}
private DynamicSelectorDesc(String input, Set<String> searched) {
this(input, searched, null, null, null, null, null, null);
}
private DynamicSelectorDesc(String input, Set<String> searched, String id, Type owner, String name, Type[] args, Type returnType, String desc) {
this.input = input;
this.searched = searched;
this.id = id;
this.owner = owner;
this.name = name;
this.args = args;
this.returnType = returnType;
this.methodDesc = desc;
}
/**
* Parse a descriptor selector from the supplied input. The input is treated
* as a descriptor id to be resolved, or if empty uses implicit coordinates
* from the selection context
*
* @param input ID string, can be empty
* @param context Selector context
* @return parsed selector
*/
public static DynamicSelectorDesc parse(String input, ISelectorContext context) {
Result result = DescriptorResolver.resolve(input, context);
if (!result.isResolved()) {
return new DynamicSelectorDesc(input, result.getSearched());
}
return DynamicSelectorDesc.of(context, result.getAnnotation());
}
/**
* Resolve a descriptor selector from the supplied context only, implicit
* coordinates are used.
*
* @param context Selector context
* @return parsed selector
*/
public static DynamicSelectorDesc resolve(ISelectorContext context) {
Result result = DescriptorResolver.resolve("", context);
if (!result.isResolved()) {
return null;
}
return DynamicSelectorDesc.of(context, result.getAnnotation());
}
/**
* Convert the supplied annotation into a selector instance
*
* @param context Selector context
* @param desc Annotation to parse
* @return selector
*/
public static DynamicSelectorDesc of(ISelectorContext context, IAnnotationHandle desc) {
return DynamicSelectorDesc.of(context.getMixin(), desc);
}
/**
* Convert the supplied annotation into a selector instance
*
* @param mixin Mixin context
* @param desc Annotation to parse
* @return selector
*/
public static DynamicSelectorDesc of(IMixinContext mixin, IAnnotationHandle desc) {
String id = desc.<String>getValue("id", "");
Type ownerClass = desc.<Type>getValue("owner", Type.VOID_TYPE);
Type owner = ownerClass == Type.VOID_TYPE ? Type.getObjectType(mixin.getTargetClassRef()) : ownerClass;
String name = desc.<String>getValue();
List<Type> args = desc.getTypeList("args");
Type returnType = desc.<Type>getValue("ret", Type.VOID_TYPE);
return new DynamicSelectorDesc(id, owner, name, args.toArray(new Type[args.size()]), returnType);
}
public String getId() {
return this.id;
}
public Type getOwner() {
return this.owner;
}
public String getName() {
return this.name;
}
public Type[] getArgs() {
return this.args;
}
public Type getReturnType() {
return this.returnType;
}
@Override
public ITargetSelector validate() throws InvalidSelectorException {
if (this.input != null) {
String extra = this.input.length() == 0 ? " searched in " + this.searched : "";
throw new InvalidSelectorException("Could not resolve @Desc(" + this.input + ")" + extra);
}
return this;
}
@Override
public ITargetSelector next() {
return this; // Recurse
}
@Override
public int getMatchCount() {
return 1;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.injection.selectors.ITargetSelector
* #match(org.spongepowered.asm.util.asm.ElementNode)
*/
@Override
public <TNode> MatchResult match(ElementNode<TNode> node) {
if (node == null) {
return MatchResult.NONE;
} else if (node.isMethod()) {
return this.matches(node.getOwnerName(), node.getName(), node.getDesc(), this.methodDesc);
} else {
return this.matches(node.getOwnerName(), node.getName(), node.getDesc(), this.returnType.getInternalName());
}
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.injection.selectors.ITargetSelector
* #matches(org.objectweb.asm.tree.AbstractInsnNode)
*/
@Override
public MatchResult match(AbstractInsnNode insn) {
if (insn instanceof MethodInsnNode) {
MethodInsnNode method = (MethodInsnNode)insn;
return this.matches(method.owner, method.name, method.desc, this.methodDesc);
} else if (insn instanceof FieldInsnNode) {
FieldInsnNode field = (FieldInsnNode)insn;
return this.matches(field.owner, field.name, field.desc, this.returnType.getInternalName());
}
return MatchResult.NONE;
}
private MatchResult matches(String owner, String name, String desc, String compareWithDesc) {
if (!compareWithDesc.equals(desc)) {
return MatchResult.NONE;
} else if (!this.owner.getInternalName().equals(owner)) {
return MatchResult.NONE;
} else if (this.name.equals(name)) {
return MatchResult.EXACT_MATCH;
} else if (this.name.equalsIgnoreCase(name)) {
return MatchResult.MATCH;
}
return MatchResult.NONE;
}
}
@@ -53,6 +53,7 @@ import org.spongepowered.asm.mixin.injection.code.MethodSlices;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.InvalidSelectorException;
import org.spongepowered.asm.mixin.injection.selectors.TargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.dynamic.DynamicSelectorDesc;
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
@@ -291,14 +292,9 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
}
protected Set<ITargetSelector> parseTargets() {
List<String> methods = Annotations.<String>getValue(this.annotation, "method", false);
if (methods == null) {
throw new InvalidInjectionException(this, String.format("%s annotation on %s is missing method name",
this.annotationType, this.methodName));
}
Set<ITargetSelector> selectors = new LinkedHashSet<ITargetSelector>();
for (String method : methods) {
for (String method : Annotations.<String>getValue(this.annotation, "method", false)) {
try {
selectors.add(TargetSelector.parseAndValidate(method, this).attach(this));
} catch (InvalidMemberDescriptorException ex) {
@@ -314,6 +310,21 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
ex.getMessage()));
}
}
for (AnnotationNode target : Annotations.<AnnotationNode>getValue(this.annotation, "target", false)) {
try {
selectors.add(DynamicSelectorDesc.of(this.mixin, Annotations.handleOf(target)).validate());
} catch (InvalidSelectorException ex) {
throw new InvalidInjectionException(this,
String.format("%s annotation on %s is decorated with an invalid selector: %s", this.annotationType, this.methodName,
ex.getMessage()));
}
}
if (selectors.size() == 0) {
throw new InvalidInjectionException(this, String.format("%s annotation on %s is missing 'method' or 'desc' to specify targets",
this.annotationType, this.methodName));
}
return selectors;
}
@@ -82,7 +82,8 @@ public class InjectionPointAnnotationContext extends SelectorAnnotationContext i
@Override
public String toString() {
return String.format("%s->@%s(%s)", this.parentContext, Bytecode.getSimpleName(this.getSelectorAnnotation()), this.getSelectorCoordinate());
return String.format("%s->@%s(%s)", this.parentContext, Bytecode.getSimpleName(this.getSelectorAnnotation()),
this.getSelectorCoordinate(false));
}
}
@@ -41,8 +41,10 @@ import org.spongepowered.asm.mixin.injection.modify.LocalVariableDiscriminator;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.InvalidSelectorException;
import org.spongepowered.asm.mixin.injection.selectors.TargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.dynamic.DynamicSelectorDesc;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionPointException;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.IMessageSink;
import com.google.common.base.Joiner;
@@ -278,6 +280,15 @@ public class InjectionPointData {
*/
public ITargetSelector getTarget() {
try {
if (Strings.isNullOrEmpty(this.target)) {
AnnotationNode desc = Annotations.<AnnotationNode>getValue(this.context.getSelectorAnnotation(), "desc");
if (desc != null) {
String id = Annotations.<String>getValue(desc, "id", "at");
if ("at".equalsIgnoreCase(id)) {
return DynamicSelectorDesc.of(this.context, Annotations.handleOf(desc));
}
}
}
return TargetSelector.parseAndValidate(this.target, this.context);
} catch (InvalidSelectorException ex) {
throw new InvalidInjectionPointException(this.getMixin(), ex, "Failed parsing @At(\"%s\").target \"%s\" on %s",
@@ -114,7 +114,7 @@ public class SelectorAnnotationContext implements ISelectorContext {
* #getSelectorCoordinate()
*/
@Override
public String getSelectorCoordinate() {
public String getSelectorCoordinate(boolean leaf) {
return this.selectorCoordinate;
}
@@ -98,6 +98,13 @@ public class AnnotatedMethodInfo implements IInjectionPointContext {
public final MethodNode getMethod() {
return this.method;
}
/**
* Get the original name of the method, if available
*/
public String getMethodName() {
return this.method.name;
}
/**
* Get the primary annotation which makes this method special
@@ -126,8 +133,8 @@ public class AnnotatedMethodInfo implements IInjectionPointContext {
* @return The selector context annotation
*/
@Override
public String getSelectorCoordinate() {
return "method";
public String getSelectorCoordinate(boolean leaf) {
return leaf ? "method" : this.getMethodName().toLowerCase();
}
/* (non-Javadoc)
@@ -84,6 +84,7 @@ public class SpecialMethodInfo extends AnnotatedMethodInfo {
/**
* Get the original name of the method, if available
*/
@Override
public String getMethodName() {
return this.methodName;
}
@@ -43,6 +43,7 @@ import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.util.asm.IAnnotationHandle;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
@@ -53,6 +54,73 @@ import com.google.common.collect.Lists;
*/
public final class Annotations {
/**
* Wrapper for AnnotationNode to support access via IAnnotationHandle
*/
public static class Handle implements IAnnotationHandle {
private final AnnotationNode annotation;
public Handle(AnnotationNode annotation) {
Preconditions.checkNotNull(annotation, "annotation");
this.annotation = annotation;
}
@Override
public String getDesc() {
return this.annotation.desc;
}
@Override
public List<IAnnotationHandle> getAnnotationList(String key) {
List<AnnotationNode> value = Annotations.<AnnotationNode>getValue(this.annotation, key, false);
List<IAnnotationHandle> list = new ArrayList<IAnnotationHandle>();
if (value != null) {
for (AnnotationNode node : value) {
list.add(new Handle(node));
}
}
return list;
}
@Override
public List<Type> getTypeList(String key) {
return this.<Type>getList(key);
}
@Override
public IAnnotationHandle getAnnotation(String key) {
AnnotationNode value = Annotations.<AnnotationNode>getValue(this.annotation, key);
return value != null ? new Handle(value) : null;
}
@Override
public <T> T getValue(String key, T defaultValue) {
return Annotations.<T>getValue(this.annotation, key, defaultValue);
}
@Override
public <T> T getValue() {
return this.getValue("value", null);
}
@Override
public <T> T getValue(String key) {
return this.getValue(key, null);
}
@Override
public <T> List<T> getList(String key) {
return Annotations.<T>getValue(this.annotation, key, false);
}
@Override
public String toString() {
return "@" + Bytecode.getSimpleName(this.annotation);
}
}
/**
* Annotations which are eligible for merge via {@link #mergeAnnotations}
*/
@@ -70,6 +138,24 @@ public final class Annotations {
private Annotations() {
// Utility class
}
/**
* Get the supplied annotation object as an annotation handle
*
* @param annotation Annotation to return
* @return Wrapped or cast annotation
*/
public static IAnnotationHandle handleOf(Object annotation) {
if (annotation instanceof IAnnotationHandle) {
return (IAnnotationHandle)annotation;
} else if (annotation instanceof AnnotationNode) {
return new Annotations.Handle((AnnotationNode)annotation);
} else if (annotation == null) {
return null;
} else {
throw new IllegalArgumentException("Unsupported annotation type: " + annotation.getClass().getName());
}
}
/**
* Set a runtime-visible annotation of the specified class on the supplied
@@ -646,6 +646,20 @@ public final class Bytecode {
}
}
/**
* Get an array of Types from an array of classes.
*
* @param classes Array of classes to convert
* @return Array of types
*/
public static Type[] getTypes(Class<?>... classes) {
Type[] types = new Type[classes.length];
for (int index = 0; index < classes.length; index++) {
types[index] = Type.getType(classes[index]);
}
return types;
}
/**
* Clones all of the labels in the source instruction list and returns the
* clones in a map of old label -&gt; new label. This is used to facilitate
@@ -0,0 +1,43 @@
/*
* 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.util.asm;
import java.lang.annotation.Annotation;
/**
* Interface for objects which can be annotated since main classes may need to
* read info from mirror annotations in the AP
*/
public interface IAnnotatedElement {
/**
* Get annotation from element
*
* @param annotationClass Annotation class to retrieve
* @return annotation handle
*/
public abstract IAnnotationHandle getAnnotation(Class<? extends Annotation> annotationClass);
}
@@ -24,6 +24,10 @@
*/
package org.spongepowered.asm.util.asm;
import java.util.List;
import org.objectweb.asm.Type;
/**
* Interface for annotation handle since main classes may need to read info from
* mirror annotations in the AP
@@ -35,4 +39,68 @@ public interface IAnnotationHandle {
*/
public abstract String getDesc();
/**
* Retrieve an annotation key as a list of annotation handles
*
* @param key key to fetch
* @return list of annotations
*/
public abstract List<IAnnotationHandle> getAnnotationList(String key);
/**
* Retrieve an annotation key as a list of annotation Types
*
* @param key key to fetch
* @return list of types
*/
public abstract List<Type> getTypeList(String key);
/**
* Get an annotation value as an annotation handle
*
* @param key key to search for in the value map
* @return value or <tt>null</tt> if not set
*/
public abstract IAnnotationHandle getAnnotation(String key);
/**
* Get a value with the specified key from this annotation, return the
* specified default value if the key is not set or is not present
*
* @param key key
* @param defaultValue value to return if the key is not set or not present
* @param <T> duck type
* @return value or default if not set
*/
public abstract <T> T getValue(String key, T defaultValue);
/**
* Get the annotation value or return null if not present or not set
*
* @param <T> duck type
* @return value or null if not present or not set
*/
public abstract <T> T getValue();
/**
* Get the annotation value with the specified key or return null if not
* present or not set
*
* @param key key to fetch
* @param <T> duck type
* @return value or null if not present or not set
*/
public abstract <T> T getValue(String key);
/**
* Retrieve the annotation value with the specified key as a list with
* values of the specified type. Returns an empty list if the value is not
* present or not set.
*
* @param key key to fetch
* @param <T> list element duck type
* @return list of values
*/
public abstract <T> List<T> getList(String key);
}