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

@@ -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);
}
}