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.openstreetmap.josm.plugins.conflation.config.parser.InstanceConstructor.java

/**
 * Return the class' public constructor with the largest number of arguments.
 *//*from w w  w .ja  v a  2 s  . co  m*/
private Constructor<?> getLargestPublicConstructor(Class<?> type) {
    Constructor<?> bestCnstr = null;
    for (Constructor<?> cnstr : type.getConstructors()) {
        if (Modifier.isPublic(cnstr.getModifiers()) && ((bestCnstr == null)
                || (cnstr.getParameterTypes().length > bestCnstr.getParameterTypes().length)))
            bestCnstr = cnstr;
    }
    return bestCnstr;
}

From source file:org.fornax.cartridges.sculptor.framework.event.annotation.PublishAdvice.java

/**
 * Check if joda date time library is used, without introducing runtime
 * dependency./*from  w  w w.  ja  va2 s  . c o  m*/
 */
private boolean isJoda(Class<?> clazz) {
    for (Constructor<?> each : clazz.getConstructors()) {
        Class<?>[] parameterTypes = each.getParameterTypes();
        if (parameterTypes.length > 0) {
            if (parameterTypes[0].getName().startsWith("org.joda.time.")) {
                return true;
            }
        }
    }
    return false;
}

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

/**
 * get constructor desc. "()V", "(Ljava/lang/String;I)V"
 * //from www.  j a v a2  s  .  com
 * @param c
 *            constructor.
 * @return desc
 */
public static String getDesc(final Constructor<?> c) {
    StringBuilder ret = new StringBuilder("(");
    Class<?>[] parameterTypes = c.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++)
        ret.append(getDesc(parameterTypes[i]));
    ret.append(')').append('V');
    return ret.toString();
}

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

/**
 * get constructor name. "()", "(java.lang.String,int)"
 * //from w w w  .  j a  va2s . c om
 * @param c
 *            constructor.
 * @return name.
 */
public static String getName(final Constructor<?> c) {
    StringBuilder ret = new StringBuilder("(");
    Class<?>[] parameterTypes = c.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        if (i > 0)
            ret.append(',');
        ret.append(getName(parameterTypes[i]));
    }
    ret.append(')');
    return ret.toString();
}

From source file:org.elasticsoftware.elasticactors.base.serialization.ObjectMapperBuilder.java

private boolean hasSingleConstrutorParameterMatching(Constructor constructor, Class parameterClass) {
    if (constructor.getParameterTypes().length == 1) {
        return constructor.getParameterTypes()[0].equals(parameterClass);
    } else {//from  w w  w .j  a  v  a2s . c o m
        return false;
    }
}

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]);
            }/*www .  j a  v  a  2 s. c  o  m*/
            constructor.newInstance(arguments);
        }
    }
}

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

public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException {
    Constructor<?> targetConstructor;
    try {/*from  w  w w .  j a  v a  2 s .  co  m*/
        targetConstructor = clazz.getConstructor(new Class<?>[] { paramType });
    } catch (NoSuchMethodException e) {
        targetConstructor = null;
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 1
                    && constructor.getParameterTypes()[0].isAssignableFrom(paramType)) {
                targetConstructor = constructor;
                break;
            }
        }
        if (targetConstructor == null) {
            throw e;
        }
    }
    return targetConstructor;
}

From source file:org.openvpms.web.component.im.edit.IMObjectEditorFactory.java

/**
 * Returns a constructor to construct a new editor.
 *
 * @param type    the editor type//from  www  . j a v  a2s  .co  m
 * @param object  the object to edit
 * @param parent  the parent object. May be {@code null}
 * @param context the layout context. May be {@code null}
 * @return a constructor to construct the editor, or {@code null} if none can be found
 */
private Constructor getConstructor(Class type, IMObject object, IMObject parent, LayoutContext context) {
    Constructor[] ctors = type.getConstructors();

    for (Constructor ctor : ctors) {
        // check parameters
        Class<?>[] ctorTypes = ctor.getParameterTypes();
        if (ctorTypes.length == 3) {
            Class<?> ctorObj = ctorTypes[0];
            Class<?> ctorParent = ctorTypes[1];
            Class<?> ctorLayout = ctorTypes[2];

            if (ctorObj.isAssignableFrom(object.getClass())
                    && ((parent != null && ctorParent.isAssignableFrom(parent.getClass()))
                            || (parent == null && IMObject.class.isAssignableFrom(ctorParent)))
                    && ((context != null && ctorLayout.isAssignableFrom(context.getClass()))
                            || (context == null && LayoutContext.class.isAssignableFrom(ctorLayout)))) {
                return ctor;
            }
        }
    }
    return null;
}

From source file:de.xirp.plugin.PluginManager.java

/**
 * Gets an instance of a plugin which is specified by the
 * PluginInfo Object by using a class loader for this plugin.
 * /*from ww  w.j av  a  2 s.c  o m*/
 * @param info
 *            Information about the plugin
 * @param robotName
 *            name of the robot this plugin is for
 * @return Instance of the requested plugin
 * @throws Exception
 *             exception when loading the plugin
 */
@SuppressWarnings("unchecked")
protected static SecurePluginView getInstance(PluginInfo info, String robotName) throws Exception {
    // try {
    URLClassLoader classLoader = getClassLoader(info);
    Class<IPlugable> claas = (Class<IPlugable>) Class.forName(info.getMainClass(), true, classLoader);
    Constructor[] constructors = claas.getConstructors();
    Constructor useConstructor = null;
    for (Constructor constructor : constructors) {
        Class[] types = constructor.getParameterTypes();
        if (types.length == 2) {
            // Locale locale = new Locale("");
            if ((types[0] == String.class) && (types[1] == PluginInfo.class)) {
                useConstructor = constructor;
                break;
            }
        } else if (types.length == 2) {
            if ((types[0] == PluginInfo.class)) {
                useConstructor = constructor;
                break;
            }
        }
    }
    if (useConstructor == null) {
        logClass.error(I18n.getString("PluginManager.log.pluginCouldNotBeInstanciatedNoConstructorFound", //$NON-NLS-1$
                info.getMainClass()));
    } else {
        Object[] arglist = new Object[useConstructor.getParameterTypes().length];
        if (arglist.length == 2) {
            arglist[0] = robotName;
            arglist[1] = info;
        } else if (arglist.length == 1) {
            arglist[0] = info;
        }
        IPlugable plugin = (IPlugable) useConstructor.newInstance(arglist);
        return new SecurePluginView(plugin);
    }
    return null;
}

From source file:org.xmlsh.util.JavaUtils.java

public static XValue newXValue(Class<?> cls, Constructor<?>[] constructors, List<XValue> args)
        throws CoreException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException {
    Constructor<?> c = getBestMatch(args, constructors);
    if (c == null)
        throw new InvalidArgumentException(
                "No construtor match found for: " + cls.getName() + "(" + getArgClassesString(args) + ")");

    Object obj = c.newInstance(getArgs(c.getParameterTypes(), args));
    return XValue.newXValue(null, obj);
}