List of usage examples for java.lang Class getConstructor
@CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:Browser.java
/** * Called by a static initializer to load any classes, fields, and methods * required at runtime to locate the user's web browser. * @return <code>true</code> if all intialization succeeded * <code>false</code> if any portion of the initialization failed *//*w w w . ja v a 2 s. c o m*/ private static boolean loadClasses() { switch (jvm) { case MRJ_2_0: try { Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget"); Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils"); Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent"); Class aeClass = Class.forName("com.apple.MacOS.ae"); aeDescClass = Class.forName("com.apple.MacOS.AEDesc"); aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[] { int.class }); appleEventConstructor = appleEventClass.getDeclaredConstructor( new Class[] { int.class, int.class, aeTargetClass, int.class, int.class }); aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class }); makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class[] { String.class }); putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass }); sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] {}); Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject"); keyDirectObject = (Integer) keyDirectObjectField.get(null); Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID"); kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null); Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID"); kAnyTransactionID = (Integer) anyTransactionIDField.get(null); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_2_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType"); Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType"); kSystemFolderType = systemFolderField.get(null); findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass }); getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class }); getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (SecurityException se) { errorMessage = se.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_0: try { Class linker = Class.forName("com.apple.mrj.jdirect.Linker"); Constructor constructor = linker.getConstructor(new Class[] { Class.class }); linkage = constructor.newInstance(new Object[] { Browser.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (InvocationTargetException ite) { errorMessage = ite.getMessage(); return false; } catch (InstantiationException ie) { errorMessage = ie.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } break; default: break; } return true; }
From source file:fxts.stations.util.BrowserLauncher.java
/** * Called by a static initializer to load any classes, fields, and methods required at runtime * to locate the user's web browser./*from ww w. ja v a 2 s .c om*/ * * @return <code>true</code> if all intialization succeeded * <code>false</code> if any portion of the initialization failed */ private static boolean loadClasses() { switch (jvm) { case MRJ_2_0: try { Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget"); Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils"); Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent"); Class aeClass = Class.forName("com.apple.MacOS.ae"); aeDescClass = Class.forName("com.apple.MacOS.AEDesc"); aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[] { int.class }); appleEventConstructor = appleEventClass.getDeclaredConstructor( new Class[] { int.class, int.class, aeTargetClass, int.class, int.class }); aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class }); makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class[] { String.class }); putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass }); sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] {}); Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject"); keyDirectObject = (Integer) keyDirectObjectField.get(null); Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID"); kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null); Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID"); kAnyTransactionID = (Integer) anyTransactionIDField.get(null); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_2_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType"); Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType"); kSystemFolderType = systemFolderField.get(null); findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass }); getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class }); getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (SecurityException se) { errorMessage = se.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_0: try { Class linker = Class.forName("com.apple.mrj.jdirect.Linker"); Constructor constructor = linker.getConstructor(new Class[] { Class.class }); linkage = constructor.newInstance(new Object[] { BrowserLauncher.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (InvocationTargetException ite) { errorMessage = ite.getMessage(); return false; } catch (InstantiationException ie) { errorMessage = ie.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } break; default: break; } return true; }
From source file:eu.stratosphere.nephele.io.compression.CompressionLoader.java
@SuppressWarnings("unchecked") private static CompressionLibrary initCompressionLibrary(String libraryClass) { Class<? extends CompressionLibrary> compressionLibraryClass; try {//from w w w .j av a 2s . c o m compressionLibraryClass = (Class<? extends CompressionLibrary>) Class.forName(libraryClass); } catch (ClassNotFoundException e1) { LOG.error(e1); return null; } if (compressionLibraryClass == null) { LOG.error("Cannot load compression library " + libraryClass); return null; } Constructor<? extends CompressionLibrary> constructor; try { constructor = compressionLibraryClass.getConstructor(String.class); } catch (SecurityException e) { LOG.error(e); return null; } catch (NoSuchMethodException e) { LOG.error(e); return null; } if (constructor == null) { LOG.error("Cannot find matching constructor for class " + compressionLibraryClass.toString()); return null; } CompressionLibrary compressionLibrary; try { compressionLibrary = constructor.newInstance(getNativeLibraryPath(libraryClass)); } catch (IllegalArgumentException e) { LOG.error(StringUtils.stringifyException(e)); return null; } catch (InstantiationException e) { LOG.error(StringUtils.stringifyException(e)); return null; } catch (IllegalAccessException e) { LOG.error(StringUtils.stringifyException(e)); return null; } catch (InvocationTargetException e) { LOG.error(StringUtils.stringifyException(e)); return null; } return compressionLibrary; }
From source file:cascading.flow.hadoop.util.HadoopUtil.java
protected static <C extends Configuration> C callCopyConstructor(Class type, Configuration parent) { try {//from www . j a v a 2s . c o m Constructor<C> constructor = type.getConstructor(parent.getClass()); return constructor.newInstance(parent); } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException exception) { throw new CascadingException("unable to create copy of: " + type); } }
From source file:com.microsoft.tfs.core.internal.wrappers.WrapperUtils.java
/** * <p>/*w ww .j a va2 s. co m*/ * Takes an array of web service objects (for example, an array of * {@link _Item}) and returns an array of web service wrapper objects of the * given type (for instance, {@link Item}). * </p> * <p> * A constructor for the given wrapper type that accepts one of the given * service objects must exist. * </p> * <p> * <code>null</code> values in the web service objects are copied into the * returned array. * </p> * * @param wrapperType * the wrapper object class name (not array class) to use (must not * be <code>null</code>) * @param webServiceObjects * the objects to wrap (if null, null is returned) * @return a new array of wrapper objects, where each wraps one of the given * web service objects */ public static Object wrap(final Class wrapperType, final Object[] webServiceObjects) { Check.notNull(wrapperType, "wrapperType"); //$NON-NLS-1$ if (webServiceObjects == null) { return null; } final Object ret = Array.newInstance(wrapperType, webServiceObjects.length); Class webServiceObjectType = null; Constructor constructor = null; if (webServiceObjects.length > 0) { try { for (int i = 0; i < webServiceObjects.length; i++) { if (constructor == null && webServiceObjects[i] != null) { webServiceObjectType = webServiceObjects[i].getClass(); constructor = wrapperType.getConstructor(new Class[] { webServiceObjectType }); } /* * Persist null values. */ Array.set(ret, i, (webServiceObjects[i] != null) ? constructor.newInstance(new Object[] { webServiceObjects[i] }) : null); } } catch (final NoSuchMethodException e) { final String message = MessageFormat.format( "Wrapper error: the desired wrapper class {0} does not have a visible constructor that accepts the web service type {1}", //$NON-NLS-1$ wrapperType, webServiceObjectType); log.error(message, e); throw new RuntimeException(message); } catch (final Exception e) { final String message = MessageFormat.format("Error wrapping {0} in {1}", //$NON-NLS-1$ webServiceObjectType, wrapperType); log.error(message, e); throw new RuntimeException(message, e); } } return ret; }
From source file:com.all.shared.json.JsonConverter.java
public static <T extends Collection<V>, V> T toTypedCollection(String json, Class<T> collectionType, Class<V> contentType) { T collection = null;/*from ww w . j a va 2 s .com*/ JSONArray jsonCollection = JSONArray.fromObject(json); try { collection = collectionType.newInstance(); Iterator<?> iterator = jsonCollection.iterator(); while (iterator.hasNext()) { try { if (PRIMITIVES.contains(contentType)) { collection.add( contentType.getConstructor(String.class).newInstance(iterator.next().toString())); } else { collection.add(toBean(iterator.next().toString(), contentType)); } } catch (Exception e) { log.warn("Could not read a " + contentType.getSimpleName() + " from a json array.", e); } } } catch (Exception e) { return toCollection(json, collectionType); } return collection; }
From source file:net.sf.morph.util.TestUtils.java
/** * Return a "not-same" instance.// www .ja v a2 s. c o m * @param type * @param o * @return */ public static Object getDifferentInstance(Class type, Object o) { if (type == null) { throw new IllegalArgumentException("Non-null type must be specified"); } if (type.isPrimitive()) { type = ClassUtils.getPrimitiveWrapper(type); } if (o != null && !type.isInstance(o)) { throw new IllegalArgumentException("Negative example object should be of type " + type); } if (type == Number.class) { type = Integer.class; } if (Number.class.isAssignableFrom(type)) { byte b = (byte) (o == null ? 0 : ((Number) o).byteValue() + 1); try { return type.getConstructor(ONE_STRING).newInstance(new Object[] { Byte.toString(b) }); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new NestableRuntimeException(e); } } if (type == Character.class) { char c = (char) (o == null ? 0 : ((Character) o).charValue() + 1); return new Character(c); } if (type == Boolean.class) { return o != null && ((Boolean) o).booleanValue() ? Boolean.FALSE : Boolean.TRUE; } if (type.isArray()) { return Array.newInstance(type.getComponentType(), 0); } if (type == Class.class) { return o == Object.class ? Class.class : Object.class; } return ClassUtils.newInstance(convertCommonInterfaces(type)); }
From source file: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.//from w ww .ja va 2s. 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.googlecode.android_scripting.rpc.MethodDescriptor.java
@SuppressWarnings("rawtypes") private static Converter<?> converterFor(Type parameterType, Class<? extends Converter> converterClass) { if (converterClass == Converter.class) { Converter<?> converter = sConverters.get(parameterType); if (converter == null) { throw new IllegalArgumentException("No predefined converter found for " + parameterType); }//from w w w. j av a 2 s . c o m return converter; } try { Constructor<?> constructor = converterClass.getConstructor(new Class<?>[0]); return (Converter<?>) constructor.newInstance(new Object[0]); } catch (Exception e) { throw new IllegalArgumentException("Cannot create converter from " + converterClass.getCanonicalName()); } }
From source file:com.buaa.cfs.utils.NetUtils.java
@SuppressWarnings("unchecked") private static <T extends IOException> T wrapWithMessage(T exception, String msg) { Class<? extends Throwable> clazz = exception.getClass(); try {// w w w. ja va 2 s . c o m Constructor<? extends Throwable> ctor = clazz.getConstructor(String.class); Throwable t = ctor.newInstance(msg); return (T) (t.initCause(exception)); } catch (Throwable e) { LOG.warn("Unable to wrap exception of type " + clazz + ": it has no (String) constructor", e); return exception; } }