List of usage examples for java.lang Class getConstructor
@CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:com.actionbarsherlock.ActionBarSherlock.java
/** * Wrap an activity with an action bar abstraction which will enable the * use of a custom implementation on platforms where a native version does * not exist./*from w ww.j a v a2 s.co m*/ * * @param activity Owning activity. * @param flags Option flags to control behavior. * @return Instance to interact with the action bar. */ public static ActionBarSherlock wrap(Activity activity, int flags) { //Create a local implementation map we can modify HashMap<Implementation, Class<? extends ActionBarSherlock>> impls = new HashMap<Implementation, Class<? extends ActionBarSherlock>>( IMPLEMENTATIONS); boolean hasQualfier; /* DPI FILTERING */ hasQualfier = false; for (Implementation key : impls.keySet()) { //Only honor TVDPI as a specific qualifier if (key.dpi() == DisplayMetrics.DENSITY_TV) { hasQualfier = true; break; } } if (hasQualfier) { final boolean isTvDpi = activity.getResources() .getDisplayMetrics().densityDpi == DisplayMetrics.DENSITY_TV; for (Iterator<Implementation> keys = impls.keySet().iterator(); keys.hasNext();) { int keyDpi = keys.next().dpi(); if ((isTvDpi && keyDpi != DisplayMetrics.DENSITY_TV) || (!isTvDpi && keyDpi == DisplayMetrics.DENSITY_TV)) { keys.remove(); } } } /* API FILTERING */ hasQualfier = false; for (Implementation key : impls.keySet()) { if (key.api() != Implementation.DEFAULT_API) { hasQualfier = true; break; } } if (hasQualfier) { final int runtimeApi = Build.VERSION.SDK_INT; int bestApi = 0; for (Iterator<Implementation> keys = impls.keySet().iterator(); keys.hasNext();) { int keyApi = keys.next().api(); if (keyApi > runtimeApi) { keys.remove(); } else if (keyApi > bestApi) { bestApi = keyApi; } } for (Iterator<Implementation> keys = impls.keySet().iterator(); keys.hasNext();) { if (keys.next().api() != bestApi) { keys.remove(); } } } if (impls.size() > 1) { throw new IllegalStateException("More than one implementation matches configuration."); } if (impls.isEmpty()) { throw new IllegalStateException("No implementations match configuration."); } Class<? extends ActionBarSherlock> impl = impls.values().iterator().next(); if (DEBUG) Log.i(TAG, "Using implementation: " + impl.getSimpleName()); try { Constructor<? extends ActionBarSherlock> ctor = impl.getConstructor(CONSTRUCTOR_ARGS); return ctor.newInstance(activity, flags); } 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); } }
From source file:au.com.borner.salesforce.client.rest.domain.AbstractJSONObject.java
@SuppressWarnings("unchecked") protected <T extends AbstractJSONObject> List<T> getChildEntities(String name, Class<T> resultClass) { try {//from ww w . j ava 2s . c om Constructor<T> constructor = resultClass.getConstructor(String.class); JSONArray children = getJSONArray(name); List<T> result = new ArrayList<T>(children.length()); for (int i = 0; i < children.length(); i++) { JSONObject jsonObject = children.getJSONObject(i); T instance = constructor.newInstance(jsonObject.toString()); // is there any way to avoid double serialisation? result.add(instance); } return result; } catch (Exception e) { throw new RuntimeException("Error parsing child elements", e); } }
From source file:com.github.dpsm.android.print.gson.GsonResultOperator.java
/** * Creates an instance of the operator for the specified target class type that uses the * specified Gson instance to de-serialize. * * @param gson the Gson instance to use for de-serialization. * @param clazz the target type to convert to. *//*from w ww . j av a 2 s. co m*/ public GsonResultOperator(final Gson gson, final Class<T> clazz) { try { mConstructor = clazz.getConstructor(JsonObject.class); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } mClass = clazz; mGSON = gson; }
From source file:com.aerospike.config.AppConfiguration.java
public AppConfiguration() throws CloudFoundryEnvironmentException { CloudFoundryEnvironment environment = new CloudFoundryEnvironment(System::getenv); if (environment.getServiceNames().size() > 0) { for (String serviceName : environment.getServiceNames()) { System.out.println("SERVICE NAME: " + serviceName); CloudFoundryService service = environment.getService(serviceName); if (labelToConfigurationClass.containsKey(service.getLabel())) { String className = labelToConfigurationClass.get(service.getLabel()); try { Class<?> clazz = Class.forName(className); Constructor<?> constructor = clazz.getConstructor(Map.class); IServiceConfiguration config = (IServiceConfiguration) constructor .newInstance(environment.getService(serviceName).getCredentials()); services.put(serviceName, config); System.out.println("ADDED CLASS : " + className); } catch (Exception e) { e.printStackTrace(); System.out.println("Error creating class " + className); }/*from w ww .jav a 2 s. co m*/ } } } }
From source file:com.fatminds.cmis.AlfrescoCmisHelper.java
public static Object convert(Object inVal, Class<?> outClass) { if (null == inVal || null == outClass) { //log.info("inVal " + inVal + " or outClass " + outClass + " are null"); return null; }//from w ww . j a v a 2 s.c om //log.info("Converting " + inVal.getClass() + " to " + outClass); if (outClass.isAssignableFrom(inVal.getClass())) return inVal; Object outVal; try { Constructor<?> c = outClass.getConstructor(inVal.getClass()); outVal = c.newInstance(inVal); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { //log.info("Failed to find constructor of " + outClass.toString() // + " with input parameter " + inVal.getClass().toString() // + ", returning unconverted value"); outVal = inVal; } 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); } //log.info("Returning an instance of " + outVal.getClass()); return outVal; }
From source file:com.github.dpsm.android.print.jackson.JacksonResultOperator.java
/** * Creates an instance of the operator for the specified target class type that uses the * specified ObjectMapper instance to de-serialize. * * @param mapper the ObjectMapper instance to use for de-serialization. * @param clazz the target type to convert to. *//*from ww w . j a va 2 s. c o m*/ public JacksonResultOperator(final ObjectMapper mapper, final Class<T> clazz) { try { mConstructor = clazz.getConstructor(ObjectNode.class); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } mClass = clazz; mMapper = mapper; }
From source file:com.all.shared.sync.SyncGenericConverter.java
private static Object readValue(String attributeName, Class<?> clazz, Map<String, Object> map) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = map.get(attributeName); if (value != null && !clazz.equals(value.getClass())) { if (clazz.equals(Date.class) && StringUtils.isNumeric(value.toString())) { return new Date(new Long(value.toString())); }//from w w w . j a v a2 s . c o m if (clazz.isEnum() && value instanceof String) { for (Object enumType : clazz.getEnumConstants()) { if (value.equals(enumType.toString())) { return enumType; } } } if (PRIMITIVES.contains(clazz)) { return clazz.getConstructor(String.class).newInstance(value.toString()); } } return value; }
From source file:com.seovic.core.Defaults.java
/** * Gets a constructor for the specified class that accepts a single String * argument.//from w w w . j av a 2s .c om * * @param type the class to find the constructor for * * @return a constructor for the specified class that accepts a single * String argument */ protected Constructor getConstructor(Class type) { try { return type.getConstructor(String.class); } catch (NoSuchMethodException e) { throw new RuntimeException("Unable to find a constructor that accepts" + " a single String argument in the " + type.getName() + " class.", e); } }
From source file:hudson.jbpm.model.TaskInstanceWrapper.java
public Object getForm() { TaskInstance ti = getTaskInstance(); try {//ww w . j a va2s. c om ClassLoader processClassLoader = ProcessClassLoaderCache.INSTANCE .getClassLoader(ti.getProcessInstance().getProcessDefinition()); String formClass = (String) ti.getVariableLocally("form"); if (formClass == null) { return new Form(ti); } else { Class<?> cl = processClassLoader.loadClass(formClass); return cl.getConstructor(TaskInstance.class).newInstance(ti); } } catch (InvocationTargetException e) { throw new RuntimeException(e.getCause()); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:jetbrains.buildServer.server.rest.util.BeanFactory.java
@Nullable private <T> Constructor<T> findConstructor(@NotNull final Class<T> clazz, Class<?>[] argTypes) { try {/* w ww.j a v a2 s.co m*/ return clazz.getConstructor(argTypes); } catch (NoSuchMethodException e) { //NOP } for (Constructor c : clazz.getConstructors()) { final Class[] reqTypes = c.getParameterTypes(); if (checkParametersMatch(argTypes, reqTypes)) { //noinspection unchecked return (Constructor<T>) c; } } return null; }