mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-21 16:12:10 +00:00
Implement new hierarchy validation logic for supermixins in the AP
This commit is contained in:
1 parent
80e6639761
commit
ce4ac1b800
9 files changed
+171
-23
No files matched your search
@@ -50,7 +50,7 @@ import net.minecraftforge.srg2source.rangeapplier.MethodData;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
|
||||
import org.spongepowered.tools.MirrorUtils;
|
||||
import org.spongepowered.tools.obfuscation.validation.IMixinValidator;
|
||||
import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass;
|
||||
|
||||
|
||||
/**
|
||||
@@ -168,7 +168,7 @@ class AnnotatedMixin {
|
||||
*/
|
||||
private final Set<String> methodMappings = new LinkedHashSet<String>();
|
||||
|
||||
public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type, Collection<IMixinValidator> validators) {
|
||||
public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type) {
|
||||
|
||||
this.annotation = MirrorUtils.getAnnotation(type, Mixin.class);
|
||||
this.mixins = mixins;
|
||||
@@ -185,12 +185,16 @@ class AnnotatedMixin {
|
||||
}
|
||||
|
||||
this.remap = AnnotatedMixins.getRemapValue(this.annotation) && this.targets.size() > 0;
|
||||
|
||||
}
|
||||
|
||||
AnnotatedMixin runValidators(ValidationPass pass, Collection<IMixinValidator> validators) {
|
||||
for (IMixinValidator validator : validators) {
|
||||
if (!validator.validate(type, this.annotation, this.targets)) {
|
||||
return;
|
||||
if (!validator.validate(pass, this.mixin, this.annotation, this.targets)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private TypeHandle initTargets() {
|
||||
@@ -246,6 +250,11 @@ class AnnotatedMixin {
|
||||
return primaryTarget;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.mixin.getSimpleName().toString();
|
||||
}
|
||||
|
||||
public AnnotationMirror getAnnotation() {
|
||||
return this.annotation;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ package org.spongepowered.tools.obfuscation;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -43,6 +45,8 @@ import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.PackageElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.Elements;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
@@ -51,7 +55,7 @@ import javax.tools.StandardLocation;
|
||||
|
||||
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
|
||||
import org.spongepowered.tools.MirrorUtils;
|
||||
import org.spongepowered.tools.obfuscation.validation.IMixinValidator;
|
||||
import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass;
|
||||
import org.spongepowered.tools.obfuscation.validation.ParentValidator;
|
||||
import org.spongepowered.tools.obfuscation.validation.TargetValidator;
|
||||
|
||||
@@ -96,6 +100,11 @@ class AnnotatedMixins implements Messager {
|
||||
*/
|
||||
private final Map<String, AnnotatedMixin> mixins = new HashMap<String, AnnotatedMixin>();
|
||||
|
||||
/**
|
||||
* Mixins created during this AP pass
|
||||
*/
|
||||
private final List<AnnotatedMixin> mixinsForPass = new ArrayList<AnnotatedMixin>();
|
||||
|
||||
/**
|
||||
* Name of the resource to write generated srgs to
|
||||
*/
|
||||
@@ -291,7 +300,10 @@ class AnnotatedMixins implements Messager {
|
||||
String name = mixinType.getQualifiedName().toString();
|
||||
|
||||
if (!this.mixins.containsKey(name)) {
|
||||
this.mixins.put(name, new AnnotatedMixin(this, mixinType, this.validators));
|
||||
AnnotatedMixin mixin = new AnnotatedMixin(this, mixinType);
|
||||
mixin.runValidators(ValidationPass.EARLY, this.validators);
|
||||
this.mixins.put(name, mixin);
|
||||
this.mixinsForPass.add(mixin);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,6 +320,25 @@ class AnnotatedMixins implements Messager {
|
||||
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>();
|
||||
|
||||
for (AnnotatedMixin mixin : this.mixins.values()) {
|
||||
for (TypeHandle mixinTarget : mixin.getTargets()) {
|
||||
if (targetType.equals(mixinTarget.getElement())) {
|
||||
minions.add(mixin.getMixin().asType());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return minions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an {@link org.spongepowered.asm.mixin.Overwrite} method
|
||||
@@ -409,6 +440,16 @@ class AnnotatedMixins implements Messager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onPassStarted() {
|
||||
this.mixinsForPass.clear();
|
||||
}
|
||||
|
||||
public void onPassCompleted() {
|
||||
for (AnnotatedMixin mixin : this.mixinsForPass) {
|
||||
mixin.runValidators(ValidationPass.LATE, this.validators);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean shouldRemap(AnnotatedMixin mixinClass, AnnotationMirror annotation) {
|
||||
return mixinClass.remap() && AnnotatedMixins.getRemapValue(annotation);
|
||||
|
||||
+9
-5
@@ -22,15 +22,13 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package org.spongepowered.tools.obfuscation.validation;
|
||||
package org.spongepowered.tools.obfuscation;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
||||
import org.spongepowered.tools.obfuscation.TypeHandle;
|
||||
|
||||
|
||||
/**
|
||||
* A mixin validator module, basically just a way of making the various sanity
|
||||
@@ -38,14 +36,20 @@ import org.spongepowered.tools.obfuscation.TypeHandle;
|
||||
*/
|
||||
public interface IMixinValidator {
|
||||
|
||||
public enum ValidationPass {
|
||||
EARLY,
|
||||
LATE
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate all the things, return false to halt processing of further
|
||||
* validators. Raise compiler errors/warnings directly.
|
||||
*
|
||||
* @param pass TODO
|
||||
* @param mixin Mixin being validated
|
||||
* @param annotation Mixin annotation
|
||||
* @param targets Mixin targets
|
||||
*
|
||||
* @return False to halt processing of further validators
|
||||
*/
|
||||
public abstract boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection<TypeHandle> targets);
|
||||
public abstract boolean validate(ValidationPass pass, TypeElement mixin, AnnotationMirror annotation, Collection<TypeHandle> targets);
|
||||
}
|
||||
@@ -73,14 +73,20 @@ public class InjectionObfuscationProcessor extends MixinProcessor {
|
||||
this.processInjectors(roundEnv, Inject.class);
|
||||
this.processInjectors(roundEnv, ModifyArg.class);
|
||||
this.processInjectors(roundEnv, Redirect.class);
|
||||
this.postProcess(roundEnv);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postProcess(RoundEnvironment roundEnv) {
|
||||
super.postProcess(roundEnv);
|
||||
|
||||
try {
|
||||
this.mixins.writeRefs();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -55,6 +55,8 @@ public abstract class MixinProcessor extends AbstractProcessor {
|
||||
* Searches and catalogues all annotated mixin classes
|
||||
*/
|
||||
protected void processMixins(RoundEnvironment roundEnv) {
|
||||
this.mixins.onPassStarted();
|
||||
|
||||
for (Element elem1 : roundEnv.getElementsAnnotatedWith(Mixin.class)) {
|
||||
if (elem1.getKind() == ElementKind.CLASS) {
|
||||
this.mixins.registerMixin((TypeElement)elem1);
|
||||
@@ -64,5 +66,9 @@ public abstract class MixinProcessor extends AbstractProcessor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void postProcess(RoundEnvironment roundEnv) {
|
||||
this.mixins.onPassCompleted();
|
||||
}
|
||||
|
||||
}
|
||||
+34
-2
@@ -22,10 +22,15 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package org.spongepowered.tools.obfuscation.validation;
|
||||
package org.spongepowered.tools.obfuscation;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
|
||||
|
||||
@@ -39,16 +44,39 @@ public abstract class MixinValidator implements IMixinValidator {
|
||||
* errors and warnings
|
||||
*/
|
||||
protected final ProcessingEnvironment processingEnv;
|
||||
|
||||
/**
|
||||
* Pass to run this validator in
|
||||
*/
|
||||
protected final ValidationPass pass;
|
||||
|
||||
/**
|
||||
* ctor
|
||||
*
|
||||
* @param processingEnv Processing environment
|
||||
*/
|
||||
public MixinValidator(ProcessingEnvironment processingEnv) {
|
||||
public MixinValidator(ProcessingEnvironment processingEnv, ValidationPass pass) {
|
||||
this.processingEnv = processingEnv;
|
||||
this.pass = pass;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.tools.obfuscation.IMixinValidator#validate(
|
||||
* org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass,
|
||||
* javax.lang.model.element.TypeElement,
|
||||
* javax.lang.model.element.AnnotationMirror, java.util.Collection)
|
||||
*/
|
||||
@Override
|
||||
public final boolean validate(ValidationPass pass, TypeElement mixin, AnnotationMirror annotation, Collection<TypeHandle> targets) {
|
||||
if (pass != this.pass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.validate(mixin, annotation, targets);
|
||||
}
|
||||
|
||||
protected abstract boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection<TypeHandle> targets);
|
||||
|
||||
/**
|
||||
* Output a compiler note
|
||||
*
|
||||
@@ -78,4 +106,8 @@ public abstract class MixinValidator implements IMixinValidator {
|
||||
protected final void error(String error, Element element) {
|
||||
this.processingEnv.getMessager().printMessage(Kind.ERROR, error, element);
|
||||
}
|
||||
|
||||
protected final Collection<TypeMirror> getMixinsTargeting(TypeMirror targetType) {
|
||||
return AnnotatedMixins.getMixinsForEnvironment(this.processingEnv).getMixinsTargeting(targetType);
|
||||
}
|
||||
}
|
||||
@@ -66,14 +66,20 @@ public class TargetObfuscationProcessor extends MixinProcessor {
|
||||
this.processMixins(roundEnv);
|
||||
this.processShadows(roundEnv);
|
||||
this.processOverwrites(roundEnv);
|
||||
this.postProcess(roundEnv);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postProcess(RoundEnvironment roundEnv) {
|
||||
super.postProcess(roundEnv);
|
||||
|
||||
try {
|
||||
this.mixins.writeSrgs();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,6 +32,7 @@ import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
||||
import org.spongepowered.tools.obfuscation.MixinValidator;
|
||||
import org.spongepowered.tools.obfuscation.TypeHandle;
|
||||
|
||||
|
||||
@@ -46,11 +47,11 @@ public class ParentValidator extends MixinValidator {
|
||||
* @param processingEnv Processing environment
|
||||
*/
|
||||
public ParentValidator(ProcessingEnvironment processingEnv) {
|
||||
super(processingEnv);
|
||||
super(processingEnv, ValidationPass.EARLY);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.tools.obfuscation.validation.IMixinValidator
|
||||
* @see org.spongepowered.tools.obfuscation.MixinValidator
|
||||
* #validate(javax.lang.model.element.TypeElement,
|
||||
* javax.lang.model.element.AnnotationMirror, java.util.Collection)
|
||||
*/
|
||||
|
||||
@@ -29,8 +29,11 @@ import java.util.Collection;
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
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.tools.obfuscation.MixinValidator;
|
||||
import org.spongepowered.tools.obfuscation.TypeHandle;
|
||||
|
||||
|
||||
@@ -45,11 +48,11 @@ public class TargetValidator extends MixinValidator {
|
||||
* @param processingEnv Processing environment
|
||||
*/
|
||||
public TargetValidator(ProcessingEnvironment processingEnv) {
|
||||
super(processingEnv);
|
||||
super(processingEnv, ValidationPass.LATE);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.tools.obfuscation.validation.IMixinValidator
|
||||
* @see org.spongepowered.tools.obfuscation.MixinValidator
|
||||
* #validate(javax.lang.model.element.TypeElement,
|
||||
* javax.lang.model.element.AnnotationMirror, java.util.Collection)
|
||||
*/
|
||||
@@ -59,11 +62,51 @@ public class TargetValidator extends MixinValidator {
|
||||
|
||||
for (TypeHandle target : targets) {
|
||||
TypeMirror targetType = target.getType();
|
||||
if (targetType != null && !this.processingEnv.getTypeUtils().isAssignable(targetType, superClass)) {
|
||||
if (targetType != null && !this.validateSuperClass(targetType, superClass)) {
|
||||
this.error("Superclass " + superClass + " of " + mixin + " was not found in the hierarchy of target class " + targetType, mixin);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean validateSuperClass(TypeMirror targetType, TypeMirror superClass) {
|
||||
if (this.isAssignable(targetType, superClass)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.validateSuperClassRecursive(targetType, superClass);
|
||||
}
|
||||
|
||||
private boolean validateSuperClassRecursive(TypeMirror targetType, TypeMirror superClass) {
|
||||
if (!(targetType instanceof DeclaredType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TypeElement targetElement = (TypeElement)((DeclaredType)targetType).asElement();
|
||||
TypeMirror targetSuper = targetElement.getSuperclass();
|
||||
if (targetSuper.getKind() == TypeKind.NONE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.checkMixinsFor(targetSuper, superClass)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.validateSuperClassRecursive(targetSuper, superClass);
|
||||
}
|
||||
|
||||
private boolean checkMixinsFor(TypeMirror targetType, TypeMirror superClass) {
|
||||
for (TypeMirror mixinType : this.getMixinsTargeting(targetType)) {
|
||||
if (this.isAssignable(mixinType, superClass)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected final boolean isAssignable(TypeMirror targetType, TypeMirror superClass) {
|
||||
return this.processingEnv.getTypeUtils().isAssignable(targetType, superClass);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user