List of usage examples for java.lang Class getConstructors
@CallerSensitive public Constructor<?>[] getConstructors() throws SecurityException
From source file:org.pentaho.platform.plugin.services.versionchecker.PentahoVersionCheckReflectHelper.java
public static List performVersionCheck(final boolean ignoreExistingUpdates, final int versionRequestFlags) { // check to see if jar is loaded before continuing if (PentahoVersionCheckReflectHelper.isVersionCheckerAvailable()) { try {/*from w w w .jav a 2 s . c o m*/ // use reflection so anyone can delete the version checker jar without pain // PentahoVersionCheckHelper helper = new PentahoVersionCheckHelper(); Class helperClass = Class .forName("org.pentaho.platform.plugin.services.versionchecker.PentahoVersionCheckHelper"); //$NON-NLS-1$ Object helper = helperClass.getConstructors()[0].newInstance(new Object[] {}); // helper.setIgnoreExistingUpdates(ignoreExistingUpdates); Method setIgnoreExistingUpdatesMethod = helperClass.getDeclaredMethod("setIgnoreExistingUpdates", //$NON-NLS-1$ new Class[] { Boolean.TYPE }); setIgnoreExistingUpdatesMethod.invoke(helper, new Object[] { new Boolean(ignoreExistingUpdates) }); // helper.setVersionRequestFlags(versionRequestFlags); Method setVersionRequestFlagsMethod = helperClass.getDeclaredMethod("setVersionRequestFlags", //$NON-NLS-1$ new Class[] { Integer.TYPE }); setVersionRequestFlagsMethod.invoke(helper, new Object[] { new Integer(versionRequestFlags) }); // helper.performUpdate(); Method performUpdateMethod = helperClass.getDeclaredMethod("performUpdate", new Class[] {}); //$NON-NLS-1$ performUpdateMethod.invoke(helper, new Object[] {}); // List results = helper.getResults(); Method getResultsMethod = helperClass.getDeclaredMethod("getResults", new Class[] {}); //$NON-NLS-1$ List results = (List) getResultsMethod.invoke(helper, new Object[] {}); return results; } catch (Exception e) { // ignore errors } } return null; }
From source file:info.novatec.testit.livingdoc.util.ClassUtils.java
@SuppressWarnings("unchecked") public static <T> Constructor<T> findBestTypedConstructor(Class<T> klass, Object... args) throws NoSuchMethodException { for (Constructor<?> constructor : klass.getConstructors()) { if (typesMatch(constructor, args)) { return (Constructor<T>) constructor; }/*from w w w.j a v a 2 s .co m*/ } throw noSuitableConstructorException(klass, args); }
From source file:info.novatec.testit.livingdoc.util.ClassUtils.java
@SuppressWarnings("unchecked") public static <T> Constructor<T> findPossibleConstructor(Class<T> klass, Object... args) throws NoSuchMethodException { for (Constructor<?> constructor : klass.getConstructors()) { if (arityMatches(constructor, args)) { return (Constructor<T>) constructor; }//from ww w . j av a 2 s. c o m } throw noSuitableConstructorException(klass, args); }
From source file:org.apache.drill.common.util.DrillExceptionUtil.java
/** * Get throwable from class name and its constructors, the candidate constructor of exception are: * 1) ExceptionClass(String message, Throwable t), * 2) ExceptionClass(Throwable t, String message), * 3) ExceptionClass(String message).//from www . j a va 2 s . c o m * if no match constructor found, the base Throwable (Exception or Error) is created as a substitution * * @param className the exception class name * @param message the exception message * @param inner the parent cause of the exception * @return Throwable */ private static Throwable getInstance(String className, String message, Throwable inner) throws ReflectiveOperationException { Class clazz; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { return new Exception(message, inner); } Constructor<Throwable>[] constructors = clazz.getConstructors(); Class<?>[] defaultParameterTypes = new Class<?>[] { String.class, Throwable.class }; Class<?>[] revertParameterTypes = new Class<?>[] { Throwable.class, String.class }; Class<?>[] singleParameterType = new Class<?>[] { String.class }; for (Constructor<Throwable> constructor : constructors) { if (Arrays.equals(defaultParameterTypes, constructor.getParameterTypes())) { return constructor.newInstance(message, inner); } if (Arrays.equals(revertParameterTypes, constructor.getParameterTypes())) { return constructor.newInstance(inner, message); } if (inner == null) { if (Arrays.equals(singleParameterType, constructor.getParameterTypes())) { return constructor.newInstance(message); } } } return getBaseInstance(clazz, message, inner); }
From source file:wicket.contrib.groovy.builder.WicketComponentBuilderFactory.java
public static WicketComponentBuilder generateComponentBuilder(String name, MarkupContainer context) throws Exception { WicketComponentBuilder localBuilderClass = checkLocalPackage(name, context); if (localBuilderClass != null) return localBuilderClass; if (nameHelperMapping.get(name) != null) return (WicketComponentBuilder) nameHelperMapping.get(name); Class componentClass = null;// w ww . j a v a 2s.c o m boolean localPackage = false; try { componentClass = checkPackageForComponent(context.getClass().getPackage().getName(), name); } catch (ClassNotFoundException e) { } if (componentClass != null) localPackage = true; else { //TODO: Also need a more robust configuration tree. Based on different levels: //Global > Applicaiton > package > Page //Right now, a local class in the page would override a global on if it were found //first. Again, this was mostly written in a day, so you get what you pay for for (int i = 0; i < searchPackageList.size(); i++) { try { componentClass = checkPackageForComponent((String) searchPackageList.get(i), name); if (componentClass != null) break; } catch (ClassNotFoundException e) { } } if (componentClass == null) return null; // throw new UnsupportedOperationException("Can't find type '"+ name +"'"); } Method localBuilder = null; try { localBuilder = componentClass.getMethod("localBuilder", new Class[0]); } catch (Exception e) { } if (localBuilder != null) { return (WicketComponentBuilder) localBuilder.invoke(null, new Object[0]); } Class wicketNodeHelperClass = (Class) helperTree.getBest(componentClass); WicketComponentBuilder helper = (WicketComponentBuilder) wicketNodeHelperClass.getConstructors()[0] .newInstance(new Object[] { componentClass }); // if(localPackage) // { // putLocalPackage(name, context, helper); // } // else // { // nameHelperMapping.put(name, helper); // } return helper; }
From source file:org.lunarray.model.descriptor.creational.util.CreationalScanUtil.java
/** * Find a no-args public constructor./* w w w . ja v a 2s. co m*/ * * @param source * The source class. May not be null. * @return The constructor, or null. * @param <E> * The entity type. */ @SuppressWarnings("unchecked") public static <E> Constructor<E> findConstructor(final Class<E> source) { Validate.notNull(source, "Source may not be null."); Constructor<E> result = null; if (!Modifier.isAbstract(source.getModifiers()) && !source.isSynthetic()) { for (final Constructor<E> constructor : (Constructor<E>[]) source.getConstructors()) { if (!constructor.isSynthetic() && (constructor.getParameterTypes().length == 0)) { result = constructor; } } } return result; }
From source file:com.asakusafw.testdriver.tools.runner.BatchTestTruncator.java
private static void resolveFlow(Conf conf, Class<? extends FlowDescription> aClass) { LOG.debug("analyzing jobflow: {}", aClass.getName()); //$NON-NLS-1$ Constructor<?>[] ctors = aClass.getConstructors(); if (FlowDescription.isJobFlow(aClass) == false || ctors.length != 1) { throw new IllegalArgumentException( MessageFormat.format(Messages.getString("BatchTestTruncator.errorInvalidJobflowClass"), //$NON-NLS-1$ aClass.getName())); }/*from ww w . j av a2s .c om*/ for (Annotation[] as : ctors[0].getParameterAnnotations()) { for (Annotation a : as) { if (a.annotationType() == Import.class) { resolveImporter(conf, ((Import) a).description()); } else if (a.annotationType() == Export.class) { resolveExporter(conf, ((Export) a).description()); } } } }
From source file:com.tacitknowledge.noexcuses.TestManager.java
/** * The internal instance map will return an object of a particular instance once one is found * in a parameter. For instance (NPI): if your object has a constructor that uses an * interface, you can supply in the instance map an implementation of that interface, * with the interface.class as key. Then when that interface is found the appropriate * object is created for the dummy.// ww w .ja va2 s . com * * This method, in contrast to the {@link TestManager#testConstruction(Class, Map)}, uses * already existing <code>instanceMap</code> entity. * * @param <T> the type of class element to test constructor method on * @param toTest the subject of construction testing, i.e. class to undergo * the process of constructor testing * @return collection of constructed objects */ @SuppressWarnings("unchecked") public static <T> Collection<T> testConstruction(Class<T> toTest) { DummyObjectBuilder dummyObjectBuilder = new DummyObjectBuilder(myInstances, ClassTypeHandlerChain.defaultTypeChain()); Collection<T> instances = new ArrayList<T>(); Constructor<T>[] constructors = (Constructor<T>[]) toTest.getConstructors(); for (Constructor<T> constructor : constructors) { instances.add(dummyObjectBuilder.createInstance(constructor)); } return instances; }
From source file:net.sf.ehcache.config.BeanHandler.java
/** * Creates a child object.// w w w . j a va 2s .c om */ private static Object createInstance(Object parent, Class childClass) throws Exception { final Constructor[] constructors = childClass.getConstructors(); ArrayList candidates = new ArrayList(); for (int i = 0; i < constructors.length; i++) { final Constructor constructor = constructors[i]; final Class[] params = constructor.getParameterTypes(); if (params.length == 0) { candidates.add(constructor); } else if (params.length == 1 && params[0].isInstance(parent)) { candidates.add(constructor); } } switch (candidates.size()) { case 0: throw new Exception("No constructor for class " + childClass.getName()); case 1: break; default: throw new Exception("Multiple constructors for class " + childClass.getName()); } final Constructor constructor = (Constructor) candidates.remove(0); if (constructor.getParameterTypes().length == 0) { return constructor.newInstance(new Object[] {}); } else { return constructor.newInstance(new Object[] { parent }); } }
From source file:jp.primecloud.auto.tool.management.main.SQLMain.java
@SuppressWarnings("unchecked") public static <T, V> List<T> selectExecuteWithResult(String sql, Class<T> clazz) throws Exception { SQLExecuter sqlExecuter = new SQLExecuterFactory().createPCCSQLExecuter(); List<Map<String, Object>> selectResults = sqlExecuter.showColumns(sql); List<T> results = new ArrayList<T>(); Constructor<?>[] constructors = clazz.getConstructors(); for (int i = 0; i < selectResults.size(); i++) { Map<String, Object> rowMap = selectResults.get(i); T newEntity = (T) constructors[0].newInstance(); BeanUtils.populate(newEntity, rowMap); results.add(newEntity);/* w ww. j a v a 2s.c om*/ } return results; }