Example usage for java.lang.reflect Constructor getName

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

Introduction

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

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of this constructor, as a string.

Usage

From source file:Main.java

public static String ctorToString(final Constructor constructor) {
    if (constructor == null) {
        return "null";
    }//from  ww w .j  a v a  2 s. c o m

    Class[] params = constructor.getParameterTypes();
    StringBuffer sb = new StringBuffer(constructor.getName());
    sb.append("(");
    for (int i = 0; i < params.length; i++) {
        String s = params[i].getName();
        sb.append(s);
        if (i < params.length - 1) {
            sb.append(", ");
        }
    }
    sb.append(")");
    return sb.toString();
}

From source file:MethodHashing.java

public static long constructorHash(Constructor method) throws Exception {
    Class[] parameterTypes = method.getParameterTypes();
    String methodDesc = method.getName() + "(";
    for (int j = 0; j < parameterTypes.length; j++) {
        methodDesc += getTypeString(parameterTypes[j]);
    }//from w  w  w .  j  a v  a  2s  . co m
    methodDesc += ")";

    long hash = 0;
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(512);
    MessageDigest messagedigest = MessageDigest.getInstance("SHA");
    DataOutputStream dataoutputstream = new DataOutputStream(
            new DigestOutputStream(bytearrayoutputstream, messagedigest));
    dataoutputstream.writeUTF(methodDesc);
    dataoutputstream.flush();
    byte abyte0[] = messagedigest.digest();
    for (int j = 0; j < Math.min(8, abyte0.length); j++)
        hash += (long) (abyte0[j] & 0xff) << j * 8;
    return hash;
}

From source file:com.github.juanmf.java2plant.Parser.java

protected static void addConstructorUse(Set<Relation> relations, Class<?> fromType, Type toType,
        Constructor<?> c) {
    String name = TypesHelper.getSimpleName(c.getName()) + "()";
    addUse(relations, fromType, toType, c, name);
}

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

private static void checksumClass(Class<?> type, MessageDigest digest) {
    // we compute a big hash of all the members of the class, and its superclasses.

    List<String> members = new ArrayList<String>();
    for (Constructor<?> c : type.getDeclaredConstructors()) {
        if (isInjectionSensitive(c)) {
            members.add(String.format("%s(%s)", c.getName(), StringUtils.join(c.getParameterTypes(), ", ")));
        }/*from  w w  w. j  a v a  2  s. c o m*/
    }
    for (Method m : type.getDeclaredMethods()) {
        if (isInjectionSensitive(m)) {
            members.add(String.format("%s(%s): %s", m.getName(), StringUtils.join(m.getParameterTypes(), ", "),
                    m.getReturnType()));
        }
    }
    for (Field f : type.getDeclaredFields()) {
        if (isInjectionSensitive(f)) {
            members.add(f.getName() + ":" + f.getType().getName());
        }
    }

    Collections.sort(members);

    Class<?> sup = type.getSuperclass();
    if (sup != null) {
        checksumClass(sup, digest);
    }
    for (String mem : members) {
        digest.update(mem.getBytes(UTF8));
    }
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.acl.ProxyUtils.java

/**
 * Finds the constructor in the unproxied superclass if proxied.
 * @param constructor  the constructor/*ww  w .  ja v a 2s.  c o  m*/
 * @return  the constructor in the unproxied class
 */
public static Constructor<?> unproxy(final Constructor<?> constructor) {
    Class<?> clazz = constructor.getDeclaringClass();

    if (!AopUtils.isCglibProxyClass(clazz)) {
        return constructor;
    }

    Class<?> searchType = unproxy(clazz);
    while (searchType != null) {
        for (Constructor<?> c : searchType.getConstructors()) {
            if (constructor.getName().equals(c.getName()) && (constructor.getParameterTypes() == null
                    || Arrays.equals(constructor.getParameterTypes(), c.getParameterTypes()))) {
                return c;
            }
        }
        searchType = searchType.getSuperclass();
    }

    return null;
}

From source file:grails.plugin.springsecurity.acl.util.ProxyUtils.java

/**
 * Finds the constructor in the unproxied superclass if proxied.
 * @param constructor  the constructor//from   ww w  .j a  v  a  2s  . c om
 * @return  the constructor in the unproxied class
 */
public static Constructor<?> unproxy(final Constructor<?> constructor) {
    Class<?> clazz = constructor.getDeclaringClass();

    if (!isProxy(clazz)) {
        return constructor;
    }

    Class<?> searchType = unproxy(clazz);
    while (searchType != null) {
        for (Constructor<?> c : searchType.getConstructors()) {
            if (constructor.getName().equals(c.getName()) && (constructor.getParameterTypes() == null
                    || Arrays.equals(constructor.getParameterTypes(), c.getParameterTypes()))) {
                return c;
            }
        }
        searchType = searchType.getSuperclass();
    }

    return null;
}

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.");
    }//from  w w w .ja  v  a  2  s .  c om
}

From source file:Mopex.java

/**
 * Returns a String that represents the signature for a constructor.
 * /*from   ww w .  ja  va2  s.c  o m*/
 * @return String
 * @param c
 *            java.lang.Constructor
 */
//start extract constructorHeaderToString
public static String signatureToString(Constructor c) {
    return c.getName() + "(" + formalParametersToString(c.getParameterTypes()) + ")";
}

From source file:org.openehealth.ipf.commons.lbs.utils.NiceClass.java

/**
 * Ensures that a constructors of a class is null safe.
 * @param obj//from www .ja  v  a 2  s. co  m
 *          object to test
 * @param args
 *          dummy arguments of all different parameter types that the constructor has
 * @param optional
 *          optional dummy arguments that can also be set to {@code null} without breaking
 *          null-safety
 * @throws any thrown exception that were not expected. Never an {@link IllegalArgumentException}.
 */
public static void checkConstructorIsNullSafe(Constructor<?> constructor, List<?> args, List<?> optional)
        throws Exception {
    log.info("checked " + constructor);

    constructor.setAccessible(true);

    Class<?>[] parameterTypes = constructor.getParameterTypes();
    Object[] parameterValues = getParameterValues(parameterTypes, args);

    for (int idx = 0; idx < parameterValues.length; ++idx) {
        if (!isOptional(constructor.getName(), idx, optional, parameterValues[idx])) {
            Object[] parameterValuesWithNull = Arrays.copyOf(parameterValues, parameterValues.length);
            parameterValuesWithNull[idx] = null;

            try {
                constructor.newInstance(parameterValuesWithNull);
                fail("Should throw an " + IllegalArgumentException.class.getSimpleName());
            } catch (InvocationTargetException e) {
                assertEquals(IllegalArgumentException.class, e.getCause().getClass());
            }
        }
    }
}

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

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

    if (c.isSynthetic()) {
        return false;
    }/*www  .j av a2 s.  co 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;
}