diff --git a/src/ap/java/org/spongepowered/tools/MirrorUtils.java b/src/ap/java/org/spongepowered/tools/MirrorUtils.java index 752b1438..f0772672 100644 --- a/src/ap/java/org/spongepowered/tools/MirrorUtils.java +++ b/src/ap/java/org/spongepowered/tools/MirrorUtils.java @@ -81,13 +81,21 @@ public abstract class MirrorUtils { return null; } - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public static T getAnnotationValue(AnnotationMirror annotation, String key, T defaultValue) { if (annotation == null) { return defaultValue; } AnnotationValue value = MirrorUtils.getAnnotationValue0(annotation, key); + if (defaultValue instanceof Enum && value != null) { + VariableElement varValue = (VariableElement)value.getValue(); + if (varValue == null) { + return defaultValue; + } + return (T)Enum.valueOf((Class)defaultValue.getClass(), varValue.getSimpleName().toString()); + } + return value != null ? (T)value.getValue() : defaultValue; } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 88418387..8c538940 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -134,6 +134,11 @@ class AnnotatedMixin { * Accessor handler */ private final AnnotatedMixinElementHandlerAccessor accessors; + + /** + * Soft implementation handler; + */ + private final AnnotatedMixinElementHandlerSoftImplements softImplements; public AnnotatedMixin(IMixinAnnotationProcessor ap, TypeElement type) { this.annotation = MirrorUtils.getAnnotation(type, Mixin.class); @@ -151,6 +156,7 @@ class AnnotatedMixin { this.shadows = new AnnotatedMixinElementHandlerShadow(ap, this); this.injectors = new AnnotatedMixinElementHandlerInjector(ap, this); this.accessors = new AnnotatedMixinElementHandlerAccessor(ap, this); + this.softImplements = new AnnotatedMixinElementHandlerSoftImplements(ap, this); } AnnotatedMixin runValidators(ValidationPass pass, Collection validators) { @@ -333,5 +339,9 @@ class AnnotatedMixin { public void registerInvoker(ExecutableElement element, AnnotationMirror invoker) { this.accessors.registerAccessor(new AnnotatedElementInvoker(element, invoker)); } + + public void registerSoftImplements(AnnotationMirror implementsAnnotation) { + this.softImplements.process(implementsAnnotation); + } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java new file mode 100644 index 00000000..ec91016f --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java @@ -0,0 +1,179 @@ +/* + * 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.tools.obfuscation; + +import java.util.Collections; +import java.util.List; + +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.type.DeclaredType; +import javax.tools.Diagnostic.Kind; + +import org.spongepowered.asm.mixin.Interface.Remap; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.MirrorUtils; +import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; + +/** + * A module for {@link AnnotatedMixin} whic handles soft-implements clauses + */ +public class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinElementHandler { + + AnnotatedMixinElementHandlerSoftImplements(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) { + super(ap, mixin); + } + + /** + * Process a soft-implements annotation on a mixin. This causes the + * interface declared in the annotation and all of its super-interfaces to + * be enumerated for member methods. Any member methods which are discovered + * in the mixin are then tested for remapability based on the strategy + * defined in the soft-implements decoration + * + * @param implementsAnnotation + */ + public void process(AnnotationMirror implementsAnnotation) { + List interfaces = MirrorUtils.>getAnnotationValue(implementsAnnotation, "value", + Collections.emptyList()); + + // Derp? + if (interfaces.size() < 1) { + this.ap.printMessage(Kind.WARNING, "Empty @Implements annotation", this.mixin.getMixin(), implementsAnnotation); + return; + } + + for (AnnotationMirror interfaceAnnotation : interfaces) { + Remap remap = MirrorUtils.getAnnotationValue(interfaceAnnotation, "remap", Remap.ALL); + if (remap == Remap.NONE) { + continue; + } + + try { + TypeHandle iface = new TypeHandle(MirrorUtils.getAnnotationValue(interfaceAnnotation, "iface")); + String prefix = MirrorUtils.getAnnotationValue(interfaceAnnotation, "prefix"); + this.processSoftImplements(remap, iface, prefix); + } catch (Exception ex) { + this.ap.printMessage(Kind.ERROR, "Unexpected error: " + ex.getClass().getName() + ": " + ex.getMessage(), this.mixin.getMixin(), + interfaceAnnotation); + } + } + } + + /** + * Recursive function which processes methods in an interface and its parent + * interfaces and adds mappings as necessary + * + * @param remap Remapping strategy to use + * @param iface Interface to enumerate + * @param prefix Prefix declared in the soft-implements decoration + */ + private void processSoftImplements(Remap remap, TypeHandle iface, String prefix) { + for (ExecutableElement method : iface.getEnclosedElements(ElementKind.METHOD)) { + this.processMethod(remap, iface, prefix, method); + } + + for (TypeHandle superInterface : iface.getInterfaces()) { + this.processSoftImplements(remap, superInterface, prefix); + } + } + + /** + * Process an interface method. Searches for the interface method with the + * declared prefix and also searches without prefix if the ALL + * strategy is selected + * + * @param remap Remapping strategy to use + * @param iface Interface to enumerate + * @param prefix Prefix declared in the soft-implements decoration + * @param method Interface method to search for + */ + private void processMethod(Remap remap, TypeHandle iface, String prefix, ExecutableElement method) { + String name = method.getSimpleName().toString(); + String sig = MirrorUtils.getJavaSignature(method); + String desc = MirrorUtils.getDescriptor(method); + + if (remap != Remap.ONLY_PREFIXED) { + MethodHandle mixinMethod = this.mixin.getHandle().findMethod(name, sig); + if (mixinMethod != null) { + this.addInterfaceMethodMapping(remap, iface, null, mixinMethod, name, desc); + } + } + + if (prefix != null) { + MethodHandle prefixedMixinMethod = this.mixin.getHandle().findMethod(prefix + name, sig); + if (prefixedMixinMethod != null) { + this.addInterfaceMethodMapping(remap, iface, prefix, prefixedMixinMethod, name, desc); + } + } + } + + /** + * Searches for obfuscation mappings for the specified interface method and + * adds mappings to the output set if obfuscation mappings are found for the + * method + * + * @param remap Remapping strategy + * @param iface Interface to enumerate + * @param prefix Prefix declared in the soft-implements decoration + * @param method Mixin method + * @param name Undecorated interface method name + * @param desc Interface method descriptor + */ + private void addInterfaceMethodMapping(Remap remap, TypeHandle iface, String prefix, MethodHandle method, String name, String desc) { + MappingMethod mapping = new MappingMethod(iface.getName(), name, desc); + ObfuscationData obfData = this.obf.getDataProvider().getObfMethod(mapping); + if (obfData.isEmpty()) { + if (remap.forceRemap()) { + this.ap.printMessage(Kind.ERROR, "No obfuscation mapping for soft-implementing method", method.getElement()); + } + return; + } + this.addMethodMappings(method.getName(), desc, this.applyPrefix(obfData, prefix)); + } + + /** + * Apply the specified name prefix to all mappings in an obfuscation data + * set + * + * @param data input data + * @param prefix prefix to apply + * @return modified mapping set or original mapping set if prefix is null + */ + private ObfuscationData applyPrefix(ObfuscationData data, String prefix) { + if (prefix == null) { + return data; + } + + ObfuscationData prefixed = new ObfuscationData(); + for (ObfuscationType type : data) { + MappingMethod mapping = data.get(type); + prefixed.add(type, mapping.addPrefix(prefix)); + } + return prefixed; + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index e247d7da..3476e279 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -147,10 +147,11 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp * Private constructor, get instances using {@link #getMixinsForEnvironment} */ private AnnotatedMixins(ProcessingEnvironment processingEnv) { - this.printMessage(Kind.NOTE, "SpongePowered Mixin Annotation Processor v" + MixinBootstrap.VERSION); - this.env = this.detectEnvironment(processingEnv); this.processingEnv = processingEnv; + + this.printMessage(Kind.NOTE, "SpongePowered MIXIN Annotation Processor Version=" + MixinBootstrap.VERSION); + this.targets = this.initTargetMap(); this.obf = new ObfuscationManager(this); this.obf.init(); @@ -479,6 +480,24 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp } } + /** + * Register an {@link org.spongepowered.asm.mixin.Implements} declaration on + * a mixin class + * + * @param mixin Annotated mixin + * @param implementsAnnotation + * {@link org.spongepowered.asm.mixin.Implements} annotation + */ + public void registerSoftImplements(TypeElement mixin, AnnotationMirror implementsAnnotation) { + AnnotatedMixin mixinClass = this.getMixin(mixin); + if (mixinClass == null) { + this.printMessage(Kind.ERROR, "Found @Implements annotation on a non-mixin class"); + return; + } + + mixinClass.registerSoftImplements(implementsAnnotation); + } + /** * Called from each AP class to notify the environment that a new pass is * starting diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java index 2b952f8f..6fde4c49 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java @@ -42,6 +42,10 @@ class MethodHandle { public MethodHandle(ExecutableElement element) { this.element = element; } + + public String getName() { + return this.element != null ? this.element.getSimpleName().toString() : null; + } /** * Get whether the element is imaginary (inaccessible via mirror) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java index 8eb7f9a3..af9bf312 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java @@ -36,6 +36,7 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.tools.Diagnostic.Kind; +import org.spongepowered.asm.mixin.Implements; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.gen.Accessor; @@ -50,7 +51,8 @@ import org.spongepowered.tools.MirrorUtils; "org.spongepowered.asm.mixin.Mixin", "org.spongepowered.asm.mixin.Shadow", "org.spongepowered.asm.mixin.Overwrite", - "org.spongepowered.asm.mixin.gen.Accessor" + "org.spongepowered.asm.mixin.gen.Accessor", + "org.spongepowered.asm.mixin.Implements" }) public class MixinObfuscationProcessorTargets extends MixinObfuscationProcessor { @@ -70,6 +72,7 @@ public class MixinObfuscationProcessorTargets extends MixinObfuscationProcessor this.processOverwrites(roundEnv); this.processAccessors(roundEnv); this.processInvokers(roundEnv); + this.processImplements(roundEnv); this.postProcess(roundEnv); return true; @@ -170,4 +173,20 @@ public class MixinObfuscationProcessorTargets extends MixinObfuscationProcessor } } } + + /** + * Searches for {@link Implements} annotations and registers them with their + * parent mixins + */ + private void processImplements(RoundEnvironment roundEnv) { + for (Element elem : roundEnv.getElementsAnnotatedWith(Implements.class)) { + if (elem.getKind() == ElementKind.CLASS || elem.getKind() == ElementKind.INTERFACE) { + AnnotationMirror implementsAnnotation = MirrorUtils.getAnnotation(elem, Implements.class); + this.mixins.registerSoftImplements((TypeElement)elem, implementsAnnotation); + } else { + this.mixins.printMessage(Kind.ERROR, "Found an @Implements annotation on an element which is not a class or interface", elem); + } + } + } + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java index 00cf471b..63dfd818 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java @@ -41,6 +41,9 @@ import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.tools.MirrorUtils; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + /** * A wrapper for TypeElement which gives us a soft-failover mechanism when * dealing with classes that are inaccessible via mirror (such as anonymous @@ -137,6 +140,34 @@ public class TypeHandle { public List getEnclosedElements() { return this.element != null ? this.element.getEnclosedElements() : Collections.emptyList(); } + + /** + * Returns enclosed elements (methods, fields, etc.) of a particular type + * + * @param kind types of element to return + */ + @SuppressWarnings("unchecked") + public List getEnclosedElements(ElementKind... kind) { + if (kind == null || kind.length < 1) { + return (List)this.getEnclosedElements(); + } + + if (this.element == null) { + return Collections.emptyList(); + } + + Builder list = ImmutableList.builder(); + for (Element elem : this.element.getEnclosedElements()) { + for (ElementKind ek : kind) { + if (elem.getKind() == ek) { + list.add((T)elem); + break; + } + } + } + + return list.build(); + } /** * Returns the enclosed element as a type mirror, or null if this is an @@ -162,6 +193,19 @@ public class TypeHandle { return new TypeHandle((DeclaredType)superClass); } + + public List getInterfaces() { + if (this.element == null) { + return Collections.emptyList(); + } + + Builder list = ImmutableList.builder(); + for (TypeMirror iface : this.element.getInterfaces()) { + list.add(new TypeHandle((DeclaredType)iface)); + } + + return list.build(); + } /** * Get whether the element is probably public @@ -187,13 +231,9 @@ public class TypeHandle { public String findDescriptor(MemberInfo memberInfo) { String desc = memberInfo.desc; if (desc == null) { - for (Element child : this.getEnclosedElements()) { - if (child.getKind() != ElementKind.METHOD) { - continue; - } - - if (child.getSimpleName().toString().equals(memberInfo.name)) { - desc = MirrorUtils.generateSignature((ExecutableElement)child); + for (ExecutableElement method : this.getEnclosedElements(ElementKind.METHOD)) { + if (method.getSimpleName().toString().equals(memberInfo.name)) { + desc = MirrorUtils.generateSignature(method); break; } } @@ -246,12 +286,7 @@ public class TypeHandle { public FieldHandle findField(String name, String type, boolean caseSensitive) { String rawType = MirrorUtils.stripGenerics(type); - for (Element element : this.getEnclosedElements()) { - if (element.getKind() != ElementKind.FIELD) { - continue; - } - - VariableElement field = (VariableElement)element; + for (VariableElement field : this.getEnclosedElements(ElementKind.FIELD)) { if (this.compareElement(field, name, type, caseSensitive)) { return new FieldHandle(field); } else if (this.compareElement(field, name, rawType, caseSensitive)) { @@ -307,23 +342,10 @@ public class TypeHandle { public MethodHandle findMethod(String name, String signature, boolean caseSensitive) { String rawSignature = MirrorUtils.stripGenerics(signature); - for (Element element : this.getEnclosedElements()) { - switch (element.getKind()) { - case CONSTRUCTOR: - case METHOD: - ExecutableElement method = (ExecutableElement)element; - if (this.compareElement(method, name, signature, caseSensitive) - || this.compareElement(method, name, rawSignature, caseSensitive)) { - return new MethodHandle(method); - } - - break; -// case STATIC_INIT: // TODO? -// break; - default: - break; + for (ExecutableElement method : this.getEnclosedElements(ElementKind.CONSTRUCTOR, ElementKind.METHOD)) { + if (this.compareElement(method, name, signature, caseSensitive) || this.compareElement(method, name, rawSignature, caseSensitive)) { + return new MethodHandle(method); } - } return null; diff --git a/src/main/java/org/spongepowered/asm/mixin/Interface.java b/src/main/java/org/spongepowered/asm/mixin/Interface.java index 791c9286..3162bdd2 100644 --- a/src/main/java/org/spongepowered/asm/mixin/Interface.java +++ b/src/main/java/org/spongepowered/asm/mixin/Interface.java @@ -36,6 +36,55 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.CLASS) public @interface Interface { + /** + * Describes the remapping strategy applied to methods matching this + * interface. + */ + public enum Remap { + + /** + * Attempt to remap all members of this interface which are declared in + * the annotated mixin, including non-prefixed methods which match. + */ + ALL, + + /** + * Attempt to remap all members of this interface which are declared in + * the annotated mixin, including non-prefixed methods which match. + * If mappings are not located for a member method, raise a compile-time + * error. + */ + FORCE(true), + + /** + * Remap only methods in the annotated mixin which are prefixed with the + * declared prefix. Note that if no prefix is defined, this has the same + * effect as {@link #NONE} + */ + ONLY_PREFIXED, + + /** + * Do not remap members matching this interface. (Equivalent to + * remap=false on other remappable annotations) + */ + NONE; + + private final boolean forceRemap; + + private Remap() { + this(false); + } + + private Remap(boolean forceRemap) { + this.forceRemap = forceRemap; + } + + public boolean forceRemap() { + return this.forceRemap; + } + + } + /** * Interface that the parent {@link Implements} indicates the mixin * implements. The interface will be hot-patched onto the target class as @@ -60,5 +109,19 @@ public @interface Interface { * @return true to mark all implementing methods as unique */ public boolean unique() default false; + + /** + * By default, the annotation processor will attempt to locate an + * obfuscation mapping for all methods soft-implemented by the interface + * declared in this {@link Interface} annotation, since it is possible that + * the declared interface may be obfuscated and therefore contain obfuscated + * member methods. However since it may be desirable to skip this pass (for + * example if an interface method intrinsically shadows a soft-implemented + * method) this setting is provided to restrict or inhibit processing of + * member methods matching this soft-implements decoration. + * + * @return Remapping strategy to use, see {@link Remap} for details. + */ + public Remap remap() default Remap.ALL; } diff --git a/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingMethod.java b/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingMethod.java index 051925ca..75e8c9df 100644 --- a/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingMethod.java +++ b/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingMethod.java @@ -95,6 +95,14 @@ public class MappingMethod implements IMapping { return new MappingMethod(this.getOwner(), this.getSimpleName(), this.getDesc()); } + public MappingMethod addPrefix(String prefix) { + String simpleName = this.getSimpleName(); + if (simpleName == null || simpleName.startsWith(prefix)) { + return this; + } + return new MappingMethod(this.getOwner(), prefix + simpleName, this.getDesc()); + } + @Override public int hashCode() { return Objects.hashCode(this.getName(), this.getDesc());