get Annotated Constructors - Java java.lang.annotation

Java examples for java.lang.annotation:Constructor Annotation

Description

get Annotated Constructors

Demo Code

/*/*ww  w .ja v a  2 s . c  om*/
 * JFox - The most lightweight Java EE Application Server!
 * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
 *
 * JFox is licenced and re-distributable under GNU LGPL.
 */
//package com.java2s;
import java.lang.annotation.Annotation;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        Class annotation = String.class;
        System.out.println(java.util.Arrays
                .toString(getAnnotatedConstructors(clazz, annotation)));
    }

    public static Constructor[] getAnnotatedConstructors(Class clazz,
            Class<? extends Annotation> annotation) {
        List<Constructor> constructorList = new ArrayList<Constructor>();
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            if (constructor.isAnnotationPresent(annotation)) {
                constructorList.add(constructor);
            }
        }
        return constructorList.toArray(new Constructor[constructorList
                .size()]);
    }
}

Related Tutorials