From 946520c0ed27dd9f1b93650036f8b8b7abdf115e Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Thu, 29 Dec 2016 16:07:15 +0000 Subject: [PATCH] Add support for pseudo mixins, closes #152. Version bump to 0.6.2. --- gradle.properties | 2 +- .../tools/obfuscation/AnnotatedMixin.java | 15 +- .../AnnotatedMixinElementHandlerAccessor.java | 4 +- .../AnnotatedMixinElementHandlerInjector.java | 16 +- .../AnnotatedMixinElementHandlerShadow.java | 6 +- .../tools/obfuscation/AnnotatedMixins.java | 25 ++- .../obfuscation/ObfuscationDataProvider.java | 22 +- .../interfaces/IObfuscationDataProvider.java | 12 ++ .../interfaces/ITypeHandleProvider.java | 14 +- .../tools/obfuscation/mirror/FieldHandle.java | 41 +++- .../obfuscation/mirror/MemberHandle.java | 61 ++++++ .../obfuscation/mirror/MethodHandle.java | 34 ++- .../tools/obfuscation/mirror/TypeHandle.java | 141 +++++++----- .../mirror/TypeHandleSimulated.java | 202 ++++++++++++++++++ .../tools/obfuscation/mirror/TypeUtils.java | 31 ++- .../asm/launch/MixinBootstrap.java | 2 +- .../org/spongepowered/asm/mixin/Pseudo.java | 55 +++++ .../asm/mixin/transformer/ClassInfo.java | 3 +- .../asm/mixin/transformer/MixinInfo.java | 25 ++- .../asm/util/SignaturePrinter.java | 4 + 20 files changed, 627 insertions(+), 88 deletions(-) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/Pseudo.java diff --git a/gradle.properties b/gradle.properties index a322054e..71821148 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.6.1 +buildVersion=0.6.2 buildType=SNAPSHOT diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index dbe15129..d8314c18 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -38,6 +38,7 @@ import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementAccessor; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementInvoker; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementInjectionPoint; @@ -112,6 +113,14 @@ class AnnotatedMixin { * True if we will actually process remappings for this mixin */ private final boolean remap; + + /** + * True if the target class is allowed to not exist at compile time, we will + * simulate the target in order to do as much validation as is feasible. + * + *

Implies remap=false

