List of usage examples for java.lang.reflect Constructor newInstance
@CallerSensitive @ForceInline public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
From source file:org.bremersee.common.spring.autoconfigure.ObjectMapperAutoConfiguration.java
private static AnnotationIntrospector findAnnotationIntrospectorPair() { try {//from www.j a va 2 s . c o m LOG.info("Trying to add a pair of annotation introspectors ('" + JacksonAnnotationIntrospector.class.getSimpleName() + " + " + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ")."); @SuppressWarnings("unchecked") Class<? extends AnnotationIntrospector> cls = (Class<? extends AnnotationIntrospector>) Class .forName(JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME); Constructor<? extends AnnotationIntrospector> constructor = cls.getConstructor(TypeFactory.class); AnnotationIntrospector secondary = constructor.newInstance(TypeFactory.defaultInstance()); // see http://wiki.fasterxml.com/JacksonJAXBAnnotations AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); AnnotationIntrospectorPair pair = new AnnotationIntrospectorPair(primary, secondary); LOG.info("The pair of annotation introspectors ('" // NOSONAR + JacksonAnnotationIntrospector.class.getSimpleName() + " + " + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ") was successfully added."); return pair; } catch (ClassNotFoundException e) { // NOSONAR LOG.warn("The pair of annotation introspectors ('" + JacksonAnnotationIntrospector.class.getSimpleName() + " " + "+ " + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ") wasn't added: " + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + " was not found."); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { LOG.warn("The pair of annotation introspectors ('" + JacksonAnnotationIntrospector.class.getSimpleName() + " " + "+ " + JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME + ") wasn't added.", e); } return null; }
From source file:com.oltpbenchmark.util.ClassUtil.java
public static <T> T newInstance(Class<T> target_class, Object params[], Class<?> classes[]) { // Class<?> const_params[] = new Class<?>[params.length]; // for (int i = 0; i < params.length; i++) { // const_params[i] = params[i].getClass(); // System.err.println("[" + i + "] " + params[i] + " " + params[i].getClass()); // } // FOR Constructor<T> constructor = ClassUtil.getConstructor(target_class, classes); T ret = null;/*from w ww . ja v a 2 s . com*/ try { ret = constructor.newInstance(params); } catch (Exception ex) { throw new RuntimeException("Failed to create new instance of " + target_class.getSimpleName(), ex); } return (ret); }
From source file:ezbake.thrift.ThriftUtils.java
@SuppressWarnings("unchecked") public static <Y extends TServiceClient> Y getClient(Class<Y> clazz, HostAndPort hostAndPort, Properties properties) throws NoSuchMethodException, TException, Exception { final Constructor<?> constructor = clazz.getConstructor(TProtocol.class); final Object ds = constructor.newInstance(getProtocol(hostAndPort, properties)); return (Y) ds; }
From source file:ezbake.thrift.ThriftUtils.java
@SuppressWarnings("unchecked") public static <Y extends TServiceClient> Y getClient(Class<Y> clazz, HostAndPort hostAndPort, String securityId, Properties properties) throws NoSuchMethodException, TException, Exception { final Constructor<?> constructor = clazz.getConstructor(TProtocol.class); final Object ds = constructor.newInstance(getProtocol(hostAndPort, securityId, properties)); return (Y) ds; }
From source file:ezbake.thrift.ThriftUtils.java
@SuppressWarnings("unchecked") public static <Y extends TServiceClient> Y getClient(Class<Y> clazz, HostAndPort hostAndPort, String securityId, Properties properties, TTransportFactory transportFactory) throws NoSuchMethodException, TException, Exception { final Constructor<?> constructor = clazz.getConstructor(TProtocol.class); final Object ds = constructor .newInstance(getProtocol(hostAndPort, securityId, properties, transportFactory)); return (Y) ds; }
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 w w. j av 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:com.expressui.core.util.ReflectionUtil.java
/** * Converts object value to given type. Converts primitives to their wrappers. * Converts strings to numbers./* ww w .jav a2s . c o m*/ * * @param value value to convert * @param type type to convert to * @param <T> type * @return converted value * @throws InvocationTargetException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException */ public static <T> T convertValue(Object value, Class<T> type) throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException { Class clazz; if (type.isPrimitive()) { clazz = ClassUtils.primitiveToWrapper(type); } else { clazz = type; } if (null == value || clazz.isAssignableFrom(value.getClass())) { return (T) value; } Constructor<T> constructor = clazz.getConstructor(new Class[] { String.class }); return constructor.newInstance(value.toString()); }
From source file:com.alibaba.wasp.client.ServerCallable.java
private static RuntimeException unwrapRuntimeException(Throwable t) { if (StringUtils.isNotEmpty(t.getMessage())) { try {//from w w w . j a va 2s . com Class exceptionClass = Class.forName(t.getMessage()); Constructor cn = exceptionClass.getConstructor(String.class); cn.setAccessible(true); String firstLine = t.getMessage(); Object ex = cn.newInstance(firstLine); if (ex instanceof RuntimeException) { return (RuntimeException) ex; } } catch (ClassNotFoundException e) { //ignore } catch (NoSuchMethodException e) { //ignore } catch (InvocationTargetException e) { //ignore } catch (InstantiationException e) { //ignore } catch (IllegalAccessException e) { //ignore } } return null; }
From source file:com.codelanx.codelanxlib.util.exception.Exceptions.java
/** * Dynamically constructs a new instance of a {@link RuntimeException} * either via a {@link RuntimeException#RuntimeException(String)} * constructor or a no-argument constructor * * @since 0.1.0// www .j a v a2 s. c o m * @version 0.1.0 * * @param <T> The {@link RuntimeException} type * @param ex The exception class to instantiate * @param message The message to add, {@code null} if there is no message * @return The newly constructed {@link RuntimeException} */ private static <T extends RuntimeException> T newException(Class<T> ex, String message) { if (message != null) { try { Constructor<T> c = ex.getConstructor(String.class); c.setAccessible(true); return c.newInstance(message); } catch (NoSuchMethodException e) { Debugger.print(Level.WARNING, String.format( "Class '%s' does not have a String " + "message constructor! Using default constructor...", ex.getName())); } catch (SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { Debugger.error(e, "Error creating new exception instance"); } } //Now try here if the previous failed or message wasn't null try { return ex.newInstance(); } catch (InstantiationException | IllegalAccessException e) { Debugger.error(e, "Error creating new exception instance"); } throw new IllegalArgumentException(String.format( "Class '%s' does not have the " + "appropriate constructors to be instantiated", ex.getName())); }
From source file:at.jku.rdfstats.hist.builder.HistogramBuilderFactory.java
private static HistogramBuilder<?> newInstance(Class<? extends HistogramBuilder<?>> clazz, String typeUri, int preferredSize, RDFStatsConfiguration conf) throws HistogramBuilderException { if (typeUri == null) throw new HistogramBuilderException("Type URI cannot be null."); try {//from w w w .j av a 2s .c om Constructor<? extends HistogramBuilder<?>> constr = clazz .getConstructor(new Class[] { RDFStatsConfiguration.class, String.class, int.class }); HistogramBuilder<?> builder = constr.newInstance(new Object[] { conf, typeUri, preferredSize }); return builder; } catch (NoSuchMethodException e) { throw new HistogramBuilderException("Couldn't create histogram builder (datatype URI: " + typeUri + "), implementation '" + clazz.getCanonicalName() + "' is invalid.", e); } catch (Exception e) { throw new HistogramBuilderException("Couldn't create histogram builder (datatype URI: " + typeUri + "), please call HistogramBuilderFactory.supports() first to check if a type is supported.", e); } }