Add support for dynamic and pattern target selectors

This commit is contained in:
Mumfrey committed 2021-01-23 21:19:13 +00:00
1 parent 922eacb357
commit 08d6c7067d
42 files changed
+1472 -237

No files matched your search

@@ -36,11 +36,21 @@ import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic.Kind;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext;
import org.spongepowered.asm.mixin.injection.struct.SelectorAnnotationContext;
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.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementAccessor;
import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementInvoker;
import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementInjectionPoint;
import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementSliceInjectionPoint;
import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementInjector;
import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerOverwrite.AnnotatedElementOverwrite;
import org.spongepowered.tools.obfuscation.interfaces.IMessagerSuppressible;
@@ -55,10 +65,12 @@ import org.spongepowered.tools.obfuscation.mirror.TypeHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils;
import org.spongepowered.tools.obfuscation.struct.InjectorRemap;
import com.google.common.base.Strings;
/**
* Information about a mixin stored during processing
*/
class AnnotatedMixin {
class AnnotatedMixin implements IMixinContext {
/**
* Mixin annotation
@@ -289,7 +301,7 @@ class AnnotatedMixin {
/**
* Get the mixin class
*/
public TypeElement getMixin() {
public TypeElement getMixinElement() {
return this.mixin;
}
@@ -303,6 +315,7 @@ class AnnotatedMixin {
/**
* Get the mixin class reference
*/
@Override
public String getClassRef() {
return this.classRef;
}
@@ -368,46 +381,100 @@ class AnnotatedMixin {
}
public void registerInjector(ExecutableElement method, AnnotationHandle inject, InjectorRemap remap) {
System.err.printf(">>> %s\n", method);
this.methods.remove(method);
this.injectors.registerInjector(new AnnotatedElementInjector(method, inject, remap));
AnnotatedElementInjector injectorElement = new AnnotatedElementInjector(method, inject, this, remap);
this.injectors.registerInjector(injectorElement);
List<AnnotationHandle> ats = inject.getAnnotationList("at");
for (AnnotationHandle at : ats) {
this.registerInjectionPoint(method, inject, at, remap, "@At(%s)");
this.registerInjectionPoint(method, inject, "at", at, remap, "@At(%s)");
}
List<AnnotationHandle> slices = inject.getAnnotationList("slice");
for (AnnotationHandle slice : slices) {
String id = slice.<String>getValue("id", "");
String coord = "slice";
if (!Strings.isNullOrEmpty(id)) {
coord += "." + id;
}
SelectorAnnotationContext sliceContext = new SelectorAnnotationContext(injectorElement, slice, coord);
AnnotationHandle from = slice.getAnnotation("from");
if (from != null) {
this.registerInjectionPoint(method, inject, from, remap, "@Slice[" + id + "](from=@At(%s))");
this.registerSliceInjectionPoint(method, inject, "from", from, remap, "@Slice[" + id + "](from=@At(%s))", sliceContext);
}
AnnotationHandle to = slice.getAnnotation("to");
if (to != null) {
this.registerInjectionPoint(method, inject, to, remap, "@Slice[" + id + "](to=@At(%s))");
this.registerSliceInjectionPoint(method, inject, "to", to, remap, "@Slice[" + id + "](to=@At(%s))", sliceContext);
}
}
}
public void registerInjectionPoint(ExecutableElement element, AnnotationHandle inject, AnnotationHandle at, InjectorRemap remap, String format) {
this.injectors.registerInjectionPoint(new AnnotatedElementInjectionPoint(element, inject, at, remap), format);
public void registerInjectionPoint(ExecutableElement element, AnnotationHandle inject, String selectorCoordinate, AnnotationHandle at,
InjectorRemap remap, String format) {
this.injectors.registerInjectionPoint(new AnnotatedElementInjectionPoint(element, inject, this, selectorCoordinate, at, remap), format);
}
public void registerSliceInjectionPoint(ExecutableElement element, AnnotationHandle inject, String selectorCoordinate, AnnotationHandle at,
InjectorRemap remap, String format, ISelectorContext parentContext) {
this.injectors.registerInjectionPoint(new AnnotatedElementSliceInjectionPoint(element, inject, this, selectorCoordinate, at, remap,
parentContext), format);
}
public void registerAccessor(ExecutableElement element, AnnotationHandle accessor, boolean shouldRemap) {
this.methods.remove(element);
this.accessors.registerAccessor(new AnnotatedElementAccessor(element, accessor, shouldRemap));
this.accessors.registerAccessor(new AnnotatedElementAccessor(element, accessor, this, shouldRemap));
}
public void registerInvoker(ExecutableElement element, AnnotationHandle invoker, boolean shouldRemap) {
this.methods.remove(element);
this.accessors.registerAccessor(new AnnotatedElementInvoker(element, invoker, shouldRemap));
this.accessors.registerAccessor(new AnnotatedElementInvoker(element, invoker, this, shouldRemap));
}
public void registerSoftImplements(AnnotationHandle implementsAnnotation) {
this.softImplements.process(implementsAnnotation);
}
@Override
public ReferenceMapper getReferenceMapper() {
return null;
}
@Override
public String getClassName() {
return this.getClassRef().replace('/', '.');
}
@Override
public String getTargetClassRef() {
throw new UnsupportedOperationException("Target class not available at compile time");
}
@Override
public IMixinInfo getMixin() {
throw new UnsupportedOperationException("MixinInfo not available at compile time");
}
@Override
public Extensions getExtensions() {
throw new UnsupportedOperationException("Mixin Extensions not available at compile time");
}
@Override
public boolean getOption(Option option) {
throw new UnsupportedOperationException("Options not available at compile time");
}
@Override
public int getPriority() {
throw new UnsupportedOperationException("Priority not available at compile time");
}
@Override
public Target getTargetMethod(MethodNode into) {
throw new UnsupportedOperationException("Target not available at compile time");
}
}
@@ -33,8 +33,10 @@ import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.obfuscation.mapping.IMapping;
import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
@@ -104,6 +106,50 @@ abstract class AnnotatedMixinElementHandler {
}
abstract static class AnnotatedElementExecutable extends AnnotatedElement<ExecutableElement> implements ISelectorContext {
private final IMixinContext context;
private final String selectorCoordinate;
public AnnotatedElementExecutable(ExecutableElement element, AnnotationHandle annotation, IMixinContext context, String selectorCoordinate) {
super(element, annotation);
this.context = context;
this.selectorCoordinate = selectorCoordinate;
}
@Override
public ISelectorContext getParent() {
return null;
}
@Override
public IMixinContext getMixin() {
return this.context;
}
@Override
public Object getMethod() {
return this.getElement();
}
@Override
public Object getSelectorAnnotation() {
return this.getAnnotation();
}
@Override
public String getSelectorCoordinate() {
return this.selectorCoordinate;
}
@Override
public String remap(String reference) {
return reference;
}
}
/**
* A name of an element which may have aliases
*/
@@ -30,18 +30,12 @@ import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic.Kind;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.mixin.gen.AccessorInfo;
import org.spongepowered.asm.mixin.gen.AccessorInfo.AccessorName;
import org.spongepowered.asm.mixin.gen.AccessorInfo.AccessorType;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorRemappable;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
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.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.asm.util.Constants;
@@ -60,21 +54,21 @@ import com.google.common.base.Strings;
/**
* A module for {@link AnnotatedMixin} which handles accessors
*/
public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementHandler implements IMixinContext {
public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementHandler {
/**
* Accessor element
*/
static class AnnotatedElementAccessor extends AnnotatedElement<ExecutableElement> {
static class AnnotatedElementAccessor extends AnnotatedElementExecutable {
protected final boolean shouldRemap;
protected final TypeMirror returnType;
protected String targetName;
public AnnotatedElementAccessor(ExecutableElement element, AnnotationHandle annotation, boolean shouldRemap) {
super(element, annotation);
public AnnotatedElementAccessor(ExecutableElement element, AnnotationHandle annotation, IMixinContext context, boolean shouldRemap) {
super(element, annotation, context, "value");
this.shouldRemap = shouldRemap;
this.returnType = this.getElement().getReturnType();
}
@@ -147,8 +141,8 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH
private AccessorType type = AccessorType.METHOD_PROXY;
public AnnotatedElementInvoker(ExecutableElement element, AnnotationHandle annotation, boolean shouldRemap) {
super(element, annotation, shouldRemap);
public AnnotatedElementInvoker(ExecutableElement element, AnnotationHandle annotation, IMixinContext context, boolean shouldRemap) {
super(element, annotation, context, shouldRemap);
}
@Override
@@ -201,56 +195,11 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH
}
}
public AnnotatedMixinElementHandlerAccessor(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
super(ap, mixin);
}
@Override
public ReferenceMapper getReferenceMapper() {
return null;
}
@Override
public String getClassName() {
return this.mixin.getClassRef().replace('/', '.');
}
@Override
public String getClassRef() {
return this.mixin.getClassRef();
}
@Override
public String getTargetClassRef() {
throw new UnsupportedOperationException("Target class not available at compile time");
}
@Override
public IMixinInfo getMixin() {
throw new UnsupportedOperationException("MixinInfo not available at compile time");
}
@Override
public Extensions getExtensions() {
throw new UnsupportedOperationException("Mixin Extensions not available at compile time");
}
@Override
public boolean getOption(Option option) {
throw new UnsupportedOperationException("Options not available at compile time");
}
@Override
public int getPriority() {
throw new UnsupportedOperationException("Priority not available at compile time");
}
@Override
public Target getTargetMethod(MethodNode into) {
throw new UnsupportedOperationException("Target not available at compile time");
}
/**
* Register a new accessor
*
@@ -387,7 +336,7 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH
}
private String inflectAccessorTarget(AnnotatedElementAccessor elem) {
return AccessorInfo.inflectTarget(elem.getSimpleName(), elem.getAccessorType(), "", this, false);
return AccessorInfo.inflectTarget(elem.getSimpleName(), elem.getAccessorType(), "", elem, false);
}
}
@@ -35,6 +35,7 @@ import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorConstructor;
@@ -42,6 +43,7 @@ import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorRemappable
import org.spongepowered.asm.mixin.injection.selectors.TargetSelector;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
import org.spongepowered.asm.mixin.injection.struct.InvalidMemberDescriptorException;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.tools.obfuscation.ReferenceManager.ReferenceConflictException;
@@ -61,12 +63,12 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
/**
* Injector element
*/
static class AnnotatedElementInjector extends AnnotatedElement<ExecutableElement> {
static class AnnotatedElementInjector extends AnnotatedElementExecutable {
private final InjectorRemap state;
public AnnotatedElementInjector(ExecutableElement element, AnnotationHandle annotation, InjectorRemap shouldRemap) {
super(element, annotation);
public AnnotatedElementInjector(ExecutableElement element, AnnotationHandle annotation, IMixinContext context, InjectorRemap shouldRemap) {
super(element, annotation, context, "method");
this.state = shouldRemap;
}
@@ -100,7 +102,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
/**
* Injection point element
*/
static class AnnotatedElementInjectionPoint extends AnnotatedElement<ExecutableElement> {
static class AnnotatedElementInjectionPoint extends AnnotatedElementExecutable {
private final AnnotationHandle at;
@@ -108,8 +110,9 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
private final InjectorRemap state;
public AnnotatedElementInjectionPoint(ExecutableElement element, AnnotationHandle inject, AnnotationHandle at, InjectorRemap state) {
super(element, inject);
public AnnotatedElementInjectionPoint(ExecutableElement element, AnnotationHandle inject, IMixinContext context, String selectorCoordinate,
AnnotationHandle at, InjectorRemap state) {
super(element, inject, context, selectorCoordinate);
this.at = at;
this.state = state;
}
@@ -122,6 +125,11 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
return this.at;
}
@Override
public Object getSelectorAnnotation() {
return this.getAt();
}
public String getAtArg(String key) {
if (this.args == null) {
this.args = new HashMap<String, String>();
@@ -147,6 +155,23 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
}
static class AnnotatedElementSliceInjectionPoint extends AnnotatedElementInjectionPoint {
private final ISelectorContext parentContext;
public AnnotatedElementSliceInjectionPoint(ExecutableElement element, AnnotationHandle inject, IMixinContext context,
String selectorCoordinate, AnnotationHandle at, InjectorRemap state, ISelectorContext parentContext) {
super(element, inject, context, selectorCoordinate, at, state);
this.parentContext = parentContext;
}
@Override
public ISelectorContext getParent() {
return this.parentContext;
}
}
AnnotatedMixinElementHandlerInjector(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
super(ap, mixin);
}
@@ -157,7 +182,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
}
for (String reference : elem.getAnnotation().<String>getList("method")) {
ITargetSelector targetSelector = TargetSelector.parse(reference);
ITargetSelector targetSelector = TargetSelector.parse(reference, elem);
try {
targetSelector.validate();
@@ -231,8 +256,8 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
String conflictType = this.mixin.isMultiTarget() ? "Multi-target" : "Target";
if (elem.hasCoerceArgument() && targetMember.getOwner() == null && targetMember.getDesc() == null) {
ITargetSelector oldMember = TargetSelector.parse(ex.getOld());
ITargetSelector newMember = TargetSelector.parse(ex.getNew());
ITargetSelector oldMember = TargetSelector.parse(ex.getOld(), elem);
ITargetSelector newMember = TargetSelector.parse(ex.getNew(), elem);
String oldName = oldMember instanceof ITargetSelectorByName ? ((ITargetSelectorByName)oldMember).getName() : oldMember.toString();
String newName = newMember instanceof ITargetSelectorByName ? ((ITargetSelectorByName)newMember).getName() : newMember.toString();
if (oldName != null && oldName.equals(newName)) {
@@ -284,7 +309,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
return;
}
ITargetSelector selector = TargetSelector.parse(reference);
ITargetSelector selector = TargetSelector.parse(reference, elem);
if (selector instanceof ITargetSelectorConstructor) {
ITargetSelectorConstructor member = (ITargetSelectorConstructor)selector;
String target = member.toCtorType();
@@ -321,7 +346,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
return;
}
ITargetSelector targetSelector = TargetSelector.parse(reference);
ITargetSelector targetSelector = TargetSelector.parse(reference, elem);
if (!(targetSelector instanceof ITargetSelectorRemappable)) {
return;
}
@@ -67,7 +67,7 @@ public class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinEl
// Derp?
if (interfaces.size() < 1) {
this.ap.printMessage(Kind.WARNING, "Empty @Implements annotation", this.mixin.getMixin(), implementsAnnotation.asMirror());
this.ap.printMessage(Kind.WARNING, "Empty @Implements annotation", this.mixin.getMixinElement(), implementsAnnotation.asMirror());
return;
}
@@ -82,8 +82,8 @@ public class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinEl
String prefix = interfaceAnnotation.<String>getValue("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.asMirror());
this.ap.printMessage(Kind.ERROR, "Unexpected error: " + ex.getClass().getName() + ": " + ex.getMessage(),
this.mixin.getMixinElement(), interfaceAnnotation.asMirror());
}
}
}
@@ -36,13 +36,15 @@ import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import org.spongepowered.asm.util.asm.IAnnotationHandle;
import com.google.common.collect.ImmutableList;
/**
* A wrapper for {@link AnnotationMirror} which provides a more convenient way
* to access annotation values.
*/
public final class AnnotationHandle {
public final class AnnotationHandle implements IAnnotationHandle {
public static final AnnotationHandle MISSING = new AnnotationHandle(null);
@@ -79,6 +81,17 @@ public final class AnnotationHandle {
return this.annotation != null;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.util.asm.IAnnotationHandle#getDesc()
*/
@Override
public String getDesc() {
if (this.annotation == null) {
return "java/lang/Annotation";
}
return TypeUtils.getInternalName(this.annotation.getAnnotationType());
}
@Override
public String toString() {
if (this.annotation == null) {