+ */ + private final boolean virtual; /** * Overwrite handler @@ -145,9 +154,11 @@ class AnnotatedMixin { this.messager = ap; this.mixin = type; this.handle = new TypeHandle(type); + this.virtual = this.handle.getAnnotation(Pseudo.class).exists(); this.annotation = this.handle.getAnnotation(Mixin.class); this.classRef = type.getQualifiedName().toString().replace('.', '/'); this.primaryTarget = this.initTargets(); +// this.remap = !this.virtual && this.annotation.getBoolean("remap", true) && this.targets.size() > 0; this.remap = this.annotation.getBoolean("remap", true) && this.targets.size() > 0; this.overwrites = new AnnotatedMixinElementHandlerOverwrite(ap, this); @@ -193,7 +204,9 @@ class AnnotatedMixin { if (this.targets.contains(type)) { continue; } - if (type == null) { + if (this.virtual) { + type = this.typeProvider.getSimulatedHandle(privateTarget, this.mixin.asType()); + } else if (type == null) { this.printMessage(Kind.ERROR, "Mixin target " + privateTarget + " could not be found", this); return null; } else if (type.isPublic()) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java index ff68d4fa..85edf039 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java @@ -185,7 +185,7 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH return; } - ObfuscationData obfData = this.obf.getDataProvider().getObfField(targetField.asMapping().move(target.getName())); + ObfuscationData obfData = this.obf.getDataProvider().getObfField(targetField.asMapping(false).move(target.getName())); if (obfData.isEmpty()) { String info = this.mixin.isMultiTarget() ? " in target " + target : ""; elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Accessor target " + elem); @@ -209,7 +209,7 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH return; } - ObfuscationData obfData = this.obf.getDataProvider().getObfMethod(targetMethod.asMapping().move(target.getName())); + ObfuscationData obfData = this.obf.getDataProvider().getObfMethod(targetMethod.asMapping(false).move(target.getName())); if (obfData.isEmpty()) { String info = this.mixin.isMultiTarget() ? " in target " + target : ""; elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Accessor target " + elem); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java index 7be6e45e..5095f874 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java @@ -128,7 +128,9 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler String desc = target.findDescriptor(targetMember); if (desc == null) { Kind error = this.mixin.isMultiTarget() ? Kind.ERROR : Kind.WARNING; - if (target.isImaginary()) { + if (target.isSimulated()) { + elem.printMessage(this.ap, Kind.NOTE, elem + " target '" + reference + "' in @Pseudo mixin will not be obfuscated"); + } else if (target.isImaginary()) { elem.printMessage(this.ap, error, elem + " target requires method signature because enclosing type information for " + target + " is unavailable"); } else if (!Constants.CTOR.equals(targetMember.name)) { @@ -138,16 +140,20 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler } String targetName = elem + " target " + targetMember.name; - MappingMethod targetMethod = new MappingMethod(target.getName(), targetMember.name, desc); + MappingMethod targetMethod = target.getMappingMethod(targetMember.name, desc); ObfuscationData obfData = this.obf.getDataProvider().getObfMethod(targetMethod); if (obfData.isEmpty()) { - Kind error = Constants.CTOR.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR; - return new Message(error, "No obfuscation mapping for " + targetName, elem.getElement(), elem.getAnnotation()); + if (target.isSimulated()) { + obfData = this.obf.getDataProvider().getRemappedMethod(targetMethod); + } else { + Kind error = Constants.CTOR.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR; + return new Message(error, "No obfuscation mapping for " + targetName, elem.getElement(), elem.getAnnotation()); + } } try { // If the original owner is unspecified, and the mixin is multi-target, we strip the owner from the obf mappings - if (targetMember.owner == null && this.mixin.isMultiTarget()) { + if ((targetMember.owner == null && this.mixin.isMultiTarget()) || target.isSimulated()) { obfData = AnnotatedMixinElementHandler.stripOwnerData(obfData); } this.obf.getReferenceManager().addMethodMapping(this.classRef, reference, obfData); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java index 26cb7428..954d6ea7 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java @@ -166,7 +166,11 @@ class AnnotatedMixinElementHandlerShadow extends AnnotatedMixinElementHandler { if (obfData.isEmpty()) { String info = this.mixin.isMultiTarget() ? " in target " + target : ""; - elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Shadow " + elem); + if (target.isSimulated()) { + elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Shadow " + elem); + } else { + elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Shadow " + elem); + } return; } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 45a941f9..b729bd83 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -62,11 +62,12 @@ import org.spongepowered.tools.obfuscation.interfaces.IJavadocProvider; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator.ValidationPass; -import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; -import org.spongepowered.tools.obfuscation.mirror.TypeHandle; -import org.spongepowered.tools.obfuscation.mirror.TypeReference; import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager; import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; +import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; +import org.spongepowered.tools.obfuscation.mirror.TypeHandle; +import org.spongepowered.tools.obfuscation.mirror.TypeHandleSimulated; +import org.spongepowered.tools.obfuscation.mirror.TypeReference; import org.spongepowered.tools.obfuscation.struct.Message; import org.spongepowered.tools.obfuscation.validation.ParentValidator; import org.spongepowered.tools.obfuscation.validation.TargetValidator; @@ -587,6 +588,24 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp return null; } + + /** + * Returns a simulated TypeHandle for a non-existing type + */ + @Override + public TypeHandle getSimulatedHandle(String name, TypeMirror simulatedTarget) { + name = name.replace('/', '.'); + int lastDotPos = name.lastIndexOf('.'); + if (lastDotPos > -1) { + String pkg = name.substring(0, lastDotPos); + PackageElement packageElement = this.processingEnv.getElementUtils().getPackageElement(pkg); + if (packageElement != null) { + return new TypeHandleSimulated(packageElement, name, simulatedTarget); + } + } + + return new TypeHandleSimulated(name, simulatedTarget); + } /* (non-Javadoc) * @see org.spongepowered.tools.obfuscation.IJavadocProvider diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java index fa910112..688409cf 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java @@ -140,6 +140,15 @@ public class ObfuscationDataProvider implements IObfuscationDataProvider { */ @Override public ObfuscationData getObfMethod(MemberInfo method) { + return this.getRemappedMethod(method, Constants.CTOR.equals(method.name)); + } + + @Override + public ObfuscationData getRemappedMethod(MemberInfo method) { + return this.getRemappedMethod(method, true); + } + + private ObfuscationData getRemappedMethod(MemberInfo method, boolean remapDescriptor) { ObfuscationData data = new ObfuscationData(); for (ObfuscationEnvironment env : this.environments) { @@ -149,7 +158,7 @@ public class ObfuscationDataProvider implements IObfuscationDataProvider { } } - if (!data.isEmpty() || !Constants.CTOR.equals(method.name)) { + if (!data.isEmpty() || !remapDescriptor) { return data; } @@ -163,6 +172,15 @@ public class ObfuscationDataProvider implements IObfuscationDataProvider { */ @Override public ObfuscationData getObfMethod(MappingMethod method) { + return this.getRemappedMethod(method, Constants.CTOR.equals(method.getSimpleName())); + } + + @Override + public ObfuscationData getRemappedMethod(MappingMethod method) { + return this.getRemappedMethod(method, true); + } + + private ObfuscationData getRemappedMethod(MappingMethod method, boolean remapDescriptor) { ObfuscationData data = new ObfuscationData(); for (ObfuscationEnvironment env : this.environments) { @@ -172,7 +190,7 @@ public class ObfuscationDataProvider implements IObfuscationDataProvider { } } - if (!data.isEmpty() || !Constants.CTOR.equals(method.getSimpleName())) { + if (!data.isEmpty() || !remapDescriptor) { return data; } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java index 12f9267d..8435b9dc 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java @@ -81,11 +81,23 @@ public interface IObfuscationDataProvider { */ public abstract ObfuscationData getObfMethod(MemberInfo method); + /** + * Get an obfuscation mapping for a method if an explicit mapping exists. + * Where no direct mapping exists, remap the descriptor of the method only. + */ + public abstract ObfuscationData getRemappedMethod(MemberInfo method); + /** * Get an obfuscation mapping for a method */ public abstract ObfuscationData getObfMethod(MappingMethod method); + /** + * Get an obfuscation mapping for a method if an explicit mapping exists. + * Where no direct mapping exists, remap the descriptor of the method only. + */ + public abstract ObfuscationData getRemappedMethod(MappingMethod method); + /** * Attempts to resolve an obfuscated field by recursively enumerating * superclasses of the target field until a match is found. If a match is diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java index 6d9140c4..c7caaa0e 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java @@ -24,6 +24,8 @@ */ package org.spongepowered.tools.obfuscation.interfaces; +import javax.lang.model.type.TypeMirror; + import org.spongepowered.tools.obfuscation.mirror.TypeHandle; /** @@ -35,8 +37,18 @@ public interface ITypeHandleProvider { * Generate a type handle for the specified type * * @param name Type name (class name) - * @return A new type handle + * @return A new type handle or null if the type could not be found */ public abstract TypeHandle getTypeHandle(String name); + + /** + * Generate a type handle for the specified type, simulate the target using + * the supplied type + * + * @param name Type name (class name) + * @param simulatedTarget Simulation target + * @return A new type handle + */ + public abstract TypeHandle getSimulatedHandle(String name, TypeMirror simulatedTarget); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java index ceeb7822..78b99f43 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java @@ -24,14 +24,17 @@ */ package org.spongepowered.tools.obfuscation.mirror; +import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import com.google.common.base.Strings; + /** * Retrieved from a {@link TypeHandle} when searching for fields */ -public class FieldHandle { +public class FieldHandle extends MemberHandle { /** * Actual element, can be null @@ -43,12 +46,29 @@ public class FieldHandle { * specialised type */ private final boolean rawType; + + public FieldHandle(TypeElement owner, VariableElement element) { + this(TypeUtils.getInternalName(owner), element); + } - public FieldHandle(VariableElement element) { - this(element, false); + public FieldHandle(String owner, VariableElement element) { + this(owner, element, false); } - public FieldHandle(VariableElement element, boolean rawType) { + public FieldHandle(TypeElement owner, VariableElement element, boolean rawType) { + this(TypeUtils.getInternalName(owner), element, rawType); + } + + public FieldHandle(String owner, VariableElement element, boolean rawType) { + this(owner, element, rawType, TypeUtils.getName(element), TypeUtils.getInternalName(element)); + } + + protected FieldHandle(String owner, String name, String desc) { + this(owner, null, false, name, desc); + } + + private FieldHandle(String owner, VariableElement element, boolean rawType, String name, String desc) { + super(owner, name, desc); this.element = element; this.rawType = rawType; } @@ -75,8 +95,17 @@ public class FieldHandle { return this.rawType; } - public MappingField asMapping() { - return new MappingField(null, this.element.getSimpleName().toString(), TypeUtils.getInternalName(this.element)); + @Override + public MappingField asMapping(boolean includeOwner) { + return new MappingField(includeOwner ? this.getOwner() : null, this.getName(), this.getDesc()); + } + + @Override + public String toString() { + String owner = this.getOwner() != null ? "L" + this.getOwner() + ";" : ""; + String name = Strings.nullToEmpty(this.getName()); + String desc = Strings.nullToEmpty(this.getDesc()); + return String.format("%s%s:%s", owner, name, desc); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java new file mode 100644 index 00000000..ce847aa4 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java @@ -0,0 +1,61 @@ +/* + * 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.mirror; + +import org.spongepowered.asm.obfuscation.mapping.IMapping; + +/** + * Abstract base class for element handles + * + * @param mapping type + */ +public abstract class MemberHandle> { + + private final String owner; + + private final String name; + + private final String desc; + + protected MemberHandle(String owner, String name, String desc) { + this.owner = owner; + this.name = name; + this.desc = desc; + } + + public final String getOwner() { + return this.owner; + } + + public final String getName() { + return this.name; + } + + public final String getDesc() { + return this.desc; + } + + public abstract T asMapping(boolean includeOwner); +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java index 615dc8ce..d6083880 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java @@ -25,25 +25,37 @@ package org.spongepowered.tools.obfuscation.mirror; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import com.google.common.base.Strings; + /** * Retrieved from a {@link TypeHandle} when searching for methods */ -public class MethodHandle { +public class MethodHandle extends MemberHandle { /** * Actual element, can be null */ private final ExecutableElement element; - public MethodHandle(ExecutableElement element) { - this.element = element; + public MethodHandle(TypeElement owner, ExecutableElement element) { + this(TypeUtils.getInternalName(owner), element); + } + + public MethodHandle(String owner, ExecutableElement element) { + this(owner, TypeUtils.getName(element), TypeUtils.getDescriptor(element)); + } + + protected MethodHandle(String owner, String name, String desc) { + this(owner, null, name, desc); } - public String getName() { - return this.element != null ? this.element.getSimpleName().toString() : null; + private MethodHandle(String owner, ExecutableElement element, String name, String desc) { + super(owner, name, desc); + this.element = element; } /** @@ -60,8 +72,16 @@ public class MethodHandle { return this.element; } - public MappingMethod asMapping() { - return new MappingMethod(null, this.element.getSimpleName().toString(), TypeUtils.getDescriptor(this.element)); + @Override + public MappingMethod asMapping(boolean includeOwner) { + return new MappingMethod(includeOwner ? this.getOwner() : null, this.getName(), this.getDesc()); } + @Override + public String toString() { + String owner = this.getOwner() != null ? "L" + this.getOwner() + ";" : ""; + String name = Strings.nullToEmpty(this.getName()); + String desc = Strings.nullToEmpty(this.getDesc()); + return String.format("%s%s%s", owner, name, desc); + } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java index 399c0f3d..cee9b6ec 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java @@ -40,6 +40,7 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; @@ -109,21 +110,21 @@ public class TypeHandle { * @see java.lang.Object#toString() */ @Override - public String toString() { + public final String toString() { return this.name.replace('/', '.'); } /** * Returns the fully qualified class name */ - public String getName() { + public final String getName() { return this.name; } /** * Returns the enclosing package element */ - public PackageElement getPackage() { + public final PackageElement getPackage() { return this.pkg; } @@ -133,16 +134,23 @@ public class TypeHandle { public TypeElement getElement() { return this.element; } + + /** + * Returns the actual element (returns null for imaginary elements) + */ + protected TypeElement getTargetElement() { + return this.element; + } public AnnotationHandle getAnnotation(Class annotationClass) { - return AnnotationHandle.of(this.element, annotationClass); + return AnnotationHandle.of(this.getTargetElement(), annotationClass); } /** * Returns enclosed elements (methods, fields, etc.) */ - public List getEnclosedElements() { - return this.element != null ? this.element.getEnclosedElements() : Collections.emptyList(); + public final List getEnclosedElements() { + return TypeHandle.getEnclosedElements(this.getTargetElement()); } /** @@ -150,27 +158,8 @@ public class TypeHandle { * * @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(); + return TypeHandle.getEnclosedElements(this.getTargetElement(), kind); } /** @@ -178,7 +167,7 @@ public class TypeHandle { * imaginary type */ public TypeMirror getType() { - return this.element != null ? this.element.asType() : null; + return this.getTargetElement() != null ? this.getTargetElement().asType() : null; } /** @@ -186,11 +175,12 @@ public class TypeHandle { * class does not have a superclass */ public TypeHandle getSuperclass() { - if (this.element == null) { + TypeElement targetElement = this.getTargetElement(); + if (targetElement == null) { return null; } - TypeMirror superClass = this.element.getSuperclass(); + TypeMirror superClass = targetElement.getSuperclass(); if (superClass == null || superClass.getKind() == TypeKind.NONE) { return null; } @@ -199,12 +189,12 @@ public class TypeHandle { } public List getInterfaces() { - if (this.element == null) { + if (this.getTargetElement() == null) { return Collections.emptyList(); } Builder list = ImmutableList.builder(); - for (TypeMirror iface : this.element.getInterfaces()) { + for (TypeMirror iface : this.getTargetElement().getInterfaces()) { list.add(new TypeHandle((DeclaredType)iface)); } @@ -215,23 +205,43 @@ public class TypeHandle { * Get whether the element is probably public */ public boolean isPublic() { - return this.element != null ? this.element.getModifiers().contains(Modifier.PUBLIC) : false; + return this.getTargetElement() != null ? this.getTargetElement().getModifiers().contains(Modifier.PUBLIC) : false; } /** * Get whether the element is imaginary (inaccessible via mirror) */ public boolean isImaginary() { - return this.element == null; + return this.getTargetElement() == null; } - public TypeReference getReference() { + /** + * Get whether this handle is simulated + */ + public boolean isSimulated() { + return false; + } + + public final TypeReference getReference() { if (this.reference == null) { this.reference = new TypeReference(this); } return this.reference; } + /** + * Return a method as a remapping candidate, usually returns a method owned + * by this class but for simulated handles we resolve the reference in the + * class hierarchy of the simulated target (eg. self) + * + * @param name Method name + * @param desc Method descriptor + * @return + */ + public MappingMethod getMappingMethod(String name, String desc) { + return new MappingMethod(this.getName(), name, desc); + } + public String findDescriptor(MemberInfo memberInfo) { String desc = memberInfo.desc; if (desc == null) { @@ -252,7 +262,7 @@ public class TypeHandle { * @param element Element to match * @return handle to the discovered field if matched or null if no match */ - public FieldHandle findField(VariableElement element) { + public final FieldHandle findField(VariableElement element) { return this.findField(element, true); } @@ -263,7 +273,7 @@ public class TypeHandle { * @param element Element to match * @return handle to the discovered field if matched or null if no match */ - public FieldHandle findField(VariableElement element, boolean caseSensitive) { + public final FieldHandle findField(VariableElement element, boolean caseSensitive) { return this.findField(element.getSimpleName().toString(), TypeUtils.getTypeName(element.asType()), caseSensitive); } @@ -275,7 +285,7 @@ public class TypeHandle { * @param type Field descriptor (java-style) * @return handle to the discovered field if matched or null if no match */ - public FieldHandle findField(String name, String type) { + public final FieldHandle findField(String name, String type) { return this.findField(name, type, true); } @@ -291,10 +301,10 @@ public class TypeHandle { String rawType = TypeUtils.stripGenerics(type); 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)) { - return new FieldHandle(field, true); + if (TypeHandle.compareElement(field, name, type, caseSensitive)) { + return new FieldHandle(this.getTargetElement(), field); + } else if (TypeHandle.compareElement(field, name, rawType, caseSensitive)) { + return new FieldHandle(this.getTargetElement(), field, true); } } @@ -308,7 +318,7 @@ public class TypeHandle { * @param element Element to match * @return handle to the discovered method if matched or null if no match */ - public MethodHandle findMethod(ExecutableElement element) { + public final MethodHandle findMethod(ExecutableElement element) { return this.findMethod(element, true); } @@ -319,7 +329,7 @@ public class TypeHandle { * @param element Element to match * @return handle to the discovered method if matched or null if no match */ - public MethodHandle findMethod(ExecutableElement element, boolean caseSensitive) { + public final MethodHandle findMethod(ExecutableElement element, boolean caseSensitive) { return this.findMethod(element.getSimpleName().toString(), TypeUtils.getJavaSignature(element), caseSensitive); } @@ -331,7 +341,7 @@ public class TypeHandle { * @param signature Method signature * @return handle to the discovered method if matched or null if no match */ - public MethodHandle findMethod(String name, String signature) { + public final MethodHandle findMethod(String name, String signature) { return this.findMethod(name, signature, true); } @@ -343,28 +353,57 @@ public class TypeHandle { * @param signature Method signature * @return handle to the discovered method if matched or null if no match */ - public MethodHandle findMethod(String name, String signature, boolean caseSensitive) { + public MethodHandle findMethod(String name, String signature, boolean matchCase) { String rawSignature = TypeUtils.stripGenerics(signature); + return TypeHandle.findMethod(this.getTargetElement(), name, signature, rawSignature, matchCase); + } - 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); + protected static MethodHandle findMethod(TypeElement target, String name, String signature, String rawSignature, boolean matchCase) { + for (ExecutableElement method : TypeHandle.getEnclosedElements(target, ElementKind.CONSTRUCTOR, ElementKind.METHOD)) { + if (TypeHandle.compareElement(method, name, signature, matchCase) || TypeHandle.compareElement(method, name, rawSignature, matchCase)) { + return new MethodHandle(target, method); } } - return null; } - private boolean compareElement(Element elem, String name, String type, boolean caseSensitive) { + protected static boolean compareElement(Element elem, String name, String type, boolean matchCase) { try { String elementName = elem.getSimpleName().toString(); String elementType = TypeUtils.getJavaSignature(elem); String rawElementType = TypeUtils.stripGenerics(elementType); - boolean compared = caseSensitive ? name.equals(elementName) : name.equalsIgnoreCase(elementName); + boolean compared = matchCase ? name.equals(elementName) : name.equalsIgnoreCase(elementName); return compared && (type.length() == 0 || type.equals(elementType) || type.equals(rawElementType)); } catch (NullPointerException ex) { return false; } } + @SuppressWarnings("unchecked") + protected static List getEnclosedElements(TypeElement targetElement, ElementKind... kind) { + if (kind == null || kind.length < 1) { + return (List)TypeHandle.getEnclosedElements(targetElement); + } + + if (targetElement == null) { + return Collections.emptyList(); + } + + Builder list = ImmutableList.builder(); + for (Element elem : targetElement.getEnclosedElements()) { + for (ElementKind ek : kind) { + if (elem.getKind() == ek) { + list.add((T)elem); + break; + } + } + } + + return list.build(); + } + + protected static List getEnclosedElements(TypeElement targetElement) { + return targetElement != null ? targetElement.getEnclosedElements() : Collections.emptyList(); + } + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java new file mode 100644 index 00000000..fe579d54 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java @@ -0,0 +1,202 @@ +/* + * 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.mirror; + +import java.lang.annotation.Annotation; + +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; + +import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.asm.util.SignaturePrinter; + +/** + * A simulated type handle, used with virtual (pseudo) mixins. For obfuscation + * purposes, we have to use some kind of context to resolve target members so + * that appropriate refmaps can be generated. For this purpose we use the mixin + * itself as the context in order to allow us to look up members in superclasses + * and superinterfaces of the mixin (in the hope that we can locate targets + * there. If we cannot achieve this, then remapping will have to be done by hand + */ +public class TypeHandleSimulated extends TypeHandle { + + private final TypeElement simulatedType; + + public TypeHandleSimulated(String name, TypeMirror type) { + this(TypeUtils.getPackage(type), name, type); + } + + public TypeHandleSimulated(PackageElement pkg, String name, TypeMirror type) { + super(pkg, name); + this.simulatedType = (TypeElement)((DeclaredType)type).asElement(); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#getElement() + */ + @Override + public TypeElement getElement() { + return null; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getTargetElement() + */ + @Override + protected TypeElement getTargetElement() { + return this.simulatedType; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#isPublic() + */ + @Override + public boolean isPublic() { + // We have no idea + return true; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#isImaginary() + */ + @Override + public boolean isImaginary() { + // it actually is, but we need the AP to assume that it isn't + return false; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#isSimulated() + */ + @Override + public boolean isSimulated() { + // hell yeah + return true; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getAnnotation(java.lang.Class) + */ + @Override + public AnnotationHandle getAnnotation(Class annotationClass) { + // nope + return null; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getSuperclass() + */ + @Override + public TypeHandle getSuperclass() { + return null; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #findDescriptor(org.spongepowered.asm.mixin.injection.struct.MemberInfo) + */ + @Override + public String findDescriptor(MemberInfo memberInfo) { + // Identity, refs need to be FQ + return memberInfo != null ? memberInfo.desc : null; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #findField(java.lang.String, java.lang.String, boolean) + */ + @Override + public FieldHandle findField(String name, String type, boolean caseSensitive) { + return new FieldHandle(null, name, type); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #findMethod(java.lang.String, java.lang.String, boolean) + */ + @Override + public MethodHandle findMethod(String name, String desc, boolean caseSensitive) { + // assume we find it + return new MethodHandle(null, name, desc); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getMappingMethod(java.lang.String, java.lang.String) + */ + @Override + public MappingMethod getMappingMethod(String name, String desc) { + // Transform the MemberInfo descriptor into a signature + String signature = new SignaturePrinter(name, desc).setFullyQualified(true).toDescriptor(); + String rawSignature = TypeUtils.stripGenerics(signature); + + // Try to locate a member anywhere in the hierarchy which matches + MethodHandle method = TypeHandleSimulated.findMethodRecursive(this.getTargetElement(), name, signature, rawSignature, true); + + // If we find one, return it otherwise just simulate the method + return method != null ? method.asMapping(true) : super.getMappingMethod(name, desc); + + } + + private static MethodHandle findMethodRecursive(TypeElement target, String name, String signature, String rawSignature, boolean matchCase) { + if (target == null) { + return null; + } + + MethodHandle method = TypeHandle.findMethod(target, name, signature, rawSignature, matchCase); + if (method != null) { + return method; + } + + for (TypeMirror iface : target.getInterfaces()) { + method = TypeHandleSimulated.findMethodRecursive(iface, name, signature, rawSignature, matchCase); + if (method != null) { + return method; + } + } + + TypeMirror superClass = target.getSuperclass(); + if (superClass == null || superClass.getKind() == TypeKind.NONE) { + return null; + } + + return TypeHandleSimulated.findMethodRecursive(superClass, name, signature, rawSignature, matchCase); + } + + private static MethodHandle findMethodRecursive(TypeMirror target, String name, String signature, String rawSignature, boolean matchCase) { + if (!(target instanceof DeclaredType)) { + return null; + } + return TypeHandleSimulated.findMethodRecursive((TypeElement)((DeclaredType)target).asElement(), name, signature, rawSignature, matchCase); + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java index 56b1118a..ae40b957 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java @@ -53,8 +53,15 @@ public abstract class TypeUtils { // No instances for you private TypeUtils() {} - public static PackageElement getPackage(TypeElement elem) { - Element parent = elem.getEnclosingElement(); + public static PackageElement getPackage(TypeMirror type) { + if (!(type instanceof DeclaredType)) { + return null; + } + return TypeUtils.getPackage((TypeElement)((DeclaredType)type).asElement()); + } + + public static PackageElement getPackage(TypeElement type) { + Element parent = type.getEnclosingElement(); while (parent != null && !(parent instanceof PackageElement)) { parent = parent.getEnclosingElement(); } @@ -89,7 +96,7 @@ public abstract class TypeUtils { desc.append(TypeUtils.getTypeName(arg.asType())); extra = true; } - desc.append(')').append(method.getReturnType().toString()); + desc.append(')').append(TypeUtils.getTypeName(method.getReturnType())); return desc.toString(); } return TypeUtils.getTypeName(element.asType()); @@ -141,8 +148,20 @@ public abstract class TypeUtils { private static String getTypeName(TypeElement elem) { return TypeUtils.getInternalName(elem).replace('/', '.'); } + + public static String getName(VariableElement field) { + return field != null ? field.getSimpleName().toString() : null; + } + + public static String getName(ExecutableElement method) { + return method != null ? method.getSimpleName().toString() : null; + } public static String getDescriptor(ExecutableElement method) { + if (method == null) { + return null; + } + StringBuilder signature = new StringBuilder(); for (VariableElement var : method.getParameters()) { @@ -154,6 +173,9 @@ public abstract class TypeUtils { } public static String getInternalName(VariableElement var) { + if (var == null) { + return null; + } return TypeUtils.getInternalName(var.asType()); } @@ -220,6 +242,9 @@ public abstract class TypeUtils { } public static String getInternalName(TypeElement elem) { + if (elem == null) { + return null; + } StringBuilder reference = new StringBuilder(); reference.append(elem.getSimpleName()); Element parent = elem.getEnclosingElement(); diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 63e3050e..96a747cd 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -62,7 +62,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.6.1"; + public static final String VERSION = "0.6.2"; // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; diff --git a/src/main/java/org/spongepowered/asm/mixin/Pseudo.java b/src/main/java/org/spongepowered/asm/mixin/Pseudo.java new file mode 100644 index 00000000..fc6c394a --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/Pseudo.java @@ -0,0 +1,55 @@ +/* + * 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; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * A Mixin marked as Pseudo is allowed to target classes which are not + * available at compile time and may not be available at runtime. This means + * that certain restrictions apply: + * + *

In particular, the superclass requirement for pseudo mixins is extremely + * important if the target has an obfuscated class in its hierarchy. For example + * let's assume that we're mixing into a class SomeCustomScreen from + * another party which extends GuiScreen which is obfuscated. + * Attempting to inject into initGui will succeed at dev time and fail + * at production time, because the reference is obfuscated. We can overcome this + * by ensuring the mixin inherits from the same superclass, thus allowing + * initGui to be resolved in the superclass hierarchy (this is not the + * case for normal mixins).

+ * + *

{@link Overwrite} methods which are not inherited from a superclass + * (if the target is obfuscated) must be decorated manually with aliases. + *

+ */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.CLASS) +public @interface Pseudo { + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index a5a38b9e..a6e654aa 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -1563,7 +1563,8 @@ public class ClassInfo extends TreeInfo { ClassNode classNode = TreeInfo.getClassNode(className); info = new ClassInfo(classNode); } catch (Exception ex) { - ex.printStackTrace(); + ClassInfo.logger.warn("Error loading class: {}", className); +// ex.printStackTrace(); } // Put null in the cache if load failed diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index 895750db..fabf4661 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -51,6 +51,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel; import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Pseudo; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.extensibility.IMixinConfig; @@ -681,6 +682,11 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { */ private final int priority; + /** + * True if the mixin is annotated with {@link Pseudo} + */ + private final boolean virtual; + /** * Mixin targets, read from the {@link Mixin} annotation on the mixin class */ @@ -770,6 +776,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { // Read the class bytes and transform try { this.priority = this.readPriority(this.pendingState.getClassNode()); + this.virtual = this.readPseudo(this.pendingState.getClassNode()); this.targetClasses = this.readTargetClasses(this.pendingState.getClassNode(), suppressPlugin); this.targetClassNames = Collections.unmodifiableList(Lists.transform(this.targetClasses, Functions.toStringFunction())); } catch (InvalidMixinException ex) { @@ -864,11 +871,15 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { private ClassInfo getTarget(String targetName, boolean checkPublic) throws InvalidMixinException { ClassInfo targetInfo = ClassInfo.forName(targetName); if (targetInfo == null) { - this.handleTargetError(String.format("@Mixin target %s was not found %s", targetName, this)); + if (this.isVirtual()) { + this.logger.debug("Skipping virtual target {} for {}", targetName, this); + } else { + this.handleTargetError(String.format("@Mixin target %s was not found %s", targetName, this)); + } return null; } this.type.validateTarget(targetName, targetInfo); - if (checkPublic && targetInfo.isPublic()) { + if (checkPublic && targetInfo.isPublic() && !this.isVirtual()) { this.handleTargetError(String.format("@Mixin target %s is public in %s and should be specified in value", targetName, this)); } return targetInfo; @@ -901,7 +912,11 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { Integer priority = ASMHelper.getAnnotationValue(mixin, "priority"); return priority == null ? this.parent.getDefaultMixinPriority() : priority.intValue(); } - + + protected boolean readPseudo(ClassNode classNode) { + return ASMHelper.getInvisibleAnnotation(classNode, Pseudo.class) != null; + } + private boolean isReloading() { return this.pendingState instanceof Reloaded; } @@ -991,6 +1006,10 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { public boolean isUnique() { return this.getState().isUnique(); } + + public boolean isVirtual() { + return this.virtual; + } /** * True if the mixin class is actually class-loadable diff --git a/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java b/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java index 9d610785..f7c02f19 100644 --- a/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java +++ b/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java @@ -123,6 +123,10 @@ public class SignaturePrinter { } } + public String getFormattedArgs() { + return this.appendArgs(new StringBuilder(), true).toString(); + } + public String getReturnType() { return SignaturePrinter.getTypeName(this.returnType, false, this.fullyQualified); }