From be783986fc98f5fd11f4b3cdf2d192536a9fb4e9 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 18 Feb 2015 17:59:07 +0000 Subject: [PATCH] Supermixin support (WIP) --- .../asm/mixin/MixinTransformerError.java | 47 +++++ .../asm/mixin/transformer/MixinData.java | 159 +++------------ .../asm/mixin/transformer/MixinInfo.java | 183 +++++++++++++++--- .../mixin/transformer/MixinTransformer.java | 31 ++- .../org/spongepowered/asm/util/ASMHelper.java | 27 ++- 5 files changed, 282 insertions(+), 165 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/MixinTransformerError.java diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinTransformerError.java b/src/main/java/org/spongepowered/asm/mixin/MixinTransformerError.java new file mode 100644 index 00000000..80585b28 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/MixinTransformerError.java @@ -0,0 +1,47 @@ +/* + * 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.asm.mixin; + + +/** + * Error to throw for things which really shouldn't happen + */ +public class MixinTransformerError extends Error { + + private static final long serialVersionUID = 1L; + + public MixinTransformerError(String message) { + super(message); + } + + public MixinTransformerError(Throwable cause) { + super(cause); + } + + public MixinTransformerError(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java index bc464663..b8ac830a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java @@ -24,25 +24,14 @@ */ package org.spongepowered.asm.mixin.transformer; -import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Set; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.tree.AbstractInsnNode; -import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; -import org.spongepowered.asm.mixin.Implements; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import org.spongepowered.asm.mixin.injection.struct.Target; -import org.spongepowered.asm.util.ASMHelper; /** @@ -55,7 +44,7 @@ public class MixinData implements IReferenceMapperContext { /** * Mixin info */ - private final MixinInfo info; + private final MixinInfo mixin; /** * Tree @@ -68,19 +57,9 @@ public class MixinData implements IReferenceMapperContext { private final ClassNode targetClass; /** - * Methods we need to rename + * Target ClassInfo */ - private final Map renamedMethods = new HashMap(); - - /** - * Interfaces soft-implemented using {@link Implements} - */ - private final List softImplements = new ArrayList(); - - /** - * All interfaces implemented by this mixin, including soft implementations - */ - private final Set interfaces = new HashSet(); + private final ClassInfo targetClassInfo; /** * Information about methods in the target class, used to keep track of @@ -92,13 +71,14 @@ public class MixinData implements IReferenceMapperContext { * ctor * * @param info Mixin information + * @param mixin Mixin classnode * @param target target class */ - MixinData(MixinInfo info, ClassNode target) { - this.info = info; - this.classNode = info.getClassNode(ClassReader.EXPAND_FRAMES); + MixinData(MixinInfo info, ClassNode mixin, ClassNode target) { + this.mixin = info; + this.classNode = mixin; this.targetClass = target; - this.prepare(); + this.targetClassInfo = ClassInfo.forName(target.name); } /* (non-Javadoc) @@ -106,7 +86,7 @@ public class MixinData implements IReferenceMapperContext { */ @Override public String toString() { - return String.format("%s:%s", this.info.getParent().getName(), this.info.getName()); + return this.mixin.toString(); } /** @@ -124,7 +104,7 @@ public class MixinData implements IReferenceMapperContext { * @return the mixin class name */ public String getClassName() { - return this.info.getClassName(); + return this.mixin.getClassName(); } /* (non-Javadoc) @@ -133,7 +113,7 @@ public class MixinData implements IReferenceMapperContext { */ @Override public String getClassRef() { - return this.info.getClassRef(); + return this.mixin.getClassRef(); } /** @@ -155,6 +135,15 @@ public class MixinData implements IReferenceMapperContext { return this.targetClass; } + /** + * Get the target classinfo + * + * @return the target class info + */ + public ClassInfo getTargetClassInfo() { + return this.targetClassInfo; + } + /** * Get a target method handle from the target class * @@ -181,7 +170,7 @@ public class MixinData implements IReferenceMapperContext { * @return the priority (only meaningful in relation to other mixins) */ public int getPriority() { - return this.info.getPriority(); + return this.mixin.getPriority(); } /** @@ -190,7 +179,7 @@ public class MixinData implements IReferenceMapperContext { * @return mixin interfaces */ public Set getInterfaces() { - return this.interfaces; + return this.mixin.getInterfaces(); } /** @@ -200,7 +189,7 @@ public class MixinData implements IReferenceMapperContext { * @return true if the sourcefile property should be set on the target class */ public boolean shouldSetSourceFile() { - return this.info.getParent().shouldSetSourceFile(); + return this.mixin.getParent().shouldSetSourceFile(); } /* (non-Javadoc) @@ -209,103 +198,7 @@ public class MixinData implements IReferenceMapperContext { */ @Override public ReferenceMapper getReferenceMapper() { - return this.info.getParent().getReferenceMapper(); - } - - /** - * Prepare the mixin, applies any pre-processing transformations - */ - private void prepare() { - this.readImplementations(); - this.findRenamedMethods(); - this.transformMethods(); - } - - /** - * Read and process any {@link Implements} annotations on the mixin - */ - private void readImplementations() { - this.interfaces.addAll(this.classNode.interfaces); - - AnnotationNode implementsAnnotation = ASMHelper.getInvisibleAnnotation(this.classNode, Implements.class); - if (implementsAnnotation == null) { - return; - } - - List interfaces = ASMHelper.getAnnotationValue(implementsAnnotation); - if (interfaces == null) { - return; - } - - for (AnnotationNode interfaceNode : interfaces) { - InterfaceInfo interfaceInfo = InterfaceInfo.fromAnnotation(interfaceNode); - this.softImplements.add(interfaceInfo); - this.interfaces.add(interfaceInfo.getInternalName()); - } - } - - /** - * Let's do this - */ - private void findRenamedMethods() { - for (MethodNode mixinMethod : this.classNode.methods) { - String oldSignature = mixinMethod.name + mixinMethod.desc; - AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); - if (shadowAnnotation != null) { - String prefix = MixinData.getAnnotationValue(shadowAnnotation, "prefix", Shadow.class); - if (mixinMethod.name.startsWith(prefix)) { - String newName = mixinMethod.name.substring(prefix.length()); - this.renamedMethods.put(oldSignature, newName); - mixinMethod.name = newName; - } - } - - for (InterfaceInfo iface : this.softImplements) { - if (iface.renameMethod(mixinMethod)) { - this.renamedMethods.put(oldSignature, mixinMethod.name); - } - } - } - } - - /** - * Apply discovered method renames to method invokations in the mixin - */ - private void transformMethods() { - for (MethodNode mixinMethod : this.classNode.methods) { - for (Iterator iter = mixinMethod.instructions.iterator(); iter.hasNext();) { - AbstractInsnNode insn = iter.next(); - if (insn instanceof MethodInsnNode) { - MethodInsnNode methodNode = (MethodInsnNode)insn; - String newName = this.renamedMethods.get(methodNode.name + methodNode.desc); - if (newName != null) { - methodNode.name = newName; - } - } - } - } - } - - /** - * Gets an annotation value or returns the default if the annotation value - * is not present - * - * @param annotation - * @param key - * @param annotationClass - * @return Value of the specified annotation node, default value if not - * specified, or null if no value or default - */ - private static String getAnnotationValue(AnnotationNode annotation, String key, Class annotationClass) { - String value = ASMHelper.getAnnotationValue(annotation, key); - if (value == null) { - try { - value = (String)annotationClass.getDeclaredMethod(key).getDefaultValue(); - } catch (NoSuchMethodException ex) { - // Don't care - } - } - return value; + return this.mixin.getParent().getReferenceMapper(); } /** @@ -315,7 +208,7 @@ public class MixinData implements IReferenceMapperContext { * @param targetClass Target class */ public void preApply(String transformedName, ClassNode targetClass) { - this.info.preApply(transformedName, targetClass); + this.mixin.preApply(transformedName, targetClass); } /** @@ -325,6 +218,6 @@ public class MixinData implements IReferenceMapperContext { * @param targetClass Target class */ public void postApply(String transformedName, ClassNode targetClass) { - this.info.postApply(transformedName, targetClass); + this.mixin.postApply(transformedName, targetClass); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index e5357f67..3ee5037c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -27,18 +27,30 @@ package org.spongepowered.asm.mixin.transformer; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; +import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.objectweb.asm.ClassReader; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; +import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.mixin.Implements; import org.spongepowered.asm.mixin.InvalidMixinException; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; +import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.util.ASMHelper; import com.google.common.base.Function; @@ -116,6 +128,16 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * Configuration plugin */ private final transient IMixinConfigPlugin plugin; + + /** + * All interfaces implemented by this mixin, including soft implementations + */ + private final transient Set interfaces = new HashSet(); + + /** + * Interfaces soft-implemented using {@link Implements} + */ + private final transient List softImplements = new ArrayList(); /** * Internal ctor, called by {@link MixinConfig} @@ -138,29 +160,14 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { this.mixinBytes = this.loadMixinClass(this.className, runTransformers); ClassNode classNode = this.getClassNode(0); - this.classInfo = this.readInfo(classNode); + this.classInfo = ClassInfo.fromClassNode(classNode); this.priority = this.readPriority(classNode); this.targetClasses = this.readTargetClasses(classNode, suppressPlugin); this.targetClassNames = Collections.unmodifiableList(Lists.transform(this.targetClasses, Functions.toStringFunction())); this.detachedSuper = this.validateTargetClasses(classNode); - } - - /** - * Initialises the class info and performs some pre-flight checks on the - * mixin - * - * @param classNode - * @return - */ - private ClassInfo readInfo(ClassNode classNode) { - ClassInfo classInfo = ClassInfo.fromClassNode(classNode); - - // isInner (shouldn't) return true for static inner classes - if (classInfo.isInner()) { - throw new InvalidMixinException("Inner class mixin must be declared static"); - } - - return classInfo; + this.validateMixin(classNode); + this.readImplementations(classNode); + this.prepare(classNode); } /** @@ -233,7 +240,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { Integer priority = ASMHelper.getAnnotationValue(mixin, "priority"); return priority == null ? 1000 : priority.intValue(); } - + private boolean validateTargetClasses(ClassNode classNode) { boolean detached = false; @@ -243,7 +250,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { continue; } - if (!targetClass.hasSuperClass(classNode.superName)) { + if (!targetClass.hasSuperClass(classNode.superName, ClassInfo.Traversal.NONE)) { throw new InvalidMixinException("Super class '" + classNode.superName.replace('/', '.') + "' of " + this.name + " was not found in the hierarchy of target class '" + targetClass + "'"); } @@ -254,6 +261,118 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { return detached; } + /** + * Performs pre-flight checks on the mixin + * + * @param classNode + */ + private void validateMixin(ClassNode classNode) { + // isInner (shouldn't) return true for static inner classes + if (this.classInfo.isInner()) { + throw new InvalidMixinException("Inner class mixin must be declared static"); + } + + // Can't have remappable fields or methods on a multi-target mixin, because after obfuscation the fields will remap to conflicting names + if (this.targetClasses.size() > 1) { + for (FieldNode field : classNode.fields) { + this.checkRemappable(Shadow.class, field.name, ASMHelper.getVisibleAnnotation(field, Shadow.class)); + } + + for (MethodNode method : classNode.methods) { + this.checkRemappable(Shadow.class, method.name, ASMHelper.getVisibleAnnotation(method, Shadow.class)); + AnnotationNode overwrite = ASMHelper.getVisibleAnnotation(method, Overwrite.class); + if (overwrite != null && ((method.access & Opcodes.ACC_STATIC) == 0 || (method.access & Opcodes.ACC_PUBLIC) == 0)) { + throw new InvalidMixinException("Found @Overwrite annotation on " + method.name + " in " + this); + } + } + } + } + + private void checkRemappable(Class annotationClass, String name, AnnotationNode annotation) { + if (annotation != null && ASMHelper.getAnnotationValue(annotation, "remap", Boolean.TRUE)) { + throw new InvalidMixinException("Found a remappable @" + annotationClass.getSimpleName() + " annotation on " + name + " in " + this); + } + } + + /** + * Prepare the mixin, applies any pre-processing transformations + */ + private ClassNode prepare(ClassNode classNode) { + this.findRenamedMethods(classNode); + this.transformMethods(classNode); + return classNode; + } + + /** + * Read and process any {@link Implements} annotations on the mixin + */ + private void readImplementations(ClassNode classNode) { + this.interfaces.addAll(classNode.interfaces); + + AnnotationNode implementsAnnotation = ASMHelper.getInvisibleAnnotation(classNode, Implements.class); + if (implementsAnnotation == null) { + return; + } + + List interfaces = ASMHelper.getAnnotationValue(implementsAnnotation); + if (interfaces == null) { + return; + } + + for (AnnotationNode interfaceNode : interfaces) { + InterfaceInfo interfaceInfo = InterfaceInfo.fromAnnotation(interfaceNode); + this.softImplements.add(interfaceInfo); + this.interfaces.add(interfaceInfo.getInternalName()); + } + } + + /** + * Let's do this + */ + private void findRenamedMethods(ClassNode classNode) { + for (MethodNode mixinMethod : classNode.methods) { + Method method = this.classInfo.findMethod(mixinMethod); + + AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); + if (shadowAnnotation != null) { + String prefix = ASMHelper.getAnnotationValue(shadowAnnotation, "prefix", Shadow.class); + if (mixinMethod.name.startsWith(prefix)) { + String newName = mixinMethod.name.substring(prefix.length()); + method.renameTo(newName); + mixinMethod.name = newName; + } + } + + for (InterfaceInfo iface : this.softImplements) { + if (iface.renameMethod(mixinMethod)) { + method.renameTo(mixinMethod.name); + } + } + } + } + + /** + * Apply discovered method renames to method invokations in the mixin + */ + private void transformMethods(ClassNode classNode) { + for (MethodNode mixinMethod : classNode.methods) { + for (Iterator iter = mixinMethod.instructions.iterator(); iter.hasNext();) { + AbstractInsnNode insn = iter.next(); + if (insn instanceof MethodInsnNode) { + MethodInsnNode methodNode = (MethodInsnNode)insn; + Method method = this.classInfo.findMethodInHierarchy(methodNode, true); + if (method != null && method.isRenamed()) { + methodNode.name = method.getName(); + } + } + } + } + } + + ClassInfo getClassInfo() { + return this.classInfo; + } + /** * Get the parent config which declares this mixin */ @@ -335,7 +454,16 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { public int getPriority() { return this.priority; } - + + /** + * Get all interfaces for this mixin + * + * @return mixin interfaces + */ + public Set getInterfaces() { + return this.interfaces; + } + /** * Get a new mixin data container for this info * @@ -343,7 +471,8 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @return */ public MixinData createData(ClassNode target) { - return new MixinData(this, target); + ClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES); + return new MixinData(this, this.prepare(classNode), target); } /** @@ -398,4 +527,12 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { this.plugin.postApply(transformedName, targetClass, this.className, this); } } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return String.format("%s:%s", this.parent.getName(), this.name); + } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index 24c7b9b2..58a1eafb 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -57,10 +57,13 @@ import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.InvalidMixinException; import org.spongepowered.asm.mixin.MixinEnvironment; +import org.spongepowered.asm.mixin.MixinTransformerError; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; +import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; +import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal; import org.spongepowered.asm.transformers.TreeTransformer; import org.spongepowered.asm.util.ASMHelper; @@ -246,7 +249,7 @@ public class MixinTransformer extends TreeTransformer { for (MixinConfig config : this.configs) { if (transformedName != null && transformedName.startsWith(config.getMixinPackage())) { - throw new RuntimeException(String.format("%s is a mixin class and cannot be referenced directly", transformedName)); + throw new NoClassDefFoundError(String.format("%s is a mixin class and cannot be referenced directly", transformedName)); } if (config.hasMixinsFor(transformedName)) { @@ -269,6 +272,8 @@ public class MixinTransformer extends TreeTransformer { } return basicClass; + } catch (Exception ex) { + throw new MixinTransformerError("An unexpected critical error was encountered", ex); } finally { this.reEntranceCheck--; } @@ -441,11 +446,16 @@ public class MixinTransformer extends TreeTransformer { throw new InvalidMixinException(String.format("Mixin classes cannot contain visible static methods or fields, found %s", field.name)); } + AnnotationNode shadow = ASMHelper.getVisibleAnnotation(field, Shadow.class); + String prefix = ASMHelper.getAnnotationValue(shadow, "prefix", Shadow.class); + if (field.name.startsWith(prefix)) { + throw new InvalidMixinException(String.format("Shadow field %s in %s has a shadow prefix. This is not allowed.", field.name, mixin)); + } + FieldNode target = this.findTargetField(targetClass, field); if (target == null) { // If this field is a shadow field but is NOT found in the target class, that's bad, mmkay - boolean isShadow = ASMHelper.getVisibleAnnotation(field, Shadow.class) != null; - if (isShadow) { + if (shadow != null) { throw new InvalidMixinException(String.format("Shadow field %s was not located in the target class", field.name)); } @@ -541,15 +551,21 @@ public class MixinTransformer extends TreeTransformer { * @param method * @param insn */ - private void updateStaticBindings(IReferenceMapperContext mixin, ClassNode target, MethodNode method, MethodInsnNode insn) { + private void updateStaticBindings(MixinData mixin, ClassNode target, MethodNode method, MethodInsnNode insn) { if (INIT.equals(method.name) || insn.owner.equals(target.name) || target.name.startsWith("<")) { return; } ClassInfo targetClass = ClassInfo.forName(target.name); - String newOwner = targetClass.findMethodInHierarchy(insn.name, insn.desc, false); - if (newOwner != null) { - insn.owner = newOwner; + Method superMethod = targetClass.findMethodInHierarchy(insn.name, insn.desc, false, Traversal.SUPER); + if (superMethod != null) { + if (superMethod.getOwner().isMixin()) { + throw new InvalidMixinException("Invalid INVOKESPECIAL in " + mixin + " resolved " + insn.owner + " -> " + superMethod.getOwner() + + " for " + insn.name + insn.desc); + } + insn.owner = superMethod.getOwner().getName(); + } else if (ClassInfo.forName(insn.owner).isMixin()) { + throw new MixinTransformerError("Error resolving INVOKESPECIAL target for " + insn.owner + "." + insn.name + " in " + mixin); } } @@ -589,6 +605,7 @@ public class MixinTransformer extends TreeTransformer { } targetClass.methods.add(method); + mixin.getTargetClassInfo().addMethod(method); ASMHelper.setVisibleAnnotation(method, MixinMerged.class, "mixin", mixin.getClassName(), diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index 4beec5d1..0a271b99 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -597,7 +597,30 @@ public class ASMHelper { T returnValue = ASMHelper.getAnnotationValue(annotation, key); return returnValue != null ? returnValue : defaultValue; } - + + /** + * Gets an annotation value or returns the default value of the annotation + * if the annotation value is not present + * + * @param annotation + * @param key + * @param annotationClass + * @return Value of the specified annotation node, default value if not + * specified, or null if no value or default + */ + @SuppressWarnings("unchecked") + public static T getAnnotationValue(AnnotationNode annotation, String key, Class annotationClass) { + T value = ASMHelper.getAnnotationValue(annotation, key); + if (value == null) { + try { + value = (T)annotationClass.getDeclaredMethod(key).getDefaultValue(); + } catch (NoSuchMethodException ex) { + // Don't care + } + } + return value; + } + /** * Get the value of an annotation node and do pseudo-duck-typing via Java's * crappy generics @@ -612,7 +635,7 @@ public class ASMHelper { public static T getAnnotationValue(AnnotationNode annotation, String key) { boolean getNextValue = false; - if (annotation.values == null) { + if (annotation == null || annotation.values == null) { return null; }