Example usage for java.lang.reflect Constructor getDeclaringClass

List of usage examples for java.lang.reflect Constructor getDeclaringClass

Introduction

In this page you can find the example usage for java.lang.reflect Constructor getDeclaringClass.

Prototype

@Override
public Class<T> getDeclaringClass() 

Source Link

Document

Returns the Class object representing the class that declares the constructor represented by this object.

Usage

From source file:ShowClass.java

/**
 * Print the modifiers, return type, name, parameter types and exception type
 * of a method or constructor. Note the use of the Member interface to allow
 * this method to work with both Method and Constructor objects
 *///  ww w  . ja  v a2  s.  c  om
public static void print_method_or_constructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
        Method m = (Method) member;
        returntype = m.getReturnType();
        parameters = m.getParameterTypes();
        exceptions = m.getExceptionTypes();
        System.out.print(
                "  " + modifiers(member.getModifiers()) + typename(returntype) + " " + member.getName() + "(");
    } else {
        Constructor c = (Constructor) member;
        parameters = c.getParameterTypes();
        exceptions = c.getExceptionTypes();
        System.out.print("  " + modifiers(member.getModifiers()) + typename(c.getDeclaringClass()) + "(");
    }

    for (int i = 0; i < parameters.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
        System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(exceptions[i]));
    }
    System.out.println(";");
}

From source file:org.robobinding.util.ConstructorUtils.java

/**
 * <p>/*from  w  ww.j av  a2 s.  co m*/
 * Checks if the specified constructor is accessible.
 * </p>
 * 
 * <p>
 * This simply ensures that the constructor is accessible.
 * </p>
 * 
 * @param <T>
 *            the constructor type
 * @param ctor
 *            the prototype constructor object, not null
 * @return the constructor, null if no matching accessible constructor found
 * @see java.lang.SecurityManager
 */
public static <T> Constructor<T> getAccessibleConstructor(final Constructor<T> ctor) {
    return MemberUtils.isAccessible(ctor) && Modifier.isPublic(ctor.getDeclaringClass().getModifiers()) ? ctor
            : null;
}

From source file:org.evosuite.setup.TestClusterUtils.java

public static void makeAccessible(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())
            || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) {
        constructor.setAccessible(true);
    }/*from   ww w  .  j  a  va 2s  .c  om*/
}

From source file:org.grouplens.grapht.util.ConstructorProxy.java

/**
 * Construct a proxy for a constructor.// ww  w  . j a v a 2s  . c o m
 * @param constructor The constructor to proxy.
 * @return The constructor proxy representing {@code constructor}.
 */
public static ConstructorProxy of(Constructor constructor) {
    Class<?>[] ptypes = constructor.getParameterTypes();
    ClassProxy[] proxies = new ClassProxy[ptypes.length];
    for (int i = ptypes.length - 1; i >= 0; i--) {
        proxies[i] = ClassProxy.of(ptypes[i]);
    }
    ConstructorProxy proxy = new ConstructorProxy(ClassProxy.of(constructor.getDeclaringClass()), proxies);
    proxy.constructor = constructor;
    return proxy;
}

From source file:xiaofei.library.hermes.util.TypeUtils.java

public static void validateAccessible(Constructor<?> constructor) throws HermesException {
    if (constructor.isAnnotationPresent(WithinProcess.class)) {
        throw new HermesException(ErrorCodes.METHOD_WITH_PROCESS,
                "Constructor " + constructor.getName() + " of class "
                        + constructor.getDeclaringClass().getName()
                        + " has a WithProcess annotation on it, so it cannot be accessed from "
                        + "outside the process.");
    }/* w  w w  . j  a  va  2  s . c o  m*/
}

From source file:org.evosuite.setup.TestUsageChecker.java

