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:im.ene.ribbon.BottomNavigationView.java
static BadgeProvider parseBadgeProvider(final BottomNavigationView navigation, final Context context, final String name) { log(TAG, INFO, "parseBadgeProvider: %s", name); if (TextUtils.isEmpty(name)) { return new BadgeProvider(navigation); }/*from ww w.ja v a 2s . c o m*/ final String fullName; if (name.startsWith(".")) { fullName = context.getPackageName() + name; } else if (name.indexOf('.') >= 0) { fullName = name; } else { // Assume stock behavior in this package (if we have one) fullName = !TextUtils.isEmpty(WIDGET_PACKAGE_NAME) ? (WIDGET_PACKAGE_NAME + '.' + name) : name; } try { Map<String, Constructor<BadgeProvider>> constructors = S_CONSTRUCTORS.get(); if (constructors == null) { constructors = new HashMap<>(); S_CONSTRUCTORS.set(constructors); } Constructor<BadgeProvider> c = constructors.get(fullName); if (c == null) { final Class<BadgeProvider> clazz = (Class<BadgeProvider>) Class.forName(fullName, true, context.getClassLoader()); c = clazz.getConstructor(CONSTRUCTOR_PARAMS); c.setAccessible(true); constructors.put(fullName, c); } return c.newInstance(navigation); } catch (Exception e) { throw new RuntimeException("Could not inflate Behavior subclass " + fullName, e); } }
From source file:org.apache.geode.cache.lucene.LuceneSearchWithRollingUpgradeDUnit.java
public static Object createCache(Properties systemProperties) throws Exception { Class distConfigClass = Thread.currentThread().getContextClassLoader() .loadClass("org.apache.geode.distributed.internal.DistributionConfigImpl"); boolean disableConfig = true; try {//from w w w .j ava 2 s .com distConfigClass.getDeclaredField("useSharedConfiguration"); } catch (NoSuchFieldException e) { disableConfig = false; } if (disableConfig) { systemProperties.put(DistributionConfig.USE_CLUSTER_CONFIGURATION_NAME, "false"); } Class cacheFactoryClass = Thread.currentThread().getContextClassLoader() .loadClass("org.apache.geode.cache.CacheFactory"); Constructor constructor = cacheFactoryClass.getConstructor(Properties.class); constructor.setAccessible(true); Object cacheFactory = constructor.newInstance(systemProperties); Method createMethod = cacheFactoryClass.getMethod("create"); createMethod.setAccessible(true); Object cache = createMethod.invoke(cacheFactory); return cache; }
From source file:com.sm.query.utils.QueryUtils.java
public static Object createInstance(Class<?> cls) throws Exception { Constructor<?> constructor = null; Object[] constructorArgs = null; Constructor<?>[] constructors = cls.getDeclaredConstructors(); //take the first constructor if (constructors.length > 0) { constructor = constructors[0];// www. j a v a 2 s. c om constructor.setAccessible(true); Class<?>[] params = constructor.getParameterTypes(); constructorArgs = new Object[params.length]; for (int i = 0; i < params.length; i++) { constructorArgs[i] = getParamArg(params[i]); } return constructor.newInstance(constructorArgs); } else return cls.newInstance(); }
From source file:org.apache.geode.internal.cache.rollingupgrade.RollingUpgradeDUnitTest.java
public static Object createClientCache(Properties systemProperties, String[] hosts, int[] ports) throws Exception { Class aClass = Thread.currentThread().getContextClassLoader() .loadClass("org.apache.geode.cache.client.ClientCacheFactory"); Constructor constructor = aClass.getConstructor(Properties.class); constructor.setAccessible(true); Object ccf = constructor.newInstance(systemProperties); Method addPoolLocatorMethod = aClass.getMethod("addPoolLocator", String.class, int.class); addPoolLocatorMethod.setAccessible(true); int hostsLength = hosts.length; for (int i = 0; i < hostsLength; i++) { addPoolLocatorMethod.invoke(ccf, hosts[i], ports[i]); }//from w ww .ja v a 2 s . c o m Method createMethod = aClass.getMethod("create"); createMethod.setAccessible(true); Object cache = createMethod.invoke(ccf); return cache; }
From source file:com.github.springtestdbunit.testutils.ExtendedTestContextManager.java
public ExtendedTestContextManager(Class<?> testClass) throws Exception { super(testClass); getTestContext().markApplicationContextDirty(); Constructor<?> constructor = testClass.getDeclaredConstructor(); constructor.setAccessible(true); this.testInstance = constructor.newInstance(); }
From source file:com.yeahmobi.yunit.testutils.ExtendedTestContextManager.java
public ExtendedTestContextManager(Class<?> testClass) throws Exception { super(testClass); getTestContext().markApplicationContextDirty(HierarchyMode.CURRENT_LEVEL); Constructor<?> constructor = testClass.getDeclaredConstructor(); constructor.setAccessible(true); this.testInstance = constructor.newInstance(); }
From source file:org.jbpm.instantiation.ConstructorInstantiator.java
public Object instantiate(Class clazz, String configuration) { Object newInstance = null;//from ww w .ja va 2 s . c o m try { Constructor constructor = clazz.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); newInstance = constructor.newInstance(new Object[] { configuration }); } catch (Exception e) { log.error("couldn't instantiate '" + clazz.getName() + "'", e); throw new JbpmException(e); } return newInstance; }
From source file:org.terasoluna.gfw.web.util.RequestUtilsTest.java
@Test public void testPrivateConstructor() throws Exception { Constructor<RequestUtils> c = RequestUtils.class.getDeclaredConstructor(); c.setAccessible(true); assertNotNull(c.newInstance());/*w w w. ja v a 2 s . c om*/ }
From source file:org.apache.geode.internal.cache.rollingupgrade.RollingUpgradeDUnitTest.java
public static Object createCache(Properties systemProperties) throws Exception { // systemProperties.put(DistributionConfig.LOG_FILE_NAME, // "rollingUpgradeCacheVM" + VM.getCurrentVMNum() + ".log"); Class distConfigClass = Thread.currentThread().getContextClassLoader() .loadClass("org.apache.geode.distributed.internal.DistributionConfigImpl"); boolean disableConfig = true; try {//from w w w . ja v a 2 s .c o m distConfigClass.getDeclaredField("useSharedConfiguration"); } catch (NoSuchFieldException e) { disableConfig = false; } if (disableConfig) { systemProperties.put(DistributionConfig.USE_CLUSTER_CONFIGURATION_NAME, "false"); } Class cacheFactoryClass = Thread.currentThread().getContextClassLoader() .loadClass("org.apache.geode.cache.CacheFactory"); Constructor constructor = cacheFactoryClass.getConstructor(Properties.class); constructor.setAccessible(true); Object cacheFactory = constructor.newInstance(systemProperties); Method createMethod = cacheFactoryClass.getMethod("create"); createMethod.setAccessible(true); Object cache = null; cache = createMethod.invoke(cacheFactory); return cache; }
From source file:org.terasoluna.gfw.web.util.ResponseUtilsTest.java
@Test public void testPrivateConstructor() throws Exception { Constructor<ResponseUtils> c = ResponseUtils.class.getDeclaredConstructor(); c.setAccessible(true); assertNotNull(c.newInstance());// w ww .j a v a 2 s.com }