From 04ac3aac18e3911af7e557a656399fbc94b096cb Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Thu, 23 Apr 2015 01:06:27 +0100 Subject: [PATCH] Output warnings for unresolved targets during AP run, closes #40 --- build.gradle | 1 + .../tools/obfuscation/AnnotatedMixin.java | 193 +++++++++++++++--- .../tools/obfuscation/AnnotatedMixins.java | 19 +- .../tools/obfuscation/FieldHandle.java | 52 +++++ .../tools/obfuscation/MethodHandle.java | 42 ++++ .../tools/obfuscation/TypeHandle.java | 140 +++++++++++++ .../mixin/injection/struct/MemberInfo.java | 9 + .../asm/util/SignaturePrinter.java | 81 ++++++-- 8 files changed, 477 insertions(+), 60 deletions(-) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/FieldHandle.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java diff --git a/build.gradle b/build.gradle index 0c55679f..5ad0bf12 100644 --- a/build.gradle +++ b/build.gradle @@ -173,6 +173,7 @@ shadowJar { include(dependency('commons-io:commons-io')) include(dependency('net.minecraftforge.srg2source:Srg2Source')) include(dependency('net.sf.jopt-simple:jopt-simple')) + include(dependency('org.ow2.asm:asm-debug-all')) } classifier = 'processor' } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 894b94dc..780c421a 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -37,7 +37,6 @@ import java.util.Set; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; -import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; @@ -72,6 +71,8 @@ class AnnotatedMixin { private final String originalName; + private final List aliases; + private String obfuscated; ShadowElementName(Element method, AnnotationMirror shadow) { @@ -84,6 +85,9 @@ class AnnotatedMixin { } this.hasPrefix = hasPrefix; this.obfuscated = this.baseName = name; + + List aliases = MirrorUtils.>getAnnotationValue(shadow, "aliases"); + this.aliases = MirrorUtils.unfold(aliases); } @Override @@ -115,6 +119,14 @@ class AnnotatedMixin { public String prefix(String name) { return this.hasPrefix ? this.prefix + name : name; } + + public boolean hasAliases() { + return this.aliases.size() > 0; + } + + public List getAliases() { + return this.aliases; + } } /** @@ -201,9 +213,9 @@ class AnnotatedMixin { // Public targets, referenced by class try { - List publiTargets = MirrorUtils.>getAnnotationValue(this.annotation, "value", + List publicTargets = MirrorUtils.>getAnnotationValue(this.annotation, "value", Collections.emptyList()); - for (TypeMirror target : MirrorUtils.unfold(publiTargets)) { + for (TypeMirror target : MirrorUtils.unfold(publicTargets)) { TypeHandle type = new TypeHandle((DeclaredType)target); if (this.targets.contains(type)) { continue; @@ -319,8 +331,10 @@ class AnnotatedMixin { /** * Register an {@link org.spongepowered.asm.mixin.Overwrite} method */ - public void registerOverwrite(ExecutableElement method) { - if (!this.validateSingleTarget("@Overwrite", method)) { + public void registerOverwrite(ExecutableElement method, AnnotationMirror overwrite) { + this.validateOverwriteTarget(method, overwrite); + + if (!this.remap || !this.validateSingleTarget("@Overwrite", method)) { return; } @@ -349,15 +363,50 @@ class AnnotatedMixin { this.addMethodMapping(mcpName, obfName, mcpSignature, obfMethod.sig); } + /** + * Checks whether the specified method exists in all targets and raises + * warnings where appropriate + */ + private void validateOverwriteTarget(ExecutableElement method, AnnotationMirror overwrite) { + String signature = method.asType().toString(); + + List aliases = MirrorUtils.unfold(MirrorUtils.>getAnnotationValue(overwrite, "aliases")); + + for (TypeHandle target : this.targets) { + if (target.isImaginary()) { + continue; + } + + // Find method as-is + MethodHandle targetMethod = target.findMethod(method); + if (targetMethod != null) { + continue; + } + + // Check aliases + for (String alias : aliases) { + if ((targetMethod = target.findMethod(alias, signature)) != null) { + break; + } + } + + if (targetMethod == null) { + this.mixins.printMessage(Kind.WARNING, "Cannot find target for @Overwrite method in " + target, method, overwrite); + } + } + } + /** * Register a {@link org.spongepowered.asm.mixin.Shadow} field */ - public void registerShadow(VariableElement field, AnnotationMirror shadow) { - if (!this.validateSingleTarget("@Shadow", field)) { + public void registerShadow(VariableElement field, AnnotationMirror shadow, boolean remap) { + ShadowElementName name = new ShadowElementName(field, shadow); + this.validateShadowTarget(field, shadow, name); + + if (!remap || !this.validateSingleTarget("@Shadow", field)) { return; } - ShadowElementName name = new ShadowElementName(field, shadow); String obfField = this.mixins.getObfField(this.targetRef + "/" + name); if (obfField == null) { @@ -368,15 +417,48 @@ class AnnotatedMixin { this.addFieldMapping(name.setObfuscatedName(obfField)); } + /** + * Checks whether the specified field exists in all targets and raises + * warnings where appropriate + */ + private void validateShadowTarget(VariableElement field, AnnotationMirror shadow, ShadowElementName name) { + String fieldType = field.asType().toString(); + + for (TypeHandle target : this.targets) { + if (target.isImaginary()) { + continue; + } + + // Search for field + FieldHandle targetField = target.findField(field); + if (targetField != null) { + continue; + } + + // Try search by alias + for (String alias : name.getAliases()) { + if ((targetField = target.findField(alias, fieldType)) != null) { + break; + } + } + + if (targetField == null) { + this.mixins.printMessage(Kind.WARNING, "Cannot find target field for @Shadow field in " + target, field, shadow); + } + } + } + /** * Register a {@link org.spongepowered.asm.mixin.Shadow} method */ - public void registerShadow(ExecutableElement method, AnnotationMirror shadow) { - if (!this.validateSingleTarget("@Shadow", method)) { + public void registerShadow(ExecutableElement method, AnnotationMirror shadow, boolean remap) { + ShadowElementName name = new ShadowElementName(method, shadow); + this.validateShadowTarget(method, shadow, name); + + if (!remap || !this.validateSingleTarget("@Shadow", method)) { return; } - ShadowElementName name = new ShadowElementName(method, shadow); String mcpSignature = MirrorUtils.generateSignature(method); MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + name, mcpSignature)); @@ -388,21 +470,62 @@ class AnnotatedMixin { this.addMethodMapping(name.setObfuscatedName(obfMethod.name), mcpSignature, obfMethod.sig); } + /** + * Checks whether the specified method exists in all targets and raises + * warnings where appropriate + */ + private void validateShadowTarget(ExecutableElement method, AnnotationMirror shadow, ShadowElementName name) { + String signature = TypeHandle.getElementSignature(method); + + for (TypeHandle target : this.targets) { + if (target.isImaginary()) { + continue; + } + + // Find method as-is + MethodHandle targetMethod = target.findMethod(method); + if (targetMethod != null) { + continue; + } + + // Find method without prefix + targetMethod = target.findMethod(name.toString(), signature); + if (targetMethod != null) { + continue; + } + + // Check aliases + for (String alias : name.getAliases()) { + if ((targetMethod = target.findMethod(alias, signature)) != null) { + break; + } + } + + if (targetMethod == null) { + this.mixins.printMessage(Kind.WARNING, "Cannot find target method for @Shadow method in " + target, method, shadow); + } + } + } + /** * Register a {@link org.spongepowered.asm.mixin.injection.Inject} method */ - public void registerInjector(ExecutableElement method, AnnotationMirror inject) { - if (!this.validateSingleTarget("@Inject", method)) { - return; - } - + public void registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) { String originalReference = MirrorUtils.getAnnotationValue(inject, "method"); MemberInfo targetMember = MemberInfo.parse(originalReference); if (targetMember.name == null) { return; } - String desc = this.findDescriptor(this.targetType, targetMember); + if (targetMember.desc != null) { + this.validateReferencedTarget(method, inject, targetMember); + } + + if (!remap || !this.validateSingleTarget("@Inject", method)) { + return; + } + + String desc = this.targetType.findDescriptor(targetMember); if (desc == null) { if (this.targetType.isImaginary()) { this.mixins.printMessage(Kind.WARNING, "@Inject target requires method signature because enclosing type information is unavailable", @@ -426,6 +549,25 @@ class AnnotatedMixin { this.mixins.getReferenceMapper().addMapping(this.classRef, originalReference, remappedReference.toString()); } + /** + * Checks whether the referenced method exists in all targets and raises + * warnings where appropriate + */ + private void validateReferencedTarget(ExecutableElement method, AnnotationMirror inject, MemberInfo reference) { + String signature = reference.toDescriptor(); + + for (TypeHandle target : this.targets) { + if (target.isImaginary()) { + continue; + } + + MethodHandle targetMethod = target.findMethod(reference.name, signature); + if (targetMethod == null) { + this.mixins.printMessage(Kind.WARNING, "Cannot find target method for @Inject in " + target, method, inject); + } + } + } + /** * Register a {@link org.spongepowered.asm.mixin.injection.At} annotation * and process the references @@ -503,23 +645,6 @@ class AnnotatedMixin { this.methodMappings.add(mapping); } - private String findDescriptor(TypeHandle targetType, MemberInfo memberInfo) { - String desc = memberInfo.desc; - if (desc == null) { - for (Element child : targetType.getEnclosedElements()) { - if (child.getKind() != ElementKind.METHOD) { - continue; - } - - if (child.getSimpleName().toString().equals(memberInfo.name)) { - desc = MirrorUtils.generateSignature((ExecutableElement)child); - break; - } - } - } - return desc; - } - static Map getAtArgs(AnnotationMirror at) { Map args = new HashMap(); List argv = MirrorUtils.>getAnnotationValue(at, "args"); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 78853498..ad1c57aa 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -54,6 +54,7 @@ import javax.tools.Diagnostic.Kind; import javax.tools.FileObject; import javax.tools.StandardLocation; +import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import org.spongepowered.tools.MirrorUtils; @@ -352,9 +353,7 @@ class AnnotatedMixins implements Messager { return; } - if (mixinClass.remap()) { - mixinClass.registerOverwrite(method); - } + mixinClass.registerOverwrite(method, MirrorUtils.getAnnotation(method, Overwrite.class)); } /** @@ -371,9 +370,7 @@ class AnnotatedMixins implements Messager { return; } - if (this.shouldRemap(mixinClass, shadow)) { - mixinClass.registerShadow(field, shadow); - } + mixinClass.registerShadow(field, shadow, this.shouldRemap(mixinClass, shadow)); } /** @@ -390,9 +387,7 @@ class AnnotatedMixins implements Messager { return; } - if (this.shouldRemap(mixinClass, shadow)) { - mixinClass.registerShadow(method, shadow); - } + mixinClass.registerShadow(method, shadow, this.shouldRemap(mixinClass, shadow)); } /** @@ -410,9 +405,9 @@ class AnnotatedMixins implements Messager { return; } - if (this.shouldRemap(mixinClass, inject)) { - mixinClass.registerInjector(method, inject); - + boolean remap = this.shouldRemap(mixinClass, inject); + mixinClass.registerInjector(method, inject, remap); + if (remap) { Object ats = MirrorUtils.getAnnotationValue(inject, "at"); if (ats instanceof List) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/FieldHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/FieldHandle.java new file mode 100644 index 00000000..6ac2c70a --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/FieldHandle.java @@ -0,0 +1,52 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) 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.tools.obfuscation; + +import javax.lang.model.element.VariableElement; + + +class FieldHandle { + + private final VariableElement element; + + private final boolean rawType; + + public FieldHandle(VariableElement element) { + this(element, false); + } + + public FieldHandle(VariableElement element, boolean rawType) { + this.element = element; + this.rawType = rawType; + } + + public VariableElement getElement() { + return this.element; + } + + public boolean isRawType() { + return this.rawType; + } +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java new file mode 100644 index 00000000..b5b769f8 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MethodHandle.java @@ -0,0 +1,42 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) 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.tools.obfuscation; + +import javax.lang.model.element.ExecutableElement; + + +class MethodHandle { + + private final ExecutableElement element; + + public MethodHandle(ExecutableElement element) { + this.element = element; + } + + public ExecutableElement getElement() { + return this.element; + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java index be223afc..f09c07db 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java @@ -28,12 +28,16 @@ import java.util.Collections; import java.util.List; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; +import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.tools.MirrorUtils; @@ -93,6 +97,14 @@ public class TypeHandle { this((TypeElement)type.asElement()); } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return this.name.replace('/', '.'); + } + /** * Returns the fully qualified class name */ @@ -142,4 +154,132 @@ public class TypeHandle { public boolean isImaginary() { return this.element == null; } + + public String findDescriptor(MemberInfo memberInfo) { + String desc = memberInfo.desc; + if (desc == null) { + for (Element child : this.getEnclosedElements()) { + if (child.getKind() != ElementKind.METHOD) { + continue; + } + + if (child.getSimpleName().toString().equals(memberInfo.name)) { + desc = MirrorUtils.generateSignature((ExecutableElement)child); + break; + } + } + } + return desc; + } + + /** + * Find a member field in this type which matches the name and declared type + * of the supplied element + * + * @param element Element to match + * @return handle to the discovered field if matched or null if no match + */ + public FieldHandle findField(VariableElement element) { + return this.findField(element.getSimpleName().toString(), element.asType().toString()); + } + + /** + * Find a member field in this type which matches the name and declared type + * specified + * + * @param name Field name to search for + * @param type Field descriptor (java-style) + * @return handle to the discovered field if matched or null if no match + */ + public FieldHandle findField(String name, String type) { + String rawType = type.replaceAll("<[^>]+>", ""); + + for (Element element : this.getEnclosedElements()) { + if (element.getKind() != ElementKind.FIELD) { + continue; + } + + VariableElement field = (VariableElement)element; + if (this.compareElement(field, name, type)) { + return new FieldHandle(field); + } else if (this.compareElement(field, name, rawType)) { + return new FieldHandle(field, true); + } + } + + return null; + } + + /** + * Find a member method in this type which matches the name and declared + * type of the supplied element + * + * @param element Element to match + * @return handle to the discovered method if matched or null if no match + */ + public MethodHandle findMethod(ExecutableElement element) { + return this.findMethod(element.getSimpleName().toString(), TypeHandle.getElementSignature(element)); + } + + /** + * Find a member method in this type which matches the name and signature + * specified + * + * @param name Method name to search for + * @param signature Method signature + * @return handle to the discovered method if matched or null if no match + */ + public MethodHandle findMethod(String name, String signature) { + String rawSignature = signature.replaceAll("<[^>]+>", ""); + + for (Element element : this.getEnclosedElements()) { + switch (element.getKind()) { + case CONSTRUCTOR: + case METHOD: + ExecutableElement method = (ExecutableElement)element; + if (this.compareElement(method, name, signature) || this.compareElement(method, name, rawSignature)) { + return new MethodHandle(method); + } + + break; +// case STATIC_INIT: // TODO? +// break; + default: + break; + } + + } + + return null; + } + + private boolean compareElement(Element elem, String name, String type) { + try { + String elementName = elem.getSimpleName().toString(); + String elementType = TypeHandle.getElementSignature(elem); + return name.equals(elementName) && (type.length() == 0 || type.equals(elementType)); + } catch (NullPointerException ex) { + return false; + } + } + + static String getElementSignature(Element element) { + if (element instanceof ExecutableElement) { + ExecutableElement method = (ExecutableElement)element; + StringBuilder desc = new StringBuilder().append("("); + boolean extra = false; + for (VariableElement arg : method.getParameters()) { + if (extra) { + desc.append(','); + } + desc.append(arg.asType().toString()); + extra = true; + } + desc.append(')').append(method.getReturnType().toString()); + return desc.toString(); + } + + return element.asType().toString(); + } + } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java index 7c7ba627..962edbe1 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java @@ -30,6 +30,7 @@ import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.FieldInsnNode; import org.objectweb.asm.tree.MethodInsnNode; import org.spongepowered.asm.mixin.transformer.IReferenceMapperContext; +import org.spongepowered.asm.util.SignaturePrinter; /** @@ -218,6 +219,14 @@ public class MemberInfo { return this.owner + "/" + this.name; } + public String toDescriptor() { + if (this.desc == null) { + return ""; + } + + return new SignaturePrinter(this).setFullyQualified(true).toDescriptor(); + } + public MethodData asMethodData() { if (!this.isFullyQualified()) { throw new RuntimeException("Cannot convert unqalified reference to MethodData"); diff --git a/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java b/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java index e66bbcd1..e4cd2e80 100644 --- a/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java +++ b/src/main/java/org/spongepowered/asm/util/SignaturePrinter.java @@ -28,6 +28,7 @@ import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; import org.objectweb.asm.tree.LocalVariableNode; import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import com.google.common.base.Strings; @@ -63,6 +64,11 @@ public class SignaturePrinter { */ private String modifiers = "private void"; + /** + * Fully qualify class names + */ + private boolean fullyQualified; + public SignaturePrinter(MethodNode method) { this(method.name, Type.VOID_TYPE, Type.getArgumentTypes(method.desc)); this.setModifiers(method); @@ -73,6 +79,10 @@ public class SignaturePrinter { this.setModifiers(method); } + public SignaturePrinter(MemberInfo member) { + this(member.name, member.desc); + } + public SignaturePrinter(String name, String desc) { this(name, Type.getReturnType(desc), Type.getArgumentTypes(desc)); } @@ -111,11 +121,11 @@ public class SignaturePrinter { } public String getReturnType() { - return SignaturePrinter.getTypeName(this.returnType, false); + return SignaturePrinter.getTypeName(this.returnType, false, this.fullyQualified); } public void setModifiers(MethodNode method) { - String returnType = SignaturePrinter.getTypeName(Type.getReturnType(method.desc), false); + String returnType = SignaturePrinter.getTypeName(Type.getReturnType(method.desc), false, this.fullyQualified); if ((method.access & Opcodes.ACC_PUBLIC) != 0) { this.setModifiers("public " + returnType); } else if ((method.access & Opcodes.ACC_PROTECTED) != 0) { @@ -132,21 +142,40 @@ public class SignaturePrinter { return this; } + public SignaturePrinter setFullyQualified(boolean fullyQualified) { + this.fullyQualified = fullyQualified; + return this; + } + + public boolean isFullyQualified() { + return this.fullyQualified; + } + @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(this.modifiers).append(" ").append(this.name).append('('); + return this.appendArgs(new StringBuilder().append(this.modifiers).append(" ").append(this.name), false).toString(); + } + + public String toDescriptor() { + StringBuilder args = this.appendArgs(new StringBuilder(), true); + return args.append(SignaturePrinter.getTypeName(this.returnType, false, this.fullyQualified)).toString(); + } + + private StringBuilder appendArgs(StringBuilder sb, boolean typesOnly) { + sb.append('('); for (int var = 0; var < this.argTypes.length; var++) { if (this.argTypes[var] == null) { continue; } else if (var > 0) { - sb.append(", "); + sb.append(','); + if (!typesOnly) { + sb.append(' '); + } } - String name = Strings.isNullOrEmpty(this.argNames[var]) ? "unnamed" + var : this.argNames[var]; + String name = typesOnly ? null : Strings.isNullOrEmpty(this.argNames[var]) ? "unnamed" + var : this.argNames[var]; this.appendType(sb, this.argTypes[var], name); } - sb.append(")"); - return sb.toString(); + return sb.append(")"); } private StringBuilder appendType(StringBuilder sb, Type type, String name) { @@ -156,17 +185,26 @@ public class SignaturePrinter { case Type.OBJECT: return this.appendType(sb, type.getClassName(), name); default: - return sb.append(SignaturePrinter.getTypeName(type, false)).append(' ').append(name); + sb.append(SignaturePrinter.getTypeName(type, false, this.fullyQualified)); + if (name != null) { + sb.append(' ').append(name); + } + return sb; } } private StringBuilder appendType(StringBuilder sb, String typeName, String name) { - typeName = typeName.substring(typeName.lastIndexOf('.') + 1); + if (!this.fullyQualified) { + typeName = typeName.substring(typeName.lastIndexOf('.') + 1); + } sb.append(typeName); if (typeName.endsWith("CallbackInfoReturnable")) { - sb.append('<').append(SignaturePrinter.getTypeName(this.returnType, true)).append('>'); + sb.append('<').append(SignaturePrinter.getTypeName(this.returnType, true, this.fullyQualified)).append('>'); } - return sb.append(' ').append(name); + if (name != null) { + sb.append(' ').append(name); + } + return sb; } /** @@ -178,6 +216,19 @@ public class SignaturePrinter { * integer primitive or "String" for java.lang.String */ public static String getTypeName(Type type, boolean box) { + return SignaturePrinter.getTypeName(type, box, false); + } + + /** + * Get the source code name for the specified type + * + * @param type Type to generate a friendly name for + * @param box True to return the equivalent boxing type for primitives + * @param fullyQualified fully-qualify class names + * @return String representation of the specified type, eg "int" for an + * integer primitive or "String" for java.lang.String + */ + public static String getTypeName(Type type, boolean box, boolean fullyQualified) { switch (type.getSort()) { case Type.VOID: return box ? "Void" : "void"; case Type.BOOLEAN: return box ? "Boolean" : "boolean"; @@ -188,10 +239,12 @@ public class SignaturePrinter { case Type.FLOAT: return box ? "Float" : "float"; case Type.LONG: return box ? "Long" : "long"; case Type.DOUBLE: return box ? "Double" : "double"; - case Type.ARRAY: return SignaturePrinter.getTypeName(type.getElementType(), box) + "[]"; + case Type.ARRAY: return SignaturePrinter.getTypeName(type.getElementType(), box, fullyQualified) + "[]"; case Type.OBJECT: String typeName = type.getClassName(); - typeName = typeName.substring(typeName.lastIndexOf('.') + 1); + if (!fullyQualified) { + typeName = typeName.substring(typeName.lastIndexOf('.') + 1); + } return typeName; default: return "Object";