mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-25 09:54:24 +00:00
Merge annotations from mixins into target classes
This commit is contained in:
1 parent
49fd7e3da7
commit
333450f64c
4 files changed
+205
-14
No files matched your search
@@ -46,7 +46,6 @@ import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import org.spongepowered.asm.mixin.Intrinsic;
|
||||
import org.spongepowered.asm.mixin.Overwrite;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
import org.spongepowered.asm.mixin.transformer.meta.MixinMerged;
|
||||
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
|
||||
@@ -194,6 +193,7 @@ public class MixinApplicator {
|
||||
|
||||
this.applyInterfaces(mixin);
|
||||
this.applyAttributes(mixin);
|
||||
this.applyAnnotations(mixin);
|
||||
this.applyFields(mixin);
|
||||
this.applyMethods(mixin);
|
||||
this.applyInitialisers(mixin);
|
||||
@@ -227,10 +227,20 @@ public class MixinApplicator {
|
||||
*/
|
||||
private void applyAttributes(MixinTargetContext mixin) {
|
||||
if (mixin.shouldSetSourceFile()) {
|
||||
this.targetClass.sourceFile = mixin.getClassNode().sourceFile;
|
||||
this.targetClass.sourceFile = mixin.getSourceFile();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin class-level annotations on the mixin into the target class
|
||||
*
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyAnnotations(MixinTargetContext mixin) {
|
||||
ClassNode sourceClass = mixin.getClassNode();
|
||||
this.mergeAnnotations(sourceClass, this.targetClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin fields from mixin class into the target class. It is vital that
|
||||
* this is done before mixinMethods because we need to compute renamed
|
||||
@@ -240,7 +250,14 @@ public class MixinApplicator {
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyFields(MixinTargetContext mixin) {
|
||||
for (FieldNode field : mixin.getClassNode().fields) {
|
||||
for (FieldNode shadow : mixin.getShadowFields()) {
|
||||
FieldNode target = this.findTargetField(shadow);
|
||||
if (target != null) {
|
||||
this.mergeAnnotations(shadow, target);
|
||||
}
|
||||
}
|
||||
|
||||
for (FieldNode field : mixin.getFields()) {
|
||||
FieldNode target = this.findTargetField(field);
|
||||
if (target == null) {
|
||||
// This is just a local field, so add it
|
||||
@@ -255,15 +272,18 @@ public class MixinApplicator {
|
||||
* @param mixin
|
||||
*/
|
||||
private void applyMethods(MixinTargetContext mixin) {
|
||||
for (MethodNode mixinMethod : mixin.getClassNode().methods) {
|
||||
for (MethodNode shadow : mixin.getShadowMethods()) {
|
||||
MethodNode target = this.findTargetMethod(shadow);
|
||||
if (target != null) {
|
||||
this.mergeAnnotations(shadow, target);
|
||||
}
|
||||
}
|
||||
|
||||
for (MethodNode mixinMethod : mixin.getMethods()) {
|
||||
// Reparent all mixin methods into the target class
|
||||
mixin.transformMethod(mixinMethod);
|
||||
|
||||
boolean isShadow = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class) != null;
|
||||
|
||||
if (isShadow) {
|
||||
continue;
|
||||
} else if (!mixinMethod.name.startsWith("<")) {
|
||||
if (!mixinMethod.name.startsWith("<")) {
|
||||
boolean isOverwrite = ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null;
|
||||
|
||||
if (MixinApplicator.hasFlag(mixinMethod, Opcodes.ACC_STATIC)
|
||||
@@ -489,7 +509,7 @@ public class MixinApplicator {
|
||||
private MethodNode getConstructor(MixinTargetContext mixin) {
|
||||
MethodNode ctor = null;
|
||||
|
||||
for (MethodNode mixinMethod : mixin.getClassNode().methods) {
|
||||
for (MethodNode mixinMethod : mixin.getMethods()) {
|
||||
if (Constants.INIT.equals(mixinMethod.name)) {
|
||||
if (MixinApplicator.hasLineNumbers(mixinMethod)) {
|
||||
if (ctor == null) {
|
||||
@@ -670,6 +690,94 @@ public class MixinApplicator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge annotations from the specified source ClassNode to the destination
|
||||
* ClassNode, replaces annotations of the equivalent type on the target with
|
||||
* annotations from the source. If the source node has no annotations then
|
||||
* no action will take place, if the target node has no annotations then a
|
||||
* new annotation list will be created. Annotations from the mixin package
|
||||
* are not merged.
|
||||
*
|
||||
* @param from ClassNode to merge annotations from
|
||||
* @param to ClassNode to merge annotations to
|
||||
*/
|
||||
private void mergeAnnotations(ClassNode from, ClassNode to) {
|
||||
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
|
||||
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge annotations from the specified source MethodNode to the destination
|
||||
* MethodNode, replaces annotations of the equivalent type on the target
|
||||
* with annotations from the source. If the source node has no annotations
|
||||
* then no action will take place, if the target node has no annotations
|
||||
* then a new annotation list will be created. Annotations from the mixin
|
||||
* package are not merged.
|
||||
*
|
||||
* @param from MethodNode to merge annotations from
|
||||
* @param to MethodNode to merge annotations to
|
||||
*/
|
||||
private void mergeAnnotations(MethodNode from, MethodNode to) {
|
||||
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
|
||||
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge annotations from the specified source FieldNode to the destination
|
||||
* FieldNode, replaces annotations of the equivalent type on the target with
|
||||
* annotations from the source. If the source node has no annotations then
|
||||
* no action will take place, if the target node has no annotations then a
|
||||
* new annotation list will be created. Annotations from the mixin package
|
||||
* are not merged.
|
||||
*
|
||||
* @param from FieldNode to merge annotations from
|
||||
* @param to FieldNode to merge annotations to
|
||||
*/
|
||||
private void mergeAnnotations(FieldNode from, FieldNode to) {
|
||||
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
|
||||
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge annotations from the source list to the target list. Returns the
|
||||
* target list or a new list if the target list was null.
|
||||
*
|
||||
* @param from Annotations to merge
|
||||
* @param to Annotation list to merge into
|
||||
* @param name Name of the item being merged, for debugging purposes
|
||||
* @return The merged list (or a new list if the target list was null)
|
||||
*/
|
||||
private List<AnnotationNode> mergeAnnotations(List<AnnotationNode> from, List<AnnotationNode> to, String name) {
|
||||
try {
|
||||
if (from == null) {
|
||||
return to;
|
||||
}
|
||||
|
||||
if (to == null) {
|
||||
to = new ArrayList<AnnotationNode>();
|
||||
}
|
||||
|
||||
for (AnnotationNode annotation : from) {
|
||||
if (annotation.desc.startsWith("L" + Constants.MIXIN_PACKAGE_REF)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Iterator<AnnotationNode> iter = to.iterator(); iter.hasNext();) {
|
||||
if (iter.next().desc.equals(annotation.desc)) {
|
||||
iter.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
to.add(annotation);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
this.logger.warn("Exception encountered whilst merging annotations for {}", name);
|
||||
}
|
||||
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first insn node with a matching opcode in the specified method
|
||||
*
|
||||
|
||||
@@ -155,8 +155,12 @@ class MixinPreProcessor {
|
||||
}
|
||||
|
||||
private void attachMethods(MixinTargetContext context) {
|
||||
for (MethodNode mixinMethod : this.classNode.methods) {
|
||||
for (Iterator<MethodNode> iter = this.classNode.methods.iterator(); iter.hasNext();) {
|
||||
MethodNode mixinMethod = iter.next();
|
||||
|
||||
if (this.processMethod(context, mixinMethod, Shadow.class, true, true)) {
|
||||
iter.remove();
|
||||
context.addShadowMethod(mixinMethod);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -234,6 +238,10 @@ class MixinPreProcessor {
|
||||
|
||||
// Shadow fields get stripped from the mixin class
|
||||
iter.remove();
|
||||
|
||||
if (shadow != null) {
|
||||
context.addShadowField(mixinField);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,8 +24,10 @@
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.transformer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -77,7 +79,17 @@ public class MixinTargetContext implements IReferenceMapperContext {
|
||||
* Target ClassInfo
|
||||
*/
|
||||
private final ClassInfo targetClassInfo;
|
||||
|
||||
|
||||
/**
|
||||
* Shadow method list
|
||||
*/
|
||||
private final List<MethodNode> shadowMethods = new ArrayList<MethodNode>();
|
||||
|
||||
/**
|
||||
* Shadow field list
|
||||
*/
|
||||
private final List<FieldNode> shadowFields = new ArrayList<FieldNode>();
|
||||
|
||||
/**
|
||||
* Information about methods in the target class, used to keep track of
|
||||
* transformations we apply
|
||||
@@ -107,7 +119,21 @@ public class MixinTargetContext implements IReferenceMapperContext {
|
||||
this.targetClass = target;
|
||||
this.targetClassInfo = ClassInfo.forName(target.name);
|
||||
this.inheritsFromMixin = mixin.getClassInfo().hasMixinInHierarchy() || this.targetClassInfo.hasMixinTargetInHierarchy();
|
||||
this.detachedSuper = !this.getClassNode().superName.equals(this.targetClass.superName);
|
||||
this.detachedSuper = !this.classNode.superName.equals(this.targetClass.superName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param method
|
||||
*/
|
||||
void addShadowMethod(MethodNode method) {
|
||||
this.shadowMethods.add(method);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param field
|
||||
*/
|
||||
void addShadowField(FieldNode field) {
|
||||
this.shadowFields.add(field);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -489,6 +515,42 @@ public class MixinTargetContext implements IReferenceMapperContext {
|
||||
public Set<String> getInterfaces() {
|
||||
return this.mixin.getInterfaces();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shadow methods in this mixin
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<MethodNode> getShadowMethods() {
|
||||
return this.shadowMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get methods to mixin
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<MethodNode> getMethods() {
|
||||
return this.classNode.methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shadow fields in this mixin
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<FieldNode> getShadowFields() {
|
||||
return this.shadowFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fields to mixin
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<FieldNode> getFields() {
|
||||
return this.classNode.fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the logging level for this mixin
|
||||
@@ -508,7 +570,16 @@ public class MixinTargetContext implements IReferenceMapperContext {
|
||||
public boolean shouldSetSourceFile() {
|
||||
return this.mixin.getParent().shouldSetSourceFile();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the source file name for the mixin
|
||||
*
|
||||
* @return mixin source file
|
||||
*/
|
||||
public String getSourceFile() {
|
||||
return this.classNode.sourceFile;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.IReferenceMapperContext
|
||||
* #getReferenceMapper()
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
*/
|
||||
package org.spongepowered.asm.util;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
|
||||
/**
|
||||
* Miscellaneous shared constants
|
||||
@@ -34,6 +36,8 @@ public abstract class Constants {
|
||||
public static final String CLINIT = "<clinit>";
|
||||
public static final String IMAGINARY_SUPER = "super$";
|
||||
public static final String DEBUG_OUTPUT_PATH = ".mixin.out";
|
||||
public static final String MIXIN_PACKAGE = Mixin.class.getPackage().getName();
|
||||
public static final String MIXIN_PACKAGE_REF = Constants.MIXIN_PACKAGE.replace('.', '/');
|
||||
|
||||
private Constants() {}
|
||||
}
|
||||
Reference in new issue
Block a user