Example usage for java.lang Class getConstructor

List of usage examples for java.lang Class getConstructor

Introduction

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

Prototype

@CallerSensitive
public Constructor<T> getConstructor(Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

Usage

From source file:io.aino.agents.wso2.mediator.AinoMediator.java

private static Object invokePropertyExpression(String expression, MediatorProperty mp) {
    try {//from   w w w  . j a  v  a 2  s  .  co  m
        // Workaround for a peculiar WSO2 update approach where an
        // intermediate (SynapsePath) class was added into the class
        // hierarchy
        Class<?> parameterClass = getParameterClass();
        Class<?> synapseXPathClass = getSynapseXpathClass();
        Method setExpression = MediatorProperty.class.getMethod("setExpression", parameterClass);
        return setExpression.invoke(mp, synapseXPathClass.getConstructor(String.class).newInstance(expression));
    } catch (Exception e) {
        throw new InvalidAgentConfigException(
                "Unable to initialize a AinoMediator due to a reflection-related exception.", e);
    }
}

From source file:com.amalto.core.storage.hibernate.FlatTypeMapping.java

@SuppressWarnings("rawtypes")
private static Serializable createCompositeId(ClassLoader classLoader, Class<?> clazz,
        List<Object> compositeIdValues) {
    try {//from   ww w .ja v a2  s  .  c o  m
        Class<?> idClass = classLoader.loadClass(clazz.getName() + "_ID"); //$NON-NLS-1$
        Class[] parameterClasses = new Class[compositeIdValues.size()];
        int i = 0;
        for (Object o : compositeIdValues) {
            parameterClasses[i++] = o.getClass();
        }
        Constructor<?> constructor = idClass.getConstructor(parameterClasses);
        return (Serializable) constructor.newInstance(compositeIdValues.toArray());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:TypeUtil.java

/** Convert String value to instance.
 * @param type The class of the instance, which may be a primitive TYPE field.
 * @param value The value as a string.//ww w.j a va  2  s.c o m
 * @return The value as an Object.
 */
public static Object valueOf(Class type, String value) {
    try {
        if (type.equals(java.lang.String.class))
            return value;

        Method m = (Method) class2Value.get(type);
        if (m != null)
            return m.invoke(null, new Object[] { value });

        if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class))
            return new Character(value.charAt(0));

        Constructor c = type.getConstructor(stringArg);
        return c.newInstance(new Object[] { value });
    }

    catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Error)
            throw (Error) (e.getTargetException());

    }
    return null;
}

From source file:com.haulmont.cuba.core.config.type.TypeFactory.java

@Nullable
private static TypeFactory getInferred(Class<?> returnType) {
    // special case due to Character.valueOf(char) does not accept String
    if (returnType == Character.class) {
        return new CharTypeFactory();
    }//w  w w .j  a  v a  2 s  .  co  m

    for (String methodName : FACTORY_METHOD_NAMES) {
        try {
            Method factoryMethod = returnType.getMethod(methodName, String.class);
            if (isAcceptableMethod(returnType, factoryMethod)) {
                return new StaticTypeFactory(factoryMethod);
            }
        } catch (NoSuchMethodException ex) {
            // Ignore failure.
        }
    }
    try {
        Constructor ctor = returnType.getConstructor(String.class);
        if (Modifier.isPublic(ctor.getModifiers())) {
            return new ConstructorTypeFactory(ctor);
        }
    } catch (NoSuchMethodException e) {
        return null;
    }
    return null;
}

From source file:hudson.util.CascadingUtil.java

/**
 * Returns project property by specified key.
 *
 * @param currentJob job that should be analyzed.
 * @param key key.//from w w w  . j  a v a 2 s  . c o m
 * @param clazz required property class.
 * If class is not null and property was not found, property of given class will be created.
 * @return {@link org.hudsonci.api.model.IProjectProperty} instance or null.
 * @throws IllegalArgumentException if currentJob is null.
 */
@SuppressWarnings("unchecked")
public static <T extends IProjectProperty> T getProjectProperty(Job currentJob, String key, Class<T> clazz) {
    if (currentJob == null) {
        throw new IllegalArgumentException("Job cannot be null");
    }
    IProjectProperty t = (IProjectProperty) currentJob.getProjectProperties().get(key);
    if (null == t && null != clazz) {
        try {
            t = clazz.getConstructor(IJob.class).newInstance(currentJob);
            t.setKey(key);
            currentJob.putProjectProperty(key, t);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
    return (T) t;
}

From source file:net.lightbody.bmp.proxy.jetty.util.TypeUtil.java

/** Convert String value to instance.
 * @param type The class of the instance, which may be a primitive TYPE field.
 * @param value The value as a string./*  w ww  . jav  a 2  s.c o m*/
 * @return The value as an Object.
 */
public static Object valueOf(Class type, String value) {
    try {
        if (type.equals(java.lang.String.class))
            return value;

        Method m = (Method) class2Value.get(type);
        if (m != null)
            return m.invoke(null, new Object[] { value });

        if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class))
            return new Character(value.charAt(0));

        Constructor c = type.getConstructor(stringArg);
        return c.newInstance(new Object[] { value });
    } catch (NoSuchMethodException e) {
        LogSupport.ignore(log, e);
    } catch (IllegalAccessException e) {
        LogSupport.ignore(log, e);
    } catch (InstantiationException e) {
        LogSupport.ignore(log, e);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Error)
            throw (Error) (e.getTargetException());
        LogSupport.ignore(log, e);
    }
    return null;
}

