diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java index b0107420..e601b523 100644 --- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java @@ -107,7 +107,7 @@ public class AccessorInfo extends SpecialMethodInfo { /** * Pattern for matching accessor names (for inflector) */ - protected static final Pattern PATTERN_ACCESSOR = Pattern.compile("^(get|set|is|invoke|call)(([A-Z])(.*?))(_\\$md.*)$"); + protected static final Pattern PATTERN_ACCESSOR = Pattern.compile("^(get|set|is|invoke|call)(([A-Z])(.*?))(_\\$md.*)?$"); /** * Accessor method argument types (raw, from method) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index 813951d4..72667a2f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -452,10 +452,8 @@ public abstract class InjectionPoint { /** * Parse an InjectionPoint from the supplied {@link At} annotation * - * @param mixin Data for the mixin containing the annotation, used to obtain + * @param info Data for the mixin containing the annotation, used to obtain * the refmap, amongst other things - * @param method The annotated handler method - * @param parent The parent annotation which owns this {@link At} annotation * @param at {@link At} annotation to parse information from * @return InjectionPoint parsed from the supplied data or null if parsing * failed diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java index 53ce4469..13e9fdb3 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java @@ -105,7 +105,7 @@ public class InjectorTarget { * Get the slice instructions for the specified * * @param injectionPoint - * @return + * @return slice */ public InsnList getSlice(InjectionPoint injectionPoint) { return this.getSlice(injectionPoint.getSlice()); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index a6e654aa..92a64249 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -53,6 +53,7 @@ import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.asm.mixin.transformer.ClassInfo.Member.Type; import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.ClassSignature; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -629,6 +630,11 @@ public class ClassInfo extends TreeInfo { * Outer class reference, not initialised until required */ private ClassInfo outerClass; + + /** + * Class signature, lazy-loaded where possible + */ + private ClassSignature signature; /** * Private constructor used to initialise the ClassInfo for {@link Object} @@ -703,10 +709,12 @@ public class ClassInfo extends TreeInfo { this.isProbablyStatic = isProbablyStatic; this.outerName = outerName; this.methodMapper = new MethodMapper(MixinEnvironment.getCurrentEnvironment(), this); + this.signature = ClassSignature.ofLazy(classNode); } void addInterface(String iface) { this.interfaces.add(iface); + this.getSignature().addInterface(iface); } void addMethod(MethodNode method) { @@ -856,6 +864,15 @@ public class ClassInfo extends TreeInfo { return this.outerClass; } + + /** + * Return the class signature + * + * @return signature as a {@link ClassSignature} instance + */ + public ClassSignature getSignature() { + return this.signature.wake(); + } /** * Class targets diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java index 35f8bc0c..16ea3e3c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java @@ -41,6 +41,8 @@ import org.apache.logging.log4j.Logger; import org.spongepowered.asm.lib.Label; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; +import org.spongepowered.asm.lib.signature.SignatureReader; +import org.spongepowered.asm.lib.signature.SignatureVisitor; import org.spongepowered.asm.lib.tree.*; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Intrinsic; @@ -277,6 +279,7 @@ class MixinApplicatorStandard { protected final void applyMixin(MixinTargetContext mixin, ApplicatorPass pass) { switch (pass) { case MAIN: + this.applySignature(mixin); this.applyInterfaces(mixin); this.applyAttributes(mixin); this.applyAnnotations(mixin); @@ -300,6 +303,10 @@ class MixinApplicatorStandard { } } + protected void applySignature(MixinTargetContext mixin) { + this.context.mergeSignature(mixin.getSignature()); + } + /** * Mixin interfaces implemented by the mixin class onto the target class * @@ -440,6 +447,12 @@ class MixinApplicatorStandard { this.targetClass.methods.add(method); mixin.addMergedMethod(method); + + if (method.signature != null) { + SignatureVisitor sv = mixin.getSignature().getRemapper(); + new SignatureReader(method.signature).accept(sv); + method.signature = sv.toString(); + } } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index 349ff53c..57a1f9b5 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -66,6 +66,7 @@ import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError; import org.spongepowered.asm.obfuscation.RemapperChain; import org.spongepowered.asm.mixin.transformer.meta.SourceMap.File; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.ClassSignature; import org.spongepowered.asm.util.Constants; /** @@ -295,6 +296,15 @@ public class MixinTargetContext implements IReferenceMapperContext { return this.targetClassInfo; } + /** + * Get the signature for this mixin class + * + * @return signature + */ + public ClassSignature getSignature() { + return this.mixin.getClassInfo().getSignature(); + } + /** * Get the SourceMap stratum for this mixin * diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java index bd043e1d..ea11fb0e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java @@ -40,6 +40,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.transformer.meta.SourceMap; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.ClassSignature; /** * Struct for containing target class information during mixin application @@ -71,6 +72,11 @@ class TargetClassContext { */ private final SourceMap sourceMap; + /** + * Target class signature + */ + private final ClassSignature signature; + /** * Mixins to apply */ @@ -103,6 +109,7 @@ class TargetClassContext { this.className = name; this.classNode = classNode; this.classInfo = ClassInfo.fromClassNode(classNode); + this.signature = this.classInfo.getSignature(); this.mixins = mixins; this.sourceMap = new SourceMap(classNode.sourceFile); this.sourceMap.addFile(this.classNode); @@ -113,76 +120,85 @@ class TargetClassContext { return this.className; } - public boolean isApplied() { + boolean isApplied() { return this.applied; } - public boolean isExportForced() { + boolean isExportForced() { return this.forceExport; } /** * Get the transformer session ID */ - public String getSessionId() { + String getSessionId() { return this.sessionId; } /** * Get the internal class name */ - public String getName() { + String getName() { return this.classNode.name; } /** * Get the class name */ - public String getClassName() { + String getClassName() { return this.className; } /** * Get the class tree */ - public ClassNode getClassNode() { + ClassNode getClassNode() { return this.classNode; } /** * Get the class methods (from the tree) */ - public List getMethods() { + List getMethods() { return this.classNode.methods; } /** * Get the class fields (from the tree) */ - public List getFields() { + List getFields() { return this.classNode.fields; } /** * Get the target class metadata */ - public ClassInfo getClassInfo() { + ClassInfo getClassInfo() { return this.classInfo; } /** * Get the mixins for this target class */ - public SortedSet getMixins() { + SortedSet getMixins() { return this.mixins; } /** * Get the source map that is generated for the target class */ - public SourceMap getSourceMap() { + SourceMap getSourceMap() { return this.sourceMap; } + + /** + * Merge the supplied signature into this class's signature + * + * @param signature signature to merge + */ + void mergeSignature(ClassSignature signature) { + this.signature.merge(signature); + } MethodNode findAliasedMethod(Deque aliases, String desc) { String alias = aliases.poll(); @@ -227,7 +243,7 @@ class TargetClassContext { * @param method method to get a target handle for * @return new or existing target handle for the supplied method */ - public Target getTargetMethod(MethodNode method) { + Target getTargetMethod(MethodNode method) { if (!this.classNode.methods.contains(method)) { throw new IllegalArgumentException("Invalid target method supplied to getTargetMethod()"); } @@ -241,13 +257,13 @@ class TargetClassContext { return target; } - public String getUniqueName(MethodNode method, boolean preservePrefix) { + String getUniqueName(MethodNode method, boolean preservePrefix) { String uniqueIndex = Integer.toHexString(this.nextUniqueMethodIndex++); String pattern = preservePrefix ? "%2$s_$md$%1$s$%3$s" : "md%s$%s$%s"; return String.format(pattern, this.sessionId.substring(30), method.name, uniqueIndex); } - public String getUniqueName(FieldNode field) { + String getUniqueName(FieldNode field) { String uniqueIndex = Integer.toHexString(this.nextUniqueFieldIndex++); return String.format("fd%s$%s$%s", this.sessionId.substring(30), field.name, uniqueIndex); } @@ -255,13 +271,15 @@ class TargetClassContext { /** * Apply mixins to this class */ - public void applyMixins() { + void applyMixins() { if (this.applied) { throw new IllegalStateException("Mixins already applied to target class " + this.className); } this.applied = true; MixinApplicatorStandard applicator = this.createApplicator(); applicator.apply(this.mixins); + + this.classNode.signature = this.signature.toString(); } private MixinApplicatorStandard createApplicator() { @@ -271,11 +289,10 @@ class TargetClassContext { return new MixinApplicatorStandard(this); } - /** * Process {@link Debug} annotations on the class after application */ - public void processDebugTasks() { + void processDebugTasks() { if (!MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERBOSE)) { return; } diff --git a/src/main/java/org/spongepowered/asm/util/ClassSignature.java b/src/main/java/org/spongepowered/asm/util/ClassSignature.java new file mode 100644 index 00000000..8abf5a3b --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/ClassSignature.java @@ -0,0 +1,1363 @@ +/* + * 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.asm.util; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.spongepowered.asm.lib.Opcodes; +import org.spongepowered.asm.lib.signature.SignatureReader; +import org.spongepowered.asm.lib.signature.SignatureVisitor; +import org.spongepowered.asm.lib.signature.SignatureWriter; +import org.spongepowered.asm.lib.tree.ClassNode; + +/** + * Represents an object-oriented view of a generic class signature. We use ASM's + * {@link SignatureVisitor} to walk over an incoming signature in order to parse + * out our internal tree. This is done so that incoming signatures from mixins + * can be merged into the target class. + */ +public class ClassSignature { + + protected static final String OBJECT = "java/lang/Object"; + + /** + * Lazy evaluation support for class signatures + */ + static class Lazy extends ClassSignature { + + private final String sig; + + private ClassSignature generated; + + Lazy(String sig) { + this.sig = sig; + } + + @Override + public ClassSignature wake() { + if (this.generated == null) { + this.generated = ClassSignature.of(this.sig); + } + return this.generated; + } + + } + + /** + * A renamable type variable handle + */ + static class TypeVar implements Comparable { + + /** + * Original type var name + */ + private final String originalName; + + /** + * Current name (conformed) + */ + private String currentName; + + TypeVar(String name) { + this.currentName = this.originalName = name; + } + + @Override + public int compareTo(TypeVar other) { + return this.currentName.compareTo(other.currentName); + } + + @Override + public String toString() { + return this.currentName; + } + + String getOriginalName() { + return this.originalName; + } + + void rename(String name) { + this.currentName = name; + } + + public boolean matches(String originalName) { + return this.originalName.equals(originalName); + } + + @Override + public boolean equals(Object obj) { + return this.currentName.equals(obj); + } + + @Override + public int hashCode() { + return this.currentName.hashCode(); + } + + } + + /** + * Interface for signature tokens + */ + static interface IToken { + + /** + * Available wildcard modifiers + */ + static final String WILDCARDS = "+-"; + + /** + * Return this token as a string representation of its type + */ + public abstract String asType(); + + /** + * Return this token as a string representation of a bound + */ + public abstract String asBound(); + + /** + * Return this token as a hard token object + */ + public abstract Token asToken(); + + /** + * Set the arrayness status of this token, logical OR is applied between + * the current internal arrayness flag and the incoming flag. + * + * @param array arrayness flag + * @return this token + */ + public abstract IToken setArray(boolean array); + + /** + * Set the wildcard on this token, only accepted if one of the valid + * {@link #WILDCARDS} + * + * @param wildcard wildcard to set + * @return this token + */ + public abstract IToken setWildcard(char wildcard); + + } + + /** + * A token in the signature, can represent a type, a collection of bounds, + * or a combination of the two. Includes internal arrayness and wildcard + * flags as well as generic type arguments. + */ + static class Token implements IToken { + + /** + * Valid symbols, supports the {@link IToken#WILDCARDS} as well as + * * for unbounded type args. + */ + static final String SYMBOLS = IToken.WILDCARDS + "*"; + + /** + * True if this token represents an inner type (output is not enclosed + * in L; + */ + private final boolean inner; + + /** + * True if this token represents an array type + */ + private boolean array; + + /** + * Symbol (wildcard, unbound or base type) + */ + private char symbol = 0; + + /** + * Type descriptor, can be null if this type is only a symbol + */ + private String type; + + /** + * Class bounds on this token, null if no bounds have been set + */ + private List classBound; + + /** + * Interface bounds on this token, null if no bounds have been set + */ + private List ifaceBound; + + /** + * Generic signature components on this token, null if this is a raw + * type token + */ + private List signature; + + /** + * Suffix elements on this token, currently only used to append inner + * class tokens to this token + */ + private List suffix; + + /** + * The last suffix element added. When a suffix element is added all + * calls to add... are delegated to the tail element. + */ + private Token tail; + + Token() { + this(false); + } + + Token(String type) { + this(type, false); + } + + Token(char symbol) { + this(); + this.symbol = symbol; + } + + Token(boolean inner) { + this(null, inner); + } + + Token(String type, boolean inner) { + this.inner = inner; + this.type = type; + } + + Token setSymbol(char symbol) { + if (this.symbol == 0 && Token.SYMBOLS.indexOf(symbol) > -1) { + this.symbol = symbol; + } + return this; + } + + Token setType(String type) { + if (this.type == null) { + this.type = type; + } + return this; + } + + boolean hasClassBound() { + return this.classBound != null; + } + + boolean hasInterfaceBound() { + return this.ifaceBound != null; + } + + @Override + public IToken setArray(boolean array) { + this.array |= array; + return this; + } + + @Override + public IToken setWildcard(char wildcard) { + if (IToken.WILDCARDS.indexOf(wildcard) == -1) { + return this; + } + return this.setSymbol(wildcard); + } + + private List getClassBound() { + if (this.classBound == null) { + this.classBound = new ArrayList(); + } + return this.classBound; + } + + private List getIfaceBound() { + if (this.ifaceBound == null) { + this.ifaceBound = new ArrayList(); + } + return this.ifaceBound; + } + + private List getSignature() { + if (this.signature == null) { + this.signature = new ArrayList(); + } + return this.signature; + } + + private List getSuffix() { + if (this.suffix == null) { + this.suffix = new ArrayList(); + } + return this.suffix; + } + + /** + * Add a type argument to this symbol + * + * @param symbol argument to add + * @return new token + */ + IToken addTypeArgument(char symbol) { + if (this.tail != null) { + return this.tail.addTypeArgument(symbol); + } + + Token token = new Token(symbol); + this.getSignature().add(token); + return token; + } + + /** + * Add a type argument to this symbol + * + * @param name argument to add + * @return new token + */ + IToken addTypeArgument(String name) { + if (this.tail != null) { + return this.tail.addTypeArgument(name); + } + + Token token = new Token(name); + this.getSignature().add(token); + return token; + } + + /** + * Add a type argument to this symbol + * + * @param token argument to add + * @return new token + */ + IToken addTypeArgument(Token token) { + if (this.tail != null) { + return this.tail.addTypeArgument(token); + } + + this.getSignature().add(token); + return token; + } + + /** + * Add a type argument to this symbol + * + * @param token argument to add + * @return new token + */ + IToken addTypeArgument(TokenHandle token) { + if (this.tail != null) { + return this.tail.addTypeArgument(token); + } + + TokenHandle handle = token.clone(); + this.getSignature().add(handle); + return handle; + } + + /** + * Add a class or interface bound to this symbol + * + * @param bound bound specifier + * @param classBound true to add a class bound, false to add an + * interface bound + * @return new token + */ + Token addBound(String bound, boolean classBound) { + if (classBound) { + return this.addClassBound(bound); + } + + return this.addInterfaceBound(bound); + } + + /** + * Add a class bound to this symbol + * + * @param bound bound specifier + * @return new token + */ + Token addClassBound(String bound) { + Token token = new Token(bound); + this.getClassBound().add(token); + return token; + } + + /** + * Add a class bound to this symbol + * + * @param bound bound specifier + * @return new token + */ + Token addInterfaceBound(String bound) { + Token token = new Token(bound); + this.getIfaceBound().add(token); + return token; + } + + /** + * Add an inner class suffix to this symbol + * + * @param name inner class name + * @return new tail token + */ + Token addInnerClass(String name) { + this.tail = new Token(name, true); + this.getSuffix().add(this.tail); + return this.tail; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return this.asType(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.util.ClassSignature.IToken#asBound() + */ + @Override + public String asBound() { + StringBuilder sb = new StringBuilder(); + + if (this.type != null) { + sb.append(this.type); + } + + if (this.classBound != null) { + for (Token token : this.classBound) { + sb.append(token.asType()); + } + } + + if (this.ifaceBound != null) { + for (Token token : this.ifaceBound) { + sb.append(':').append(token.asType()); + } + } + + return sb.toString(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.util.ClassSignature.IToken#asType() + */ + @Override + public String asType() { + return this.asType(false); + } + + /** + * Convert this token to a string representation of its type, optionally + * generate only a raw representation (no generic signature) + * + * @param raw if true, only emits the raw type + * @return string representation of this token + */ + public String asType(boolean raw) { + StringBuilder sb = new StringBuilder(); + + if (this.array) { + sb.append('['); + } + + if (this.symbol != 0) { + sb.append(this.symbol); + } + + if (this.type == null) { + return sb.toString(); + } + + if (!this.inner) { + sb.append('L'); + } + + sb.append(this.type); + + if (!raw) { + if (this.signature != null) { + sb.append('<'); + for (IToken token : this.signature) { + sb.append(token.asType()); + } + sb.append('>'); + } + + if (this.suffix != null) { + for (IToken token : this.suffix) { + sb.append('.').append(token.asType()); + } + } + } + + if (!this.inner) { + sb.append(';'); + } + + return sb.toString(); + } + + boolean isRaw() { + return this.signature == null; + } + + String getClassType() { + return this.type != null ? this.type : ClassSignature.OBJECT; + } + + @Override + public Token asToken() { + return this; + } + + } + + /** + * TokenHandle is used to provide indirection to {@link Token}, primarily + * for storing mappings of TypeVars to tokens but also to store Tokens as + * type parameters on other Tokens where we may need to include a wildcard + * or array marker without altering the underlying Token. + */ + class TokenHandle implements IToken { + + /** + * Inner token, never null + */ + final Token token; + + /** + * True if this handle is an array type + */ + boolean array; + + /** + * Wildcard for this handle + */ + char wildcard; + + TokenHandle() { + this(new Token()); + } + + TokenHandle(Token token) { + this.token = token; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.util.ClassSignature.IToken + * #setArray(boolean) + */ + @Override + public IToken setArray(boolean array) { + this.array |= array; + return this; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.util.ClassSignature.IToken + * #setWildcard(char) + */ + @Override + public IToken setWildcard(char wildcard) { + if (IToken.WILDCARDS.indexOf(wildcard) > -1) { + this.wildcard = wildcard; + } + return this; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.util.ClassSignature.IToken#asBound() + */ + @Override + public String asBound() { + return this.token.asBound(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.util.ClassSignature.IToken#asType() + */ + @Override + public String asType() { + StringBuilder sb = new StringBuilder(); + + if (this.wildcard > 0) { + sb.append(this.wildcard); + } + + if (this.array) { + sb.append('['); + } + + return sb.append(ClassSignature.this.getTypeVar(this)).toString(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.util.ClassSignature.IToken#asToken() + */ + @Override + public Token asToken() { + return this.token; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return this.token.toString(); + } + + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + @Override + public TokenHandle clone() { + return new TokenHandle(this.token); + } + + } + + /** + * Base visitor for parsing signatures using {@link SignatureReader}, a + * reference to instances of this class are deliberately not maintained in + * order to free the memory consumed by the visitors after parsing. + */ + class SignatureParser extends SignatureVisitor { + + /** + * Base for the various element visitors used + */ + abstract class SignatureElement extends SignatureVisitor { + + public SignatureElement() { + super(Opcodes.ASM5); + } + + } + + /** + * Base class for elements which propagate information through to an + * underlying token + */ + abstract class TokenElement extends SignatureElement { + + /** + * The token for this element + */ + protected Token token; + + /** + * True if the next element to be appended is an array element + */ + private boolean array; + + public Token getToken() { + if (this.token == null) { + this.token = new Token(); + } + return this.token; + } + + protected void setArray() { + this.array = true; + } + + private boolean getArray() { + boolean array = this.array; + this.array = false; + return array; + } + + @Override + public void visitClassType(String name) { + this.getToken().setType(name); + } + + @Override + public SignatureVisitor visitClassBound() { + this.getToken(); + return new BoundElement(this, true); + } + + @Override + public SignatureVisitor visitInterfaceBound() { + this.getToken(); + return new BoundElement(this, false); + } + + @Override + public void visitInnerClassType(String name) { + this.token.addInnerClass(name); + } + + @Override + public SignatureVisitor visitArrayType() { + this.setArray(); + return this; + } + + @Override + public SignatureVisitor visitTypeArgument(char wildcard) { + return new TypeArgElement(this, wildcard); + } + + Token addTypeArgument() { + return this.token.addTypeArgument('*').asToken(); + } + + IToken addTypeArgument(char symbol) { + return this.token.addTypeArgument(symbol).setArray(this.getArray()); + } + + IToken addTypeArgument(String name) { + return this.token.addTypeArgument(name).setArray(this.getArray()); + } + + IToken addTypeArgument(Token token) { + return this.token.addTypeArgument(token).setArray(this.getArray()); + } + + IToken addTypeArgument(TokenHandle token) { + return this.token.addTypeArgument(token).setArray(this.getArray()); + } + + } + + /** + * A formal type parameter + */ + class FormalParamElement extends TokenElement { + + /** + * Handle to the underlying token for this type + */ + private final TokenHandle handle; + + FormalParamElement(String param) { + this.handle = ClassSignature.this.getType(param); + this.token = this.handle.asToken(); + } + + } + + /** + * A type argument + */ + class TypeArgElement extends TokenElement { + + /** + * Element that this is an argument for + */ + private final TokenElement type; + + /** + * True if the next token appended has a wildcard qualifier + */ + private final char wildcard; + + TypeArgElement(TokenElement type, char wildcard) { + this.type = type; + this.wildcard = wildcard; + } + + @Override + public SignatureVisitor visitArrayType() { + this.type.setArray(); + return this; + } + + @Override + public void visitBaseType(char descriptor) { + this.token = this.type.addTypeArgument(descriptor).asToken(); + } + + @Override + public void visitTypeVariable(String name) { + TokenHandle token = ClassSignature.this.getType(name); + this.token = this.type.addTypeArgument(token).setWildcard(this.wildcard).asToken(); + } + + @Override + public void visitClassType(String name) { + this.token = this.type.addTypeArgument(name).setWildcard(this.wildcard).asToken(); + } + + @Override + public void visitTypeArgument() { + this.token.addTypeArgument('*'); + } + + @Override + public SignatureVisitor visitTypeArgument(char wildcard) { + return new TypeArgElement(this, wildcard); + } + + @Override + public void visitEnd() { + } + + } + + /** + * A class (or interface) bound element + */ + class BoundElement extends TokenElement { + + /** + * The element which owns this bound + */ + private final TokenElement type; + + /** + * True if this element represents a class bound, false if interface + */ + private final boolean classBound; + + BoundElement(TokenElement type, boolean classBound) { + this.type = type; + this.classBound = classBound; + } + + @Override + public void visitClassType(String name) { + this.token = this.type.token.addBound(name, this.classBound); + } + + @Override + public void visitTypeArgument() { + this.token.addTypeArgument('*'); + } + + @Override + public SignatureVisitor visitTypeArgument(char wildcard) { + return new TypeArgElement(this, wildcard); + } + + } + + /** + * Superclass element + */ + class SuperClassElement extends TokenElement { + + @Override + public void visitEnd() { + ClassSignature.this.setSuperClass(this.token); + } + + } + + /** + * Interface element + */ + class InterfaceElement extends TokenElement { + + @Override + public void visitEnd() { + ClassSignature.this.addInterface(this.token); + } + + } + + /** + * The params element. For some reason {@link #visitFormalTypeParameter} + * doesn't return a visitor so we need to proxy the relevant calls + * through to the current params element. + */ + private FormalParamElement param; + + SignatureParser() { + super(Opcodes.ASM5); + } + + @Override + public void visitFormalTypeParameter(String name) { + this.param = new FormalParamElement(name); + } + + @Override + public SignatureVisitor visitClassBound() { + return this.param.visitClassBound(); + } + + @Override + public SignatureVisitor visitInterfaceBound() { + return this.param.visitInterfaceBound(); + } + + @Override + public SignatureVisitor visitSuperclass() { + return new SuperClassElement(); + } + + @Override + public SignatureVisitor visitInterface() { + return new InterfaceElement(); + } + + } + + /** + * Remapper + */ + class SignatureRemapper extends SignatureWriter { + + private final Set localTypeVars = new HashSet(); + + @Override + public void visitFormalTypeParameter(String name) { + this.localTypeVars.add(name); + super.visitFormalTypeParameter(name); + } + + @Override + public void visitTypeVariable(String name) { + if (!this.localTypeVars.contains(name)) { + TypeVar typeVar = ClassSignature.this.getTypeVar(name); + if (typeVar != null) { + super.visitTypeVariable(typeVar.toString()); + return; + } + } + super.visitTypeVariable(name); + } + + } + + /** + * Type vars defined in this signature, represents the list of formal + * parameters, ordered + */ + private final Map types = new LinkedHashMap(); + + /** + * The superclass defined in the signature + */ + private Token superClass = new Token(ClassSignature.OBJECT); + + /** + * Interfaces mentioned in the signature + */ + private final List interfaces = new ArrayList(); + + /** + * Interfaces added manually + */ + private final Deque rawInterfaces = new LinkedList(); + + ClassSignature() { + } + + /** + * Use a {@link SignatureReader} to visit the supplied signature and build + * the signature model + * + * @param signature signature string to parse + * @return fluent interface + */ + private ClassSignature read(String signature) { + if (signature != null) { + try { + new SignatureReader(signature).accept(new SignatureParser()); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + return this; + } + + /** + * Get the type var for the specified var name + * + * @param varName type var to lookup + * @return type var for the supplied type var name + */ + protected TypeVar getTypeVar(String varName) { + for (TypeVar typeVar : this.types.keySet()) { + if (typeVar.matches(varName)) { + return typeVar; + } + } + return null; + } + + /** + * Get the token for the specified type var name, creating it if necessary + * + * @param varName type var to lookup + * @return type token for the supplied type var name + */ + protected TokenHandle getType(String varName) { + for (TypeVar typeVar : this.types.keySet()) { + if (typeVar.matches(varName)) { + return this.types.get(typeVar); + } + } + + TokenHandle handle = new TokenHandle(); + this.types.put(new TypeVar(varName), handle); + return handle; + } + + /** + * Get the type var matching the supplied type token, or the raw token type + * if no mapping exists for the supplied token handle + * + * @param handle type token handle to lookup + * @return type var (with prefix and suffix) or raw type name + */ + protected String getTypeVar(TokenHandle handle) { + for (Entry type : this.types.entrySet()) { + TypeVar typeVar = type.getKey(); + TokenHandle typeHandle = type.getValue(); + if (handle == typeHandle || handle.asToken() == typeHandle.asToken()) { + return "T" + typeVar + ";"; + } + } + return handle.token.asType(); + } + + /** + * Add a type var to this signature, the type var must not exist + * + * @param typeVar type var to add + * @param handle type var's type token + * @throws IllegalArgumentException if the specified type var already exists + */ + protected void addTypeVar(TypeVar typeVar, TokenHandle handle) throws IllegalArgumentException { + if (this.types.containsKey(typeVar)) { + throw new IllegalArgumentException("TypeVar " + typeVar + " is already present on " + this); + } + + this.types.put(typeVar, handle); + } + + /** + * Set the superclass for this signature + * + * @param superClass super class to set + */ + protected void setSuperClass(Token superClass) { + this.superClass = superClass; + } + + /** + * Get the raw superclass type of this signature as a string + * + * @return superclass type as a string + */ + public String getSuperClass() { + return this.superClass.asType(true); + } + + /** + * Add an interface to this signature + * + * @param iface interface to add + */ + protected void addInterface(Token iface) { + if (!iface.isRaw()) { + String raw = iface.asType(true); + for (ListIterator iter = this.interfaces.listIterator(); iter.hasNext();) { + Token intrface = iter.next(); + if (intrface.isRaw() && intrface.asType(true).equals(raw)) { + iter.set(iface); + return; + } + } + } + + this.interfaces.add(iface); + } + + /** + * Add a raw interface declaration to this signature + * + * @param iface interface name to add (bin format) + */ + public void addInterface(String iface) { + this.rawInterfaces.add(iface); + } + + /** + * Add a raw interface which was previously enqueued + * + * @param iface interface to add + */ + protected void addRawInterface(String iface) { + Token token = new Token(iface); + String raw = token.asType(true); + for (Token intrface : this.interfaces) { + if (intrface.asType(true).equals(raw)) { + return; + } + } + this.interfaces.add(token); + } + + /** + * Merges another class signature into this one. The other signature is + * first conformed so that no formal type parameters overlap with formal + * type parameters defined on this signature. No attempt is made to combine + * formal type parameters, this method merely ensures that parameters do + * not overlap. + * + * @param other Class signature to merge into this one + */ + public void merge(ClassSignature other) { + try { + Set typeVars = new HashSet(); + for (TypeVar typeVar : this.types.keySet()) { + typeVars.add(typeVar.toString()); + } + + other.conform(typeVars); + } catch (IllegalStateException ex) { + // Oh crap, this means we couldn't conform one or more type params! + ex.printStackTrace(); + return; + } + + for (Entry type : other.types.entrySet()) { + this.addTypeVar(type.getKey(), type.getValue()); + } + + for (Token iface : other.interfaces) { + this.addInterface(iface); + } + } + + private void conform(Set typeVars) { + for (TypeVar typeVar : this.types.keySet()) { + String name = this.findUniqueName(typeVar.getOriginalName(), typeVars); + typeVar.rename(name); + typeVars.add(name); + } + } + + /** + * Finds a unique name for typeVar which does not exist in the + * typeVars which is as close as possible to the original name. + * + * @param typeVar type var to conform + * @param typeVars existing type vars + * @return new name for the type var + */ + private String findUniqueName(String typeVar, Set typeVars) { + if (!typeVars.contains(typeVar)) { + return typeVar; + } + + if (typeVar.length() == 1) { + String name = this.findOffsetName(typeVar.charAt(0), typeVars); + if (name != null) { + return name; + } + } + + String name = this.findOffsetName('T', typeVars, "", typeVar); + if (name != null) { + return name; + } + + name = this.findOffsetName('T', typeVars, typeVar, ""); + if (name != null) { + return name; + } + + name = this.findOffsetName('T', typeVars, "T", typeVar); + if (name != null) { + return name; + } + + name = this.findOffsetName('T', typeVars, "", typeVar + "Type"); + if (name != null) { + return name; + } + + throw new IllegalStateException("Failed to conform type var: " + typeVar); + } + + /** + * Find an offset name for a single-char typevar. + * + * @param c type var + * @param typeVars existing type var + * @return offset type var or null if var could not be conformed + */ + private String findOffsetName(char c, Set typeVars) { + return this.findOffsetName(c, typeVars, "", ""); + } + + /** + * Find an offset name for a type var + * + * @param c Char to rotate + * @param typeVars existing vars + * @param prefix type var prefix + * @param suffix type var suffix + * @return conformed var name or null if none can be found + */ + private String findOffsetName(char c, Set typeVars, String prefix, String suffix) { + String name = String.format("%s%s%s", prefix, c, suffix); + if (!typeVars.contains(name)) { + return name; + } + + if (c > 0x40 && c < 0x5B) { + for (int s = c - 0x40; s + 0x41 != c; s = ++s % 0x1A) { + name = String.format("%s%s%s", prefix, (char)(s + 0x41), suffix); + if (!typeVars.contains(name)) { + return name; + } + } + } + + return null; + } + + /** + * Get a remapper for type vars in this signature + * + * @return signature visitor + */ + public SignatureVisitor getRemapper() { + return new SignatureRemapper(); + } + + /** + * Converts this signature into a string representation compatible with the + * signature attribute of a Java class + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + while (this.rawInterfaces.size() > 0) { + this.addRawInterface(this.rawInterfaces.remove()); + } + + StringBuilder sb = new StringBuilder(); + + if (this.types.size() > 0) { + boolean valid = false; + StringBuilder types = new StringBuilder(); + for (Entry type : this.types.entrySet()) { + String bound = type.getValue().asBound(); + if (!bound.isEmpty()) { + types.append(type.getKey()).append(':').append(bound); + valid = true; + } + } + + if (valid) { + sb.append('<').append(types).append('>'); + } + } + + sb.append(this.superClass.asType()); + + for (Token iface : this.interfaces) { + sb.append(iface.asType()); + } + + return sb.toString(); + } + + /** + * Wake up this signature if it is lazy-loaded + */ + public ClassSignature wake() { + return this; + } + + /** + * Parse a generic class signature from the supplied string + * + * @param signature signature string to parse + * @return parsed signature object + */ + public static ClassSignature of(String signature) { + return new ClassSignature().read(signature); + } + + /** + * Parse a generic class signature from the supplied class node, uses the + * declared signature if present, else falls back to generating a raw + * signature from the class itself + * + * @param classNode class node to parse + * @return parsed signature + */ + public static ClassSignature of(ClassNode classNode) { + if (classNode.signature != null) { + return ClassSignature.of(classNode.signature); + } + + return ClassSignature.generate(classNode); + } + + /** + * Returns a lazy-evaluated signature object. For classes with a signature + * present this saves having to do the parse until we actually need it. For + * classes with no signature we just go ahead and generate it from the + * supplied ClassNode while we have it + * + * @param classNode class node to parse + * @return parsed signature or lazy-load handle + */ + public static ClassSignature ofLazy(ClassNode classNode) { + if (classNode.signature != null) { + return new ClassSignature.Lazy(classNode.signature); + } + + return ClassSignature.generate(classNode); + } + + /** + * Generate a rough (raw) signature from the supplied classnode + * + * @param classNode class node to generate a signature for + * @return generated signature + */ + private static ClassSignature generate(ClassNode classNode) { + ClassSignature generated = new ClassSignature(); + generated.setSuperClass(new Token(classNode.superName != null ? classNode.superName : ClassSignature.OBJECT)); + for (String iface : classNode.interfaces) { + generated.addInterface(new Token(iface)); + } + return generated; + } + +}