public static boolean canUse(Constructor<?> c) {

    if (c.isSynthetic()) {
        return false;
    }/*from  w ww . j a  va 2s  . c  o m*/

    // synthetic constructors are OK
    if (Modifier.isAbstract(c.getDeclaringClass().getModifiers()))
        return false;

    // TODO we could enable some methods from Object, like getClass
    //if (c.getDeclaringClass().equals(java.lang.Object.class))
    //   return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().isAnonymousClass())
        return false;

    if (c.getDeclaringClass().isLocalClass()) {
        logger.debug("Skipping constructor of local class " + c.getName());
        return false;
    }

    if (c.getDeclaringClass().isMemberClass() && !TestUsageChecker.canUse(c.getDeclaringClass()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (Properties.hasTargetClassBeenLoaded() && !c.getDeclaringClass().equals(targetClass)) {
            logger.debug("Excluding deprecated constructor " + c.getName());
            return false;
        }
    }

    if (isForbiddenNonDeterministicCall(c)) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        TestClusterUtils.makeAccessible(c);
        return true;
    }

    for (java.lang.reflect.Type paramType : c.getGenericParameterTypes()) {
        if (!canUse(paramType))
            return false;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            TestClusterUtils.makeAccessible(c);
            return true;
        }
    }

    return false;
}

From source file:com.springframework.beans.BeanUtils.java

/**
 * Convenience method to instantiate a class using the given constructor.
 * As this method doesn't try to load classes by name, it should avoid
 * class-loading issues./*from   w  w  w  .  j a v a 2  s.  c  o m*/
 * <p>Note that this method tries to set the constructor accessible
 * if given a non-accessible (that is, non-public) constructor.
 * @param ctor the constructor to instantiate
 * @param args the constructor arguments to apply
 * @return the new instance
 * @throws BeanInstantiationException if the bean cannot be instantiated
 */
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
    Assert.notNull(ctor, "Constructor must not be null");
    try {
        ReflectionUtils.makeAccessible(ctor);
        return ctor.newInstance(args);
    } catch (InstantiationException ex) {
        throw new BeanInstantiationException(ctor.getDeclaringClass(), "Is it an abstract class?", ex);
    } catch (IllegalAccessException ex) {
        throw new BeanInstantiationException(ctor.getDeclaringClass(), "Is the constructor accessible?", ex);
    } catch (IllegalArgumentException ex) {
        throw new BeanInstantiationException(ctor.getDeclaringClass(), "Illegal arguments for constructor", ex);
    } catch (InvocationTargetException ex) {
        throw new BeanInstantiationException(ctor.getDeclaringClass(), "Constructor threw exception",
                ex.getTargetException());
    }
}

From source file:co.jirm.mapper.definition.SqlParameterDefinition.java

private static Map<String, SqlParameterDefinition> getSqlBeanParametersFromConstructorProperties(
        Constructor<?> c, SqlObjectConfig config) {
    Map<String, SqlParameterDefinition> parameters = new LinkedHashMap<String, SqlParameterDefinition>();

    String[] constructorProperties = c.getAnnotation(ConstructorProperties.class).value();
    Class<?>[] pts = c.getParameterTypes();

    for (int i = 0; i < pts.length; i++) {
        parameters.put(constructorProperties[i],
                parameterDef(config, c.getDeclaringClass(), constructorProperties[i], pts[i], i));
    }/*from   www. j av  a 2 s .com*/

    return parameters;
}

From source file:com.liferay.cli.support.util.ReflectionUtils.java

/**
 * Make the given constructor accessible, explicitly setting it accessible
 * if necessary. The <code>setAccessible(true)</code> method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active).// w  ww  .j a  v a  2s.  co  m
 * 
 * @param ctor the constructor to make accessible
 * @see java.lang.reflect.Constructor#setAccessible
 */
public static void makeAccessible(final Constructor<?> ctor) {
    if (!Modifier.isPublic(ctor.getModifiers())
            || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) {
        ctor.setAccessible(true);
    }
}

From source file:org.regenstrief.hl7.util.UtilHL7.java

/**
 * Retrieves a Class of the desired type
 * /*from w ww  . j a va  2 s  . c o  m*/
 * @param valueType the desired type
 * @return the Class
 **/
public final static Class<? extends HL7Data> getClass(final String valueType) {
    final Constructor<? extends HL7Data> cons = nameCache.get(valueType);
    return cons == null ? null : cons.getDeclaringClass();
}