From source file:org.openqa.grid.internal.BaseRemoteProxy.java

/**
 * Takes a registration request and return the RemoteProxy associated to it. It can be any class
 * extending RemoteProxy./*from   www . j  a v  a  2  s .c om*/
 *
 * @param request  The request
 * @param registry The registry to use
 * @return a new instance built from the request.
 */
@SuppressWarnings("unchecked")
public static <T extends RemoteProxy> T getNewInstance(RegistrationRequest request, Registry registry) {
    try {
        String proxyClass = request.getRemoteProxyClass();
        if (proxyClass == null) {
            log.fine("No proxy class. Using default");
            proxyClass = BaseRemoteProxy.class.getCanonicalName();
        }
        Class<?> clazz = Class.forName(proxyClass);
        log.fine("Using class " + clazz.getName());
        Object[] args = new Object[] { request, registry };
        Class<?>[] argsClass = new Class[] { RegistrationRequest.class, Registry.class };
        Constructor<?> c = clazz.getConstructor(argsClass);
        Object proxy = c.newInstance(args);
        if (proxy instanceof RemoteProxy) {
            ((RemoteProxy) proxy).setupTimeoutListener();
            return (T) proxy;
        } else {
            throw new InvalidParameterException("Error: " + proxy.getClass() + " isn't a remote proxy");
        }

    } catch (InvocationTargetException e) {
        log.log(Level.SEVERE, e.getTargetException().getMessage(), e.getTargetException());
        throw new InvalidParameterException("Error: " + e.getTargetException().getMessage());

    } catch (Exception e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new InvalidParameterException("Error: " + e.getMessage());
    }
}

From source file:com.connectsdk.service.DeviceService.java

public static DeviceService getService(Class<? extends DeviceService> clazz, ServiceConfig serviceConfig) {
    try {// www .  j  a v  a2s.c  o  m
        Constructor<? extends DeviceService> constructor = clazz.getConstructor(ServiceConfig.class);

        return constructor.newInstance(serviceConfig);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.xhsoft.framework.common.utils.ClassUtil.java

/**
 * //  w  w w.  j  a v  a 2s.  c  om
 * @param className
 * @param parent
 * @param parameterTypes
 * @param parametersObjects
 * @return
 * @throws ClassNotFoundException
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws IllegalArgumentException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws InvocationTargetException - Object
 * @author: lizj
 */
public static Object getInstance(String className, String parent, Class<?>[] parameterTypes,
        Object[] parametersObjects) throws ClassNotFoundException, SecurityException, NoSuchMethodException,
        IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
    Class<?> clazz = ClassUtil.getClass(className, parent);

    Constructor<?> c = clazz.getConstructor(parameterTypes);

    return c.newInstance(parametersObjects);
}

From source file:org.exist.xquery.modules.jfreechart.JFreeChartFactory.java

private static void setCategoryItemLabelGenerator(JFreeChart chart, Configuration config)
        throws XPathException {
    String className = config.getCategoryItemLabelGeneratorClass();
    CategoryItemLabelGenerator generator = null;

    if (className != null) {
        try {/*from ww  w .jav  a  2 s  . c  o m*/
            Class generatorClass = Class.forName(className);

            Class[] argsClass = new Class[] { String.class, NumberFormat.class };
            String param = config.getCategoryItemLabelGeneratorParameter();
            NumberFormat fmt = new DecimalFormat(config.getCategoryItemLabelGeneratorNumberFormat());
            Object[] args = new Object[] { param, fmt };

            Constructor argsConstructor = generatorClass.getConstructor(argsClass);

            generator = (CategoryItemLabelGenerator) argsConstructor.newInstance(args);
        } catch (Exception e) {
            throw (new XPathException(
                    "Cannot instantiate CategoryItemLabelGeneratorClass: " + className + ", exception: " + e));
        }

        if (chart.getPlot() instanceof SpiderWebPlot) {
            ((SpiderWebPlot) chart.getPlot()).setLabelGenerator(generator);
        } else {
            CategoryItemRenderer renderer = ((CategoryPlot) chart.getPlot()).getRenderer();

            renderer.setBaseItemLabelGenerator(generator);

            renderer.setItemLabelsVisible(true);
        }
    }
}