List of usage examples for java.lang.reflect Constructor getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessDataGetterN3Utils.java
private static ProcessDataGetterN3 instantiateClass(String processorClass, JSONObject jsonObject) { ProcessDataGetterN3 pn = null;//from ww w . j av a2 s.co m try { Class<?> clz = Class.forName(processorClass); Constructor<?>[] ctList = clz.getConstructors(); for (Constructor<?> ct : ctList) { Class<?>[] parameterTypes = ct.getParameterTypes(); if (parameterTypes.length > 0 && parameterTypes[0].isAssignableFrom(jsonObject.getClass())) { pn = (ProcessDataGetterN3) ct.newInstance(jsonObject); } else { pn = (ProcessDataGetterN3) ct.newInstance(); } } } catch (Exception ex) { log.error("Error occurred instantiating " + processorClass, ex); } return pn; }
From source file:Main.java
private static final Constructor findCtor(Class<?> clazz, List<Class<?>> actualArgs) { for (final Constructor m : clazz.getConstructors()) { if (callableWith(m.getParameterTypes(), actualArgs)) { return m; }//from w ww. java2 s . c o m } return null; }
From source file:Main.java
public static String parseParameterTypes(Constructor<?> constructor) { String parameterTypes = ""; for (Class<?> parameterClass : constructor.getParameterTypes()) parameterTypes += parseClassType(parameterClass); return parameterTypes; }
From source file:Main.java
private static void loadProviderIfNecessary(String providerClassName) { if (providerClassName == null) { return;//from w w w .j a v a 2s. co m } final Class<?> klass; try { final ClassLoader sysLoader = ClassLoader.getSystemClassLoader(); if (sysLoader != null) { klass = sysLoader.loadClass(providerClassName); } else { klass = Class.forName(providerClassName); } } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); return; } Constructor<?> constructor = null; for (Constructor<?> c : klass.getConstructors()) { if (c.getParameterTypes().length == 0) { constructor = c; break; } } if (constructor == null) { System.err.println("No zero-arg constructor found for " + providerClassName); System.exit(1); return; } final Object o; try { o = constructor.newInstance(); } catch (Exception e) { e.printStackTrace(); System.exit(1); return; } if (!(o instanceof Provider)) { System.err.println("Not a Provider class: " + providerClassName); System.exit(1); } Security.insertProviderAt((Provider) o, 1); }
From source file:ShowClass.java
/** * Print the modifiers, return type, name, parameter types and exception type * of a method or constructor. Note the use of the Member interface to allow * this method to work with both Method and Constructor objects *//* ww w . j a va 2 s . c om*/ public static void print_method_or_constructor(Member member) { Class returntype = null, parameters[], exceptions[]; if (member instanceof Method) { Method m = (Method) member; returntype = m.getReturnType(); parameters = m.getParameterTypes(); exceptions = m.getExceptionTypes(); System.out.print( " " + modifiers(member.getModifiers()) + typename(returntype) + " " + member.getName() + "("); } else { Constructor c = (Constructor) member; parameters = c.getParameterTypes(); exceptions = c.getExceptionTypes(); System.out.print(" " + modifiers(member.getModifiers()) + typename(c.getDeclaringClass()) + "("); } for (int i = 0; i < parameters.length; i++) { if (i > 0) System.out.print(", "); System.out.print(typename(parameters[i])); } System.out.print(")"); if (exceptions.length > 0) System.out.print(" throws "); for (int i = 0; i < exceptions.length; i++) { if (i > 0) System.out.print(", "); System.out.print(typename(exceptions[i])); } System.out.println(";"); }
From source file:org.grouplens.grapht.util.ConstructorProxy.java
/** * Construct a proxy for a constructor.//from w ww . ja va 2s . c o m * @param constructor The constructor to proxy. * @return The constructor proxy representing {@code constructor}. */ public static ConstructorProxy of(Constructor constructor) { Class<?>[] ptypes = constructor.getParameterTypes(); ClassProxy[] proxies = new ClassProxy[ptypes.length]; for (int i = ptypes.length - 1; i >= 0; i--) { proxies[i] = ClassProxy.of(ptypes[i]); } ConstructorProxy proxy = new ConstructorProxy(ClassProxy.of(constructor.getDeclaringClass()), proxies); proxy.constructor = constructor; return proxy; }
From source file:Main.java
public static String ctorToString(final Constructor constructor) { if (constructor == null) { return "null"; }// ww w.j a v a2 s . co m Class[] params = constructor.getParameterTypes(); StringBuffer sb = new StringBuffer(constructor.getName()); sb.append("("); for (int i = 0; i < params.length; i++) { String s = params[i].getName(); sb.append(s); if (i < params.length - 1) { sb.append(", "); } } sb.append(")"); return sb.toString(); }
From source file:fi.luontola.cqrshotel.JsonSerializationTest.java
private static Object newDummy(Class<?> type) throws Exception { Constructor<?> ctor = type.getConstructors()[0]; Class<?>[] paramTypes = ctor.getParameterTypes(); Object[] params = new Object[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) { params[i] = randomValue(paramTypes[i]); }/*w w w . ja v a 2 s .co m*/ return ctor.newInstance(params); }
From source file:org.ovirt.engine.sdk.mapping.Mapper.java
/** * Fetches class contractor for mapping context * /* w w w.j a v a 2s. co m*/ * @param to * class to look at * * @return .ctr * * @throws NoSuchMethodException */ private static <T> Constructor<?> getConstracor(Class<T> to) throws NoSuchMethodException { for (Constructor<?> ctr : to.getConstructors()) { if (ctr.getParameterTypes().length > 0 && ctr.getParameterTypes()[0].equals(HttpProxyBroker.class)) { return ctr; } } throw new NoSuchMethodException("HttpProxyBroker"); }
From source file:org.openvpms.web.component.im.edit.EditDialogFactory.java
/** * Returns a constructor to construct a new dialog. * * @param type the editor dialog type * @param editor the editor/*from w ww . j a v a2 s.co m*/ * @param context the context * @return a constructor to construct the dialog, or {@code null} if none can be found */ private static Constructor getConstructor(Class type, IMObjectEditor editor, Context context) { Constructor[] ctors = type.getConstructors(); for (Constructor ctor : ctors) { // check parameters Class<?>[] ctorTypes = ctor.getParameterTypes(); if (ctorTypes.length == 2) { Class<?> ctorEditor = ctorTypes[0]; Class<?> ctorContext = ctorTypes[1]; if (ctorEditor.isAssignableFrom(editor.getClass()) && ctorContext.isAssignableFrom(context.getClass())) { return ctor; } } } return null; }