Allow demoting warnings for decorated Final fields using Mutable, re #99

This commit is contained in:
Mumfrey committed 2016-01-27 14:56:53 +00:00
1 parent 93dca4fc57
commit 529c8ad202
4 files changed
+70 -10

No files matched your search

@@ -0,0 +1,40 @@
/*
* 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;
/**
* Use in conjunction with {@link Final} to indicate that whilst a field is
* final in the target class, mutation within the mixin is intentional.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Mutable {
}
@@ -45,6 +45,7 @@ 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.Mutable;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Member.Type;
import com.google.common.collect.ImmutableList;
@@ -197,6 +198,11 @@ public class ClassInfo extends TreeInfo {
*/
private boolean decoratedFinal;
/**
* True if this member is decorated with {@link Mutable}
*/
private boolean decoratedMutable;
protected Member(Member member) {
this(member.type, member.memberName, member.memberDesc, member.modifiers, member.isInjected);
this.currentName = member.currentName;
@@ -254,9 +260,14 @@ public class ClassInfo extends TreeInfo {
public boolean isDecoratedFinal() {
return this.decoratedFinal;
}
public boolean isDecoratedMutable() {
return this.decoratedMutable;
}
public void setDecoratedFinal(boolean decoratedFinal) {
public void setDecoratedFinal(boolean decoratedFinal, boolean decoratedMutable) {
this.decoratedFinal = decoratedFinal;
this.decoratedMutable = decoratedMutable;
}
public boolean matchesFlags(int flags) {
@@ -42,10 +42,11 @@ 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.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;
@@ -245,6 +246,7 @@ class MixinPreProcessor {
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;
if (!this.validateField(context, mixinField, shadow)) {
iter.remove();
continue;
@@ -288,7 +290,7 @@ class MixinPreProcessor {
if (field == null) {
throw new InvalidMixinException(this.mixin, "Unable to locate field surrogate: " + mixinField.name + " in " + this.mixin);
}
field.setDecoratedFinal(isFinal);
field.setDecoratedFinal(isFinal, isMutable);
if (this.verboseLogging && MixinApplicator.hasFlag(target, Opcodes.ACC_FINAL) != isFinal) {
String message = isFinal
@@ -394,15 +394,22 @@ public class MixinTargetContext implements IReferenceMapperContext {
}
for (Entry<FieldNode, Field> shadow : this.shadowFields.entrySet()) {
FieldNode shadowField = shadow.getKey();
if (!shadowField.desc.equals(fieldNode.desc) || !shadowField.name.equals(fieldNode.name)) {
FieldNode shadowFieldNode = shadow.getKey();
if (!shadowFieldNode.desc.equals(fieldNode.desc) || !shadowFieldNode.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);
Field shadowField = shadow.getValue();
if (shadowField.isDecoratedFinal()) {
if (shadowField.isDecoratedMutable()) {
if (this.mixin.getParent().getEnvironment().getOption(Option.DEBUG_VERBOSE)) {
MixinTargetContext.logger.warn("Write access to @Mutable @Final field {} in {}::{}", shadowField, this.mixin, method.name);
}
} else {
MixinTargetContext.logger.error("Write access detected to @Final field {} in {}::{}", shadowField, this.mixin, method.name);
if (this.mixin.getParent().getEnvironment().getOption(Option.DEBUG_VERIFY)) {
throw new InvalidMixinException(this.mixin, "Write access detected to @Final field " + shadowField + " in " + this.mixin
+ "::" + method.name);
}
}
}
return;