List of usage examples for java.lang.reflect Constructor setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
A SecurityException is also thrown if this object is a Constructor object for the class Class and flag is true.
From source file:net.neevek.android.lib.paginize.util.AnnotationUtils.java
private static Object getTargetListener(Class clazz, Object object, Map<Class, Object> targetListenerCache, Class targetListenerClass, String tag) throws InstantiationException, IllegalAccessException, InvocationTargetException { if (targetListenerClass == null || targetListenerClass == void.class) { return null; }/*from w w w. j a v a 2 s .c o m*/ Object targetListener = targetListenerCache.get(targetListenerClass); if (targetListener == null) { try { boolean isStatic = Modifier.isStatic(targetListenerClass.getModifiers()); if (isStatic) { Constructor ctor = targetListenerClass.getDeclaredConstructor(); ctor.setAccessible(true); targetListener = ctor.newInstance(); } else { Constructor ctor = targetListenerClass.getDeclaredConstructor(clazz); ctor.setAccessible(true); targetListener = ctor.newInstance(object); } targetListenerCache.put(targetListenerClass, targetListener); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("The 'listener' field: '" + targetListenerClass.getSimpleName() + "' in " + tag + " must contain a default constructor without arguments."); } } return targetListener; }
From source file:org.apache.tinkerpop.gremlin.process.computer.VertexProgram.java
/** * A helper method to construct a {@link VertexProgram} given the content of the supplied configuration. * The class of the VertexProgram is read from the {@link VertexProgram#VERTEX_PROGRAM} static configuration key. * Once the VertexProgram is constructed, {@link VertexProgram#loadState} method is called with the provided graph and configuration. * * @param graph The graph that the vertex program will execute against * @param configuration A configuration with requisite information to build a vertex program * @param <V> The vertex program type * @return the newly constructed vertex program *//* w w w . j a v a2 s . c o m*/ public static <V extends VertexProgram> V createVertexProgram(final Graph graph, final Configuration configuration) { try { final Class<V> vertexProgramClass = (Class) Class.forName(configuration.getString(VERTEX_PROGRAM)); final Constructor<V> constructor = vertexProgramClass.getDeclaredConstructor(); constructor.setAccessible(true); final V vertexProgram = constructor.newInstance(); vertexProgram.loadState(graph, configuration); return vertexProgram; } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
From source file:Main.java
public static Object getNewObject(Class<?> outerClass, String innerClassName, Class<?> parameterTypes[], Object parameters[]) throws Exception { // Get all inner classes Class<?> innerClasses[] = outerClass.getDeclaredClasses(); // find the inner class that matches the order Constructor<?> constructor = null; for (int index = 0; index < innerClasses.length; index++) { if (innerClassName.equals(innerClasses[index].getSimpleName())) { constructor = innerClasses[index].getConstructor(parameterTypes); }// www .j a v a 2 s .co m } if (constructor != null) { constructor.setAccessible(true); Object obj = constructor.newInstance(parameters); return obj; } return null; }
From source file:org.assertj.db.common.AbstractTest.java
/** * Returns an instance of a {@code Changes}. * * @param changesList The list of changes. * @return An instance./*from w w w .jav a 2s. co m*/ * @throws Exception Exception */ protected static Changes getChanges(List<Change> changesList) throws Exception { Constructor<Changes> constructor = Changes.class.getDeclaredConstructor(); constructor.setAccessible(true); Changes changes = constructor.newInstance(); Field field = Changes.class.getDeclaredField("changesList"); field.setAccessible(true); field.set(changes, changesList); return changes; }
From source file:org.apache.hadoop.util.ReflectionUtils.java
/** Create an object for the given class and initialize it from conf * * @param theClass class of which an object is created * @param conf Configuration//w ww.j av a 2 s .c om * @return a new object */ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> theClass, Configuration conf) { T result; try { Constructor<T> meth = theClass.getDeclaredConstructor(EMPTY_ARRAY); meth.setAccessible(true); result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } setConf(result, conf); return result; }
From source file:org.apache.tinkerpop.gremlin.process.computer.MapReduce.java
/** * A helper method to construct a {@link MapReduce} given the content of the supplied configuration. * The class of the MapReduce is read from the {@link MapReduce#MAP_REDUCE} static configuration key. * Once the MapReduce is constructed, {@link MapReduce#loadState} method is called with the provided configuration. * * @param graph The graph that the MapReduce job will run against * @param configuration A configuration with requisite information to build a MapReduce * @return the newly constructed MapReduce *///w w w . j a v a 2s . c om public static <M extends MapReduce> M createMapReduce(final Graph graph, final Configuration configuration) { try { final Class<M> mapReduceClass = (Class) Class.forName(configuration.getString(MAP_REDUCE)); final Constructor<M> constructor = mapReduceClass.getDeclaredConstructor(); constructor.setAccessible(true); final M mapReduce = constructor.newInstance(); mapReduce.loadState(graph, configuration); return mapReduce; } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
From source file:org.apache.hama.util.ReflectionUtils.java
/** * Create an instance with corresponded class and object values supplied. * Constructor/* www . j a v a2 s. com*/ * * @param theClass supplies instance to be created. * @param parameters are class type of object values supplied. * @param values are parameters applied when instance is created. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T newInstance(Class<T> theClass, Class[] parameters, Object[] values) { T result; try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (null == meth) { meth = theClass.getDeclaredConstructor(parameters); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(values); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:hivemall.xgboost.XGBoostUDTF.java
@Nonnull private static Booster createXGBooster(final Map<String, Object> params, final List<LabeledPoint> input) throws NoSuchMethodException, XGBoostError, IllegalAccessException, InvocationTargetException, InstantiationException {/*from w w w . j a v a 2 s . com*/ Class<?>[] args = { Map.class, DMatrix[].class }; Constructor<Booster> ctor = Booster.class.getDeclaredConstructor(args); ctor.setAccessible(true); return ctor.newInstance(new Object[] { params, new DMatrix[] { new DMatrix(input.iterator(), "") } }); }
From source file:org.assertj.db.common.AbstractTest.java
/** * Returns an instance of a {@code Row}. * //from ww w . j a v a 2s . c o m * @param pksNameList The list of the primary keys name. * @param columnsNameList The list of the columns name. * @param valuesList The values in the row. * @return An instance. * @throws Exception Exception */ protected static Row getRow(List<String> pksNameList, List<String> columnsNameList, List<?> valuesList) throws Exception { Constructor<Row> constructor = Row.class.getDeclaredConstructor(List.class, List.class, List.class); constructor.setAccessible(true); return constructor.newInstance(pksNameList, columnsNameList, valuesList); }
From source file:com.oltpbenchmark.util.ClassUtil.java
/** Create an object for the given class and initialize it from conf * * @param theClass class of which an object is created * @param expected the expected parent class or interface * @return a new object/* w ww . j a va 2 s .c o m*/ */ public static <T> T newInstance(Class<?> theClass, Class<T> expected) { T result; try { if (!expected.isAssignableFrom(theClass)) { throw new Exception("Specified class " + theClass.getName() + "" + "does not extend/implement " + expected.getName()); } Class<? extends T> clazz = (Class<? extends T>) theClass; Constructor<? extends T> meth = clazz.getDeclaredConstructor(EMPTY_ARRAY); meth.setAccessible(true); result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return result; }