Better wiring for injector annotations, fixes issue w/missed annotations

ModifyVariable and ModifyConstant were previously missing from declared
annotation types. Now delegate to the same registration logic as runtime
This commit is contained in:
Mumfrey committed 2019-11-15 12:58:06 +00:00
1 parent eb7a548f92
commit 6d9fcfe635
6 files changed
+217 -41

No files matched your search

@@ -60,6 +60,7 @@ import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.asm.util.ITokenProvider;
import org.spongepowered.asm.util.VersionNumber;
import org.spongepowered.asm.util.logging.MessageRouter;
import org.spongepowered.tools.obfuscation.interfaces.IJavadocProvider;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator;
@@ -144,9 +145,12 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
private AnnotatedMixins(ProcessingEnvironment processingEnv) {
this.env = this.detectEnvironment(processingEnv);
this.processingEnv = processingEnv;
MessageRouter.setMessager(processingEnv.getMessager());
this.printMessage(Kind.NOTE, "SpongePowered MIXIN Annotation Processor Version=" + MixinBootstrap.VERSION);
this.checkPluginVersion(this.getOption(SupportedOptions.PLUGIN_VERSION));
String pluginVersion = this.checkPluginVersion(this.getOption(SupportedOptions.PLUGIN_VERSION));
String pluginVersionString = pluginVersion != null ? String.format(" (MixinGradle Version=%s)", pluginVersion) : "";
this.printMessage(Kind.NOTE, "SpongePowered MIXIN Annotation Processor Version=" + MixinBootstrap.VERSION + pluginVersionString);
this.targets = this.initTargetMap();
this.obf = new ObfuscationManager(this);
@@ -165,9 +169,9 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
* the user if the version of MixinGradle is out of date relative to the
* Mixin library version
*/
private void checkPluginVersion(String version) {
private String checkPluginVersion(String version) {
if (version == null) {
return;
return null;
}
VersionNumber pluginVersion = VersionNumber.parse(version);
@@ -176,6 +180,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
this.printMessage(Kind.WARNING, String.format("MixinGradle version %s is out of date. Update to the recommended version %s",
pluginVersion, recommendedVersion));
}
return pluginVersion.toString();
}
protected TargetMap initTargetMap() {
@@ -25,10 +25,10 @@
package org.spongepowered.tools.obfuscation;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
@@ -37,26 +37,25 @@ import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils;
/**
* Annotation processor which finds {@link Inject} and {@link At} annotations in
* mixin classes and generates obfuscation mappings
* Annotation processor which finds injector annotations in mixin classes and
* generates obfuscation mappings
*/
@SupportedAnnotationTypes({
"org.spongepowered.asm.mixin.injection.Inject",
"org.spongepowered.asm.mixin.injection.ModifyArg",
"org.spongepowered.asm.mixin.injection.ModifyArgs",
"org.spongepowered.asm.mixin.injection.Redirect",
"org.spongepowered.asm.mixin.injection.At"
})
public class MixinObfuscationProcessorInjection extends MixinObfuscationProcessor {
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> supportedAnnotationTypes = new HashSet<String>();
supportedAnnotationTypes.add(At.class.getName());
for (Class<? extends Annotation> annotationType : InjectionInfo.getRegisteredAnnotations()) {
supportedAnnotationTypes.add(annotationType.getName());
}
return supportedAnnotationTypes;
}
/* (non-Javadoc)
* @see javax.annotation.processing.AbstractProcessor
@@ -71,12 +70,9 @@ public class MixinObfuscationProcessorInjection extends MixinObfuscationProcesso
}
this.processMixins(roundEnv);
this.processInjectors(roundEnv, Inject.class);
this.processInjectors(roundEnv, ModifyArg.class);
this.processInjectors(roundEnv, ModifyArgs.class);
this.processInjectors(roundEnv, Redirect.class);
this.processInjectors(roundEnv, ModifyVariable.class);
this.processInjectors(roundEnv, ModifyConstant.class);
for (Class<? extends Annotation> annotationType : InjectionInfo.getRegisteredAnnotations()) {
this.processInjectors(roundEnv, annotationType);
}
this.postProcess(roundEnv);
return true;
@@ -115,4 +111,5 @@ public class MixinObfuscationProcessorInjection extends MixinObfuscationProcesso
}
}
}
}
@@ -64,7 +64,8 @@ public final class SupportedOptions {
SupportedOptions.OVERWRITE_ERROR_LEVEL,
SupportedOptions.DEFAULT_OBFUSCATION_ENV,
SupportedOptions.DEPENDENCY_TARGETS_FILE,
SupportedOptions.MAPPING_TYPES
SupportedOptions.MAPPING_TYPES,
SupportedOptions.PLUGIN_VERSION
);
options.addAll(
ObfuscationServices.getInstance().getSupportedOptions()
@@ -33,8 +33,8 @@ import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.Map.Entry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.tools.Diagnostic.Kind;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;
@@ -67,8 +67,11 @@ import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.asm.ASM;
import org.spongepowered.asm.util.asm.ElementNode;
import org.spongepowered.asm.util.asm.MethodNodeEx;
import org.spongepowered.asm.util.logging.MessageRouter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
/**
* Contructs information about an injection from an {@link Inject} annotation
@@ -637,7 +640,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
}
static String describeInjector(IMixinContext mixin, AnnotationNode annotation, MethodNode method) {
return String.format("%s->@%s::%s%s", mixin.toString(), Bytecode.getSimpleName(annotation), method.name, method.desc);
return String.format("%s->@%s::%s%s", mixin.toString(), Bytecode.getSimpleName(annotation), MethodNodeEx.getName(method), method.desc);
}
/**
@@ -662,7 +665,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
}
return sb.toString();
}
/**
* Register an injector info class. The supplied class must be decorated
* with an {@link AnnotationType} annotation for registration purposes.
@@ -675,8 +678,6 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
throw new IllegalArgumentException("Injection info class " + type + " is not annotated with @AnnotationType");
}
Logger logger = LogManager.getLogger("mixin");
InjectorEntry entry;
try {
entry = new InjectorEntry(annotationType.value(), type);
@@ -684,11 +685,12 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
throw new MixinError("InjectionInfo class " + type.getName() + " is missing a valid constructor");
}
InjectorEntry existing = InjectionInfo.registry.get(entry.simpleName);
if (existing != null && !existing.type.equals(type)) {
logger.debug("WARNING: Overriding InjectionInfo for {} with {} (previously {})", annotationType.value().getSimpleName(), type.getName(),
existing.type.getName());
if (existing != null) { // && !existing.type.equals(type)) {
MessageRouter.getMessager().printMessage(Kind.WARNING, String.format("Overriding InjectionInfo for @%s with %s (previously %s)",
annotationType.value().getSimpleName(), type.getName(), existing.type.getName()));
} else {
logger.debug("Registering new injector for {} with {}", annotationType.value().getSimpleName(), type.getName());
MessageRouter.getMessager().printMessage(Kind.OTHER, String.format("Registering new injector for @%s with %s",
annotationType.value().getSimpleName(), type.getName()));
}
InjectionInfo.registry.put(entry.simpleName, entry);
@@ -699,5 +701,9 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
}
InjectionInfo.registeredAnnotations = annotations.toArray(InjectionInfo.registeredAnnotations);
}
public static Set<Class<? extends Annotation>> getRegisteredAnnotations() {
return ImmutableSet.<Class<? extends Annotation>>copyOf(InjectionInfo.registeredAnnotations);
}
}
@@ -31,10 +31,11 @@ import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.logging.MessageRouter;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
@@ -255,7 +256,6 @@ public final class ReferenceMapper implements IReferenceMapper, Serializable {
* @return new refmap or {@link #DEFAULT_MAPPER} if reading fails
*/
public static ReferenceMapper read(String resourcePath) {
Logger logger = LogManager.getLogger("mixin");
Reader reader = null;
try {
IMixinService service = MixinService.getService();
@@ -267,9 +267,11 @@ public final class ReferenceMapper implements IReferenceMapper, Serializable {
return mapper;
}
} catch (JsonParseException ex) {
logger.error("Invalid REFMAP JSON in " + resourcePath + ": " + ex.getClass().getName() + " " + ex.getMessage());
MessageRouter.getMessager().printMessage(Kind.ERROR, String.format("Invalid REFMAP JSON in %s: %s %s",
resourcePath, ex.getClass().getName(), ex.getMessage()));
} catch (Exception ex) {
logger.error("Failed reading REFMAP JSON from " + resourcePath + ": " + ex.getClass().getName() + " " + ex.getMessage());
MessageRouter.getMessager().printMessage(Kind.ERROR, String.format("Failed reading REFMAP JSON from %s: %s %s",
resourcePath, ex.getClass().getName(), ex.getMessage()));
} finally {
Closeables.closeQuietly(reader);
}
@@ -0,0 +1,165 @@
/*
* 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.util.logging;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic.Kind;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Logging router for objects which may need to log messages during standard
* runtime or during Annotation Processor sessions. Provides a single interface
* to obtain a {@link Messager} to write log entries in either environment.
*/
public final class MessageRouter {
/**
* Implementation of Messager which writes to a logger
*/
static class LoggingMessager implements Messager {
/**
* Logger
*/
private static final Logger logger = LogManager.getLogger("mixin");
@Override
public void printMessage(Kind kind, CharSequence msg) {
LoggingMessager.logger.log(LoggingMessager.messageKindToLoggingLevel(kind), msg);
}
@Override
public void printMessage(Kind kind, CharSequence msg, Element e) {
LoggingMessager.logger.log(LoggingMessager.messageKindToLoggingLevel(kind), msg);
}
@Override
public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a) {
LoggingMessager.logger.log(LoggingMessager.messageKindToLoggingLevel(kind), msg);
}
@Override
public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
LoggingMessager.logger.log(LoggingMessager.messageKindToLoggingLevel(kind), msg);
}
private static Level messageKindToLoggingLevel(Kind kind) {
switch (kind) {
case ERROR:
return Level.ERROR;
case WARNING:
case MANDATORY_WARNING:
return Level.WARN;
case NOTE:
return Level.INFO;
case OTHER:
default:
return Level.DEBUG;
}
}
}
/**
* Debug-level messages are expressed as {@link Kind#OTHER} but we don't
* want to emit them. This wrapper just intercepts debug messages so they
* don't spam the compiler output.
*/
static class DebugInterceptingMessager implements Messager {
private final Messager wrapped;
DebugInterceptingMessager(Messager messager) {
this.wrapped = messager;
}
@Override
public void printMessage(Kind kind, CharSequence msg) {
if (kind != Kind.OTHER) {
this.wrapped.printMessage(kind, msg);
}
}
@Override
public void printMessage(Kind kind, CharSequence msg, Element e) {
if (kind != Kind.OTHER) {
this.wrapped.printMessage(kind, msg, e);
}
}
@Override
public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a) {
if (kind != Kind.OTHER) {
this.wrapped.printMessage(kind, msg, e, a);
}
}
@Override
public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
if (kind != Kind.OTHER) {
this.wrapped.printMessage(kind, msg, e, a, v);
}
}
}
/**
* Current message sink
*/
private static Messager messager;
/**
* Utility class
*/
private MessageRouter() {}
/**
* Get the current messager. If no messager is available the returned
* Messager sinks messages to a log4j2 logger
*/
public static Messager getMessager() {
if (MessageRouter.messager == null) {
MessageRouter.messager = new LoggingMessager();
}
return MessageRouter.messager;
}
/**
* Set the messager to use, this should only be called by the AP
*
* @param messager new messager to sink messages, can be null to revert to
* log4j2 logging
*/
public static void setMessager(Messager messager) {
MessageRouter.messager = messager == null ? null : new DebugInterceptingMessager(messager);
}
}