Example usage for java.lang.reflect Constructor getParameterTypes

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

Introduction

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

Prototype

@Override
public Class<?>[] getParameterTypes() 

Source Link

Usage

From source file:org.eiichiro.bootleg.Types.java

/**
 * Returns <code>true</code> if the specified type is an user define value 
 * type.//from w w w  .  j ava 2s  .c o  m
 * User defined value type must satisfy either of the following condition.
 * <ol>
 * <li>(The class) has a public constructor that takes one String.class parameter.</li>
 * <li>Has a public static factory method that named 'valueOf' and takes one 
 * String.class parameter.</li>
 * </ol>
 * 
 * @param type The type to be tested.
 * @return <code>true</code> if the specified type is an user define value 
 * type.
 */
public static boolean isUserDefinedValueType(Type type) {
    Class<?> rawType = getRawType(type);

    if (rawType == null) {
        return false;
    }

    for (Constructor<?> constructor : rawType.getConstructors()) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();

        if (parameterTypes.length == 1 && parameterTypes[0].equals(String.class)) {
            return true;
        }
    }

    for (Method method : rawType.getMethods()) {
        if (method.getName().equals("valueOf") && Modifier.isStatic(method.getModifiers())) {
            return true;
        }
    }

    return false;
}

From source file:org.hibernate.internal.util.ReflectHelper.java

/**
 * Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping
 * {@link Type types}.//w w  w . j  av a  2  s  .  c  om
 *
 * @param clazz The class needing instantiation
 * @param types The types representing the required ctor param signature
 * @return The matching constructor.
 * @throws PropertyNotFoundException Indicates we could not locate an appropriate constructor (todo : again with PropertyNotFoundException???)
 */
public static Constructor getConstructor(Class clazz, Type[] types) throws PropertyNotFoundException {
    final Constructor[] candidates = clazz.getConstructors();
    for (int i = 0; i < candidates.length; i++) {
        final Constructor constructor = candidates[i];
        final Class[] params = constructor.getParameterTypes();
        if (params.length == types.length) {
            boolean found = true;
            for (int j = 0; j < params.length; j++) {
                final boolean ok = params[j].isAssignableFrom(types[j].getReturnedClass())
                        || (types[j] instanceof PrimitiveType
                                && params[j] == ((PrimitiveType) types[j]).getPrimitiveClass());
                if (!ok) {
                    found = false;
                    break;
                }
            }
            if (found) {
                constructor.setAccessible(true);
                return constructor;
            }
        }
    }
    throw new PropertyNotFoundException("no appropriate constructor in class: " + clazz.getName());
}

From source file:org.seasar.mayaa.impl.util.ObjectUtil.java

