| 1 | package net.sourceforge.retroweaver.runtime.java.lang.reflect; |
| 2 | |
| 3 | import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC; |
| 4 | import static org.objectweb.asm.Opcodes.ACC_VARARGS; |
| 5 | |
| 6 | import java.lang.reflect.Constructor; |
| 7 | import java.lang.reflect.Method; |
| 8 | |
| 9 | import net.sourceforge.retroweaver.runtime.java.lang.annotation.AIB; |
| 10 | import net.sourceforge.retroweaver.runtime.java.lang.annotation.Annotation; |
| 11 | |
| 12 | /** |
| 13 | * A mirror of java.lang.reflect.Constructor. Unfortunately, although this class |
| 14 | * is almost a direct copy of Method_, there's not an easy way to share the |
| 15 | * implementation between the two, because Method and Constructor don't have any |
| 16 | * sort of typed relationship. |
| 17 | * |
| 18 | * @author Toby Reyelts Date: Feb 21, 2005 Time: 2:40:01 AM |
| 19 | */ |
| 20 | public class Constructor_ { |
| 21 | |
| 22 | private Constructor_() { |
| 23 | // private constructor |
| 24 | } |
| 25 | |
| 26 | // Returns this element's annotation for the specified type if such an |
| 27 | // annotation is present, else null. |
| 28 | public static <T extends Annotation> T getAnnotation(final Constructor cons, final Class<T> annotationType) { |
| 29 | final Class c = cons.getDeclaringClass(); |
| 30 | return AIB.getAib(c).getMethodAnnotation(cons.getName(), cons.getParameterTypes(), Void.class, annotationType); |
| 31 | } |
| 32 | |
| 33 | // Returns all annotations present on this element. |
| 34 | // |
| 35 | // We have to search superclasses and interfaces, looking for annotations |
| 36 | // which are meta-annotated with the @Inherited Annotation. |
| 37 | // (Yuk) |
| 38 | // |
| 39 | public static Annotation[] getAnnotations(final Constructor cons) { |
| 40 | return getDeclaredAnnotations(cons); |
| 41 | } |
| 42 | |
| 43 | // Returns all annotations that are directly present on this element. |
| 44 | public static Annotation[] getDeclaredAnnotations(final Constructor cons) { |
| 45 | final Class c = cons.getDeclaringClass(); |
| 46 | return AIB.getAib(c).getMethodAnnotations(cons.getName(), cons.getParameterTypes(), Void.class); |
| 47 | } |
| 48 | |
| 49 | // Returns true if an annotation for the specified type is present on this |
| 50 | // element, else false. |
| 51 | public static boolean isAnnotationPresent(final Constructor cons, final Class<? extends Annotation> annotationType) { |
| 52 | return getAnnotation(cons, annotationType) != null; |
| 53 | } |
| 54 | |
| 55 | public static Annotation[][] getParameterAnnotations(final Constructor cons) { |
| 56 | final Class c = cons.getDeclaringClass(); |
| 57 | return AIB.getAib(c).getMethodParameterAnnotations(cons.getName(), cons.getParameterTypes(), Void.class); |
| 58 | } |
| 59 | |
| 60 | // Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise. |
| 61 | public static boolean isVarArgs(final Constructor cons) { |
| 62 | final Class c = cons.getDeclaringClass(); |
| 63 | return ReflectionDescriptor.getReflectionDescriptor(c).testConstructorAccess(cons, ACC_VARARGS); |
| 64 | } |
| 65 | |
| 66 | // Returns true if this constructor is a synthetic constructor; returns false otherwise. |
| 67 | public static boolean isSynthetic(final Constructor cons) { |
| 68 | final Class c = cons.getDeclaringClass(); |
| 69 | return ReflectionDescriptor.getReflectionDescriptor(c).testConstructorAccess(cons, ACC_SYNTHETIC); |
| 70 | } |
| 71 | |
| 72 | } |