Add Final annotation, version bump to 0.4.13

This commit is contained in:
Mumfrey committed 2016-01-20 15:04:51 +00:00
1 parent 35acd06d4b
commit 8c450d4282
8 files changed
+268 -182

No files matched your search

@@ -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
@@ -0,0 +1,62 @@
/*
* 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;
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:
*
* <ul>
* <li>
* 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 <tt>final</tt>. This
* is required since it is normally desirable to remove the <tt>final</tt>
* modifier from shadow fields to avoid unwanted field initialisers. If
* {@link Option#DEBUG_VERIFY} is <tt>true</tt>, then an
* {@link InvalidMixinException} is thrown.
* </li>
* <li>
* 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.
* </li>
* </ul>
*/
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Final {
}
@@ -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));
}
}
@@ -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.<Integer>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;
}
@@ -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.</p>
*/
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<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;
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);
}
}
}
@@ -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<FieldNode> shadowFields = new ArrayList<FieldNode>();
private final Map<FieldNode, Field> shadowFields = new LinkedHashMap<FieldNode, Field>();
/**
* 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<AbstractInsnNode> 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<FieldNode, Field> 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<MethodNode> getShadowMethods() {
public Collection<MethodNode> getShadowMethods() {
return this.shadowMethods;
}
@@ -738,8 +777,8 @@ public class MixinTargetContext implements IReferenceMapperContext {
*
* @return shadow fields in the mixin
*/
public List<FieldNode> getShadowFields() {
return this.shadowFields;
public Collection<FieldNode> getShadowFields() {
return this.shadowFields.keySet();
}
/**