Example usage for java.lang Class getDeclaredConstructors

List of usage examples for java.lang Class getDeclaredConstructors

Introduction

In this page you can find the example usage for java.lang Class getDeclaredConstructors.

Prototype

@CallerSensitive
public Constructor<?>[] getDeclaredConstructors() throws SecurityException 

Source Link

Document

Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.

Usage

From source file:Main.java

public static void print_class(Class c) {
    if (c.isInterface()) {
        System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c));
    } else if (c.getSuperclass() != null) {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends "
                + typename(c.getSuperclass()));
    } else {//from w  w w  .  j  a v a  2 s  .c  o  m
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c));
    }

    Class[] interfaces = c.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
        if (c.isInterface())
            System.out.print(" extends ");
        else
            System.out.print(" implements ");
        for (int i = 0; i < interfaces.length; i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(typename(interfaces[i]));
        }
    }

    System.out.println(" {");

    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
        print_method_or_constructor(constructors[i]);

    Field[] fields = c.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        print_field(fields[i]);
    }
    Method[] methods = c.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++)
        print_method_or_constructor(methods[i]);
    System.out.println("}");
}

From source file:org.pircbotx.hooks.events.MassEventTest.java

@Test(dataProvider = "eventObjectDataProvider", dataProviderClass = TestUtils.class, description = "Verify event has a single constructor")
public void singleConstructorTest(Class<?> event) {
    assertEquals(event.getDeclaredConstructors().length, 1,
            wrapClass(event, "No constructor or extra constructor found"));
}

From source file:br.com.caelum.vraptor.ioc.spring.InjectionBeanPostProcessor.java

@SuppressWarnings({ "rawtypes" })
private Constructor checkIfThereIsOnlyOneNonDefaultConstructor(Class beanClass) {
    Constructor[] constructors = beanClass.getDeclaredConstructors();
    if (constructors.length == 1) {
        if (constructors[0].getParameterTypes().length > 0) {
            return constructors[0];
        }//from  w  w w. j av  a 2 s  .  c  o m
    }
    return null;
}

From source file:org.pircbotx.hooks.events.MassEventTest.java

@Test(dependsOnMethods = "singleConstructorTest", dataProvider = "eventObjectDataProvider", dataProviderClass = TestUtils.class, description = "Verify event has a single constructor")
public void firstConstructorArgBotTest(Class<?> event) {
    Constructor constructor = event.getDeclaredConstructors()[0];
    assertEquals(constructor.getParameterTypes()[0], PircBotX.class,
            wrapClass(event, "First parameter of constructor isn't of PircBotX type"));
}

From source file:com.opengamma.masterdb.security.SecurityTestCase.java

private static <T> Constructor<T> getDefaultConstructor(final Class<T> clazz) {
    Constructor<T> defaultConstructor = null;
    Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
    for (Constructor<?> constructor : declaredConstructors) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        if (parameterTypes.length == 0) {
            defaultConstructor = (Constructor<T>) constructor;
            break;
        }/* w w  w .j  av a  2s  . co  m*/
    }
    return defaultConstructor;
}

From source file:org.force66.beantester.valuegens.TemporalValueGenerator.java

public TemporalValueGenerator(Class<?> type) {
    this.temporalType = type;
    Constructor<?>[] allConstructors = type.getDeclaredConstructors();
    for (Constructor<?> ctor : allConstructors) {
        if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0].getName().equals("long")) {
            longConstructor = ctor;/*from   w w  w  .  j a  v a2 s  .com*/
        }
    }

    Validate.notNull(longConstructor,
            "Not a temporal object with a long constructor.   class=" + type.getName());
}

From source file:org.eurekastreams.commons.reflection.ReflectiveInstantiator.java

/**
 * Instantiate an object of the input class type with the empty constructor.
 * Private constructors are handled./*from   ww w .  j ava 2 s  .  com*/
 *
 * @param objType
 *            the type of class to instantiate.
 *
 * @return an object of the input class type.
 */
public Object instantiateObject(final Class<?> objType) {
    Constructor<?> emptyConstructor = null;
    for (Constructor<?> constructor : objType.getDeclaredConstructors()) {
        if (constructor.getParameterTypes().length == 0) {
            emptyConstructor = constructor;
            break;
        }
    }
    if (emptyConstructor == null) {
        String message = "Cannot find empty constructor for " + objType.getName();
        log.error(message);
        throw new RuntimeException(message);
    }

    // set it accessible, just in case
    emptyConstructor.setAccessible(true);

    Object obj = null;
    try {
        obj = emptyConstructor.newInstance(new Object[0]);
    } catch (Exception e) {
        String message = "Couldn't instantiate: " + objType.getName();
        log.error(message, e);
        throw new RuntimeException(message);
    }

    return obj;
}

From source file:com.glaf.core.util.ReflectUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> Constructor<T> findMatchingConstructor(Class<T> clazz, Object[] args) {
    for (Constructor constructor : clazz.getDeclaredConstructors()) {
        if (matches(constructor.getParameterTypes(), args)) {
            return constructor;
        }//www.ja v  a2  s.co  m
    }
    return null;
}

From source file:se.vgregion.portal.innovatinosslussen.domain.TypesBeanTest.java

public void callConstructors(Class... types)
        throws IllegalAccessException, InvocationTargetException, InstantiationException {
    for (Class type : types) {
        Constructor[] constructors = type.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            Class[] parameterTypes = constructor.getParameterTypes();
            Object[] arguments = new Object[parameterTypes.length];
            for (int i = 0; i < arguments.length; i++) {
                arguments[i] = defaultPrim.get(parameterTypes[i]);
            }// w  ww.j av  a  2 s  .  c  o m
            constructor.newInstance(arguments);
        }
    }
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * /*  www  .j a  v  a 2  s. c om*/
 * ?annotationClass
 * 
 * @param targetClass
 *            Class
 * @param annotationClass
 *            Class
 * 
 * @return List
 */
public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) {
    Assert.notNull(targetClass, "targetClass?");
    Assert.notNull(annotationClass, "annotationClass?");

    List<T> result = new ArrayList<T>();
    Annotation annotation = targetClass.getAnnotation(annotationClass);
    if (annotation != null) {
        result.add((T) annotation);
    }
    Constructor[] constructors = targetClass.getDeclaredConstructors();
    // ?
    CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator());

    Field[] fields = targetClass.getDeclaredFields();
    // ?
    CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator());

    Method[] methods = targetClass.getDeclaredMethods();
    // ?
    CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator());

    for (Class<?> superClass = targetClass.getSuperclass(); superClass == null
            || superClass == Object.class; superClass = superClass.getSuperclass()) {
        List<T> temp = getAnnotations(superClass, annotationClass);
        if (CollectionUtils.isNotEmpty(temp)) {
            CollectionUtils.addAll(result, temp.iterator());
        }
    }

    return result;
}