List of usage examples for java.lang Class getConstructors
@CallerSensitive public Constructor<?>[] getConstructors() throws SecurityException
From source file:org.eclipse.winery.repository.rest.resources._support.AbstractComponentsResource.java
/** * @return an instance of the requested resource * @throws NotFoundException if resource doesn't exist. *//* ww w .j a v a2 s . c o m*/ public static AbstractComponentInstanceResource getComponentInstanceResource(DefinitionsChildId tcId) { String type = Util.getTypeForComponentId(tcId.getClass()); if (!RepositoryFactory.getRepository().exists(tcId)) { AbstractComponentsResource.LOGGER.debug("Definition child id " + tcId.toString() + " not found"); throw new NotFoundException("Definition child id " + tcId.toString() + " not found"); } Class<? extends AbstractComponentInstanceResource> newResource = AbstractComponentsResource .getComponentInstanceResourceClassForType(type); Constructor<?>[] constructors = newResource.getConstructors(); assert (constructors.length == 1); AbstractComponentInstanceResource newInstance; try { newInstance = (AbstractComponentInstanceResource) constructors[0].newInstance(tcId); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { AbstractComponentsResource.LOGGER.error("Could not instantiate sub resource " + tcId); throw new IllegalStateException("Could not instantiate sub resource", e); } return newInstance; }
From source file:org.openehr.rm.binding.DADLBinding.java
private static Constructor fullConstructor(Class klass) { if (klass == null) { return null; }/* w w w. j av a2 s.c om*/ Constructor[] array = klass.getConstructors(); for (Constructor constructor : array) { if (constructor.isAnnotationPresent(FullConstructor.class)) { return constructor; } } return null; }
From source file:org.eclipse.winery.repository.rest.resources.AbstractComponentsResource.java
/** * @return an instance of the requested resource * @throws NotFoundException if resource doesn't exist. *//*w w w. j av a2 s.co m*/ public static AbstractComponentInstanceResource getComponentInstaceResource(TOSCAComponentId tcId) { String type = Util.getTypeForComponentId(tcId.getClass()); if (!RepositoryFactory.getRepository().exists(tcId)) { AbstractComponentsResource.LOGGER.debug("TOSCA component id " + tcId.toString() + " not found"); throw new NotFoundException("TOSCA component id " + tcId.toString() + " not found"); } Class<? extends AbstractComponentInstanceResource> newResource = AbstractComponentsResource .getComponentInstanceResourceClassForType(type); Constructor<?>[] constructors = newResource.getConstructors(); assert (constructors.length == 1); AbstractComponentInstanceResource newInstance; try { newInstance = (AbstractComponentInstanceResource) constructors[0].newInstance(tcId); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { AbstractComponentsResource.LOGGER.error("Could not instantiate sub resource " + tcId); throw new IllegalStateException("Could not instantiate sub resource", e); } return newInstance; }
From source file:com.buffalokiwi.api.APIResponse.java
/** * Clone an api response /*from ww w. j a v a 2 s .c om*/ * @param <T> some class that extends APIResponse * @param that Some response to clone * @param type The class * @return A copy of that * @throws java.lang.NoSuchMethodException * @throws java.lang.InstantiationException * @throws java.lang.reflect.InvocationTargetException * @throws java.lang.IllegalAccessException */ public static <T extends APIResponse> T copyFrom(final IAPIResponse that, Class<T> type) throws NoSuchMethodException, InstantiationException, InvocationTargetException, IllegalArgumentException, IllegalAccessException { for (final Constructor<?> c : type.getConstructors()) { if (c.getParameterCount() == 6) { final T r = (T) c.newInstance(that.getProtocolVersion(), that.getStatusLine(), that.headers(), that.getRedirectLocations(), that.getBytes(), that.getResponseCharsetName()); return r; } } //T r = type.getConstructor( ProtocolVersion.class, StatusLine.class, List.class ).newInstance( that.getProtocolVersion(), that.getStatusLine(), that.headers()); //r.setContent( that.getResponseContent(), that.getResponseCharsetName()); //return r; throw new NoSuchMethodException("Failed to locate constructor in class " + type); }
From source file:org.apache.wiki.util.ClassUtil.java
/** * This method is used to locate and instantiate a mapped class. * You may redefine anything in the resource file which is located in your classpath * under the name <code>{@value #MAPPINGS}</code>. * <p>/*w ww .j a v a 2s. co m*/ * This is an extremely powerful system, which allows you to remap many of * the JSPWiki core classes to your own class. Please read the documentation * included in the default <code>{@value #MAPPINGS}</code> file to see * how this method works. * <p> * This method takes in an object array for the constructor arguments for classes * which have more than two constructors. * * @param requestedClass The name of the class you wish to instantiate. * @param initargs The parameters to be passed to the constructor. May be <code>null</code>. * @return An instantiated Object. * @throws WikiException If the class cannot be found or instantiated. The error is logged. * @since 2.5.40 */ public static Object getMappedObject(String requestedClass, Object... initargs) throws WikiException { try { Class<?> cl = getMappedClass(requestedClass); Constructor<?>[] ctors = cl.getConstructors(); // // Try to find the proper constructor by comparing the // initargs array classes and the constructor types. // for (int c = 0; c < ctors.length; c++) { Class<?>[] params = ctors[c].getParameterTypes(); if (params.length == initargs.length) { for (int arg = 0; arg < initargs.length; arg++) { if (params[arg].isAssignableFrom(initargs[arg].getClass())) { // // Ha, found it! Instantiating and returning... // return ctors[c].newInstance(initargs); } } } } // // No arguments, so we can just call a default constructor and // ignore the arguments. // Object o = cl.newInstance(); return o; } catch (InstantiationException e) { log.info("Cannot instantiate requested class " + requestedClass, e); throw new WikiException("Failed to instantiate class " + requestedClass, e); } catch (IllegalAccessException e) { log.info("Cannot access requested class " + requestedClass, e); throw new WikiException("Failed to instantiate class " + requestedClass, e); } catch (IllegalArgumentException e) { log.info("Illegal arguments when constructing new object", e); throw new WikiException("Failed to instantiate class " + requestedClass, e); } catch (InvocationTargetException e) { log.info("You tried to instantiate an abstract class " + requestedClass, e); throw new WikiException("Failed to instantiate class " + requestedClass, e); } }
From source file:org.wso2.carbon.governance.metadata.Util.java
private static Map<String, VersionBaseProvider> getVersionBaseProviderMap() throws MetadataException { if (versionBaseProviderMap != null) { return versionBaseProviderMap; }//from ww w. jav a2s . c o m ConcurrentHashMap<String, VersionBaseProvider> providerMap = new ConcurrentHashMap<String, VersionBaseProvider>(); try { FileInputStream fileInputStream = new FileInputStream(getConfigFile()); StAXOMBuilder builder = new StAXOMBuilder(fileInputStream); OMElement configElement = builder.getDocumentElement(); OMElement metadataProviders = configElement.getFirstChildWithName(new QName("metadataProviders")) .getFirstChildWithName(new QName("versionBaseProviders")); Iterator<OMElement> itr = metadataProviders.getChildrenWithLocalName("provider"); while (itr.hasNext()) { OMElement metadataProvider = itr.next(); String providerClass = metadataProvider.getAttributeValue(new QName("class")).trim(); String mediaType = metadataProvider.getAttributeValue(new QName(Constants.ATTRIBUTE_MEDIA_TYPE)); ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class<VersionBaseProvider> classObj = (Class<VersionBaseProvider>) Class.forName(providerClass, true, loader); if (!providerMap.containsKey(mediaType)) { providerMap.put(mediaType, (VersionBaseProvider) classObj.getConstructors()[0].newInstance(mediaType)); } else { // log.error("Classification URI already exists") } } } catch (Exception e) { throw new MetadataException(e.getMessage(), e); } return Util.versionBaseProviderMap = providerMap; }
From source file:net.neevek.android.lib.paginize.util.AnnotationUtils.java
public static void handleAnnotatedConstructors(Class clazz, Object object, ViewFinder viewFinder, boolean initForLazy) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {/*w w w . j ava 2 s .com*/ Constructor[] constructors = clazz.getConstructors(); for (int i = 0; i < constructors.length; ++i) { Constructor constructor = constructors[i]; Annotation[] annotations = constructor.getAnnotations(); for (int j = 0; j < annotations.length; ++j) { Annotation anno = annotations[j]; if (!(anno instanceof ListenerDefs)) { continue; } ListenerDefs annoContainer = (ListenerDefs) anno; if (annoContainer.value().length == 0) { continue; } Map<Class, Object> targetListenerCache = new HashMap<Class, Object>(); Annotation[] setListenerAnnoArray = annoContainer.value(); for (int k = 0; k < setListenerAnnoArray.length; ++k) { SetListeners setListenersAnno = (SetListeners) setListenerAnnoArray[k]; if ((!initForLazy && setListenersAnno.lazy()) || (initForLazy && !setListenersAnno.lazy())) { continue; } View view = viewFinder.findViewById(setListenersAnno.view()); if (view == null) { if (initForLazy && setListenersAnno.lazy()) { // view == null is tolerable in this case, we assume that this field // be injected later with another call to ViewWrapper.lazyInitializeLayout(). continue; } throw new IllegalArgumentException("The view specified in @SetListeners is not found: 0x" + Integer.toHexString(setListenersAnno.view()) + ", if this field is meant to be injected lazily, remember to specify the 'lazy' attribute."); } Object targetListener = getTargetListener(clazz, object, targetListenerCache, setListenersAnno.listener(), "@SetListeners"); if (targetListener == null) { targetListener = object; } AnnotationUtils.setListenersForView(view, setListenersAnno.listenerTypes(), targetListener); } } } }
From source file:xiaofei.library.hermes.util.TypeUtils.java
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>[] parameterTypes) throws HermesException { Constructor<?> result = null; Constructor<?>[] constructors = clazz.getConstructors(); for (Constructor<?> constructor : constructors) { if (classAssignable(constructor.getParameterTypes(), parameterTypes)) { if (result != null) { throw new HermesException(ErrorCodes.TOO_MANY_MATCHING_CONSTRUCTORS_FOR_CREATING_INSTANCE, "The class " + clazz.getName() + " has too many constructors whose " + " parameter types match the required types."); } else { result = constructor;// www. j a v a2 s .c om } } } if (result == null) { throw new HermesException(ErrorCodes.CONSTRUCTOR_NOT_FOUND, "The class " + clazz.getName() + " do not have a constructor whose " + " parameter types match the required types."); } return result; }
From source file:com.mani.cucumber.ReflectionUtils.java
private static <T> Constructor<T> findConstructor(Class<T> clazz, Object[] params) { for (Constructor<?> constructor : clazz.getConstructors()) { Class<?>[] paramTypes = constructor.getParameterTypes(); if (matches(paramTypes, params)) { @SuppressWarnings("unchecked") Constructor<T> rval = (Constructor<T>) constructor; return rval; }/*from ww w . ja v a 2 s .com*/ } throw new IllegalStateException("No appropriate constructor found for " + clazz.getCanonicalName()); }
From source file:com.opengamma.util.ReflectionUtils.java
/** * Finds a constructor from a Class.//from w w w. j av a2 s .c om * * @param <T> the type * @param type the type to create, not null * @param arguments the arguments, not null * @return the constructor, not null * @throws RuntimeException if the class cannot be loaded */ @SuppressWarnings("unchecked") public static <T> Constructor<T> findConstructorByArguments(final Class<T> type, final Object... arguments) { Class<?>[] paramTypes = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; i++) { paramTypes[i] = (arguments[i] != null ? arguments[i].getClass() : null); } Constructor<?> matched = null; for (Constructor<?> constructor : type.getConstructors()) { if (org.apache.commons.lang.ClassUtils.isAssignable(paramTypes, constructor.getParameterTypes())) { if (matched == null) { matched = constructor; } else { throw new OpenGammaRuntimeException("Multiple matching constructors: " + type); } } } if (matched == null) { throw new OpenGammaRuntimeException("No matching constructor: " + type); } return (Constructor<T>) matched; }