public static Object newInstance(Constructor constructor, Object[] argValues) {
    if (constructor == null || argValues == null
            || constructor.getParameterTypes().length != argValues.length) {
        throw new IllegalArgumentException();
    }/*w ww .j  a v a2  s .  c  om*/
    try {
        return constructor.newInstance(argValues);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.konakart.apiexamples.BaseApiExample.java

/**
 * Utility method to instantiate an engine instance. The class name of the engine is passed in
 * as a parameter so this method may be used to instantiate a POJO engine, a SOAP engine, an RMI
 * engine or a JSON engine./*from  w  w w .  j  a v  a2 s .  co m*/
 * 
 * @param engineClassName
 * @param config
 * @return Returns an Engine Instance
 * @throws IllegalArgumentException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws ClassNotFoundException
 */
protected static KKEngIf getKKEngByName(String engineClassName, EngineConfig config)
        throws IllegalArgumentException, InstantiationException, IllegalAccessException,
        InvocationTargetException, ClassNotFoundException {
    Class<?> engineClass = Class.forName(engineClassName);
    KKEngIf kkeng = null;
    Constructor<?>[] constructors = engineClass.getConstructors();
    Constructor<?> engConstructor = null;
    if (constructors != null && constructors.length > 0) {
        for (int i = 0; i < constructors.length; i++) {
            Constructor<?> constructor = constructors[i];
            Class<?>[] parmTypes = constructor.getParameterTypes();
            if (parmTypes != null && parmTypes.length == 1) {
                String parmName = parmTypes[0].getName();
                if (parmName != null && parmName.equals("com.konakart.appif.EngineConfigIf")) {
                    engConstructor = constructor;
                }
            }
        }
    }

    if (engConstructor != null) {
        kkeng = (KKEngIf) engConstructor.newInstance(config);
    }

    return kkeng;
}

From source file:sx.blah.discord.SpoofBot.java

/**
 * Randomly constructs an object.// w  w  w  .  jav a  2 s  . c o m
 *
 * @param client The discord client.
 * @param clazz The class to construct.
 * @param <T> The type of object to construct.
 * @return The constructed object (or null if not possible).
 *
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws InstantiationException
 */
public static <T> T randomizeObject(IDiscordClient client, Class<T> clazz)
        throws IllegalAccessException, InvocationTargetException, InstantiationException {
    if (canBeRandomized(clazz)) {
        if (String.class.isAssignableFrom(clazz))
            return (T) getRandString();
        else if (Character.class.isAssignableFrom(clazz))
            return (T) getRandCharacter();
        else if (Boolean.class.isAssignableFrom(clazz))
            return (T) getRandBoolean();
        else if (Number.class.isAssignableFrom(clazz))
            return (T) getRandNumber((Class<? extends Number>) clazz);
        else if (Void.class.isAssignableFrom(clazz))
            return null;
        else if (IDiscordClient.class.isAssignableFrom(clazz))
            return (T) client;

    } else {
        outer: for (Constructor constructor : clazz.getConstructors()) {
            Object[] parameters = new Object[constructor.getParameterCount()];
            for (Class<?> param : constructor.getParameterTypes()) {
                if (!canBeRandomized(param))
                    continue outer;
            }
            if (parameters.length > 0) {
                for (int i = 0; i < parameters.length; i++) {
                    parameters[i] = randomizeObject(client, constructor.getParameterTypes()[i]);
                }
                return (T) constructor.newInstance(parameters);
            } else {
                return (T) constructor.newInstance();
            }
        }
    }
    return null;
}

From source file:org.cellprofiler.preferences.CellProfilerPreferences.java

/**
 * Get the preferences factory supplied by the JRE or
 * provided as a service.//from  www .  ja  v  a  2  s .  co  m
 * 
 * @return the default preferences factory.
 */
static private PreferencesFactory getJREPreferencesFactory() {
    synchronized (lock) {
        if (delegatePreferencesFactory == null) {
            do {
                /*
                 * First, see if there is a PreferencesFactory
                 * provided as a service.
                 */
                final ServiceLoader<PreferencesFactory> pfServiceLoader = ServiceLoader
                        .loadInstalled(PreferencesFactory.class);
                final Iterator<PreferencesFactory> pfIter = pfServiceLoader.iterator();
                if (pfIter.hasNext()) {
                    delegatePreferencesFactory = pfIter.next();
                    break;
                }
                /*
                 * Next, try the WindowsPreferencesFactory if OS is Windows.
                 */
                String pfName = (SystemUtils.IS_OS_WINDOWS) ? "java.util.prefs.WindowsPreferencesFactory"
                        : "java.util.prefs.FilePreferencesFactory";
                try {
                    Class<?> pfClass = Class.forName("java.util.prefs.WindowsPreferencesFactory", false, null);
                    Class<?> pfFuckYou = Class.forName("java.util.prefs.WindowsPreferences", true, null);
                    Constructor<?>[] pfConstructors = pfClass.getDeclaredConstructors();
                    for (Constructor<?> c : pfConstructors) {
                        if (c.getParameterTypes().length == 0) {
                            /*
                             * Bad boy - it's package-private AND I CALL IT ANYWAY BAH HA HA HA HA HA HA
                             */
                            c.setAccessible(true);
                            delegatePreferencesFactory = (PreferencesFactory) c.newInstance(new Object[0]);
                            break;
                        }
                    }
                    break;
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                /*
                 * And as a last resort, there's always our headless
                 * preferences factory.
                 */
                delegatePreferencesFactory = new HeadlessPreferencesFactory();
            } while (false);
        }
    }
    return delegatePreferencesFactory;

}

From source file:Mopex.java

/**
 * Creates constructor with the signature of c and a new name. It adds some
 * code after generating a super statement to call c. This method is used
 * when generating a subclass of class that declared c.
 * // ww w. j  av  a2s  .com
 * @return String
 * @param c
 *            java.lang.Constructor
 * @param name
 *            String
 * @param code
 *            String
 */
//start extract createRenamedConstructor
public static String createRenamedConstructor(Constructor c, String name, String code) {
    Class[] pta = c.getParameterTypes();
    String fpl = formalParametersToString(pta);
    String apl = actualParametersToString(pta);
    Class[] eTypes = c.getExceptionTypes();
    String result = name + "(" + fpl + ")\n";
    if (eTypes.length != 0)
        result += "    throws " + classArrayToString(eTypes) + "\n";
    result += "{\n    super(" + apl + ");\n" + code + "}\n";
    return result;
}

From source file:Mopex.java

/**
 * Returns a String that represents the signature for a constructor.
 * /*ww w  .  j  av a  2  s .  com*/
 * @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:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java

public static boolean[] findByRefParams(Constructor<?> method) {

    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    Class<?>[] paramTypes = method.getParameterTypes();
    return findByRefParams(paramAnnotations, paramTypes);

}

From source file:org.eclipse.wb.internal.core.eval.evaluators.InvocationEvaluator.java

/**
 * If {@link ThisExpression} argument values is not compatible with parameter, replace it with
 * <code>null</code>./*  www .j a  va2s.  c o m*/
 */
private static void fixThisExpressionArguments(Constructor<?> constructor, List<Expression> argumentExpressions,
        Object[] argumentValues) {
    Class<?>[] parameterTypes = constructor.getParameterTypes();
    Assert.isTrue(parameterTypes.length == argumentValues.length,
            "Incompatible count of parameters %s and arguments %s", parameterTypes.length,
            argumentValues.length);
    for (int i = 0; i < parameterTypes.length; i++) {
        // empty varArgs
        if (i >= argumentExpressions.size()) {
            break;
        }
        // check for ThisExpression
        if (argumentExpressions.get(i) instanceof ThisExpression) {
            Class<?> parameterType = parameterTypes[i];
            Object argument = argumentValues[i];
            if (argument != null && !parameterType.isAssignableFrom(argument.getClass())) {
                argumentValues[i] = null;
            }
        }
    }
}