From 21881482ae7d088bda57c9dfe93da4068bc41970 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Sun, 31 Jan 2021 22:49:08 +0000 Subject: [PATCH] WIP but mostly working Desc selector functionality --- .../tools/obfuscation/AnnotatedMixin.java | 31 +- .../AnnotatedMixinElementHandler.java | 21 +- ...atedMixinElementHandlerSoftImplements.java | 7 +- .../obfuscation/mirror/AnnotationHandle.java | 31 +- .../spongepowered/asm/mixin/injection/At.java | 6 + .../asm/mixin/injection/Desc.java | 51 ++++ .../asm/mixin/injection/Descriptors.java | 45 +++ .../asm/mixin/injection/Inject.java | 10 +- .../asm/mixin/injection/ModifyArg.java | 10 +- .../asm/mixin/injection/ModifyArgs.java | 10 +- .../asm/mixin/injection/ModifyConstant.java | 10 +- .../asm/mixin/injection/ModifyVariable.java | 10 +- .../asm/mixin/injection/Redirect.java | 10 +- .../injection/selectors/ISelectorContext.java | 5 +- .../injection/selectors/TargetSelector.java | 8 +- .../selectors/dynamic/DescriptorResolver.java | 245 ++++++++++++++++ .../dynamic/DynamicSelectorDesc.java | 266 ++++++++++++++++++ .../mixin/injection/struct/InjectionInfo.java | 25 +- .../InjectionPointAnnotationContext.java | 3 +- .../injection/struct/InjectionPointData.java | 11 + .../struct/SelectorAnnotationContext.java | 2 +- .../asm/mixin/struct/AnnotatedMethodInfo.java | 11 +- .../asm/mixin/struct/SpecialMethodInfo.java | 1 + .../spongepowered/asm/util/Annotations.java | 86 ++++++ .../org/spongepowered/asm/util/Bytecode.java | 14 + .../asm/util/asm/IAnnotatedElement.java | 43 +++ .../asm/util/asm/IAnnotationHandle.java | 68 +++++ 27 files changed, 998 insertions(+), 42 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/Desc.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/Descriptors.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java create mode 100644 src/main/java/org/spongepowered/asm/util/asm/IAnnotatedElement.java diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 5713f306..2143f37d 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -24,6 +24,7 @@ */ package org.spongepowered.tools.obfuscation; +import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -47,6 +48,8 @@ import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.refmap.IMixinContext; import org.spongepowered.asm.mixin.refmap.ReferenceMapper; import org.spongepowered.asm.mixin.transformer.ext.Extensions; +import org.spongepowered.asm.util.asm.IAnnotatedElement; +import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementAccessor; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementInvoker; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementInjectionPoint; @@ -70,7 +73,7 @@ import com.google.common.base.Strings; /** * Information about a mixin stored during processing */ -class AnnotatedMixin implements IMixinContext { +class AnnotatedMixin implements IMixinContext, IAnnotatedElement { /** * Mixin annotation @@ -386,13 +389,13 @@ class AnnotatedMixin implements IMixinContext { AnnotatedElementInjector injectorElement = new AnnotatedElementInjector(method, inject, this, remap); this.injectors.registerInjector(injectorElement); - List ats = inject.getAnnotationList("at"); - for (AnnotationHandle at : ats) { - this.registerInjectionPoint(method, inject, "at", at, remap, "@At(%s)"); + List ats = inject.getAnnotationList("at"); + for (IAnnotationHandle at : ats) { + this.registerInjectionPoint(method, inject, "at", (AnnotationHandle)at, remap, "@At(%s)"); } - List slices = inject.getAnnotationList("slice"); - for (AnnotationHandle slice : slices) { + List slices = inject.getAnnotationList("slice"); + for (IAnnotationHandle slice : slices) { String id = slice.getValue("id", ""); String coord = "slice"; if (!Strings.isNullOrEmpty(id)) { @@ -400,14 +403,15 @@ class AnnotatedMixin implements IMixinContext { } SelectorAnnotationContext sliceContext = new SelectorAnnotationContext(injectorElement, slice, coord); - AnnotationHandle from = slice.getAnnotation("from"); + IAnnotationHandle from = slice.getAnnotation("from"); if (from != null) { - this.registerSliceInjectionPoint(method, inject, "from", from, remap, "@Slice[" + id + "](from=@At(%s))", sliceContext); + this.registerSliceInjectionPoint(method, inject, "from", (AnnotationHandle)from, remap, "@Slice[" + id + "](from=@At(%s))", + sliceContext); } - AnnotationHandle to = slice.getAnnotation("to"); + IAnnotationHandle to = slice.getAnnotation("to"); if (to != null) { - this.registerSliceInjectionPoint(method, inject, "to", to, remap, "@Slice[" + id + "](to=@At(%s))", sliceContext); + this.registerSliceInjectionPoint(method, inject, "to", (AnnotationHandle)to, remap, "@Slice[" + id + "](to=@At(%s))", sliceContext); } } } @@ -449,7 +453,7 @@ class AnnotatedMixin implements IMixinContext { @Override public String getTargetClassRef() { - throw new UnsupportedOperationException("Target class not available at compile time"); + return this.primaryTarget.getName(); } @Override @@ -476,5 +480,10 @@ class AnnotatedMixin implements IMixinContext { public Target getTargetMethod(MethodNode into) { throw new UnsupportedOperationException("Target not available at compile time"); } + + @Override + public IAnnotationHandle getAnnotation(Class annotationClass) { + return AnnotationHandle.of(this.mixin, annotationClass); + } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java index 433fba93..e571eadf 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java @@ -24,6 +24,7 @@ */ package org.spongepowered.tools.obfuscation; +import java.lang.annotation.Annotation; import java.util.List; import javax.annotation.processing.Messager; @@ -42,6 +43,8 @@ import org.spongepowered.asm.obfuscation.mapping.common.MappingField; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.ConstraintParser; import org.spongepowered.asm.util.ConstraintParser.Constraint; +import org.spongepowered.asm.util.asm.IAnnotatedElement; +import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.asm.util.throwables.ConstraintViolationException; import org.spongepowered.asm.util.throwables.InvalidConstraintException; import org.spongepowered.tools.obfuscation.interfaces.IMessagerSuppressible; @@ -66,7 +69,7 @@ abstract class AnnotatedMixinElementHandler { * * @param type of inner element */ - abstract static class AnnotatedElement { + abstract static class AnnotatedElement implements IAnnotatedElement { protected final E element; @@ -104,6 +107,11 @@ abstract class AnnotatedMixinElementHandler { messager.printMessage(kind, msg, this.element, this.annotation.asMirror(), suppressedBy); } + @Override + public IAnnotationHandle getAnnotation(Class annotationClass) { + return AnnotationHandle.of(this.element, annotationClass); + } + } abstract static class AnnotatedElementExecutable extends AnnotatedElement implements ISelectorContext { @@ -130,7 +138,12 @@ abstract class AnnotatedMixinElementHandler { @Override public Object getMethod() { - return this.getElement(); + return new IAnnotatedElement() { + @Override + public IAnnotationHandle getAnnotation(Class annotationClass) { + return AnnotationHandle.of(AnnotatedElementExecutable.this.getElement(), annotationClass); + } + }; } @Override @@ -139,8 +152,8 @@ abstract class AnnotatedMixinElementHandler { } @Override - public String getSelectorCoordinate() { - return this.selectorCoordinate; + public String getSelectorCoordinate(boolean leaf) { + return leaf ? this.selectorCoordinate : TypeUtils.getName(this.element); } @Override diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java index 330494ed..c3c51553 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java @@ -33,6 +33,7 @@ import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.Interface.Remap; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; import org.spongepowered.tools.obfuscation.mirror.MethodHandle; @@ -63,7 +64,7 @@ public class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinEl return; } - List interfaces = implementsAnnotation.getAnnotationList("value"); + List interfaces = implementsAnnotation.getAnnotationList("value"); // Derp? if (interfaces.size() < 1) { @@ -71,7 +72,7 @@ public class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinEl return; } - for (AnnotationHandle interfaceAnnotation : interfaces) { + for (IAnnotationHandle interfaceAnnotation : interfaces) { Remap remap = interfaceAnnotation.getValue("remap", Remap.ALL); if (remap == Remap.NONE) { continue; @@ -83,7 +84,7 @@ public class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinEl this.processSoftImplements(remap, iface, prefix); } catch (Exception ex) { this.ap.printMessage(Kind.ERROR, "Unexpected error: " + ex.getClass().getName() + ": " + ex.getMessage(), - this.mixin.getMixinElement(), interfaceAnnotation.asMirror()); + this.mixin.getMixinElement(), ((AnnotationHandle)interfaceAnnotation).asMirror()); } } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java index 469226cf..63890786 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java @@ -28,6 +28,7 @@ import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.ListIterator; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; @@ -35,7 +36,9 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; +import javax.lang.model.type.TypeMirror; +import org.objectweb.asm.Type; import org.spongepowered.asm.util.asm.IAnnotationHandle; import com.google.common.collect.ImmutableList; @@ -109,6 +112,7 @@ public final class AnnotationHandle implements IAnnotationHandle { * @return value or default if not set * @param duck type */ + @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public T getValue(String key, T defaultValue) { if (this.annotation == null) { @@ -133,6 +137,7 @@ public final class AnnotationHandle implements IAnnotationHandle { * @param duck type * @return value or null if not present or not set */ + @Override public T getValue() { return this.getValue("value", null); } @@ -145,6 +150,7 @@ public final class AnnotationHandle implements IAnnotationHandle { * @param duck type * @return value or null if not present or not set */ + @Override public T getValue(String key) { return this.getValue(key, null); } @@ -167,7 +173,8 @@ public final class AnnotationHandle implements IAnnotationHandle { * @param key key to search for in the value map * @return value or null if not set */ - public AnnotationHandle getAnnotation(String key) { + @Override + public IAnnotationHandle getAnnotation(String key) { Object value = this.getValue(key); if (value instanceof AnnotationMirror) { return AnnotationHandle.of((AnnotationMirror)value); @@ -200,6 +207,7 @@ public final class AnnotationHandle implements IAnnotationHandle { * @param list element duck type * @return list of values */ + @Override public List getList(String key) { List list = this.>getValue(key, Collections.emptyList()); return AnnotationHandle.unwrapAnnotationValueList(list); @@ -211,15 +219,16 @@ public final class AnnotationHandle implements IAnnotationHandle { * @param key key to fetch * @return list of annotations */ - public List getAnnotationList(String key) { + @Override + public List getAnnotationList(String key) { Object val = this.getValue(key, null); if (val == null) { - return Collections.emptyList(); + return Collections.emptyList(); } // Fix for JDT, single values are just returned as a bare AnnotationMirror if (val instanceof AnnotationMirror) { - return ImmutableList.of(AnnotationHandle.of((AnnotationMirror)val)); + return ImmutableList.of(AnnotationHandle.of((AnnotationMirror)val)); } @SuppressWarnings("unchecked") List list = (List)val; @@ -227,7 +236,19 @@ public final class AnnotationHandle implements IAnnotationHandle { for (AnnotationValue value : list) { annotations.add(new AnnotationHandle((AnnotationMirror)value.getValue())); } - return Collections.unmodifiableList(annotations); + return Collections.unmodifiableList(annotations); + } + + @Override + public List getTypeList(String key) { + List list = this.getList(key); + for (ListIterator iter = list.listIterator(); iter.hasNext();) { + Object next = iter.next(); + if (next instanceof TypeMirror) { + iter.set(Type.getType(TypeUtils.getInternalName((TypeMirror)next))); + } + } + return list; } protected AnnotationValue getAnnotationValue(String key) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/At.java b/src/main/java/org/spongepowered/asm/mixin/injection/At.java index 789843a5..6136d580 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/At.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/At.java @@ -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 ordinal allows a particular opcode to diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Desc.java b/src/main/java/org/spongepowered/asm/mixin/injection/Desc.java new file mode 100644 index 00000000..a61e809f --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Desc.java @@ -0,0 +1,51 @@ +/* + * 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 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 { }; + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Descriptors.java b/src/main/java/org/spongepowered/asm/mixin/injection/Descriptors.java new file mode 100644 index 00000000..6bb3a8fc --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Descriptors.java @@ -0,0 +1,45 @@ +/* + * 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 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(); + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java index 1fcaf47a..d3122829 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java @@ -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 @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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java index 645a60bf..fe9ffe82 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java @@ -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 @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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java index 69018c32..bb702003 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java @@ -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 @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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java index 6e7ba332..9870a40e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java @@ -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 @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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java index 83acc4fe..d8f76088 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java @@ -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 @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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java index 6df4b87f..3bca4255 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java @@ -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 @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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ISelectorContext.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ISelectorContext.java index 9178198e..0652aa14 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ISelectorContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ISelectorContext.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java index 39640c98..ccddc13a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java @@ -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 dynamicSelectors = new LinkedHashMap(); + 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) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java new file mode 100644 index 00000000..d7b18da0 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java @@ -0,0 +1,245 @@ +/* + * 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.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 searched; + + /** + * The resolved descriptor + */ + private final IAnnotationHandle desc; + + Result(Set 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 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 searched = new LinkedHashSet(); + 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 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 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 searched, String coordinate, + List 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 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 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; + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java new file mode 100644 index 00000000..9b05de8b --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java @@ -0,0 +1,266 @@ +/* + * 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.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. + * + *

TODO complete javadoc

+ */ +@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 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 searched) { + this(input, searched, null, null, null, null, null, null); + } + + private DynamicSelectorDesc(String input, Set 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.getValue("id", ""); + Type ownerClass = desc.getValue("owner", Type.VOID_TYPE); + Type owner = ownerClass == Type.VOID_TYPE ? Type.getObjectType(mixin.getTargetClassRef()) : ownerClass; + String name = desc.getValue(); + List args = desc.getTypeList("args"); + Type returnType = desc.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 MatchResult match(ElementNode 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; + } + +} 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 e94cf747..9c4e0280 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 @@ -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 parseTargets() { - List methods = Annotations.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 selectors = new LinkedHashSet(); - for (String method : methods) { + + for (String method : Annotations.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.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; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointAnnotationContext.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointAnnotationContext.java index 1228ad64..66815fa8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointAnnotationContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointAnnotationContext.java @@ -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)); } } 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 edded14b..80f95927 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 @@ -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.getValue(this.context.getSelectorAnnotation(), "desc"); + if (desc != null) { + String id = Annotations.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", diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/SelectorAnnotationContext.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/SelectorAnnotationContext.java index 3d1d2f8f..ce959e8e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/SelectorAnnotationContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/SelectorAnnotationContext.java @@ -114,7 +114,7 @@ public class SelectorAnnotationContext implements ISelectorContext { * #getSelectorCoordinate() */ @Override - public String getSelectorCoordinate() { + public String getSelectorCoordinate(boolean leaf) { return this.selectorCoordinate; } diff --git a/src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java b/src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java index 07aba734..4ce05960 100644 --- a/src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java @@ -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) diff --git a/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java b/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java index 09344f69..181feffa 100644 --- a/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java @@ -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; } diff --git a/src/main/java/org/spongepowered/asm/util/Annotations.java b/src/main/java/org/spongepowered/asm/util/Annotations.java index e50725f7..892317df 100644 --- a/src/main/java/org/spongepowered/asm/util/Annotations.java +++ b/src/main/java/org/spongepowered/asm/util/Annotations.java @@ -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 getAnnotationList(String key) { + List value = Annotations.getValue(this.annotation, key, false); + List list = new ArrayList(); + if (value != null) { + for (AnnotationNode node : value) { + list.add(new Handle(node)); + } + } + return list; + } + + @Override + public List getTypeList(String key) { + return this.getList(key); + } + + @Override + public IAnnotationHandle getAnnotation(String key) { + AnnotationNode value = Annotations.getValue(this.annotation, key); + return value != null ? new Handle(value) : null; + } + + @Override + public T getValue(String key, T defaultValue) { + return Annotations.getValue(this.annotation, key, defaultValue); + } + + @Override + public T getValue() { + return this.getValue("value", null); + } + + @Override + public T getValue(String key) { + return this.getValue(key, null); + } + + @Override + public List getList(String key) { + return Annotations.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 diff --git a/src/main/java/org/spongepowered/asm/util/Bytecode.java b/src/main/java/org/spongepowered/asm/util/Bytecode.java index 0bc9e7a2..66f877b7 100644 --- a/src/main/java/org/spongepowered/asm/util/Bytecode.java +++ b/src/main/java/org/spongepowered/asm/util/Bytecode.java @@ -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 -> new label. This is used to facilitate diff --git a/src/main/java/org/spongepowered/asm/util/asm/IAnnotatedElement.java b/src/main/java/org/spongepowered/asm/util/asm/IAnnotatedElement.java new file mode 100644 index 00000000..0a450969 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/asm/IAnnotatedElement.java @@ -0,0 +1,43 @@ +/* + * 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.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 annotationClass); + +} diff --git a/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java b/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java index 15348b4c..2695337c 100644 --- a/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java +++ b/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java @@ -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 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 getTypeList(String key); + + /** + * Get an annotation value as an annotation handle + * + * @param key key to search for in the value map + * @return value or null 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 duck type + * @return value or default if not set + */ + public abstract T getValue(String key, T defaultValue); + + /** + * Get the annotation value or return null if not present or not set + * + * @param duck type + * @return value or null if not present or not set + */ + public abstract 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 duck type + * @return value or null if not present or not set + */ + public abstract 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 list element duck type + * @return list of values + */ + public abstract List getList(String key); + }