AP resolves method mappings in superclasses when direct mapping fails

This commit is contained in:
Mumfrey committed 2017-08-09 18:08:22 +01:00
1 parent f1f60c3ee8
commit d8c4f6f0a7
10 files changed
+269 -50

No files matched your search

@@ -100,7 +100,7 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler
}
private boolean registerOverwriteForTarget(AnnotatedElementOverwrite elem, TypeHandle target) {
MappingMethod targetMethod = new MappingMethod(target.getName(), elem.getSimpleName(), elem.getDesc());
MappingMethod targetMethod = target.getMappingMethod(elem.getSimpleName(), elem.getDesc());
ObfuscationData<MappingMethod> obfData = this.obf.getDataProvider().getObfMethod(targetMethod);
if (obfData.isEmpty()) {
@@ -220,25 +220,28 @@ public abstract class ObfuscationEnvironment implements IObfuscationEnvironment
public MappingMethod getObfMethod(MappingMethod method, boolean lazyRemap) {
if (this.initMappings()) {
boolean remapped = true;
MappingMethod originalMethod = method.copy();
MappingMethod methodMapping = this.mappingProvider.getMethodMapping(method);
MappingMethod mapping = null;
for (MappingMethod md = method; md != null && mapping == null; md = md.getSuper()) {
mapping = this.mappingProvider.getMethodMapping(md);
}
// If no obf mapping, we can attempt to remap the owner class
if (methodMapping == null) {
if (mapping == null) {
if (lazyRemap) {
return null;
}
methodMapping = originalMethod;
mapping = method.copy();
remapped = false;
}
String remappedOwner = this.getObfClass(methodMapping.getOwner());
if (remappedOwner == null || remappedOwner.equals(method.getOwner()) || remappedOwner.equals(methodMapping.getOwner())) {
return remapped ? methodMapping : null;
String remappedOwner = this.getObfClass(mapping.getOwner());
if (remappedOwner == null || remappedOwner.equals(method.getOwner()) || remappedOwner.equals(mapping.getOwner())) {
return remapped ? mapping : null;
}
if (remapped) {
return methodMapping.move(remappedOwner);
return mapping.move(remappedOwner);
}
String desc = ObfuscationUtil.mapDescriptor(methodMapping.getDesc(), this.remapper);
return new MappingMethod(remappedOwner, methodMapping.getSimpleName(), desc);
String desc = ObfuscationUtil.mapDescriptor(mapping.getDesc(), this.remapper);
return new MappingMethod(remappedOwner, mapping.getSimpleName(), desc);
}
return null;
}
@@ -311,19 +314,19 @@ public abstract class ObfuscationEnvironment implements IObfuscationEnvironment
return null;
}
MappingField fieldMapping = this.mappingProvider.getFieldMapping(field);
MappingField mapping = this.mappingProvider.getFieldMapping(field);
// If no obf mapping, we can attempt to remap the owner class
if (fieldMapping == null) {
if (mapping == null) {
if (lazyRemap) {
return null;
}
fieldMapping = field;
mapping = field;
}
String remappedOwner = this.getObfClass(fieldMapping.getOwner());
if (remappedOwner == null || remappedOwner.equals(field.getOwner()) || remappedOwner.equals(fieldMapping.getOwner())) {
return fieldMapping != field ? fieldMapping : null;
String remappedOwner = this.getObfClass(mapping.getOwner());
if (remappedOwner == null || remappedOwner.equals(field.getOwner()) || remappedOwner.equals(mapping.getOwner())) {
return mapping != field ? mapping : null;
}
return fieldMapping.move(remappedOwner);
return mapping.move(remappedOwner);
}
/**
@@ -25,9 +25,9 @@
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 org.spongepowered.tools.obfuscation.mirror.mapping.ResolvableMappingMethod;
import com.google.common.base.Strings;
@@ -40,22 +40,24 @@ public class MethodHandle extends MemberHandle<MappingMethod> {
* Actual element, can be null
*/
private final ExecutableElement element;
public MethodHandle(TypeElement owner, ExecutableElement element) {
this(TypeUtils.getInternalName(owner), element);
}
public MethodHandle(String owner, ExecutableElement element) {
/**
* Handle to the owner, if available
*/
private final TypeHandle ownerHandle;
public MethodHandle(TypeHandle owner, ExecutableElement element) {
this(owner, element, TypeUtils.getName(element), TypeUtils.getDescriptor(element));
}
protected MethodHandle(String owner, String name, String desc) {
protected MethodHandle(TypeHandle owner, String name, String desc) {
this(owner, null, name, desc);
}
private MethodHandle(String owner, ExecutableElement element, String name, String desc) {
super(owner, name, desc);
private MethodHandle(TypeHandle owner, ExecutableElement element, String name, String desc) {
super(owner != null ? owner.getName() : null, name, desc);
this.element = element;
this.ownerHandle = owner;
}
/**
@@ -79,7 +81,13 @@ public class MethodHandle extends MemberHandle<MappingMethod> {
@Override
public MappingMethod asMapping(boolean includeOwner) {
return new MappingMethod(includeOwner ? this.getOwner() : null, this.getName(), this.getDesc());
if (includeOwner) {
if (this.ownerHandle != null) {
return new ResolvableMappingMethod(this.ownerHandle, this.getName(), this.getDesc());
}
return new MappingMethod(this.getOwner(), this.getName(), this.getDesc());
}
return new MappingMethod(null, this.getName(), this.getDesc());
}
@Override
@@ -41,6 +41,7 @@ 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.tools.obfuscation.mirror.mapping.ResolvableMappingMethod;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
@@ -131,12 +132,13 @@ public class TypeHandle {
/**
* Returns the actual element (returns null for imaginary elements)
*/
public TypeElement getElement() {
public final TypeElement getElement() {
return this.element;
}
/**
* Returns the actual element (returns null for imaginary elements)
* Returns the actual element (returns simulated value for imaginary
* elements)
*/
protected TypeElement getTargetElement() {
return this.element;
@@ -253,7 +255,7 @@ public class TypeHandle {
* @return this handle as a mapping method
*/
public MappingMethod getMappingMethod(String name, String desc) {
return new MappingMethod(this.getName(), name, desc);
return new ResolvableMappingMethod(this, name, desc);
}
/**
@@ -379,11 +381,12 @@ public class TypeHandle {
*/
public MethodHandle findMethod(String name, String signature, boolean matchCase) {
String rawSignature = TypeUtils.stripGenerics(signature);
return TypeHandle.findMethod(this.getTargetElement(), name, signature, rawSignature, matchCase);
return TypeHandle.findMethod(this, name, signature, rawSignature, matchCase);
}
protected static MethodHandle findMethod(TypeElement target, String name, String signature, String rawSignature, boolean matchCase) {
for (ExecutableElement method : TypeHandle.<ExecutableElement>getEnclosedElements(target, ElementKind.CONSTRUCTOR, ElementKind.METHOD)) {
protected static MethodHandle findMethod(TypeHandle target, String name, String signature, String rawSignature, boolean matchCase) {
for (ExecutableElement method : TypeHandle.<ExecutableElement>getEnclosedElements(target.getTargetElement(),
ElementKind.CONSTRUCTOR, ElementKind.METHOD)) {
if (TypeHandle.compareElement(method, name, signature, matchCase) || TypeHandle.compareElement(method, name, rawSignature, matchCase)) {
return new MethodHandle(target, method);
}
@@ -57,14 +57,6 @@ public class TypeHandleSimulated extends TypeHandle {
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()
@@ -160,15 +152,16 @@ public class TypeHandleSimulated extends TypeHandle {
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);
MethodHandle method = TypeHandleSimulated.findMethodRecursive(this, 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) {
private static MethodHandle findMethodRecursive(TypeHandle target, String name, String signature, String rawSignature, boolean matchCase) {
TypeElement elem = target.getTargetElement();
if (elem == null) {
return null;
}
@@ -177,14 +170,14 @@ public class TypeHandleSimulated extends TypeHandle {
return method;
}
for (TypeMirror iface : target.getInterfaces()) {
for (TypeMirror iface : elem.getInterfaces()) {
method = TypeHandleSimulated.findMethodRecursive(iface, name, signature, rawSignature, matchCase);
if (method != null) {
return method;
}
}
TypeMirror superClass = target.getSuperclass();
TypeMirror superClass = elem.getSuperclass();
if (superClass == null || superClass.getKind() == TypeKind.NONE) {
return null;
}
@@ -196,7 +189,8 @@ public class TypeHandleSimulated extends TypeHandle {
if (!(target instanceof DeclaredType)) {
return null;
}
return TypeHandleSimulated.findMethodRecursive((TypeElement)((DeclaredType)target).asElement(), name, signature, rawSignature, matchCase);
TypeElement element = (TypeElement)((DeclaredType)target).asElement();
return TypeHandleSimulated.findMethodRecursive(new TypeHandle(element), name, signature, rawSignature, matchCase);
}
}
@@ -152,8 +152,8 @@ public abstract class TypeUtils {
*
* <pre>(int,int)boolean</pre>
*
* @param element element to generate descriptor for
* @return descriptor
* @param element element to generate java signature for
* @return java signature
*/
public static String getJavaSignature(Element element) {
if (element instanceof ExecutableElement) {
@@ -172,6 +172,88 @@ public abstract class TypeUtils {
}
return TypeUtils.getTypeName(element.asType());
}
/**
* Get a java-style signature from the specified bytecode descriptor
*
* @param descriptor descriptor to convert to java signature
* @return java signature
*/
public static String getJavaSignature(String descriptor) {
if (descriptor == null) {
return null;
}
StringBuilder signature = new StringBuilder().append('(');
char[] desc = descriptor.toCharArray();
for (int pos = 1; pos < desc.length;) {
pos += TypeUtils.appendToSignature(signature, desc, pos);
if (pos < desc.length && desc[pos] != ')' && desc[pos - 1] != ')') {
signature.append(',');
}
}
return signature.toString();
}
private static int appendToSignature(final StringBuilder signature, final char[] desc, int pos) {
switch (desc[pos]) {
case 'V':
signature.append("void");
return 1;
case 'Z':
signature.append("boolean");
return 1;
case 'C':
signature.append("char");
return 1;
case 'B':
signature.append("byte");
return 1;
case 'S':
signature.append("short");
return 1;
case 'I':
signature.append("int");
return 1;
case 'F':
signature.append("float");
return 1;
case 'J':
signature.append("long");
return 1;
case 'D':
signature.append("double");
return 1;
case '[':
int arraySize = 0;
while (desc[pos] == '[') {
++arraySize;
++pos;
}
int length = TypeUtils.appendToSignature(signature, desc, pos);
for (int i = 0; i < arraySize; i++) {
signature.append("[]");
}
return length + arraySize;
case 'L':
length = 0;
pos++;
while (desc[pos] != ';') {
++length;
if (desc[pos] == '/') {
signature.append('.');
} else {
signature.append(desc[pos]);
}
++pos;
}
return length + 2;
case ')':
signature.append(')');
return 1;
default:
throw new IllegalStateException("Unexpected character '" + desc[pos] + "' in signature");
}
}
/**
* Get the type name for the specified type
@@ -0,0 +1,108 @@
/*
* 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.mirror.mapping;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.tools.obfuscation.mirror.TypeHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils;
/**
* A mapping method obtained from a {@link TypeHandle}. The context for this
* mapping allows references in superclasses to be resolved when necessary since
* the hierarchy information is available.
*/
public final class ResolvableMappingMethod extends MappingMethod {
private final TypeHandle ownerHandle;
public ResolvableMappingMethod(TypeHandle owner, String name, String desc) {
super(owner.getName(), name, desc);
this.ownerHandle = owner;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.obfuscation.mapping.common.MappingMethod
* #getSuper()
*/
@Override
public MappingMethod getSuper() {
if (this.ownerHandle == null) {
return super.getSuper();
}
String name = this.getSimpleName();
String desc = this.getDesc();
String signature = TypeUtils.getJavaSignature(desc);
TypeHandle superClass = this.ownerHandle.getSuperclass();
if (superClass != null) {
// Check whether superclass contains the method and return it
if (superClass.findMethod(name, signature) != null) {
return superClass.getMappingMethod(name, desc);
}
}
// Check whether any interfaces contain the method and return it
for (TypeHandle iface : this.ownerHandle.getInterfaces()) {
if (iface.findMethod(name, signature) != null) {
return iface.getMappingMethod(name, desc);
}
}
// Otherwise if superclass is defined, try to look further up the hierarchy
if (superClass != null) {
return superClass.getMappingMethod(name, desc).getSuper();
}
return super.getSuper();
}
/**
* Specialised version of <tt>move</tt> which allows this resolvable method
* to be reparented to a new type handle
*
* @param newOwner new owner
* @return remapped method
*/
public MappingMethod move(TypeHandle newOwner) {
return new ResolvableMappingMethod(newOwner, this.getSimpleName(), this.getDesc());
}
@Override
public MappingMethod remap(String newName) {
return new ResolvableMappingMethod(this.ownerHandle, newName, this.getDesc());
}
@Override
public MappingMethod transform(String newDesc) {
return new ResolvableMappingMethod(this.ownerHandle, this.getSimpleName(), newDesc);
}
@Override
public MappingMethod copy() {
return new ResolvableMappingMethod(this.ownerHandle, this.getSimpleName(), this.getDesc());
}
}