mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-20 12:19:52 +00:00
0.7 Add support for inner classes in Mixins. Updates #187.
This commit is contained in:
1 parent
c6ce27efb0
commit
c117ae8724
14 files changed
+532
-54
No files matched your search
@@ -62,7 +62,7 @@ public abstract class MixinBootstrap {
|
||||
/**
|
||||
* Subsystem version
|
||||
*/
|
||||
public static final String VERSION = "0.6.14";
|
||||
public static final String VERSION = "0.7";
|
||||
|
||||
// Consts
|
||||
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ public final class ArgsClassGenerator implements IClassGenerator {
|
||||
|
||||
public static final String GETTER_PREFIX = "$";
|
||||
|
||||
private static final String CLASS_NAME_BASE = "org.spongepowered.asm.synthetic.Args$";
|
||||
private static final String CLASS_NAME_BASE = Constants.SYNTHETIC_PACKAGE + ".args.Args$";
|
||||
|
||||
private static final String OBJECT = "java/lang/Object";
|
||||
private static final String OBJECT_ARRAY = "[L" + ArgsClassGenerator.OBJECT + ";";
|
||||
|
||||
@@ -245,6 +245,12 @@ public final class ClassInfo extends TreeInfo {
|
||||
*/
|
||||
private String currentName;
|
||||
|
||||
/**
|
||||
* Current descriptor of the member, may be different from
|
||||
* {@link #memberDesc} if the member has been remapped
|
||||
*/
|
||||
private String currentDesc;
|
||||
|
||||
/**
|
||||
* True if this member is decorated with {@link Final}
|
||||
*/
|
||||
@@ -263,6 +269,7 @@ public final class ClassInfo extends TreeInfo {
|
||||
protected Member(Member member) {
|
||||
this(member.type, member.memberName, member.memberDesc, member.modifiers, member.isInjected);
|
||||
this.currentName = member.currentName;
|
||||
this.currentDesc = member.currentDesc;
|
||||
this.unique = member.unique;
|
||||
}
|
||||
|
||||
@@ -276,6 +283,7 @@ public final class ClassInfo extends TreeInfo {
|
||||
this.memberDesc = desc;
|
||||
this.isInjected = injected;
|
||||
this.currentName = name;
|
||||
this.currentDesc = desc;
|
||||
this.modifiers = access;
|
||||
}
|
||||
|
||||
@@ -287,18 +295,26 @@ public final class ClassInfo extends TreeInfo {
|
||||
return this.currentName;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
public String getOriginalDesc() {
|
||||
return this.memberDesc;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return this.currentDesc;
|
||||
}
|
||||
|
||||
public boolean isInjected() {
|
||||
return this.isInjected;
|
||||
}
|
||||
|
||||
public boolean isRenamed() {
|
||||
return this.currentName != this.memberName;
|
||||
return !this.currentName.equals(this.memberName);
|
||||
}
|
||||
|
||||
public boolean isRemapped() {
|
||||
return !this.currentDesc.equals(this.memberDesc);
|
||||
}
|
||||
|
||||
public boolean isPrivate() {
|
||||
return (this.modifiers & Opcodes.ACC_PRIVATE) != 0;
|
||||
}
|
||||
@@ -364,11 +380,15 @@ public final class ClassInfo extends TreeInfo {
|
||||
this.currentName = name;
|
||||
return name;
|
||||
}
|
||||
|
||||
public String remapTo(String desc) {
|
||||
this.currentDesc = desc;
|
||||
return desc;
|
||||
}
|
||||
|
||||
public boolean equals(String name, String desc) {
|
||||
return (this.memberName.equals(name)
|
||||
|| this.currentName.equals(name))
|
||||
&& this.memberDesc.equals(desc);
|
||||
return (this.memberName.equals(name) || this.currentName.equals(name))
|
||||
&& (this.memberDesc.equals(desc) || this.currentDesc.equals(desc));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -378,9 +398,8 @@ public final class ClassInfo extends TreeInfo {
|
||||
}
|
||||
|
||||
Member other = (Member)obj;
|
||||
return (other.memberName.equals(this.memberName)
|
||||
|| other.currentName.equals(this.currentName))
|
||||
&& other.memberDesc.equals(this.memberDesc);
|
||||
return (other.memberName.equals(this.memberName) || other.currentName.equals(this.currentName))
|
||||
&& (other.memberDesc.equals(this.memberDesc) || other.currentDesc.equals(this.currentDesc));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -396,6 +415,7 @@ public final class ClassInfo extends TreeInfo {
|
||||
protected String getDisplayFormat() {
|
||||
return "%s%s";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -709,20 +729,20 @@ public final class ClassInfo extends TreeInfo {
|
||||
|
||||
boolean isProbablyStatic = true;
|
||||
String outerName = classNode.outerClass;
|
||||
if (outerName == null) {
|
||||
for (FieldNode field : classNode.fields) {
|
||||
if ((field.access & Opcodes.ACC_SYNTHETIC) != 0) {
|
||||
if (field.name.startsWith("this$")) {
|
||||
isProbablyStatic = false;
|
||||
for (FieldNode field : classNode.fields) {
|
||||
if ((field.access & Opcodes.ACC_SYNTHETIC) != 0) {
|
||||
if (field.name.startsWith("this$")) {
|
||||
isProbablyStatic = false;
|
||||
if (outerName == null) {
|
||||
outerName = field.desc;
|
||||
if (outerName.startsWith("L")) {
|
||||
if (outerName != null && outerName.startsWith("L")) {
|
||||
outerName = outerName.substring(1, outerName.length() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.fields.add(new Field(field, this.isMixin));
|
||||
}
|
||||
|
||||
this.fields.add(new Field(field, this.isMixin));
|
||||
}
|
||||
|
||||
this.isProbablyStatic = isProbablyStatic;
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
/*
|
||||
* This file is part of Mixin, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) SpongePowered <https://www.spongepowered.org>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.transformer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.spongepowered.asm.lib.AnnotationVisitor;
|
||||
import org.spongepowered.asm.lib.ClassReader;
|
||||
import org.spongepowered.asm.lib.ClassVisitor;
|
||||
import org.spongepowered.asm.lib.ClassWriter;
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.lib.commons.Remapper;
|
||||
import org.spongepowered.asm.lib.commons.RemappingClassAdapter;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
|
||||
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
|
||||
|
||||
/**
|
||||
* Class generator which creates unique copies of inner classes within mixins
|
||||
* which are specialised to the target class.
|
||||
*/
|
||||
final class InnerClassGenerator implements IClassGenerator {
|
||||
|
||||
/**
|
||||
* Information about an inner class instance. Implements {@link Remapper} so
|
||||
* that it can participate in the remapping process.
|
||||
*/
|
||||
static class InnerClassInfo extends Remapper {
|
||||
|
||||
/**
|
||||
* Generated class name
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Original class name
|
||||
*/
|
||||
private final String originalName;
|
||||
|
||||
/**
|
||||
* Mixin which owns this inner class
|
||||
*/
|
||||
private final MixinInfo owner;
|
||||
|
||||
/**
|
||||
* Mixin target context
|
||||
*/
|
||||
private final MixinTargetContext target;
|
||||
|
||||
/**
|
||||
* Name of the owner mixin (class ref)
|
||||
*/
|
||||
private final String ownerName;
|
||||
|
||||
/**
|
||||
* Name of the target class (class ref)
|
||||
*/
|
||||
private final String targetName;
|
||||
|
||||
InnerClassInfo(String name, String originalName, MixinInfo owner, MixinTargetContext target) {
|
||||
this.name = name;
|
||||
this.originalName = originalName;
|
||||
this.owner = owner;
|
||||
this.ownerName = owner.getClassRef();
|
||||
this.target = target;
|
||||
this.targetName = target.getTargetClassRef();
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
String getOriginalName() {
|
||||
return this.originalName;
|
||||
}
|
||||
|
||||
MixinInfo getOwner() {
|
||||
return this.owner;
|
||||
}
|
||||
|
||||
MixinTargetContext getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
String getOwnerName() {
|
||||
return this.ownerName;
|
||||
}
|
||||
|
||||
String getTargetName() {
|
||||
return this.targetName;
|
||||
}
|
||||
|
||||
byte[] getClassBytes() throws ClassNotFoundException, IOException {
|
||||
return TreeInfo.loadClass(this.originalName, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to remap synthetic accessor methods which have been renamed when
|
||||
* being applied to the target class.
|
||||
*/
|
||||
@Override
|
||||
public String mapMethodName(String owner, String name, String desc) {
|
||||
if (this.ownerName.equalsIgnoreCase(owner)) {
|
||||
Method method = this.owner.getClassInfo().findMethod(name, desc, ClassInfo.INCLUDE_ALL);
|
||||
if (method != null) {
|
||||
return method.getName();
|
||||
}
|
||||
}
|
||||
return super.mapMethodName(owner, name, desc);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.lib.commons.Remapper#map(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String map(String key) {
|
||||
if (this.originalName.equals(key)) {
|
||||
return this.name;
|
||||
} else if (this.ownerName.equals(key)) {
|
||||
return this.targetName;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Just a basic remapping adapter, but we also decorate the transformed
|
||||
* class with a meta annotation describing the original class.
|
||||
*/
|
||||
static class InnerClassAdapter extends RemappingClassAdapter {
|
||||
|
||||
private final InnerClassInfo info;
|
||||
|
||||
public InnerClassAdapter(ClassVisitor cv, InnerClassInfo info) {
|
||||
super(Opcodes.ASM5, cv, info);
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.lib.ClassVisitor
|
||||
* #visitSource(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public void visitSource(String source, String debug) {
|
||||
super.visitSource(source, debug);
|
||||
AnnotationVisitor av = this.cv.visitAnnotation("Lorg/spongepowered/asm/mixin/transformer/meta/MixinInner;", false);
|
||||
av.visit("mixin", this.info.getOwner().toString());
|
||||
av.visit("name", this.info.getOriginalName().substring(this.info.getOriginalName().lastIndexOf('/') + 1));
|
||||
av.visitEnd();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.lib.commons.RemappingClassAdapter
|
||||
* #visitInnerClass(java.lang.String, java.lang.String,
|
||||
* java.lang.String, int)
|
||||
*/
|
||||
@Override
|
||||
public void visitInnerClass(String name, String outerName, String innerName, int access) {
|
||||
if (name.startsWith(this.info.getOriginalName() + "$")) {
|
||||
throw new InvalidMixinException(this.info.getOwner(), "Found unsupported nested inner class " + name + " in "
|
||||
+ this.info.getOriginalName());
|
||||
}
|
||||
|
||||
super.visitInnerClass(name, outerName, innerName, access);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Logger
|
||||
*/
|
||||
private static final Logger logger = LogManager.getLogger("mixin");
|
||||
|
||||
/**
|
||||
* Singleton instance (for now)
|
||||
*/
|
||||
private static InnerClassGenerator instance;
|
||||
|
||||
/**
|
||||
* Mapping of target class context ids to generated inner class names, used
|
||||
* so we don't accidentally conform the same class twice.
|
||||
*/
|
||||
private final Map<String, String> innerClassNames = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* Mapping of generated class names to the respective inner class info
|
||||
*/
|
||||
private final Map<String, InnerClassInfo> innerClasses = new HashMap<String, InnerClassInfo>();
|
||||
|
||||
private InnerClassGenerator() {
|
||||
// private, use factory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param owner Mixin which owns the original inner class
|
||||
* @param originalName Original inner class name
|
||||
* @param context Target class
|
||||
* @return new name
|
||||
*/
|
||||
public String registerInnerClass(MixinInfo owner, String originalName, MixinTargetContext context) {
|
||||
String id = String.format("%s%s", originalName, context);
|
||||
String ref = this.innerClassNames.get(id);
|
||||
if (ref == null) {
|
||||
ref = InnerClassGenerator.getUniqueReference(originalName, context);
|
||||
this.innerClassNames.put(id, ref);
|
||||
this.innerClasses.put(ref, new InnerClassInfo(ref, originalName, owner, context));
|
||||
InnerClassGenerator.logger.debug("Inner class {} in {} on {} gets unique name {}", originalName, owner.getClassRef(),
|
||||
context.getTargetClassRef(), ref);
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.IClassGenerator
|
||||
* #generate(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public byte[] generate(String name) {
|
||||
String ref = name.replace('.', '/');
|
||||
InnerClassInfo info = this.innerClasses.get(ref);
|
||||
if (info != null) {
|
||||
return this.generate(info);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a specialised inner class by taking the original class bytecode
|
||||
* and remapping it against the target class.
|
||||
*
|
||||
* @param info inner class info to process
|
||||
* @return generated class or null if generation failed
|
||||
*/
|
||||
private byte[] generate(InnerClassInfo info) {
|
||||
try {
|
||||
InnerClassGenerator.logger.debug("Generating mapped inner class {} (originally {})", info.getName(), info.getOriginalName());
|
||||
ClassReader cr = new ClassReader(info.getClassBytes());
|
||||
ClassWriter cw = new MixinClassWriter(cr, 0);
|
||||
cr.accept(new InnerClassAdapter(cw, info), ClassReader.EXPAND_FRAMES);
|
||||
return cw.toByteArray();
|
||||
} catch (InvalidMixinException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
InnerClassGenerator.logger.catching(ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* To avoid accidental clashes with existing target classes, or classes from
|
||||
* multiple mixins, each remapped class gets a unique name.
|
||||
*
|
||||
* @param originalName Original inner class name
|
||||
* @param context Target context
|
||||
* @return unique class name
|
||||
*/
|
||||
private static String getUniqueReference(String originalName, MixinTargetContext context) {
|
||||
String name = originalName.substring(originalName.lastIndexOf('$') + 1);
|
||||
if (name.matches("^[0-9]+$")) {
|
||||
name = "Anonymous";
|
||||
}
|
||||
return String.format("%s$%s$%s", context.getTargetClassRef(), name, UUID.randomUUID().toString().replace("-", ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton instance
|
||||
*/
|
||||
public static InnerClassGenerator getInstance() {
|
||||
if (InnerClassGenerator.instance == null) {
|
||||
InnerClassGenerator.instance = new InnerClassGenerator();
|
||||
}
|
||||
return InnerClassGenerator.instance;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -64,6 +64,7 @@ import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
|
||||
import org.spongepowered.asm.mixin.transformer.throwables.MixinReloadException;
|
||||
import org.spongepowered.asm.mixin.transformer.throwables.MixinTargetAlreadyLoadedException;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.launchwrapper.LaunchClassLoaderUtil;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
@@ -106,6 +107,10 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
return this.getVisibleAnnotation(Surrogate.class) != null;
|
||||
}
|
||||
|
||||
public boolean isSynthetic() {
|
||||
return Bytecode.hasFlag(this, Opcodes.ACC_SYNTHETIC);
|
||||
}
|
||||
|
||||
public AnnotationNode getVisibleAnnotation(Class<? extends Annotation> annotationClass) {
|
||||
return Annotations.getVisible(this, annotationClass);
|
||||
}
|
||||
@@ -191,6 +196,11 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
* Synthetic inner classes
|
||||
*/
|
||||
protected final Set<String> syntheticInnerClasses = new HashSet<String>();
|
||||
|
||||
/**
|
||||
* Non-synthetic inner classes
|
||||
*/
|
||||
protected final Set<String> innerClasses = new HashSet<String>();
|
||||
|
||||
/**
|
||||
* Initial ClassNode created for mixin validation, not used for actual
|
||||
@@ -243,6 +253,10 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
Set<String> getSyntheticInnerClasses() {
|
||||
return this.syntheticInnerClasses;
|
||||
}
|
||||
|
||||
Set<String> getInnerClasses() {
|
||||
return this.innerClasses;
|
||||
}
|
||||
|
||||
Set<String> getInterfaces() {
|
||||
return this.interfaces;
|
||||
@@ -325,7 +339,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
this.validateRemappable(Shadow.class, method.name, Annotations.getVisible(method, Shadow.class));
|
||||
AnnotationNode overwrite = Annotations.getVisible(method, Overwrite.class);
|
||||
if (overwrite != null && ((method.access & Opcodes.ACC_STATIC) == 0 || (method.access & Opcodes.ACC_PUBLIC) == 0)) {
|
||||
throw new InvalidMixinException(MixinInfo.this, "Found @Overwrite annotation on " + method.name + " in " + this);
|
||||
throw new InvalidMixinException(MixinInfo.this, "Found @Overwrite annotation on " + method.name + " in " + MixinInfo.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -374,12 +388,12 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
void readInnerClasses() {
|
||||
for (InnerClassNode inner : this.classNode.innerClasses) {
|
||||
ClassInfo innerClass = ClassInfo.forName(inner.name);
|
||||
if (innerClass.isSynthetic() && innerClass.isProbablyStatic()) {
|
||||
if ((inner.outerName != null && inner.outerName.equals(this.classInfo.getName()))
|
||||
|| inner.name.startsWith(this.classNode.name + "$")) {
|
||||
if ((inner.outerName != null && inner.outerName.equals(this.classInfo.getName()))
|
||||
|| inner.name.startsWith(this.classNode.name + "$")) {
|
||||
if (innerClass.isProbablyStatic() && innerClass.isSynthetic()) {
|
||||
this.syntheticInnerClasses.add(inner.name);
|
||||
} else {
|
||||
throw new InvalidMixinException(MixinInfo.this, "Unhandled synthetic inner class found: " + inner.name);
|
||||
this.innerClasses.add(inner.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1076,6 +1090,13 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
return Collections.unmodifiableSet(this.getState().getSyntheticInnerClasses());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user-defined inner classes for this mixin
|
||||
*/
|
||||
Set<String> getInnerClasses() {
|
||||
return Collections.unmodifiableSet(this.getState().getInnerClasses());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target class list for this mixin
|
||||
*/
|
||||
|
||||
@@ -30,11 +30,14 @@ import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.spongepowered.asm.lib.ClassReader;
|
||||
import org.spongepowered.asm.lib.ClassVisitor;
|
||||
import org.spongepowered.asm.lib.ClassWriter;
|
||||
import org.spongepowered.asm.lib.FieldVisitor;
|
||||
import org.spongepowered.asm.lib.MethodVisitor;
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.lib.Type;
|
||||
import org.spongepowered.asm.lib.tree.AnnotationNode;
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
import org.spongepowered.asm.lib.tree.FieldNode;
|
||||
import org.spongepowered.asm.lib.tree.InsnNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodNode;
|
||||
@@ -50,7 +53,9 @@ import org.spongepowered.asm.transformers.TreeTransformer;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
|
||||
/**
|
||||
* TODO Description for MixinPostProcessor
|
||||
* Performs post-processing tasks required for certain classes which pass
|
||||
* through the mixin transformer, such as transforming synthetic inner classes
|
||||
* and populating accessor methods in accessor mixins.
|
||||
*/
|
||||
class MixinPostProcessor extends TreeTransformer implements MixinConfig.IListener {
|
||||
|
||||
@@ -138,26 +143,37 @@ class MixinPostProcessor extends TreeTransformer implements MixinConfig.IListene
|
||||
* new home in the target class
|
||||
*/
|
||||
private byte[] processSyntheticInner(byte[] bytes) {
|
||||
ClassNode classNode = this.readClass(bytes, true);
|
||||
ClassReader cr = new ClassReader(bytes);
|
||||
ClassWriter cw = new MixinClassWriter(cr, 0);
|
||||
|
||||
// Make the class public
|
||||
classNode.access |= Opcodes.ACC_PUBLIC;
|
||||
|
||||
// Make package-private fields public
|
||||
for (FieldNode field : classNode.fields) {
|
||||
if ((field.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
|
||||
field.access |= Opcodes.ACC_PUBLIC;
|
||||
ClassVisitor visibilityVisitor = new ClassVisitor(Opcodes.ASM5, cw) {
|
||||
|
||||
@Override
|
||||
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
|
||||
super.visit(version, access | Opcodes.ACC_PUBLIC, name, signature, superName, interfaces);
|
||||
}
|
||||
}
|
||||
|
||||
// Make package-private methods public
|
||||
for (MethodNode method : classNode.methods) {
|
||||
if ((method.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
|
||||
method.access |= Opcodes.ACC_PUBLIC;
|
||||
|
||||
|
||||
@Override
|
||||
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
|
||||
if ((access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
|
||||
access |= Opcodes.ACC_PUBLIC;
|
||||
}
|
||||
return super.visitField(access, name, desc, signature, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
|
||||
if ((access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
|
||||
access |= Opcodes.ACC_PUBLIC;
|
||||
}
|
||||
return super.visitMethod(access, name, desc, signature, exceptions);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return this.writeClass(classNode);
|
||||
cr.accept(visibilityVisitor, ClassReader.EXPAND_FRAMES);
|
||||
return cw.toByteArray();
|
||||
}
|
||||
|
||||
private byte[] processAccessor(byte[] bytes, MixinInfo mixin) {
|
||||
|
||||
+10
-4
@@ -405,6 +405,11 @@ class MixinPreProcessorStandard {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (method.isSynthetic()) {
|
||||
context.transformDescriptor(mixinMethod);
|
||||
method.remapTo(mixinMethod.desc);
|
||||
}
|
||||
|
||||
MethodNode target = context.findMethod(mixinMethod, null);
|
||||
if (target == null) {
|
||||
return false;
|
||||
@@ -412,7 +417,7 @@ class MixinPreProcessorStandard {
|
||||
|
||||
String type = method.isUnique() ? "@Unique" : "synthetic";
|
||||
|
||||
if ((mixinMethod.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) != 0) {
|
||||
if (Bytecode.getVisibility(mixinMethod).ordinal() < Visibility.PUBLIC.ordinal()) {
|
||||
String uniqueName = context.getUniqueName(mixinMethod, false);
|
||||
MixinPreProcessorStandard.logger.log(this.mixin.getLoggingLevel(), "Renaming {} method {}{} to {} in {}",
|
||||
type, mixinMethod.name, mixinMethod.desc, uniqueName, this.mixin);
|
||||
@@ -458,10 +463,11 @@ class MixinPreProcessorStandard {
|
||||
iter.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
context.transformDescriptor(mixinField);
|
||||
|
||||
|
||||
Field field = this.mixin.getClassInfo().findField(mixinField);
|
||||
context.transformDescriptor(mixinField);
|
||||
field.remapTo(mixinField.desc);
|
||||
|
||||
if (field.isUnique() && isShadow) {
|
||||
throw new InvalidMixinException(this.mixin, "@Shadow field " + mixinField.name + " cannot be @Unique");
|
||||
}
|
||||
|
||||
@@ -63,6 +63,9 @@ import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.ClassSignature;
|
||||
import org.spongepowered.asm.util.Constants;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
|
||||
/**
|
||||
* This object keeps track of data for applying a mixin to a specific target
|
||||
* class <em>during</em> a mixin application. This is a single-use object which
|
||||
@@ -102,6 +105,11 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
* Target ClassInfo
|
||||
*/
|
||||
private final ClassInfo targetClassInfo;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private final BiMap<String, String> innerClasses = HashBiMap.<String, String>create();
|
||||
|
||||
/**
|
||||
* Shadow method list
|
||||
@@ -171,6 +179,11 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
this.detachedSuper = !this.classNode.superName.equals(this.getTarget().getClassNode().superName);
|
||||
this.sessionId = context.getSessionId();
|
||||
this.requireVersion(classNode.version);
|
||||
|
||||
InnerClassGenerator icg = InnerClassGenerator.getInstance();
|
||||
for (String innerClass : this.mixin.getInnerClasses()) {
|
||||
this.innerClasses.put(innerClass, icg.registerInnerClass(this.mixin, innerClass, this));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -396,7 +409,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles "re-parenting" the method supplied, changes all references to the
|
||||
* mixin class to refer to the target class (for field accesses and method
|
||||
@@ -408,6 +421,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
public void transformMethod(MethodNode method) {
|
||||
this.validateMethod(method);
|
||||
this.transformDescriptor(method);
|
||||
this.transformLVT(method);
|
||||
|
||||
// Offset line numbers per the stratum
|
||||
this.stratum.applyOffset(method);
|
||||
@@ -451,6 +465,25 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the Local Variable Table (LVT) for the specified mixin method.
|
||||
*
|
||||
* @param method Method node to transform
|
||||
*/
|
||||
private void transformLVT(MethodNode method) {
|
||||
if (method.localVariables == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (LocalVariableNode local : method.localVariables) {
|
||||
if (local == null || local.desc == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
local.desc = this.transformSingleDescriptor(Type.getType(local.desc));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a method invocation/reference in the method. Updates static
|
||||
* and dynamic bindings.
|
||||
@@ -469,6 +502,9 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
methodRef.setName(md.getName());
|
||||
}
|
||||
this.upgradeMethodRef(method, methodRef, md);
|
||||
} else if (this.innerClasses.containsKey(methodRef.getOwner())) {
|
||||
methodRef.setOwner(this.innerClasses.get(methodRef.getOwner()));
|
||||
methodRef.setDesc(this.transformMethodDescriptor(methodRef.getDesc()));
|
||||
} else if (this.detachedSuper || this.inheritsFromMixin) {
|
||||
if (methodRef.getOpcode() == Opcodes.INVOKESPECIAL) {
|
||||
this.updateStaticBinding(method, methodRef);
|
||||
@@ -576,6 +612,11 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
|
||||
if (typeInsn.desc.equals(this.getClassRef())) {
|
||||
typeInsn.desc = this.getTarget().getClassRef();
|
||||
} else {
|
||||
String newName = this.innerClasses.get(typeInsn.desc);
|
||||
if (newName != null) {
|
||||
typeInsn.desc = newName;
|
||||
}
|
||||
}
|
||||
|
||||
this.transformDescriptor(typeInsn);
|
||||
@@ -745,7 +786,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
* @param field Field node to transform
|
||||
*/
|
||||
public void transformDescriptor(FieldNode field) {
|
||||
if (!this.inheritsFromMixin) {
|
||||
if (!this.inheritsFromMixin && this.innerClasses.size() == 0) {
|
||||
return;
|
||||
}
|
||||
field.desc = this.transformSingleDescriptor(field.desc, false);
|
||||
@@ -757,7 +798,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
* @param method Method node to transform
|
||||
*/
|
||||
public void transformDescriptor(MethodNode method) {
|
||||
if (!this.inheritsFromMixin) {
|
||||
if (!this.inheritsFromMixin && this.innerClasses.size() == 0) {
|
||||
return;
|
||||
}
|
||||
method.desc = this.transformMethodDescriptor(method.desc);
|
||||
@@ -770,7 +811,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
* @param member Reference to the method or field
|
||||
*/
|
||||
public void transformDescriptor(MemberRef member) {
|
||||
if (!this.inheritsFromMixin) {
|
||||
if (!this.inheritsFromMixin && this.innerClasses.size() == 0) {
|
||||
return;
|
||||
}
|
||||
if (member.isField()) {
|
||||
@@ -786,7 +827,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
* @param typeInsn Type instruction node to transform
|
||||
*/
|
||||
public void transformDescriptor(TypeInsnNode typeInsn) {
|
||||
if (!this.inheritsFromMixin) {
|
||||
if (!this.inheritsFromMixin && this.innerClasses.size() == 0) {
|
||||
return;
|
||||
}
|
||||
typeInsn.desc = this.transformSingleDescriptor(typeInsn.desc, true);
|
||||
@@ -822,6 +863,15 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
return desc;
|
||||
}
|
||||
|
||||
String innerClassName = this.innerClasses.get(type);
|
||||
if (innerClassName != null) {
|
||||
return desc.replace(type, innerClassName);
|
||||
}
|
||||
|
||||
if (this.innerClasses.inverse().containsKey(type)) {
|
||||
return desc;
|
||||
}
|
||||
|
||||
ClassInfo typeInfo = ClassInfo.forName(type);
|
||||
|
||||
if (!typeInfo.isMixin()) {
|
||||
|
||||
@@ -477,6 +477,7 @@ public class MixinTransformer extends TreeTransformer {
|
||||
this.postProcessor = new MixinPostProcessor();
|
||||
|
||||
this.generators.add(ArgsClassGenerator.getInstance());
|
||||
this.generators.add(InnerClassGenerator.getInstance());
|
||||
}
|
||||
|
||||
private IHotSwap initHotSwapper() {
|
||||
|
||||
@@ -67,7 +67,7 @@ abstract class TreeInfo {
|
||||
* @param flags ClassReader flags
|
||||
* @return ASM Tree view of the specified class
|
||||
*/
|
||||
protected static ClassNode getClassNode(byte[] classBytes, int flags) {
|
||||
static ClassNode getClassNode(byte[] classBytes, int flags) {
|
||||
ClassNode classNode = new ClassNode();
|
||||
ClassReader classReader = new ClassReader(classBytes);
|
||||
classReader.accept(classNode, flags);
|
||||
@@ -84,7 +84,7 @@ abstract class TreeInfo {
|
||||
* @throws ClassNotFoundException if the specified class could not be loaded
|
||||
* @throws IOException if an error occurs whilst reading the specified class
|
||||
*/
|
||||
protected static byte[] loadClass(String className, boolean runTransformers) throws ClassNotFoundException, IOException {
|
||||
static byte[] loadClass(String className, boolean runTransformers) throws ClassNotFoundException, IOException {
|
||||
String transformedName = className.replace('/', '.');
|
||||
String name = MixinEnvironment.getCurrentEnvironment().unmap(transformedName);
|
||||
byte[] classBytes = TreeInfo.getClassBytes(name, transformedName);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* This file is part of Mixin, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) SpongePowered <https://www.spongepowered.org>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.transformer.meta;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Decoration annotation used by the mixin inner class generator to mark inner
|
||||
* classes which have been generated from an existing inner class in a mixin
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
public @interface MixinInner {
|
||||
|
||||
/**
|
||||
* The mixin which owns the original inner class
|
||||
*/
|
||||
public String mixin();
|
||||
|
||||
/**
|
||||
* The original name of the inner class
|
||||
*/
|
||||
public String name();
|
||||
|
||||
}
|
||||
@@ -55,4 +55,5 @@ public @interface MixinMerged {
|
||||
* @return mixin priority
|
||||
*/
|
||||
public int priority();
|
||||
|
||||
}
|
||||
@@ -42,6 +42,8 @@ public abstract class Constants {
|
||||
public static final String OBJECT = "Ljava/lang/Object;";
|
||||
public static final String CLASS = "Ljava/lang/Class;";
|
||||
|
||||
public static final String SYNTHETIC_PACKAGE = "org.spongepowered.asm.synthetic";
|
||||
|
||||
private Constants() {}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user