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.n52.server.util.SosAdapterFactory.java
/** * Creates an adapter to make requests to an SOS instance. If the given metadata does not contain a full qualified * class name defining the adapter implementation the default one is returned. * * @param metadata the SOS metadata where to create the SOS adapter implementation from. * @return the custom adapter implementation, or the default {@link SOSAdapter}. *///from w w w . j av a 2 s. co m public static SOSAdapter createSosAdapter(SOSMetadata metadata) { String adapter = metadata.getAdapter(); String sosVersion = metadata.getSosVersion(); try { SOSAdapter sosAdapter = new SOSAdapter(sosVersion); sosAdapter.setHttpClient(createHttpClient(metadata)); if (adapter == null) { return sosAdapter; } else { if (!SOSAdapter.class.isAssignableFrom(Class.forName(adapter))) { LOGGER.warn("'{}' is not an SOSAdapter implementation! Create default.", adapter); return sosAdapter; } @SuppressWarnings("unchecked") // unassignable case handled already Class<SOSAdapter> clazz = (Class<SOSAdapter>) Class.forName(adapter); Class<?>[] arguments = new Class<?>[] { String.class }; Constructor<SOSAdapter> constructor = clazz.getConstructor(arguments); sosAdapter = constructor.newInstance(sosVersion); sosAdapter.setHttpClient(createHttpClient(metadata)); return sosAdapter; } } catch (ClassNotFoundException e) { throw new RuntimeException("Could not find Adapter class '" + adapter + "'.", e); } catch (NoSuchMethodException e) { throw new RuntimeException("Invalid Adapter constructor for '" + adapter + "'.", e); } catch (InstantiationException e) { throw new RuntimeException("Could not create Adapter for '" + adapter + "'.", e); } catch (IllegalAccessException e) { throw new RuntimeException("Not allowed to create Adapter for '" + adapter + "'.", e); } catch (InvocationTargetException e) { throw new RuntimeException("Instantiation failed for Adapter " + adapter + "'.", e); } }
From source file:io.cloudex.framework.cloud.api.ApiUtils.java
/** * Return an IOException from the metaData error * @param metaData//from w w w .ja v a 2s.c om * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static IOException exceptionFromCloudExError(VmMetaData metaData, String instanceId) { Class clazz = IOException.class; String message = metaData.getMessage(); if (StringUtils.isNoneBlank(metaData.getException())) { try { clazz = Class.forName(metaData.getException()); } catch (ClassNotFoundException e) { log.warn("failed to load exception class from evm"); } } Exception cause; try { Constructor ctor = clazz.getDeclaredConstructor(String.class); ctor.setAccessible(true); cause = (Exception) ctor.newInstance(message); } catch (Exception e) { log.warn("failed to load exception class from evm"); cause = new IOException(message); } return new ProcessorException(PROCESSOR_EXCEPTION + instanceId, cause, instanceId); }
From source file:libepg.common.SectionBodyMaker.java
public static final SectionBody init(TABLE_ID tableID, byte[] data) throws InvocationTargetException { try {/*from w w w. ja va 2s . com*/ Object[] args = { tableID, data }; Class<?>[] params = { TABLE_ID.class, byte[].class }; Constructor<SectionBody> constructor = SectionBody.class.getDeclaredConstructor(params); constructor.setAccessible(true); SectionBody target = constructor.newInstance(args); return target; } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException | SecurityException ex) { LOG.fatal(ex); } return null; }
From source file:ExceptionUtils.java
/** * Create a new Exception, setting the cause if possible. * @param clazz/*from w w w . j av a 2s . co m*/ * @param message * @param cause * @return A Throwable. */ public static Throwable createWithCause(Class clazz, String message, Throwable cause) { Throwable re = null; if (causesAllowed) { try { Constructor constructor = clazz.getConstructor(new Class[] { String.class, Throwable.class }); re = (Throwable) constructor.newInstance(new Object[] { message, cause }); } catch (RuntimeException e) { throw e; } catch (Exception e) { causesAllowed = false; } } if (re == null) { try { Constructor constructor = clazz.getConstructor(new Class[] { String.class }); re = (Throwable) constructor.newInstance(new Object[] { message + " caused by " + cause }); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException("Error caused " + e); // should be impossible } } return re; }
From source file:com.espertech.esper.util.ConstructorHelper.java
/** * Find and invoke constructor matching the argument number and types returning an instance * of given class.// ww w .j a v a2 s. com * @param clazz is the class of instance to construct * @param arguments is the arguments for the constructor to match in number and type * @return instance of class * @throws IllegalAccessException thrown if no access to class * @throws NoSuchMethodException thrown when the constructor is not found * @throws InvocationTargetException thrown when the ctor throws and exception * @throws InstantiationException thrown when the class cannot be loaded */ public static Object invokeConstructor(Class clazz, Object[] arguments) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException { Class parameterTypes[] = new Class[arguments.length]; for (int i = 0; i < arguments.length; i++) { parameterTypes[i] = arguments[i].getClass(); } // Find a constructor that matches exactly Constructor ctor = getRegularConstructor(clazz, parameterTypes); if (ctor != null) { return ctor.newInstance(arguments); } // Find a constructor with the same number of assignable parameters (such as int -> Integer). ctor = findMatchingConstructor(clazz, parameterTypes); if (ctor != null) { return ctor.newInstance(arguments); } // Find an Object[] constructor, which always matches (throws an exception if not found) ctor = getObjectArrayConstructor(clazz); return ctor.newInstance(new Object[] { arguments }); }
From source file:com.doculibre.constellio.utils.connector.ConnectorPropertyInheritanceResolver.java
@SuppressWarnings("unchecked") public static <T extends Object> T newInheritedClassPropertyInstance(ConnectorInstance connectorInstance, String propertyName, Class[] paramTypes, Object[] args) { T inheritedClassPropertyInstance;//from ww w . jav a2 s . c o m String propertyValue = getStringPropertyValue(connectorInstance, propertyName); if (StringUtils.isEmpty(propertyValue)) { propertyValue = getStringPropertyValue(connectorInstance.getConnectorType(), propertyName); } if (StringUtils.isNotEmpty(propertyValue)) { PluginAwareClassLoader pluginAwareClassLoader = new PluginAwareClassLoader(); try { Thread.currentThread().setContextClassLoader(pluginAwareClassLoader); Class<T> propertyValueClass = (Class<T>) Class.forName(propertyValue); Constructor<T> constructor = propertyValueClass.getConstructor(paramTypes); inheritedClassPropertyInstance = constructor.newInstance(args); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } finally { Thread.currentThread().setContextClassLoader(pluginAwareClassLoader.getDefaultClassLoader()); } } else { inheritedClassPropertyInstance = null; } return inheritedClassPropertyInstance; }
From source file:com.bluecloud.ioc.classloader.ClassHandler.java
/** * <h3></h3> ?Class Instance * //from ww w . j av a2 s .co m * @param className * ?? * @param index * Class?constructorIndex?0Class? * 0 * @param initargs * ?? * @return classNameClassObject * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws NoSuchMethodException * @throws SecurityException */ public static Object createObject(String className, int index, Object[] initargs) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException { Class<?> clazz = (Class<?>) getClass(className, false); if (index < 0 || index > clazz.getConstructors().length - 1) { index = 0; } Constructor<?> constructor = clazz.getConstructors()[index]; return constructor.newInstance(initargs); }
From source file:Main.java
private static int getExifOrientation(String src) throws IOException { int orientation = 1; try {// ww w .j ava 2 s .c om /** * if your are targeting only api level >= 5 * ExifInterface exif = new ExifInterface(src); * orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); */ if (Build.VERSION.SDK_INT >= 5) { Class<?> exifClass = Class.forName("android.media.ExifInterface"); Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class }); Object exifInstance = exifConstructor.newInstance(new Object[] { src }); Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[] { String.class, int.class }); Field tagOrientationField = exifClass.getField("TAG_ORIENTATION"); String tagOrientation = (String) tagOrientationField.get(null); orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1 }); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } 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(); } catch (NoSuchFieldException e) { e.printStackTrace(); } return orientation; }
From source file:com.github.fge.jsonschema.processors.build.ValidatorBuilder.java
private static KeywordValidator buildKeyword(final Constructor<? extends KeywordValidator> constructor, final JsonNode node) throws ProcessingException { try {//from w ww. j a va 2s . c o m return constructor.newInstance(node); } catch (InstantiationException e) { throw new ProcessingException(ERRMSG, e); } catch (IllegalAccessException e) { throw new ProcessingException(ERRMSG, e); } catch (InvocationTargetException e) { throw new ProcessingException(ERRMSG, e); } }
From source file:Main.java
public static Object newInstance(String paramString, Object[] paramArrayOfObject) { try {//from ww w. j ava2 s . c o m Constructor localConstructor = Class.forName(paramString) .getDeclaredConstructor(getArgsClasses(paramArrayOfObject)); localConstructor.setAccessible(true); return localConstructor.newInstance(paramArrayOfObject); } catch (Exception var3) { return null; } }