Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/638'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed Jan 21, 2024
2 parents 3104a5b + 1edd5f1 commit 6948758
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 143 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ sourceSets {
ap {
compileClasspath += main.output
ext.languageVersion = 8
ext.compatibility = '1.6'
ext.compatibility = '1.8'
}
fernflower {
compileClasspath += main.output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public AnnotatedMixin(IMixinAnnotationProcessor ap, TypeElement type) {
this.mappings = this.obf.createMappingConsumer();
this.messager = ap;
this.mixin = type;
this.handle = new TypeHandle(type);
this.handle = new TypeHandle(type, ap.getTypeProvider());
this.methods = new ArrayList<MethodHandle>(this.handle.getMethods());
this.virtual = this.handle.getAnnotation(Pseudo.class).exists();
this.annotation = this.handle.getAnnotation(Mixin.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
import org.spongepowered.tools.obfuscation.mirror.MethodHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils.Equivalency;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils.EquivalencyResult;

import com.google.common.base.Strings;

Expand Down Expand Up @@ -304,19 +302,11 @@ private void registerInvokerForTarget(AnnotatedElementInvoker elem, TypeHandle t
}

private void registerFactoryForTarget(AnnotatedElementInvoker elem, TypeHandle target) {
EquivalencyResult equivalency = TypeUtils.isEquivalentType(this.ap.getProcessingEnvironment(), elem.getReturnType(), target.getTypeMirror());
if (equivalency.type != Equivalency.EQUIVALENT) {
if (equivalency.type == Equivalency.EQUIVALENT_BUT_RAW && equivalency.rawType == 1) {
elem.printMessage(this.ap, MessageType.INVOKER_RAW_RETURN_TYPE, "Raw return type for Factory @Invoker", SuppressedBy.RAW_TYPES);
} else if (equivalency.type == Equivalency.BOUNDS_MISMATCH) {
elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_GENERIC_ARGS, "Invalid Factory @Invoker return type, generic type args of "
+ target.getTypeMirror() + " are incompatible with " + elem.getReturnType() + ". " + equivalency);
return;
} else {
elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_RETURN_TYPE, "Invalid Factory @Invoker return type, expected "
+ target.getTypeMirror() + " but found " + elem.getReturnType());
return;
}
String returnType = TypeUtils.getTypeName(elem.getReturnType());
if (!returnType.equals(target.toString())) {
elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_RETURN_TYPE, "Invalid Factory @Invoker return type, expected "
+ target + " but found " + returnType);
return;
}
if (!elem.isStatic()) {
elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_NONSTATIC, "Factory @Invoker must be static");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void process(AnnotationHandle implementsAnnotation) {
}

try {
TypeHandle iface = new TypeHandle(interfaceAnnotation.<DeclaredType>getValue("iface"));
TypeHandle iface = this.ap.getTypeProvider().getTypeHandle(interfaceAnnotation.<DeclaredType>getValue("iface"));
String prefix = interfaceAnnotation.<String>getValue("prefix");
this.processSoftImplements(remap, iface, prefix);
} catch (Exception ex) {
Expand Down
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 @@ -158,7 +159,7 @@ private AnnotatedMixins(ProcessingEnvironment processingEnv) {
this.targets = this.initTargetMap();
this.obf = new ObfuscationManager(this);
this.obf.init();

this.validators = ImmutableList.<IMixinValidator>of(
new ParentValidator(this),
new TargetValidator(this)
Expand Down Expand Up @@ -243,7 +244,7 @@ public ProcessingEnvironment getProcessingEnvironment() {
public CompilerEnvironment getCompilerEnvironment() {
return this.env;
}

@Override
public Integer getToken(String token) {
if (this.tokenCache.containsKey(token)) {
Expand Down Expand Up @@ -370,17 +371,13 @@ public AnnotatedMixin getMixin(String mixinType) {
return this.mixins.get(mixinType);
}

public Collection<TypeMirror> getMixinsTargeting(TypeMirror targetType) {
return this.getMixinsTargeting((TypeElement)((DeclaredType)targetType).asElement());
}

public Collection<TypeMirror> getMixinsTargeting(TypeElement targetType) {
List<TypeMirror> minions = new ArrayList<TypeMirror>();
public Collection<TypeHandle> getMixinsTargeting(TypeHandle targetType) {
List<TypeHandle> minions = new ArrayList<TypeHandle>();

for (TypeReference mixin : this.targets.getMixinsTargeting(targetType)) {
TypeHandle handle = mixin.getHandle(this.processingEnv);
if (handle != null && handle.hasTypeMirror()) {
minions.add(handle.getTypeMirror());
TypeHandle handle = mixin.getHandle(this);
if (handle != null) {
minions.add(handle);
}
}

Expand Down Expand Up @@ -590,7 +587,7 @@ public void printMessage(MessageType type, CharSequence msg, Element element, Su
this.printMessage(type.getKind(), type.decorate(msg), element, suppressedBy);
}
}

/**
* Print a message to the AP messager
*/
Expand Down Expand Up @@ -648,7 +645,7 @@ public void printMessage(MessageType type, CharSequence msg, Element element, An
this.printMessage(type.getKind(), type.decorate(msg), element, annotation, value);
}
}

/**
* Print a message to the AP messager
*/
Expand Down Expand Up @@ -688,37 +685,42 @@ 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 {
return new TypeHandle(element);
return new TypeHandle(element, this);
} catch (NullPointerException ex) {
// probably bad package
}
}

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, this);
}


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

/**
* Get a TypeHandle representing the supplied type in the current processing
* environment
Expand All @@ -728,11 +730,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 Expand Up @@ -810,11 +812,11 @@ public TypeHandle getSimulatedHandle(String name, TypeMirror simulatedTarget) {
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(packageElement, name, simulatedTarget, this);
}
}

return new TypeHandleSimulated(name, simulatedTarget);
return new TypeHandleSimulated(name, simulatedTarget, this);
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

import org.spongepowered.asm.util.asm.IAnnotationHandle;
import org.spongepowered.tools.obfuscation.interfaces.IMessagerSuppressible;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator;
import org.spongepowered.tools.obfuscation.interfaces.IOptionProvider;
import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider;
import org.spongepowered.tools.obfuscation.mirror.TypeHandle;

/**
Expand All @@ -57,7 +57,12 @@ public abstract class MixinValidator implements IMixinValidator {
* Option provider
*/
protected final IOptionProvider options;


/**
* Type handle provider
*/
protected final ITypeHandleProvider typeHandleProvider;

/**
* Pass to run this validator in
*/
Expand All @@ -73,6 +78,7 @@ public MixinValidator(IMixinAnnotationProcessor ap, ValidationPass pass) {
this.processingEnv = ap.getProcessingEnvironment();
this.messager = ap;
this.options = ap;
this.typeHandleProvider = ap.getTypeProvider();
this.pass = pass;
}

Expand All @@ -94,7 +100,7 @@ public final boolean validate(ValidationPass pass, TypeElement mixin, IAnnotatio

protected abstract boolean validate(TypeElement mixin, IAnnotationHandle annotation, Collection<TypeHandle> targets);

protected final Collection<TypeMirror> getMixinsTargeting(TypeMirror targetType) {
protected final Collection<TypeHandle> getMixinsTargeting(TypeHandle targetType) {
return AnnotatedMixins.getMixinsForEnvironment(this.processingEnv).getMixinsTargeting(targetType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ public MappingMethod getObfMethod(ITargetSelectorRemappable method) {
}

// See if we can get the superclass from the reference
TypeMirror superClass = type.getElement().getSuperclass();
if (superClass.getKind() != TypeKind.DECLARED) {
TypeHandle superClass = type.getSuperclass();
if (superClass == null) {
return null;
}

// Well we found it, let's inflect the class name and recurse the search
String superClassName = ((TypeElement)((DeclaredType)superClass).asElement()).getQualifiedName().toString();
String superClassName = superClass.getSimpleName();
return this.getObfMethod(method.move(superClassName.replace('.', '/')));
}

Expand Down
10 changes: 0 additions & 10 deletions src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,6 @@ void addMixin(TypeReference target, TypeReference mixin) {
Set<TypeReference> mixins = this.getMixinsFor(target);
mixins.add(mixin);
}

/**
* Get mixin classes which target the specified class
*
* @param target Target class
* @return Collection of mixins registered as targetting the specified class
*/
Collection<TypeReference> getMixinsTargeting(TypeElement target) {
return this.getMixinsTargeting(new TypeHandle(target));
}

/**
* Get mixin classes which target the specified class
Expand Down
Loading

0 comments on commit 6948758

Please sign in to comment.