001    package net.sourceforge.retroweaver.runtime.java.lang.reflect;
002    
003    import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC;
004    import static org.objectweb.asm.Opcodes.ACC_VARARGS;
005    
006    import java.lang.reflect.Constructor;
007    import java.lang.reflect.Method;
008    
009    import net.sourceforge.retroweaver.runtime.java.lang.annotation.AIB;
010    import net.sourceforge.retroweaver.runtime.java.lang.annotation.Annotation;
011    
012    /**
013     * A mirror of java.lang.reflect.Constructor. Unfortunately, although this class
014     * is almost a direct copy of Method_, there's not an easy way to share the
015     * implementation between the two, because Method and Constructor don't have any
016     * sort of typed relationship.
017     * 
018     * @author Toby Reyelts Date: Feb 21, 2005 Time: 2:40:01 AM
019     */
020    public class Constructor_ {
021    
022            private Constructor_() {
023                    // private constructor
024            }
025    
026            // Returns this element's annotation for the specified type if such an
027            // annotation is present, else null.
028            public static <T extends Annotation> T getAnnotation(final Constructor cons, final Class<T> annotationType) {
029                    final Class c = cons.getDeclaringClass();
030                    return AIB.getAib(c).getMethodAnnotation(cons.getName(), cons.getParameterTypes(), Void.class, annotationType);
031            }
032    
033            // Returns all annotations present on this element.
034            //
035            // We have to search superclasses and interfaces, looking for annotations
036            // which are meta-annotated with the @Inherited Annotation.
037            // (Yuk)
038            //
039            public static Annotation[] getAnnotations(final Constructor cons) {
040                    return getDeclaredAnnotations(cons);
041            }
042    
043            // Returns all annotations that are directly present on this element.
044            public static Annotation[] getDeclaredAnnotations(final Constructor cons) {
045                    final Class c = cons.getDeclaringClass();
046                    return AIB.getAib(c).getMethodAnnotations(cons.getName(), cons.getParameterTypes(), Void.class);
047            }
048    
049            // Returns true if an annotation for the specified type is present on this
050            // element, else false.
051            public static boolean isAnnotationPresent(final Constructor cons, final Class<? extends Annotation> annotationType) {
052                    return getAnnotation(cons, annotationType) != null;
053            }
054    
055            public static Annotation[][] getParameterAnnotations(final Constructor cons) {
056                    final Class c = cons.getDeclaringClass();
057                    return AIB.getAib(c).getMethodParameterAnnotations(cons.getName(), cons.getParameterTypes(), Void.class);
058            }
059    
060            // Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise.
061            public static boolean isVarArgs(final Constructor cons) {
062                    final Class c = cons.getDeclaringClass();
063                    return ReflectionDescriptor.getReflectionDescriptor(c).testConstructorAccess(cons, ACC_VARARGS);
064            }
065    
066            // Returns true if this constructor is a synthetic constructor; returns false otherwise.
067            public static boolean isSynthetic(final Constructor cons) {
068                    final Class c = cons.getDeclaringClass();
069                    return ReflectionDescriptor.getReflectionDescriptor(c).testConstructorAccess(cons, ACC_SYNTHETIC);
070            }
071    
072    }