AP: Improve validation of mixin superclasses.

Extending an already-compiled mixin no longer results in an error, and extending (a mixin that targets) your target class does result in an error.

(cherry picked from commit 7f7dbc1f37)
This commit is contained in:
LlamaLad7 committed 2023-08-12 17:38:57 +01:00
1 parent be3b94df6a
commit ebc72cf782
2 files changed
+15 -12

No files matched your search

@@ -294,19 +294,16 @@ public class TypeHandle {
}
/**
* Gets whether this handle is the same as or a supertype of the other handle
* Gets whether this handle is a supertype of the other handle
*/
public boolean isAssignableFrom(TypeHandle other) {
if (this.getName().equals(other.getName())) {
return true;
}
public boolean isSuperTypeOf(TypeHandle other) {
List<TypeHandle> superTypes = new ArrayList<>();
if (other.getSuperclass() != null) {
superTypes.add(other.getSuperclass());
}
superTypes.addAll(other.getInterfaces());
for (TypeHandle superType : superTypes) {
if (this.isAssignableFrom(superType)) {
if (this.name.equals(superType.name) || this.isSuperTypeOf(superType)) {
return true;
}
}
@@ -30,6 +30,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.asm.util.asm.IAnnotationHandle;
@@ -110,16 +111,21 @@ public class TargetValidator extends MixinValidator {
}
private boolean validateSuperClass(TypeHandle targetType, TypeHandle superClass) {
return targetType.isImaginary() || targetType.isSimulated() || superClass.isAssignableFrom(targetType) || this.checkMixinsFor(targetType, superClass);
return targetType.isImaginary() || targetType.isSimulated() || superClass.isSuperTypeOf(targetType) || this.checkMixinsFor(targetType, superClass);
}
private boolean checkMixinsFor(TypeHandle targetType, TypeHandle superClass) {
for (TypeHandle mixinType : this.getMixinsTargeting(targetType)) {
if (mixinType.isAssignableFrom(superClass)) {
private boolean checkMixinsFor(TypeHandle targetType, TypeHandle superMixin) {
IAnnotationHandle annotation = superMixin.getAnnotation(Mixin.class);
for (Object target : annotation.getList()) {
if (this.typeHandleProvider.getTypeHandle(target).isSuperTypeOf(targetType)) {
return true;
}
}
return targetType.getSuperclass() != null && this.checkMixinsFor(targetType.getSuperclass(), superClass);
for (String target : annotation.<String>getList("targets")) {
if (this.typeHandleProvider.getTypeHandle(target).isSuperTypeOf(targetType)) {
return true;
}
}
return false;
}
}