Aggressively resolve soft targets containing '$' and other minor fixes

Fixes #390
This commit is contained in:
Mumfrey committed 2020-05-02 12:34:55 +01:00
1 parent 4585277f9f
commit b5299bfebc
5 files changed
+82 -15

No files matched your search

@@ -225,6 +225,10 @@ class AnnotatedMixin {
} else if (type == null) {
this.printMessage(Kind.ERROR, "Mixin target " + softTarget + " could not be found", this);
return null;
} else if (type.isImaginary()) {
this.printMessage(Kind.WARNING, "Mixin target " + softTarget + " could not be fully resolved.", this,
SuppressedBy.UNRESOLVABLE_TARGET);
return null;
} else if (type.isPublic()) {
SuppressedBy suppressedBy = (type.getPackage().isUnnamed()) ? SuppressedBy.DEFAULT_PACKAGE : SuppressedBy.PUBLIC_TARGET;
this.printMessage(Kind.WARNING, "Mixin target " + softTarget + " is public and must be specified in value", this, suppressedBy);
@@ -105,12 +105,12 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH
return TypeUtils.getTypeName(this.getTargetType());
}
public String getAccessorDesc() {
public String getTargetDesc() {
return TypeUtils.getInternalName(this.getTargetType());
}
public ITargetSelectorRemappable getContext() {
return new MemberInfo(this.getTargetName(), null, this.getAccessorDesc());
return new MemberInfo(this.getTargetName(), null, this.getTargetDesc());
}
public AccessorType getAccessorType() {
@@ -186,8 +186,8 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH
}
@Override
public String getAccessorDesc() {
return TypeUtils.getDescriptor(this.getElement());
public String getTargetDesc() {
return this.getDesc();
}
@Override
@@ -294,7 +294,7 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH
return;
}
targetField = new FieldHandle(target.getName(), elem.getTargetName(), elem.getDesc());
targetField = new FieldHandle(target.getName(), elem.getTargetName(), elem.getTargetDesc());
}
if (!elem.shouldRemap()) {
@@ -326,7 +326,7 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH
return;
}
targetMethod = new MethodHandle(target, elem.getTargetName(), elem.getDesc());
targetMethod = new MethodHandle(target, elem.getTargetName(), elem.getTargetDesc());
}
if (!elem.shouldRemap()) {
@@ -624,7 +624,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
name = name.replace('/', '.');
Elements elements = this.processingEnv.getElementUtils();
TypeElement element = elements.getTypeElement(name);
TypeElement element = this.getTypeElement(name, elements);
if (element != null) {
try {
return new TypeHandle(element);
@@ -645,6 +645,66 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
return null;
}
/**
* Get a TypeElement representing another type in the current processing
* environment. This method attempts substitution of '$' symbols appearing
* in the class name when requesting TypeElements from the processing env.
*/
public TypeElement getTypeElement(String name) {
return this.getTypeElement(name.replace('/', '.'), this.processingEnv.getElementUtils());
}
private TypeElement getTypeElement(String name, Elements elements) {
// Try fetching directly first
TypeElement element = elements.getTypeElement(name);
if (element != null || name.indexOf('$') < 0) {
return element;
}
int lastDotPos = name.lastIndexOf('.');
String pkg = lastDotPos > -1 ? name.substring(0, lastDotPos) : "";
name = name.substring(pkg.length());
// Name has '$' in it, try naive replacement first, this will work for
// most cases where there is a single '$' symbol
element = elements.getTypeElement(pkg + name.replace('$', '.'));
if (element != null) {
return element;
}
char[] source = name.toCharArray();
char[] dest = new char[source.length];
int occurs = 0;
for (int offset = 0; offset < source.length; offset++) {
if (source[offset] == '$') {
occurs++;
}
}
// Ok someone is trolling if there are more than 10 '$' symbols in a
// name! 10 is an arbitary number, we could do more but the loop below
// is expensive and something horrible is probably happening if there
// are that many '$' signs. We can also skip here if occurs < 2 because
// we already tested that case above when we replaced "all"
if (occurs > 10 || occurs < 2) {
return null;
}
// Try all possible combinations of replacing '$' with '.' starting at
// the end of the string. Not really ideal but if we haven't resolved
// the class name by replacing all '$' with '.' then this is our only
// remaining approach.
for (int mask = 1; mask < 1 << occurs && element == null; mask++) {
for (int offset = source.length - 1, index = 0; offset >= 0; offset--) {
dest[offset] = (source[offset] == '$' && (mask & (1 << index++)) != 0) ? '.' : source[offset];
}
element = elements.getTypeElement(pkg + new String(dest));
}
return element;
}
/**
* Returns a simulated TypeHandle for a non-existing type
*/
@@ -70,6 +70,13 @@ public enum SuppressedBy {
*/
PUBLIC_TARGET("public-target"),
/**
* Suppress warning when a mixin target is resolved by the AP as imaginary
* (unresolvable via Mirror at compile time). For example for anonymous
* inner classes or other synthetic member classes.
*/
UNRESOLVABLE_TARGET("unresolvable-target"),
/**
* The default java "raw types" suppressions
*/
@@ -24,24 +24,20 @@
*/
package org.spongepowered.asm.mixin.struct;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.tools.Diagnostic.Kind;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.IInjectionPointContext;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.util.logging.MessageRouter;
/**
* Data bundle for an annotated method in a mixin
*/
public class AnnotatedMethodInfo implements IInjectionPointContext {
/**
* Log more things
*/
protected static final Logger logger = LogManager.getLogger("mixin");
/**
* Mixin context
*/
@@ -100,7 +96,7 @@ public class AnnotatedMethodInfo implements IInjectionPointContext {
@Override
public void addMessage(String format, Object... args) {
if (this.context.getOption(Option.DEBUG_VERBOSE)) {
AnnotatedMethodInfo.logger.warn(String.format(format, args));
MessageRouter.getMessager().printMessage(Kind.WARNING, String.format(format, args));
}
}