List of usage examples for java.lang Class getConstructor
@CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:azkaban.common.utils.Utils.java
/** * Returns true if the constructor with the given args exists. * //from w ww . j av a2 s. c o m * @param c The class * @param args The arguments * @return */ public static boolean constructorExist(Class<?> c, Object... args) { Class<?>[] argTypes = getTypes(args); try { @SuppressWarnings("unused") Constructor<?> cons = c.getConstructor(argTypes); } catch (NoSuchMethodException e) { return false; } return true; }
From source file:com.microsoft.applicationinsights.web.internal.cookies.Cookie.java
/** * Gets the cookie from the given http request. * @param eClass The required cookie type. * @param request THe http request to get the cookies from. * @param cookieName The cookie name./*from w w w . ja va 2s .c o m*/ * @param <E> The required cookie type. * @return Cookie from the required type. */ public static <E> E getCookie(Class<E> eClass, HttpServletRequest request, String cookieName) { javax.servlet.http.Cookie[] cookies = request.getCookies(); if (cookies == null) { return null; } javax.servlet.http.Cookie httpCookie = null; for (javax.servlet.http.Cookie cookie : cookies) { if (cookie.getName().equals(cookieName)) { httpCookie = cookie; } } if (httpCookie == null) { // Http cookie hasn't been found. return null; } E instance = null; try { instance = eClass.getConstructor(javax.servlet.http.Cookie.class).newInstance(httpCookie); } catch (Exception e) { InternalLogger.INSTANCE.error("Failed to create %s cookie with error: %s", cookieName, e.getMessage()); } return instance; }
From source file:eu.stratosphere.nephele.profiling.ProfilingUtils.java
/** * Creates an instance of the job manager's profiling component. * // w w w . j a v a2 s. c o m * @param profilerClassName * the class name of the profiling component to load * @param jobManagerBindAddress * the address the job manager's RPC server is bound to * @return an instance of the job manager profiling component or <code>null</code> if an error occurs */ @SuppressWarnings("unchecked") public static JobManagerProfiler loadJobManagerProfiler(String profilerClassName, InetAddress jobManagerBindAddress) { final Class<? extends JobManagerProfiler> profilerClass; try { profilerClass = (Class<? extends JobManagerProfiler>) Class.forName(profilerClassName); } catch (ClassNotFoundException e) { LOG.error("Cannot find class " + profilerClassName + ": " + StringUtils.stringifyException(e)); return null; } JobManagerProfiler profiler = null; try { final Constructor<JobManagerProfiler> constr = (Constructor<JobManagerProfiler>) profilerClass .getConstructor(InetAddress.class); profiler = constr.newInstance(jobManagerBindAddress); } catch (InvocationTargetException e) { LOG.error("Cannot create profiler: " + StringUtils.stringifyException(e)); return null; } catch (NoSuchMethodException e) { LOG.error("Cannot create profiler: " + StringUtils.stringifyException(e)); return null; } catch (InstantiationException e) { LOG.error("Cannot create profiler: " + StringUtils.stringifyException(e)); return null; } catch (IllegalAccessException e) { LOG.error("Cannot create profiler: " + StringUtils.stringifyException(e)); return null; } catch (IllegalArgumentException e) { LOG.error("Cannot create profiler: " + StringUtils.stringifyException(e)); return null; } return profiler; }
From source file:com.puppycrawl.tools.checkstyle.utils.CommonUtil.java
/** * Gets constructor of targetClass.//from w ww .j a v a2 s . c om * @param targetClass * from which constructor is returned * @param parameterTypes * of constructor * @param <T> type of the target class object. * @return constructor of targetClass or {@link IllegalStateException} if any exception occurs * @see Class#getConstructor(Class[]) */ public static <T> Constructor<T> getConstructor(Class<T> targetClass, Class<?>... parameterTypes) { try { return targetClass.getConstructor(parameterTypes); } catch (NoSuchMethodException ex) { throw new IllegalStateException(ex); } }
From source file:org.appverse.web.framework.backend.api.converters.helpers.ConverterUtils.java
/** * Creates target bean of targetClass type copying source properties to ones * with the same name at target. Beans and collections are copied as * detached objects to allow lazy initialization. * // ww w. ja v a 2s . co m * @param source * Source Bean to copy * @param targetClass * Type of target bean to generate * @param converters * List of conveters of child bean properties * @return Target bean of targetClass type with source data converted * @throws Exception */ public static <TargetBean extends AbstractBean> TargetBean convert(AbstractBean source, Class<TargetBean> targetClass, IBeanConverter[] converters) throws Exception { // If source bean is null return null target bean if (source == null) { return null; } // Create target bean using target class default constructor Constructor<TargetBean> constructor = targetClass.getConstructor(new Class[] {}); TargetBean target = constructor.newInstance(new Object[] {}); // Obtain source fields (including inherited fields) Field[] sourceFields = getBeanFields(source); // Call class methods to copy simple properties, bean properties and // collection properties ConverterUtils.copyPlainFields(source, target, sourceFields); ConverterUtils.convertBeanFields(source, target, converters, sourceFields); ConverterUtils.convertBeanCollectionFields(source, target, converters, sourceFields); return target; }
From source file:Main.java
/** * Checks whether objects of the given class can be converted to the specified * type./*w w w . ja v a2 s .c o m*/ * * @see #convert(Object, Class) */ public static boolean canConvert(final Class<?> c, final Class<?> type) { // ensure type is well-behaved, rather than a primitive type final Class<?> saneType = getNonprimitiveType(type); // OK if the existing object can be casted if (canCast(c, saneType)) return true; // OK if string if (saneType == String.class) return true; // OK if source type is string and destination type is character // (in this case, the first character of the string would be used) if (String.class.isAssignableFrom(c) && saneType == Character.class) { return true; } // OK if appropriate wrapper constructor exists try { saneType.getConstructor(c); return true; } catch (final Exception exc) { // no known way to convert return false; } }
From source file:io.apiman.manager.api.micro.ManagerApiMicroServiceCdiFactory.java
/** * Creates a custom component from a loaded class. * @param componentType/*ww w . ja v a 2 s.co m*/ * @param componentClass * @param configProperties */ @SuppressWarnings("unchecked") private static <T> T createCustomComponent(Class<T> componentType, Class<?> componentClass, Map<String, String> configProperties) throws Exception { if (componentClass == null) { throw new IllegalArgumentException("Invalid component spec (class not found)."); //$NON-NLS-1$ } try { Constructor<?> constructor = componentClass.getConstructor(Map.class); return (T) constructor.newInstance(configProperties); } catch (Exception e) { } return (T) componentClass.getConstructor().newInstance(); }
From source file:de.kp.ames.web.function.domain.JsonBusinessProvider.java
/** * @param jaxrHandle//from ww w .ja v a 2 s . c o m * @param objectName * @return */ private static IJsonRegistryObject createJsonObjectForName(JaxrHandle jaxrHandle, String objectName) throws Exception { String MODUL_PACKAGE = "de.kp.ames.web.function.domain.model"; Class<?> clazz = Class.forName(MODUL_PACKAGE + "." + objectName); Constructor<?> constructor = clazz.getConstructor(JaxrHandle.class); Object instance = constructor.newInstance(jaxrHandle); return (IJsonRegistryObject) instance; }
From source file:lucee.transformer.bytecode.reflection.ASMProxyFactory.java
private static ASMMethod newInstance(Class<?> asmClass, Class<?> decClass, Class[] params) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException { Constructor<ASMMethod> constr = (Constructor<ASMMethod>) asmClass .getConstructor(new Class[] { Class.class, Class[].class }); return constr.newInstance(new Object[] { decClass, params }); //return (ASMMethod) asmClass.newInstance(); }
From source file:com.inmobi.grill.server.metastore.JAXBUtils.java
public static Storage storageFromXStorage(XStorage xs) { if (xs == null) { return null; }/*from w ww .ja v a 2s .c o m*/ Storage storage = null; try { Class<?> clazz = Class.forName(xs.getClassname()); Constructor<?> constructor = clazz.getConstructor(String.class); storage = (Storage) constructor.newInstance(new Object[] { xs.getName() }); storage.addProperties(mapFromXProperties(xs.getProperties())); return storage; } catch (Exception e) { throw new WebApplicationException( "Could not create storage class" + xs.getClassname() + "with name:" + xs.getName(), e); } }