List of usage examples for java.lang Class newInstance
@CallerSensitive @Deprecated(since = "9") public T newInstance() throws InstantiationException, IllegalAccessException
From source file:Main.java
public static <S, T> T castObject(S source, Class<T> targetClass) throws IllegalAccessException, InstantiationException { T newInstance = targetClass.newInstance(); for (Field field : source.getClass().getDeclaredFields()) { for (Field fieldTarget : targetClass.getDeclaredFields()) { if (isFieldsEqual(field, fieldTarget)) { setField(getField(field, source), fieldTarget, newInstance); }/*w ww.j av a 2 s. co m*/ } } return newInstance; }
From source file:com.makotosan.vimeodroid.vimeo.VimeoJsonParser.java
public static <T extends VimeoObject> T parse(Class<T> c, String json) throws IllegalAccessException, InstantiationException, IOException, JSONException { T vimeoObject = c.newInstance(); JSONObject jsonObject = (JSONObject) new JSONTokener(json).nextValue(); return vimeoObject; }
From source file:Main.java
/** * Get {@link AssetManager} using reflection *//* w w w . java2s. com*/ private static Object getAssetManager() { Class assetManagerClass = null; try { assetManagerClass = Class.forName("android.content.res.AssetManager"); return assetManagerClass.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static <T> List<T> createRangeData(Class<T> clazz, int value) { List<T> list = new ArrayList<>(); for (int i = 0; i < value; i++) { try {/*from w ww . j a v a 2 s .c om*/ list.add(clazz.newInstance()); } catch (Exception e) { e.printStackTrace(); } } return list; }
From source file:framework.ReloadingServer.java
private static void run(String className, String method) { try {//from ww w .j ava 2 s.c o m Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className); Object obj = clazz.newInstance(); obj.getClass().getMethod(method).invoke(obj); } catch (Exception e) { Loggers.SERVER.error(e.getMessage(), e); } }
From source file:Main.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <K, V extends Collection<T>, T> void addToMap(Map<K, V> data, K key, T value, Class<? extends Collection> clz) { V values = data.get(key);//from w w w.j a va 2 s.co m if (values == null) { try { values = (V) clz.newInstance(); } catch (InstantiationException e) { throw new IllegalArgumentException("Failed to create collection class", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Failed to create collection class", e); } data.put(key, values); } values.add(value); }
From source file:com.tacitknowledge.util.migration.MigrationRunnerFactory.java
public static MigrationRunnerStrategy getMigrationRunnerStrategy(String strategy) { log.info("Strategy received '" + strategy + "'"); if (StringUtils.isBlank(strategy)) { return new OrderedMigrationRunnerStrategy(); }/*from www. jav a 2 s . c om*/ try { Class c = Class.forName(strategy.trim()); MigrationRunnerStrategy runnerStrategy = (MigrationRunnerStrategy) c.newInstance(); return runnerStrategy; } catch (Exception e) { throw new IllegalArgumentException("Strategy selected " + strategy + " cannot be instantiated ", e); } }
From source file:cz.lidinsky.editor.Action.java
public static Action getAction(Properties settings, String key) throws ClassNotFoundException, InstantiationException, IllegalAccessException { // get class name String className = settings.getProperty(key + "_action_class"); // create class Class<Action> actionClass = (Class<Action>) Class.forName(className); // create instance Action action = actionClass.newInstance(); // set text param setLabel(action, settings.getProperty(key + "_action_text")); // return action return action; }
From source file:edu.wisc.hrs.dao.HrsUtils.java
/** * Utility for creating HRS schema generated wrapper objects that simply have a setValue method *//*from w w w . j a v a 2 s. c o m*/ public static <T> T createValue(Class<T> clazz, Object value) { final T instance; try { instance = clazz.newInstance(); } catch (InstantiationException e) { ReflectionUtils.handleReflectionException(e); return null; //This will never run, just used to make compiler's final check happy } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); return null; //This will never run, just used to make compiler's final check happy } callMethodSafe(instance, "setValue", value); return instance; }
From source file:com.tuplejump.stargate.lucene.AnalyzerFactory.java
public static Analyzer getAnalyzer(String analyzerName) { try {/*w w w .j a v a2 s. c o m*/ Analyzers analyzer = Analyzers.valueOf(analyzerName); switch (analyzer) { case SimpleAnalyzer: { return new SimpleAnalyzer(); } case StandardAnalyzer: { return new StandardAnalyzer(); } case StopAnalyzer: { return new StopAnalyzer(); } case WhitespaceAnalyzer: { return new WhitespaceAnalyzer(); } case KeywordAnalyzer: { return new CaseInsensitiveKeywordAnalyzer(); } default: { return new StandardAnalyzer(); } } } catch (IllegalArgumentException e) { try { Class clazz = ClassUtils.getClass(analyzerName); return (Analyzer) clazz.newInstance(); } catch (ClassNotFoundException e1) { throw new IllegalArgumentException("No analyzer class found with name [" + analyzerName + "]"); } catch (InstantiationException e1) { throw new IllegalArgumentException("Could not construct an object of analyzer class with name [" + analyzerName + "]. " + "Unable to find no-args constructor"); } catch (IllegalAccessException e1) { e1.printStackTrace(); } } return null; }