Skip to content

Commit

Permalink
Prioritise TypeHandleASM when it is available.
Browse files Browse the repository at this point in the history
Fixes remapping of synthetic members in resolvable classes.
  • Loading branch information
LlamaLad7 authored and modmuss50 committed Feb 15, 2023
1 parent 42a2c0a commit c33a48b
Showing 1 changed file with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.spongepowered.tools.obfuscation.mirror.TypeHandleASM;
import org.spongepowered.tools.obfuscation.mirror.TypeHandleSimulated;
import org.spongepowered.tools.obfuscation.mirror.TypeReference;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils;
import org.spongepowered.tools.obfuscation.struct.InjectorRemap;
import org.spongepowered.tools.obfuscation.validation.ParentValidator;
import org.spongepowered.tools.obfuscation.validation.TargetValidator;
Expand Down Expand Up @@ -688,6 +689,24 @@ public TypeHandle getTypeHandle(String name) {
name = name.replace('/', '.');

Elements elements = this.processingEnv.getElementUtils();
PackageElement pkg = null;

int lastDotPos = name.lastIndexOf('.');
if (lastDotPos > -1) {
String pkgName = name.substring(0, lastDotPos);
pkg = elements.getPackageElement(pkgName);
}

if (pkg != null) {
// ASM gives the most and most accurate information. Try that first.
TypeHandle asmTypeHandle = TypeHandleASM.of(pkg, name.substring(lastDotPos + 1), this);
if (asmTypeHandle != null) {
return asmTypeHandle;
}
}

// ASM may be unable to resolve the class, for example if it's currently being compiled.
// Mirror is our next best bet.
TypeElement element = this.getTypeElement(name, elements);
if (element != null) {
try {
Expand All @@ -697,25 +716,12 @@ public TypeHandle getTypeHandle(String name) {
}
}

int lastDotPos = name.lastIndexOf('.');
if (lastDotPos > -1) {
String pkgName = name.substring(0, lastDotPos);
PackageElement pkg = elements.getPackageElement(pkgName);
if (pkg != null) {
// If we can resolve the package but not the class, it's possible
// we're dealing with a class that mirror can't access, such as
// an anonymous class. The class might be available via the
// classpath though, so let's attempt to read the class with ASM
TypeHandle asmTypeHandle = TypeHandleASM.of(pkg, name.substring(lastDotPos + 1), this);
if (asmTypeHandle != null) {
return asmTypeHandle;
}

// Couldn't resolve the class, so just return an imaginary handle
return new TypeHandle(pkg, name);
}
if (pkg != null) {
// Couldn't resolve the class, but could resolve the package, so just return an imaginary handle.
return new TypeHandle(pkg, name);
}

// Couldn't even resolve the package, all hope is lost.
return null;
}

Expand All @@ -728,11 +734,11 @@ public TypeHandle getTypeHandle(Object type) {
if (type instanceof TypeHandle) {
return (TypeHandle)type;
} else if (type instanceof DeclaredType) {
return new TypeHandle((DeclaredType)type);
return this.getTypeHandle(TypeUtils.getInternalName((DeclaredType) type));
} else if (type instanceof Type) {
return this.getTypeHandle(((Type)type).getClassName());
} else if (type instanceof TypeElement) {
return new TypeHandle((DeclaredType)((TypeElement)type).asType());
return this.getTypeHandle(TypeUtils.getInternalName((TypeElement) type));
} else if (type instanceof String) {
return this.getTypeHandle(type.toString());
}
Expand Down

0 comments on commit c33a48b

Please sign in to comment.