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:com.sparkplatform.api.core.PropertyAsserter.java
@SuppressWarnings("unchecked") private static Object invokeDefaultConstructorEvenIfPrivate(Class type) { try {/*from w ww . j a v a 2 s . c o m*/ Constructor ctor = type.getDeclaredConstructor(); ctor.setAccessible(true); return ctor.newInstance(); } catch (Exception ex) { throw new RuntimeException("Could not invoke default constructor on type " + type, ex); } }
From source file:org.cloudata.core.common.util.ReflectionUtils.java
/** Create an object for the given class and initialize it from conf * /* w w w. ja v a 2s. com*/ * @param theClass class of which an object is created * @param conf Configuration * @return a new object */ public static Object newInstance(Class<?> theClass, CloudataConf conf) { Object result; try { Constructor meth = CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(emptyArray); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } setConf(result, conf); return result; }
From source file:org.apache.tez.engine.runtime.RuntimeUtils.java
@SuppressWarnings("unchecked") public static <T> T getNewInstance(Class<T> theClass, TezEngineTaskContext context) { T result;//from w w w . j av a 2s . co m try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(CONTEXT_ARRAY); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(context); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:com.sixt.service.framework.protobuf.ProtobufUtil.java
private static <TYPE extends Message> TYPE.Builder getBuilder(Class<TYPE> messageClass) throws NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException { Constructor<TYPE> constructor = messageClass.getDeclaredConstructor(); constructor.setAccessible(true); TYPE instance = constructor.newInstance(); return instance.newBuilderForType(); }
From source file:org.apache.bookkeeper.common.util.ReflectionUtils.java
/** * Create an object for the given class. * * @param theCls//from www. jav a 2 s . co m * class of which an object is created. * @return a new object */ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> theCls) { T result; try { Constructor<T> meth = (Constructor<T>) constructorCache.get(theCls); if (null == meth) { meth = theCls.getDeclaredConstructor(); meth.setAccessible(true); constructorCache.put(theCls, meth); } result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.apache.tez.engine.runtime.RuntimeUtils.java
@SuppressWarnings("unchecked") public static <T> T getNewInputInstance(Class<T> theClass, TezEngineTaskContext context, int index) { T result;/* ww w .j a v a 2 s.c om*/ try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(CONTEXT_INT_ARRAY); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(context, index); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:Main.java
private static Constructor<?> getDefaultConstructor(@NonNull Class<?> cls) { final Constructor[] ctors = cls.getDeclaredConstructors(); Constructor ctor = null; for (Constructor ct : ctors) { ctor = ct;//from w w w. j av a 2s .c om if (ctor.getGenericParameterTypes().length == 0) break; } if (ctor == null) throw new IllegalStateException("No default constructor found for " + cls.getName()); ctor.setAccessible(true); return ctor; }
From source file:org.solovyev.android.games.game2048.PreferenceManagerCompat.java
/** * Creates the {@link android.preference.PreferenceManager}. * * @param activity activity//from w w w. j av a 2 s . com * @return The {@link android.preference.PreferenceManager} used by this activity. */ @Nonnull private static PreferenceManager newPreferenceManager(@Nonnull Activity activity) { try { Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class); c.setAccessible(true); return c.newInstance(activity, FIRST_REQUEST_CODE); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.alibaba.wasp.client.ServerCallable.java
private static RuntimeException unwrapRuntimeException(Throwable t) { if (StringUtils.isNotEmpty(t.getMessage())) { try {// w w w . j av a 2 s. c o m Class exceptionClass = Class.forName(t.getMessage()); Constructor cn = exceptionClass.getConstructor(String.class); cn.setAccessible(true); String firstLine = t.getMessage(); Object ex = cn.newInstance(firstLine); if (ex instanceof RuntimeException) { return (RuntimeException) ex; } } catch (ClassNotFoundException e) { //ignore } catch (NoSuchMethodException e) { //ignore } catch (InvocationTargetException e) { //ignore } catch (InstantiationException e) { //ignore } catch (IllegalAccessException e) { //ignore } } return null; }
From source file:com.alibaba.wasp.util.JVMClusterUtil.java
/** * Creates a {@link FServerThread}.//from ww w . ja v a2 s.com * Call 'start' on the returned thread to make it run. * @param c Configuration to use. * @param hrsc Class to create. * @param index Used distinguishing the object returned. * @throws java.io.IOException * @return FServer added. */ public static JVMClusterUtil.FServerThread createFServerThread(final Configuration c, final Class<? extends FServer> hrsc, final int index) throws IOException { FServer server; try { Constructor<? extends FServer> ctor = hrsc.getConstructor(Configuration.class); ctor.setAccessible(true); server = ctor.newInstance(c); } catch (InvocationTargetException ite) { Throwable target = ite.getTargetException(); throw new RuntimeException("Failed construction of FServer: " + hrsc.toString() + ((target.getCause() != null) ? target.getCause().getMessage() : ""), target); } catch (Exception e) { IOException ioe = new IOException(); ioe.initCause(e); throw ioe; } return new JVMClusterUtil.FServerThread(server, index); }