From 8c450d428264f536f138abd4a80d2017f26261b4 Mon Sep 17 00:00:00 2001
From: Mumfrey
Date: Wed, 20 Jan 2016 15:04:51 +0000
Subject: [PATCH] Add Final annotation, version bump to 0.4.13
---
checkstyle.xml | 259 +++++++-----------
gradle.properties | 2 +-
.../asm/launch/MixinBootstrap.java | 2 +-
.../org/spongepowered/asm/mixin/Final.java | 62 +++++
.../asm/mixin/transformer/ClassInfo.java | 37 ++-
.../mixin/transformer/MixinApplicator.java | 8 +-
.../mixin/transformer/MixinPreProcessor.java | 27 +-
.../mixin/transformer/MixinTargetContext.java | 53 +++-
8 files changed, 268 insertions(+), 182 deletions(-)
create mode 100644 src/main/java/org/spongepowered/asm/mixin/Final.java
diff --git a/checkstyle.xml b/checkstyle.xml
index 25b8ebfd..3e06aaf5 100644
--- a/checkstyle.xml
+++ b/checkstyle.xml
@@ -1,171 +1,100 @@
-
-
+
+
-
+ This configuration file was written by the eclipse-cs plugin configuration editor
+-->
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/gradle.properties b/gradle.properties
index ebfaec96..2069d9c3 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -4,5 +4,5 @@ packaging=jar
description=Mixin
url=https://www.spongepowered.org
organization=SpongePowered
-buildVersion=0.4.12
+buildVersion=0.4.13
buildType=SNAPSHOT
diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
index 66339f75..aeaad19a 100644
--- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
+++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
- public static final String VERSION = "0.4.12";
+ public static final String VERSION = "0.4.13";
/**
* Blackboard key where the subsystem version will be stored to indicate
diff --git a/src/main/java/org/spongepowered/asm/mixin/Final.java b/src/main/java/org/spongepowered/asm/mixin/Final.java
new file mode 100644
index 00000000..1b78a4de
--- /dev/null
+++ b/src/main/java/org/spongepowered/asm/mixin/Final.java
@@ -0,0 +1,62 @@
+/*
+ * This file is part of Mixin, licensed under the MIT License (MIT).
+ *
+ * Copyright (c) SpongePowered
+ * 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;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.spongepowered.asm.mixin.MixinEnvironment.Option;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.transformer.InvalidMixinException;
+
+/**
+ * This annotation has two uses:
+ *
+ *
+ * -
+ * On an {@link Shadow} field, it can be used to raise an error-level log
+ * message if any write occurrences appear in the mixin bytecode. This can
+ * be used in place of declaring the field as actually final. This
+ * is required since it is normally desirable to remove the final
+ * modifier from shadow fields to avoid unwanted field initialisers. If
+ * {@link Option#DEBUG_VERIFY} is true, then an
+ * {@link InvalidMixinException} is thrown.
+ *
+ * -
+ * On an {@link Inject injector} or {@link Overwrite overwritten} method,
+ * it is equivalent to setting the priority of the containing mixin to
+ * {@link Integer#MAX_VALUE} but applies only to the annotated method. This
+ * allows methods to mark themselves as effectively final, preventing their
+ * replacement by later mixins with higher priority.
+ *
+ *
+ */
+@Target({ ElementType.METHOD, ElementType.FIELD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Final {
+
+}
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java
index 33e24861..8fdc1a3d 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java
@@ -43,6 +43,7 @@ import org.spongepowered.asm.lib.tree.FieldNode;
import org.spongepowered.asm.lib.tree.FrameNode;
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.transformer.ClassInfo.Member.Type;
@@ -190,6 +191,11 @@ public class ClassInfo extends TreeInfo {
* if the member has been renamed
*/
private String currentName;
+
+ /**
+ * True if this member is decorated with {@link Final}
+ */
+ private boolean decoratedFinal;
protected Member(Member member) {
this(member.type, member.memberName, member.memberDesc, member.modifiers, member.isInjected);
@@ -237,6 +243,22 @@ public class ClassInfo extends TreeInfo {
return (this.modifiers & Opcodes.ACC_STATIC) != 0;
}
+ public boolean isAbstract() {
+ return (this.modifiers & Opcodes.ACC_ABSTRACT) != 0;
+ }
+
+ public boolean isFinal() {
+ return (this.modifiers & Opcodes.ACC_FINAL) != 0;
+ }
+
+ public boolean isDecoratedFinal() {
+ return this.decoratedFinal;
+ }
+
+ public void setDecoratedFinal(boolean decoratedFinal) {
+ this.decoratedFinal = decoratedFinal;
+ }
+
public boolean matchesFlags(int flags) {
return (((~this.modifiers | (flags & ClassInfo.INCLUDE_PRIVATE)) & ClassInfo.INCLUDE_PRIVATE) != 0
&& ((~this.modifiers | (flags & ClassInfo.INCLUDE_STATIC)) & ClassInfo.INCLUDE_STATIC) != 0);
@@ -282,11 +304,11 @@ public class ClassInfo extends TreeInfo {
@Override
public String toString() {
- return this.memberName + this.memberDesc;
+ return String.format(this.getDisplayFormat(), this.memberName, this.memberDesc);
}
- public boolean isAbstract() {
- return (this.modifiers & Opcodes.ACC_ABSTRACT) != 0;
+ protected String getDisplayFormat() {
+ return "%s%s";
}
}
@@ -419,6 +441,11 @@ public class ClassInfo extends TreeInfo {
return super.equals(obj);
}
+
+ @Override
+ protected String getDisplayFormat() {
+ return "%s:%s";
+ }
}
private static final Logger logger = LogManager.getLogger("mixin");
@@ -579,9 +606,7 @@ public class ClassInfo extends TreeInfo {
}
}
- if ((field.access & Opcodes.ACC_STATIC) == 0) {
- this.fields.add(new Field(field, this.isMixin));
- }
+ this.fields.add(new Field(field, this.isMixin));
}
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java
index 4b79f97a..142ada0b 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java
@@ -46,6 +46,7 @@ import org.spongepowered.asm.lib.tree.LabelNode;
import org.spongepowered.asm.lib.tree.LineNumberNode;
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.Intrinsic;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
@@ -380,7 +381,12 @@ public class MixinApplicator {
int priority = ASMHelper.getAnnotationValue(merged, "priority");
if (priority >= mixin.getPriority() && !owner.equals(mixin.getClassName())) {
- this.logger.warn("Method overwrite conflict for {}, previously written by {}. Skipping method.", method.name, owner);
+ this.logger.warn("Method overwrite conflict for {} in {}, previously written by {}. Skipping method.", method.name, mixin, owner);
+ return true;
+ }
+
+ if (ASMHelper.getVisibleAnnotation(target, Final.class) != null) {
+ this.logger.warn("Method overwrite conflict for @Final method {} in {} declared by {}. Skipping method.", method.name, mixin, owner);
return true;
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java
index fbb6d89a..dfa23fbe 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java
@@ -30,6 +30,8 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.AnnotationNode;
@@ -38,10 +40,12 @@ 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.Overwrite;
import org.spongepowered.asm.mixin.RemapperChain;
import org.spongepowered.asm.mixin.Shadow;
+import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
@@ -68,6 +72,11 @@ import org.spongepowered.asm.util.Constants;
* class members for obvious reasons.
*/
class MixinPreProcessor {
+
+ /**
+ * Logger
+ */
+ private static final Logger logger = LogManager.getLogger("mixin");
/**
* The mixin
@@ -79,11 +88,14 @@ class MixinPreProcessor {
*/
private final ClassNode classNode;
+ private final boolean verboseLogging;
+
private boolean prepared, attached;
MixinPreProcessor(MixinInfo mixin, ClassNode classNode) {
this.mixin = mixin;
this.classNode = classNode;
+ this.verboseLogging = mixin.getParent().getEnvironment().getOption(Option.DEBUG_VERBOSE);
}
/**
@@ -212,6 +224,7 @@ class MixinPreProcessor {
for (Iterator 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;
if (!this.validateField(context, mixinField, shadow)) {
iter.remove();
continue;
@@ -252,7 +265,19 @@ class MixinPreProcessor {
iter.remove();
if (shadow != null) {
- context.addShadowField(mixinField);
+ if (field == null) {
+ throw new InvalidMixinException(this.mixin, "Unable to locate field surrogate: " + mixinField.name + " in " + this.mixin);
+ }
+ field.setDecoratedFinal(isFinal);
+
+ if (this.verboseLogging && MixinApplicator.hasFlag(target, Opcodes.ACC_FINAL) != isFinal) {
+ String message = isFinal
+ ? "@Shadow field {}::{} is decorated with @Final but target is not final"
+ : "@Shadow target {}::{} is final but shadow is not decorated with @Final";
+ MixinPreProcessor.logger.warn(message, this.mixin, mixinField.name);
+ }
+
+ context.addShadowField(mixinField, field);
}
}
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
index 3c44b2a5..4b12d9db 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
@@ -25,19 +25,25 @@
package org.spongepowered.asm.mixin.transformer;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.lib.Handle;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.*;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel;
+import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.SoftOverride;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.InjectionError;
@@ -45,6 +51,7 @@ import org.spongepowered.asm.mixin.injection.InjectionValidationException;
import org.spongepowered.asm.mixin.injection.InjectorGroupInfo;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.asm.mixin.injection.struct.Target;
+import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal;
import org.spongepowered.asm.util.ASMHelper;
import org.spongepowered.asm.util.Constants;
@@ -58,6 +65,11 @@ import org.spongepowered.asm.util.Constants;
* in the mixin to the appropriate members in the target class hierarchy.
*/
public class MixinTargetContext implements IReferenceMapperContext {
+
+ /**
+ * Logger
+ */
+ private static final Logger logger = LogManager.getLogger("mixin");
/**
* Mixin info
@@ -87,7 +99,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
/**
* Shadow field list
*/
- private final List shadowFields = new ArrayList();
+ private final Map shadowFields = new LinkedHashMap();
/**
* Information about methods in the target class, used to keep track of
@@ -141,10 +153,10 @@ public class MixinTargetContext implements IReferenceMapperContext {
}
/**
- * @param field
+ * @param fieldNode
*/
- void addShadowField(FieldNode field) {
- this.shadowFields.add(field);
+ void addShadowField(FieldNode fieldNode, Field fieldInfo) {
+ this.shadowFields.put(fieldNode, fieldInfo);
}
/* (non-Javadoc)
@@ -286,6 +298,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
this.transformMethodRef(method, iter, new MemberRef.Method((MethodInsnNode)insn));
} else if (insn instanceof FieldInsnNode) {
this.transformFieldRef(method, iter, new MemberRef.Field((FieldInsnNode)insn));
+ this.checkFinal(method, iter, (FieldInsnNode)insn);
} else if (insn instanceof TypeInsnNode) {
this.transformTypeNode(method, iter, (TypeInsnNode)insn, lastInsn);
} else if (insn instanceof LdcInsnNode) {
@@ -370,6 +383,32 @@ public class MixinTargetContext implements IReferenceMapperContext {
}
}
+ private void checkFinal(MethodNode method, Iterator iter, FieldInsnNode fieldNode) {
+ if (!fieldNode.owner.equals(this.targetClass.name)) {
+ return;
+ }
+
+ int opcode = fieldNode.getOpcode();
+ if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) {
+ return;
+ }
+
+ for (Entry shadow : this.shadowFields.entrySet()) {
+ FieldNode shadowField = shadow.getKey();
+ if (!shadowField.desc.equals(fieldNode.desc) || !shadowField.name.equals(fieldNode.name)) {
+ continue;
+ }
+ if (shadow.getValue().isDecoratedFinal()) {
+ MixinTargetContext.logger.error("Write access detected to @Final field {} in {}::{}", shadow.getValue(), this.mixin, method.name);
+ if (this.mixin.getParent().getEnvironment().getOption(Option.DEBUG_VERIFY)) {
+ throw new InvalidMixinException(this.mixin, "Write access detected to @Final field " + shadow.getValue() + " in " + this.mixin
+ + "::" + method.name);
+ }
+ }
+ return;
+ }
+ }
+
/**
* Transforms type operations (eg. cast, instanceof) in the method being
* processed. Changes references to mixin classes to that of the appropriate
@@ -720,7 +759,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
*
* @return shadow methods in the mixin
*/
- public List getShadowMethods() {
+ public Collection getShadowMethods() {
return this.shadowMethods;
}
@@ -738,8 +777,8 @@ public class MixinTargetContext implements IReferenceMapperContext {
*
* @return shadow fields in the mixin
*/
- public List getShadowFields() {
- return this.shadowFields;
+ public Collection getShadowFields() {
+ return this.shadowFields.keySet();
}
/**