mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-19 22:19:34 +00:00
Add handling in the AP for soft implements annotations, closes #151
This commit is contained in:
1 parent
78434b5f11
commit
4934071ee7
9 files changed
+365
-33
No files matched your search
@@ -81,13 +81,21 @@ public abstract class MirrorUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public static <T> 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<? extends Enum>)defaultValue.getClass(), varValue.getSimpleName().toString());
|
||||
}
|
||||
|
||||
return value != null ? (T)value.getValue() : defaultValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<IMixinValidator> 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);
|
||||
}
|
||||
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* 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.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<AnnotationMirror> interfaces = MirrorUtils.<List<AnnotationMirror>>getAnnotationValue(implementsAnnotation, "value",
|
||||
Collections.<AnnotationMirror>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.<Remap>getAnnotationValue(interfaceAnnotation, "remap", Remap.ALL);
|
||||
if (remap == Remap.NONE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
TypeHandle iface = new TypeHandle(MirrorUtils.<DeclaredType>getAnnotationValue(interfaceAnnotation, "iface"));
|
||||
String prefix = MirrorUtils.<String>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.<ExecutableElement>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 <tt>ALL</tt>
|
||||
* 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<MappingMethod> 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<MappingMethod> applyPrefix(ObfuscationData<MappingMethod> data, String prefix) {
|
||||
if (prefix == null) {
|
||||
return data;
|
||||
}
|
||||
|
||||
ObfuscationData<MappingMethod> prefixed = new ObfuscationData<MappingMethod>();
|
||||
for (ObfuscationType type : data) {
|
||||
MappingMethod mapping = data.get(type);
|
||||
prefixed.add(type, mapping.addPrefix(prefix));
|
||||
}
|
||||
return prefixed;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
+20
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<? extends Element> getEnclosedElements() {
|
||||
return this.element != null ? this.element.getEnclosedElements() : Collections.<Element>emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns enclosed elements (methods, fields, etc.) of a particular type
|
||||
*
|
||||
* @param kind types of element to return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Element> List<T> getEnclosedElements(ElementKind... kind) {
|
||||
if (kind == null || kind.length < 1) {
|
||||
return (List<T>)this.getEnclosedElements();
|
||||
}
|
||||
|
||||
if (this.element == null) {
|
||||
return Collections.<T>emptyList();
|
||||
}
|
||||
|
||||
Builder<T> list = ImmutableList.<T>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<TypeHandle> getInterfaces() {
|
||||
if (this.element == null) {
|
||||
return Collections.<TypeHandle>emptyList();
|
||||
}
|
||||
|
||||
Builder<TypeHandle> list = ImmutableList.<TypeHandle>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.<ExecutableElement>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.<VariableElement>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.<ExecutableElement>getEnclosedElements(ElementKind.CONSTRUCTOR, ElementKind.METHOD)) {
|
||||
if (this.compareElement(method, name, signature, caseSensitive) || this.compareElement(method, name, rawSignature, caseSensitive)) {
|
||||
return new MethodHandle(method);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user