Support @Unique on fields, bump to 0.5.10

This commit is contained in:
Mumfrey committed 2016-06-17 22:28:49 +01:00
1 parent 2cd02e91a4
commit 75f327fa56
9 files changed
+103 -43

No files matched your search

@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.5.9";
public static final String VERSION = "0.5.10";
// Consts
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
@@ -59,17 +59,29 @@ public final class Mixins {
private Mixins() {}
public static void addConfigurations(String... configFiles) {
MixinEnvironment fallback = MixinEnvironment.getDefaultEnvironment();
for (String configFile : configFiles) {
Mixins.createConfiguration(configFile, fallback);
}
}
/**
* Add a mixin configuration resource
*
* @param configFile path to configuration resource
*/
public static void addConfiguration(String configFile) {
Mixins.addConfiguration(configFile, MixinEnvironment.getDefaultEnvironment());
Mixins.createConfiguration(configFile, MixinEnvironment.getDefaultEnvironment());
}
@Deprecated
static void addConfiguration(String configFile, MixinEnvironment fallback) {
Mixins.createConfiguration(configFile, fallback);
}
@SuppressWarnings("deprecation")
private static void createConfiguration(String configFile, MixinEnvironment fallback) {
Config config = null;
try {
@@ -78,13 +90,19 @@ public final class Mixins {
Mixins.logger.error("Error encountered reading mixin config " + configFile + ": " + ex.getClass().getName() + " " + ex.getMessage(), ex);
}
if (config != null) {
MixinEnvironment env = config.getEnvironment();
if (env != null) {
env.registerConfig(configFile);
}
Mixins.getConfigs().add(config);
Mixins.registerConfiguration(config);
}
private static void registerConfiguration(Config config) {
if (config == null) {
return;
}
MixinEnvironment env = config.getEnvironment();
if (env != null) {
env.registerConfig(config.getName());
}
Mixins.getConfigs().add(config);
}
/**
@@ -30,11 +30,12 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation, when applied to a member method in a mixin, indicates that
* the method <b>should never</b> overwrite a matching method in the target
* class. This indicates that the method differs from the normal "overlay-like"
* behaviour of mixins in general, and should only ever be <em>added</em> to the
* target.
* This annotation, when applied to a member method or field in a mixin,
* indicates that the member <b>should never</b> overwrite a matching membver in
* the target class. This indicates that the member differs from the normal
* "overlay-like" behaviour of mixins in general, and should only ever be
* <em>added</em> to the target. For public fields, the annotation has no
* effect.
*
* <p>Typical usage of this annotation would be to decorate a utility method in
* a mixin, or mark an interface-implementing method which must not overwrite a
@@ -64,7 +65,7 @@ import java.lang.annotation.Target;
* </li>
* </ul>
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Unique {
@@ -46,6 +46,7 @@ import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Member.Type;
import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode;
@@ -210,9 +211,15 @@ public class ClassInfo extends TreeInfo {
*/
private boolean decoratedMutable;
/**
* True if this member is decorated with {@link Unique}
*/
private boolean unique;
protected Member(Member member) {
this(member.type, member.memberName, member.memberDesc, member.modifiers, member.isInjected);
this.currentName = member.currentName;
this.unique = member.unique;
}
protected Member(Type type, String name, String desc, int access) {
@@ -264,6 +271,14 @@ public class ClassInfo extends TreeInfo {
return (this.modifiers & Opcodes.ACC_FINAL) != 0;
}
public boolean isUnique() {
return this.unique;
}
public void setUnique(boolean unique) {
this.unique = unique;
}
public boolean isDecoratedFinal() {
return this.decoratedFinal;
}
@@ -337,40 +352,34 @@ public class ClassInfo extends TreeInfo {
private final List<FrameData> frames;
private boolean unique;
public Method(Member member) {
super(member);
this.frames = member instanceof Method ? ((Method)member).frames : null;
this.unique = member instanceof Method ? ((Method)member).unique : false;
}
public Method(MethodNode method) {
this(method, false);
this.setUnique(ASMHelper.getVisibleAnnotation(method, Unique.class) != null);
}
public Method(MethodNode method, boolean injected) {
super(Type.METHOD, method.name, method.desc, method.access, injected);
this.frames = this.gatherFrames(method);
this.unique = ASMHelper.getVisibleAnnotation(method, Unique.class) != null;
}
public Method(String name, String desc) {
super(Type.METHOD, name, desc, Opcodes.ACC_PUBLIC, false);
this.frames = null;
this.unique = false;
}
public Method(String name, String desc, int access) {
super(Type.METHOD, name, desc, access, false);
this.frames = null;
this.unique = false;
}
public Method(String name, String desc, int access, boolean injected) {
super(Type.METHOD, name, desc, access, injected);
this.frames = null;
this.unique = false;
}
private List<FrameData> gatherFrames(MethodNode method) {
@@ -388,14 +397,6 @@ public class ClassInfo extends TreeInfo {
return this.frames;
}
public boolean isUnique() {
return this.unique;
}
public void setUnique(boolean unique) {
this.unique = unique;
}
@Override
public ClassInfo getOwner() {
return ClassInfo.this;
@@ -451,6 +452,14 @@ public class ClassInfo extends TreeInfo {
public Field(FieldNode field, boolean injected) {
super(Type.FIELD, field.name, field.desc, field.access, injected);
this.setUnique(ASMHelper.getVisibleAnnotation(field, Unique.class) != null);
if (ASMHelper.getVisibleAnnotation(field, Shadow.class) != null) {
boolean decoratedFinal = ASMHelper.getVisibleAnnotation(field, Final.class) != null;
boolean decoratedMutable = ASMHelper.getVisibleAnnotation(field, Mutable.class) != null;
this.setDecoratedFinal(decoratedFinal, decoratedMutable);
}
}
public Field(String name, String desc, int access) {
@@ -46,6 +46,10 @@ public class Config {
this.name = config.getName();
this.config = config;
}
public String getName() {
return this.name;
}
/**
* Access inner config
@@ -36,10 +36,8 @@ import org.spongepowered.asm.lib.tree.FieldInsnNode;
import org.spongepowered.asm.lib.tree.FieldNode;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.Surrogate;
@@ -341,8 +339,8 @@ class MixinPreProcessorStandard {
for (Iterator<FieldNode> iter = this.classNode.fields.iterator(); iter.hasNext();) {
FieldNode mixinField = iter.next();
AnnotationNode shadow = ASMHelper.getVisibleAnnotation(mixinField, Shadow.class);
boolean isFinal = ASMHelper.getVisibleAnnotation(mixinField, Final.class) != null;
boolean isMutable = ASMHelper.getVisibleAnnotation(mixinField, Mutable.class) != null;
boolean isShadow = shadow != null;
if (!this.validateField(context, mixinField, shadow)) {
iter.remove();
continue;
@@ -351,6 +349,10 @@ class MixinPreProcessorStandard {
context.transformDescriptor(mixinField);
Field field = this.mixin.getClassInfo().findField(mixinField);
if (field.isUnique() && isShadow) {
throw new InvalidMixinException(this.mixin, "@Shadow field " + mixinField.name + " cannot be @Unique");
}
FieldNode target = context.findField(mixinField, shadow);
if (target == null) {
if (shadow == null) {
@@ -363,7 +365,29 @@ class MixinPreProcessorStandard {
}
mixinField.name = target.name;
field.renameTo(target.name);
}
}
if (field.isUnique()) {
if ((mixinField.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) != 0) {
String uniqueName = context.getUniqueName(mixinField);
MixinPreProcessorStandard.logger.log(this.mixin.getLoggingLevel(), "Renaming @Unique field {}{} to {} in {}",
mixinField.name, mixinField.desc, uniqueName, this.mixin);
mixinField.name = uniqueName;
field.renameTo(uniqueName);
continue;
}
if (this.strictUnique) {
throw new InvalidMixinException(this.mixin, "Field conflict, @Unique field " + mixinField.name + " in " + this.mixin
+ " cannot overwrite " + target.name + target.desc + " in " + context.getTarget());
}
MixinPreProcessorStandard.logger.warn("Discarding @Unique public field {} in {} because it already exists in {}. "
+ "Note that declared FIELD INITIALISERS will NOT be removed!", mixinField.name, this.mixin, context.getTarget());
iter.remove();
continue;
}
// Check that the shadow field has a matching descriptor
if (!target.desc.equals(mixinField.desc)) {
@@ -382,12 +406,8 @@ class MixinPreProcessorStandard {
// Shadow fields get stripped from the mixin class
iter.remove();
if (shadow != null) {
if (field == null) {
throw new InvalidMixinException(this.mixin, "Unable to locate field surrogate: " + mixinField.name + " in " + this.mixin);
}
field.setDecoratedFinal(isFinal, isMutable);
if (isShadow) {
boolean isFinal = field.isDecoratedFinal();
if (this.verboseLogging && ASMHelper.hasFlag(target, Opcodes.ACC_FINAL) != isFinal) {
String message = isFinal
? "@Shadow field {}::{} is decorated with @Final but target is not final"
@@ -979,6 +979,10 @@ public class MixinTargetContext implements IReferenceMapperContext {
return this.targetClass.getUniqueName(method);
}
public String getUniqueName(FieldNode field) {
return this.targetClass.getUniqueName(field);
}
/**
* Scans the target class for injector methods and prepares discovered
* injectors
@@ -95,9 +95,9 @@ class TargetClassContext {
private final boolean disableHandlerRemap;
/**
* Unique method index
* Unique method and field indices
*/
private int nextUniqueMethodIndex = 0;
private int nextUniqueMethodIndex, nextUniqueFieldIndex;
/**
* True once mixins have been applied to this class
@@ -265,6 +265,10 @@ class TargetClassContext {
return String.format("md%s$%s$%d", this.sessionId.substring(30), method.name, this.nextUniqueMethodIndex++);
}
public String getUniqueName(FieldNode field) {
return String.format("fd%s$%s$%d", this.sessionId.substring(30), field.name, this.nextUniqueFieldIndex++);
}
/**
* Apply mixins to this class
*/