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:edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFServiceTest.java
@SuppressWarnings("unchecked") private Comparator<Object> buildRowIndexedLiteralSortByLang() { try {/*from ww w. j a v a2 s . com*/ Class<?> clazz = Class.forName(COLLATOR_CLASSNAME); Class<?>[] argTypes = { LanguageFilteringRDFService.class }; Constructor<?> constructor = clazz.getDeclaredConstructor(argTypes); constructor.setAccessible(true); return (Comparator<Object>) constructor.newInstance(filteringRDFService); } catch (Exception e) { throw new RuntimeException("Could not create a collator", e); } }
From source file:org.onosproject.yang.compiler.translator.tojava.utils.JavaIdentifierSyntaxTest.java
/** * Unit test for private constructor.//from w ww .j a va2 s . com * * @throws SecurityException if any security violation is observed. * @throws NoSuchMethodException if when the method is not found. * @throws IllegalArgumentException if there is illegal argument found. * @throws InstantiationException if instantiation is provoked for the * private constructor. * @throws IllegalAccessException if instance is provoked or a method is * provoked. * @throws InvocationTargetException when an exception occurs by the method * or constructor. */ @Test public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?>[] classesToConstruct = { JavaIdentifierSyntax.class }; for (Class<?> clazz : classesToConstruct) { Constructor<?> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); assertThat(null, not(constructor.newInstance())); } }
From source file:edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFServiceTest.java
private Object buildRowIndexedLiteral(String language) { try {//from w ww . j a va 2 s . c o m Class<?> clazz = Class.forName(RIL_CLASSNAME); Class<?>[] argTypes = { LanguageFilteringRDFService.class, Literal.class, Integer.TYPE }; Constructor<?> constructor = clazz.getDeclaredConstructor(argTypes); constructor.setAccessible(true); Literal l = new LiteralStub(language); int i = literalIndex++; return constructor.newInstance(filteringRDFService, l, i); } catch (Exception e) { throw new RuntimeException("Could not create a row-indexed literal", e); } }
From source file:eu.stratosphere.nephele.ipc.Server.java
public static <T> T newInstance(Class<T> theClass) { T result;// w w w .j a v a 2s . c o m Constructor<T> meth = null; try { meth = theClass.getDeclaredConstructor(EMPTY_ARRAY); meth.setAccessible(true); result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.apache.tajo.storage.StorageManager.java
/** * Returns the proper StorageManager instance according to the storeType * * @param tajoConf Tajo system property. * @param storeType Storage type/*ww w . j a v a2s. c o m*/ * @param managerKey Key that can identify each storage manager(may be a path) * @return * @throws java.io.IOException */ private static synchronized StorageManager getStorageManager(TajoConf tajoConf, StoreType storeType, String managerKey) throws IOException { String typeName; switch (storeType) { case HBASE: typeName = "hbase"; break; default: typeName = "hdfs"; } synchronized (storageManagers) { String storeKey = typeName + "_" + managerKey; StorageManager manager = storageManagers.get(storeKey); if (manager == null) { Class<? extends StorageManager> storageManagerClass = tajoConf.getClass( String.format("tajo.storage.manager.%s.class", typeName), null, StorageManager.class); if (storageManagerClass == null) { throw new IOException("Unknown Storage Type: " + typeName); } try { Constructor<? extends StorageManager> constructor = (Constructor<? extends StorageManager>) CONSTRUCTOR_CACHE .get(storageManagerClass); if (constructor == null) { constructor = storageManagerClass .getDeclaredConstructor(new Class<?>[] { StoreType.class }); constructor.setAccessible(true); CONSTRUCTOR_CACHE.put(storageManagerClass, constructor); } manager = constructor.newInstance(new Object[] { storeType }); } catch (Exception e) { throw new RuntimeException(e); } manager.init(tajoConf); storageManagers.put(storeKey, manager); } return manager; } }
From source file:com.facebook.stetho.json.ObjectMapper.java
private <T> T _convertFromJSONObject(JSONObject jsonObject, Class<T> type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, JSONException { Constructor<T> constructor = type.getDeclaredConstructor((Class[]) null); constructor.setAccessible(true); T instance = constructor.newInstance(); Field[] fields = type.getFields(); for (int i = 0; i < fields.length; ++i) { Field field = fields[i];//from w w w . j av a2 s . co m Object value = jsonObject.opt(field.getName()); Object setValue = getValueForField(field, value); try { field.set(instance, getValueForField(field, value)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Class: " + type.getSimpleName() + " " + "Field: " + field.getName() + " type " + setValue.getClass().getName(), e); } } return instance; }
From source file:org.metaservice.core.maven.MavenPomParser.java
private ModelResolver makeModelResolver() { try {/* w w w . j a v a 2s. c o m*/ Injector injector = Guice.createInjector(new MavenAetherModule(), new AbstractModule() { @Override protected void configure() { Multibinder<RepositoryConnectorFactory> multibinder = Multibinder.newSetBinder(binder(), RepositoryConnectorFactory.class); multibinder.addBinding().to(FileRepositoryConnectorFactory.class); multibinder.addBinding().to(WagonRepositoryConnectorFactory.class); //fake wagon configurator bind(WagonConfigurator.class).toInstance(new WagonConfigurator() { @Override public void configure(Wagon wagon, Object configuration) throws Exception { } }); //fake wagon provider //todo test for reusage? bind(WagonProvider.class).toInstance(new WagonProvider() { @Override public Wagon lookup(String roleHint) throws Exception { return new HttpWagon(); } @Override public void release(Wagon wagon) { } }); } }); Class c = Class.forName("org.apache.maven.repository.internal.DefaultModelResolver"); Constructor ct = c.getConstructor(new Class[] { RepositorySystemSession.class, RequestTrace.class, String.class, ArtifactResolver.class, RemoteRepositoryManager.class, List.class }); ct.setAccessible(true); //todo handle resoultion repositories // maybe using standard configuration files and dynamic reading of repositories in pom? RemoteRepository.Builder b = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2"); DefaultRepositorySystemSession systemSession = MavenRepositorySystemUtils.newSession(); systemSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() .newInstance(systemSession, new LocalRepository("/foo"))); return (org.apache.maven.model.resolution.ModelResolver) ct.newInstance(systemSession, null, null, injector.getInstance(ArtifactResolver.class), injector.getInstance(RemoteRepositoryManager.class), Arrays.asList(b.build())); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.ocpsoft.pretty.faces.util.ServiceLoader.java
private S prepareInstance(Class<? extends S> serviceClass) { try {/*from ww w . j a va2s . c o m*/ // TODO Support the SM Constructor<? extends S> constructor = serviceClass.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (NoClassDefFoundError e) { log.warn("Could not instantiate service class " + serviceClass.getName(), e); return null; } catch (InvocationTargetException e) { throw new RuntimeException("Error instantiating " + serviceClass, e.getCause()); } catch (IllegalArgumentException e) { throw new RuntimeException("Error instantiating " + serviceClass, e); } catch (InstantiationException e) { throw new RuntimeException("Error instantiating " + serviceClass, e); } catch (IllegalAccessException e) { throw new RuntimeException("Error instantiating " + serviceClass, e); } catch (SecurityException e) { throw new RuntimeException("Error instantiating " + serviceClass, e); } catch (NoSuchMethodException e) { throw new RuntimeException("Error instantiating " + serviceClass, e); } }
From source file:com.nkahoang.screenstandby.PreferenceListFragment.java
/** * Creates the {@link PreferenceManager}. * /*from w ww. ja va 2s . c o m*/ * @return The {@link PreferenceManager} used by this activity. */ private PreferenceManager onCreatePreferenceManager() { try { Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class); c.setAccessible(true); PreferenceManager preferenceManager = c.newInstance(this.getActivity(), FIRST_REQUEST_CODE); return preferenceManager; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:org.silverpeas.core.test.rule.CommonAPI4Test.java
private void managedThreadFactory() { try {/*from www.j av a 2s. c o m*/ Constructor<ManagedThreadPool> managedThreadPoolConstructor = ManagedThreadPool.class .getDeclaredConstructor(); managedThreadPoolConstructor.setAccessible(true); ManagedThreadPool managedThreadPool = managedThreadPoolConstructor.newInstance(); ManagedThreadFactory managedThreadFactory = Thread::new; FieldUtils.writeField(managedThreadPool, "managedThreadFactory", managedThreadFactory, true); when(TestBeanContainer.getMockedBeanContainer().getBeanByType(ManagedThreadPool.class)) .thenReturn(managedThreadPool); } catch (IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException e) { throw new SilverpeasRuntimeException(e); } }