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:Main.java
public static void main(String[] argv) throws Exception { Class cls = java.lang.String.class; Method method = cls.getMethods()[0]; Field field = cls.getFields()[0]; Constructor constructor = cls.getConstructors()[0]; field.setAccessible(true);/*from ww w . j av a 2 s . com*/ constructor.setAccessible(true); method.setAccessible(true); }
From source file:EmailAliases.java
public static void main(String... args) { try {// ww w .j a v a 2s .com Constructor ctor = EmailAliases.class.getDeclaredConstructor(HashMap.class); ctor.setAccessible(true); EmailAliases email = (EmailAliases) ctor.newInstance(defaultAliases); email.printKeys(); // production code should handle these exceptions more gracefully } catch (InstantiationException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); } catch (NoSuchMethodException x) { x.printStackTrace(); } }
From source file:Charge.java
public static void main(String... args) { try {//from ww w . j a v a 2s. co m Class<?> c = Charge.class; Constructor[] ctors = c.getDeclaredConstructors(); for (Constructor ctor : ctors) { out.format("Constructor: %s%n", ctor.toGenericString()); ctor.setAccessible(true); ctor.newInstance(); } // production code should handle these exceptions more gracefully } catch (InstantiationException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); } }
From source file:Main.java
public static void main(String... args) { Constructor[] ctors = Console.class.getDeclaredConstructors(); Constructor ctor = null; for (int i = 0; i < ctors.length; i++) { ctor = ctors[i];//from w w w . j a v a2 s . c o m if (ctor.getGenericParameterTypes().length == 0) break; } try { ctor.setAccessible(true); Console c = (Console) ctor.newInstance(); Field f = c.getClass().getDeclaredField("cs"); f.setAccessible(true); out.format("Console charset : %s%n", f.get(c)); out.format("Charset.defaultCharset(): %s%n", Charset.defaultCharset()); // production code should handle these exceptions more gracefully } catch (InstantiationException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } catch (NoSuchFieldException x) { x.printStackTrace(); } }
From source file:Main.java
public static Constructor getConstructor(Class<?> targetClass, Class<?>... parameterTypes) throws NoSuchMethodException { Constructor constructor = targetClass.getConstructor(parameterTypes); constructor.setAccessible(true); return constructor; }
From source file:Main.java
public static <T> T newInstance(Class<T> cls) throws IllegalArgumentException { try {/*from ww w .j av a2 s.c o m*/ Constructor<T> con = cls.getDeclaredConstructor(); con.setAccessible(true); return con.newInstance(); } catch (Exception e) { throw new IllegalArgumentException(e); } }
From source file:org.apache.hadoop.mapred.nativetask.util.OutputUtil.java
public static NativeTaskOutput createNativeTaskOutput(Configuration conf, String id) { Class<?> clazz = conf.getClass(OutputUtil.NATIVE_TASK_OUTPUT_MANAGER, NativeTaskOutputFiles.class); LOG.info(OutputUtil.NATIVE_TASK_OUTPUT_MANAGER + " = " + clazz.getName()); try {/* w ww. j a v a2 s .co m*/ Constructor<?> ctor = clazz.getConstructor(Configuration.class, String.class); ctor.setAccessible(true); NativeTaskOutput instance = (NativeTaskOutput) ctor.newInstance(conf, id); return instance; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static NetworkInfo createNetworkInfo(final int type, final boolean connected) throws Exception { Constructor<NetworkInfo> ctor = NetworkInfo.class.getDeclaredConstructor(int.class); ctor.setAccessible(true); NetworkInfo networkInfo = ctor.newInstance(0); Field typeField = NetworkInfo.class.getDeclaredField("mNetworkType"); Field connectedField = NetworkInfo.class.getDeclaredField("mState"); Field detailedStateField = NetworkInfo.class.getDeclaredField("mDetailedState"); typeField.setAccessible(true);//from w w w .j a v a2 s. co m connectedField.setAccessible(true); detailedStateField.setAccessible(true); typeField.setInt(networkInfo, type); connectedField.set(networkInfo, connected == true ? NetworkInfo.State.CONNECTED : NetworkInfo.State.DISCONNECTED); detailedStateField.set(networkInfo, connected == true ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED); return networkInfo; }
From source file:Main.java
@SuppressWarnings("unchecked") public static final <E> E getInstance(Class<E> clazz) { if (INSTANCE_MAP.containsKey(clazz)) { return (E) INSTANCE_MAP.get(clazz); } else {//from ww w .j a va 2s . c o m E instance; synchronized (clazz) { instance = (E) INSTANCE_MAP.get(clazz); if (instance == null) { try { Constructor<E> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); instance = constructor.newInstance(); INSTANCE_MAP.put(clazz, instance); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } return instance; } }
From source file:Main.java
public static <T> T newInstance(Class<T> clazz) { try {//from www. j a va2 s . co m final Constructor<T> constructor = clazz.getDeclaredConstructor(); if (!constructor.isAccessible()) { constructor.setAccessible(true); } return constructor.newInstance(); } catch (final Exception e) { throw new RuntimeException(e); } }