List of usage examples for java.lang Class getConstructor
@CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:com.opengamma.util.rest.ExceptionThrowingClientFilter.java
@Override public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException { ClientResponse response = getNext().handle(cr); if (response.getStatus() < 300) { return response; // normal valid response }//w w w. j a va2 s .c o m MultivaluedMap<String, String> headers = response.getHeaders(); String exType = headers.getFirst(EXCEPTION_TYPE); String exMsg = headers.getFirst(EXCEPTION_MESSAGE); if (exMsg == null) { exMsg = headers.getFirst(EXCEPTION_POINT); } UniformInterfaceException uiex; if (response.getStatus() == 404) { uiex = new UniformInterfaceException404NotFound(response, true); } else if (response.getStatus() == 204) { uiex = new UniformInterfaceException204NoContent(response, true); } else { uiex = new UniformInterfaceException(response, true); } if (exType == null) { throw uiex; // standard UniformInterfaceException as we have nothing to add } RuntimeException exception; try { Class<? extends RuntimeException> cls = Thread.currentThread().getContextClassLoader().loadClass(exType) .asSubclass(RuntimeException.class); exception = cls.getConstructor(String.class) .newInstance("Server threw exception: " + StringUtils.defaultString(exMsg)); } catch (Exception ex) { // unable to create transparently, so use standard exception exception = new OpenGammaRuntimeException( "Server threw exception: " + exType + ": " + StringUtils.defaultString(exMsg)); } exception.initCause(uiex); throw exception; // transparently throw exception as seen on the server }
From source file:mojo.view.rest.DataController.java
@ResponseBody @RequestMapping(value = "/select-view", method = RequestMethod.GET) public DataPage<?> doFetchView(@RequestBody Select<E> spec, @RequestParam String view) { try {/*from w w w .ja v a 2 s. c om*/ DataPage<E> page = service.select(spec); List<Object> views = new ArrayList<Object>(page.getData().size()); Class<?> viewClass = Class.forName(view); for (E entity : page.getData()) { Class<?> entityClass = entity.getClass(); Constructor<?> viewConstructor = viewClass.getConstructor(entityClass); Object obj = viewConstructor.newInstance(entity); views.add(obj); } DataPage<Object> result = new DataPage<Object>(); result.setTotal(page.getTotal()); result.setData(views); return result; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:jgraspcheckstyle.indentation.HandlerFactory.java
/** * registers a handler//from w w w. j a v a 2 s . c om * * @param aType type from TokenTypes * @param aHandlerClass the handler to register */ private void register(int aType, Class aHandlerClass) { try { final Constructor ctor = aHandlerClass .getConstructor(new Class[] { IndentationCheck.class, DetailAST.class, // current AST ExpressionHandler.class, // parent }); mTypeHandlers.put(new Integer(aType), ctor); } ///CLOVER:OFF catch (NoSuchMethodException e) { throw new RuntimeException("couldn't find ctor for " + aHandlerClass); } catch (SecurityException e) { LOG.debug("couldn't find ctor for " + aHandlerClass, e); throw new RuntimeException("couldn't find ctor for " + aHandlerClass); } ///CLOVER:ON }
From source file:iddb.core.model.dao.DAOFactory.java
/** * @param key/* w w w . j av a 2s . com*/ * @param value * @return * @throws Exception * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ @SuppressWarnings({ "rawtypes", "unchecked" }) private Object createCachedInstance(String iface, Object impl) throws Exception { String[] ifacePart = StringUtils.split(iface, "."); String ifaceName = ifacePart[ifacePart.length - 1]; log.debug("Getting cached instance for {} - {}", iface, ifaceName); ClassLoader loader = this.getClass().getClassLoader(); try { Class clz = loader.loadClass("iddb.core.model.dao.cached." + ifaceName + "Cached"); Constructor cons = clz.getConstructor(new Class[] { Class.forName(iface) }); return cons.newInstance(impl); } catch (SecurityException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (IllegalArgumentException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (ClassNotFoundException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (NoSuchMethodException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (InstantiationException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (IllegalAccessException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (InvocationTargetException e) { e.printStackTrace(); log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } }
From source file:net.sf.nmedit.jtheme.JTContext.java
public <T extends JTComponent> T createComponentInstance(Class<T> clazz) throws JTException { T component;/*from w w w . jav a 2 s. c o m*/ try { component = clazz.getConstructor(new Class<?>[] { JTContext.class }).newInstance(new Object[] { this }); UIDefaults defaults = getUIDefaults(); if (hasUIClass(component, defaults)) { component.setUI(defaults.getUI(component)); } } catch (Throwable t) { JTException e = new JTException("could not create instance of " + clazz); e.initCause(t); throw e; } return component; }
From source file:org.dawnsci.persistence.json.function.FunctionBean.java
/** * Method that converts a function bean to an IFunction using reflection * // w w w . j a v a2 s. co m * @return IFunction * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ @JsonIgnore public IFunction getIFunction() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { IFunction function = null; IParameter[] params = getParameters(); Class<?> clazz = Class.forName(getType()); // If a Jexl expression if (clazz.equals(JexlExpressionFunction.class)) { Constructor<?> constructor = clazz.getConstructor(String.class); function = (IFunction) constructor.newInstance((String) getName()); for (int i = 0; i < params.length; i++) { ((JexlExpressionFunction) function).setParameter(i, params[i]); } } else { // For all other cases try to return an instance of IFunction with parameters Constructor<?> constructor = clazz.getConstructor(IParameter[].class); function = (IFunction) constructor.newInstance((Object) params); } return function; }
From source file:me.timothy.ddd.quests.QuestManager.java
public void convoEnded(Entity entity, String questName, int[] choiceTree) { try {/*from ww w.ja v a 2 s . com*/ for (Quest quest : acceptedQuests) { if (quest.getClass().getName().equals(questName)) { quest.onContinued(entity, choiceTree); return; } } Class<?> hopefullyTheQuestsClass = Class.forName(questName); Constructor<?> constr = hopefullyTheQuestsClass.getConstructor(QuestManager.class); Quest quest = (Quest) constr.newInstance(this); acceptedQuests.add(quest); quest.onApplied(entity, choiceTree); } catch (Exception e) { logger.printf(Level.WARN, "convo ended with an invalid quest %s", questName); logger.catching(e); } }
From source file:br.com.binarti.simplesearchexpr.converter.NumberSearchDataConverter.java
@SuppressWarnings("rawtypes") @Override/* w ww . ja v a 2 s. c om*/ protected Object asSingleObject(SimpleSearchExpressionField field, String value, Map<String, Object> params, Class<?> targetType) { if (value == null) return null; if (targetType.isPrimitive()) { targetType = ClassUtils.primitiveToWrapper(targetType); } try { Constructor constructor = targetType.getConstructor(String.class); return constructor.newInstance(value.trim()); } catch (Exception e) { throw new SimpleSearchDataConverterException( "Error converter " + value + " to " + targetType.getClass()); } }
From source file:com.glaf.core.util.ReflectUtils.java
public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException { Constructor<?> targetConstructor; try {//from ww w .java 2 s. c om 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.jaqpot.core.elastic.ElasticIndexer.java
public String index(ObjectMapper mapper) { try {// ww w.jav a 2s .c o m JaqpotEntity strippedEntity = entity; Class<? extends JaqpotEntity> entityClass = entity.getClass(); String entityName = entityClass.getAnnotation(XmlRootElement.class).name(); if ("##default".equals(entityName)) { entityName = entityClass.getSimpleName().toLowerCase(); } Class<? extends AbstractMetaStripper> stripperClass = STRIP_CLUB.get(entityClass); if (stripperClass != null) { try { AbstractMetaStripper stripper = stripperClass.getConstructor(entityClass).newInstance(entity); strippedEntity = stripper.strip(); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(ElasticIndexer.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException("Impossible! A MetaStripper doesn't have a necessary constructor!"); } } String jsonString = mapper.writeValueAsString(strippedEntity); IndexResponse response = ElasticClient.getClient().prepareIndex("jaqpot", entityName, entity.getId()) .setSource(jsonString).execute().actionGet(); return response.getId(); } catch (JsonProcessingException ex) { Logger.getLogger(ElasticIndexer.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException("Incredible! JAKSON Couldn't serialize an entity!"); } }