diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index fa6ebde3..9261fa91 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -28,7 +28,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import javax.annotation.processing.Messager; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; @@ -44,6 +43,7 @@ import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor. import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementInjectionPoint; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementInjector; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerOverwrite.AnnotatedElementOverwrite; +import org.spongepowered.tools.obfuscation.interfaces.IMessagerSuppressible; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator.ValidationPass; @@ -68,7 +68,7 @@ class AnnotatedMixin { /** * Messager */ - private final Messager messager; + private final IMessagerSuppressible messager; /** * Type handle provider diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java index b32dc0f5..53827a82 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java @@ -374,7 +374,7 @@ abstract class AnnotatedMixinElementHandler { this.ap.printMessage(Kind.ERROR, ex.getMessage(), method, annotation.asMirror()); } } catch (InvalidConstraintException ex) { - this.ap.printMessage(Kind.WARNING, ex.getMessage(), method, annotation.asMirror(), "constraints"); + this.ap.printMessage(Kind.WARNING, ex.getMessage(), method, annotation.asMirror(), SuppressedBy.CONSTRAINTS); } } @@ -421,7 +421,7 @@ abstract class AnnotatedMixinElementHandler { this.validateMethodVisibility(method, annotation, type, target, targetMethod); } } else if (!merge) { - this.printMessage(Kind.WARNING, "Cannot find target for " + type + " method in " + target, method, annotation, "target"); + this.printMessage(Kind.WARNING, "Cannot find target for " + type + " method in " + target, method, annotation, SuppressedBy.TARGET); } } } @@ -436,9 +436,11 @@ abstract class AnnotatedMixinElementHandler { Visibility visMethod = TypeUtils.getVisibility(method); String visibility = "visibility of " + visTarget + " method in " + target; if (visTarget.ordinal() > visMethod.ordinal()) { - this.printMessage(Kind.WARNING, visMethod + " " + type + " method cannot reduce " + visibility, method, annotation, "visibility"); + this.printMessage(Kind.WARNING, visMethod + " " + type + " method cannot reduce " + visibility, method, annotation, + SuppressedBy.VISIBILITY); } else if (visTarget == Visibility.PRIVATE && visMethod.ordinal() > visTarget.ordinal()) { - this.printMessage(Kind.WARNING, visMethod + " " + type + " method will upgrade " + visibility, method, annotation, "visibility"); + this.printMessage(Kind.WARNING, visMethod + " " + type + " method will upgrade " + visibility, method, annotation, + SuppressedBy.VISIBILITY); } } @@ -469,7 +471,8 @@ abstract class AnnotatedMixinElementHandler { } if (targetField == null) { - this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " field in " + target, field, annotation.asMirror(), "target"); + this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " field in " + target, field, annotation.asMirror(), + SuppressedBy.TARGET); } } } @@ -493,12 +496,13 @@ abstract class AnnotatedMixinElementHandler { MethodHandle targetMethod = target.findMethod(nameRef.getName(), signature); if (targetMethod == null) { - this.ap.printMessage(Kind.WARNING, "Cannot find target method for " + type + " in " + target, method, inject.asMirror(), "target"); + this.ap.printMessage(Kind.WARNING, "Cannot find target method for " + type + " in " + target, method, inject.asMirror(), + SuppressedBy.TARGET); } } } - private void printMessage(Kind kind, String msg, Element e, AnnotationHandle annotation, String suppressedBy) { + private void printMessage(Kind kind, String msg, Element e, AnnotationHandle annotation, SuppressedBy suppressedBy) { if (annotation == null) { this.ap.printMessage(kind, msg, e, suppressedBy); } else { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java index 5966ba34..6f94957a 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java @@ -294,7 +294,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler ObfuscationData remapped = this.obf.getDataProvider().getRemappedMethod(m); if (remapped.isEmpty()) { this.ap.printMessage(Kind.WARNING, "Cannot find class mapping for " + subject + " '" + target + "'", elem.getElement(), - elem.getAnnotation().asMirror(), "mapping"); + elem.getAnnotation().asMirror(), SuppressedBy.MAPPING); return; } @@ -346,7 +346,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler ObfuscationData obfFieldData = this.obf.getDataProvider().getObfFieldRecursive(targetMember); if (obfFieldData.isEmpty()) { this.ap.printMessage(Kind.WARNING, "Cannot find field mapping for " + subject + " '" + reference + "'", elem.getElement(), - errorsOn, "mapping"); + errorsOn, SuppressedBy.MAPPING); return; } this.obf.getReferenceManager().addFieldMapping(this.classRef, reference, targetMember, obfFieldData); @@ -355,7 +355,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler if (obfMethodData.isEmpty()) { if (targetMember.getOwner() == null || !targetMember.getOwner().startsWith("java/lang/")) { this.ap.printMessage(Kind.WARNING, "Cannot find method mapping for " + subject + " '" + reference + "'", elem.getElement(), - errorsOn, "mapping"); + errorsOn, SuppressedBy.MAPPING); return; } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java index 1905baf7..ced0cb81 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java @@ -86,16 +86,16 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler String javadoc = this.ap.getJavadocProvider().getJavadoc(elem.getElement()); if (javadoc == null) { - this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing javadoc comment", elem.getElement(), "overwrite"); + this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing javadoc comment", elem.getElement(), SuppressedBy.OVERWRITE); return; } if (!javadoc.toLowerCase(Locale.ROOT).contains("@author")) { - this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @author tag", elem.getElement(), "overwrite"); + this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @author tag", elem.getElement(), SuppressedBy.OVERWRITE); } if (!javadoc.toLowerCase(Locale.ROOT).contains("@reason")) { - this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @reason tag", elem.getElement(), "overwrite"); + this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @reason tag", elem.getElement(), SuppressedBy.OVERWRITE); } } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index fbe9bf62..fdbfee26 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -513,11 +513,11 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider return annotation.getBoolean("remap", mixinClass.remap()); } - private static boolean shouldSuppress(Element element, String suppressedBy) { + private static boolean shouldSuppress(Element element, SuppressedBy suppressedBy) { if (element == null || suppressedBy == null) { return false; } - if (AnnotationHandle.of(element, SuppressWarnings.class).getList().contains(suppressedBy)) { + if (AnnotationHandle.of(element, SuppressWarnings.class).getList().contains(suppressedBy.getToken())) { return true; } return AnnotatedMixins.shouldSuppress(element.getEnclosingElement(), suppressedBy); @@ -545,7 +545,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider * Print a message to the AP messager */ @Override - public void printMessage(Kind kind, CharSequence msg, Element element, String suppressedBy) { + public void printMessage(Kind kind, CharSequence msg, Element element, SuppressedBy suppressedBy) { if (kind != Kind.WARNING || !AnnotatedMixins.shouldSuppress(element, suppressedBy)) { this.processingEnv.getMessager().printMessage(kind, msg, element); } @@ -563,7 +563,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider * Print a message to the AP messager */ @Override - public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, String suppressedBy) { + public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, SuppressedBy suppressedBy) { if (kind != Kind.WARNING || !AnnotatedMixins.shouldSuppress(element, suppressedBy)) { this.processingEnv.getMessager().printMessage(kind, msg, element, annotation); } @@ -581,7 +581,8 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider * Print a message to the AP messager */ @Override - public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, AnnotationValue value, String suppressedBy) { + public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, AnnotationValue value, + SuppressedBy suppressedBy) { if (kind != Kind.WARNING || !AnnotatedMixins.shouldSuppress(element, suppressedBy)) { this.processingEnv.getMessager().printMessage(kind, msg, element, annotation, value); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/SuppressedBy.java b/src/ap/java/org/spongepowered/tools/obfuscation/SuppressedBy.java new file mode 100644 index 00000000..8fdcc74d --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/SuppressedBy.java @@ -0,0 +1,75 @@ +/* + * 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.tools.obfuscation; + +/** + * A centralised list of tokens supported by the AP for use in + * {@link SuppressWarnings} annotations. Collected here mainly for + * self-documentation purposes. + */ +public enum SuppressedBy { + + /** + * Suppress warnings for constraint violations + */ + CONSTRAINTS("constraints"), + + /** + * Suppress warnings about overwrite visibility upgrading/downgrading target + * visibility + */ + VISIBILITY("visibility"), + + /** + * Suppress warnings when an injector target cannot be found + */ + TARGET("target"), + + /** + * Suppress warnings when a class, method or field mapping cannot be located + */ + MAPPING("mapping"), + + /** + * Suppress warnings for when an @Overwrite method is missing + * javadoc, or author or reason tags + */ + OVERWRITE("overwrite"); + + private final String token; + + private SuppressedBy(String token) { + this.token = token; + } + + /** + * Get the string token which is used in the {@link SuppressWarnings} + * annotation + */ + public String getToken() { + return this.token; + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerSuppressible.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerSuppressible.java index 4295e676..9f4f8500 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerSuppressible.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerSuppressible.java @@ -30,6 +30,8 @@ import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.tools.Diagnostic; +import org.spongepowered.tools.obfuscation.SuppressedBy; + /** * An extended {@link Messager} which supports messages that can be suppressed * using the standard Java {@link SuppressWarnings} annotation. @@ -46,7 +48,7 @@ public interface IMessagerSuppressible extends Messager { * @param suppressedBy the {@link SuppressWarnings} value which will silence * this message */ - void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, String suppressedBy); + void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, SuppressedBy suppressedBy); /** * Prints a message of the specified kind at the location of the @@ -59,7 +61,7 @@ public interface IMessagerSuppressible extends Messager { * @param suppressedBy the {@link SuppressWarnings} value which will silence * this message */ - void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, String suppressedBy); + void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, SuppressedBy suppressedBy); /** * Prints a message of the specified kind at the location of the @@ -74,7 +76,7 @@ public interface IMessagerSuppressible extends Messager { * @param suppressedBy the {@link SuppressWarnings} value which will silence * this message */ - void printMessage(Diagnostic.Kind kind,CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v, String suppressedBy); + void printMessage(Diagnostic.Kind kind,CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v, SuppressedBy suppressedBy); }