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.puppycrawl.tools.checkstyle.checks.javadoc.AbstractTypeAwareCheckTest.java
@Test public void testClassAliasToString() throws Exception { Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$Token"); Class<?> regularClassType = Class .forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$RegularClass"); Constructor<?> regularClassConstructor = regularClassType.getDeclaredConstructor(tokenType, String.class, AbstractTypeAwareCheck.class); regularClassConstructor.setAccessible(true); Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class, int.class, int.class); Object token = tokenConstructor.newInstance("blablabla", 1, 1); Object regularClass = regularClassConstructor.newInstance(token, "sur", new JavadocMethodCheck()); Class<?> classAliasType = Class .forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$ClassAlias"); Class<?> abstractTypeInfoType = Class .forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$AbstractClassInfo"); Constructor<?> classAliasConstructor = classAliasType.getDeclaredConstructor(tokenType, abstractTypeInfoType);/*from w w w. ja v a 2s . com*/ classAliasConstructor.setAccessible(true); Object classAlias = classAliasConstructor.newInstance(token, regularClass); Method toString = classAlias.getClass().getDeclaredMethod("toString"); toString.setAccessible(true); String result = (String) toString.invoke(classAlias); assertEquals("ClassAlias[alias Token[blablabla(1x1)] for Token[blablabla(1x1)]]", result); }
From source file:com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractTypeAwareCheckTest.java
@Test public void testClassRegularClass() throws Exception { Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$Token"); Class<?> regularClassType = Class .forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$RegularClass"); Constructor<?> regularClassConstructor = regularClassType.getDeclaredConstructor(tokenType, String.class, AbstractTypeAwareCheck.class); regularClassConstructor.setAccessible(true); try {//from ww w. j ava2 s . co m regularClassConstructor.newInstance(null, "", new JavadocMethodCheck()); } catch (InvocationTargetException ex) { assertTrue(ex.getCause() instanceof IllegalArgumentException); assertEquals("ClassInfo's name should be non-null", ex.getCause().getMessage()); } Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class, int.class, int.class); Object token = tokenConstructor.newInstance("blablabla", 1, 1); Object regularClass = regularClassConstructor.newInstance(token, "sur", new JavadocMethodCheck()); Method toString = regularClass.getClass().getDeclaredMethod("toString"); toString.setAccessible(true); String result = (String) toString.invoke(regularClass); assertEquals("RegularClass[name=Token[blablabla(1x1)], in class=sur, loadable=true," + " class=null]", result); Method setClazz = regularClass.getClass().getDeclaredMethod("setClazz", Class.class); setClazz.setAccessible(true); Class<?> arg = null; setClazz.invoke(regularClass, arg); Method getClazz = regularClass.getClass().getDeclaredMethod("getClazz"); getClazz.setAccessible(true); assertNull(getClazz.invoke(regularClass)); }
From source file:org.paxml.util.ReflectUtils.java
/** * Construct object from class using the default constructor. * //www . j a v a 2s . c o m * @param <T> * the class type * @param clazz * the class * @param constructParams * the parameters to call constructor with * @return the object * @throws RuntimeException * if the construction fails. */ public static <T> T createObject(Class<? extends T> clazz, Object... constructParams) { Constructor<T> con = null; Class[] argTypes = null; for (Constructor c : clazz.getDeclaredConstructors()) { argTypes = c.getParameterTypes(); if (argTypes.length == constructParams.length) { con = c; break; } } if (con == null) { throw new PaxmlRuntimeException("No constructor found with " + constructParams.length + " parameters!"); } try { Object[] args = new Object[constructParams.length]; for (int i = args.length - 1; i >= 0; i--) { args[i] = coerceType(constructParams[i], argTypes[i]); } con.setAccessible(true); return con.newInstance(args); } catch (Exception e) { throw new PaxmlRuntimeException("Cannot create instance from class: " + clazz.getName(), e); } }
From source file:org.codehaus.groovy.grails.orm.hibernate.GrailsSessionContext.java
protected Constructor<?> lookupConstructor(String className, Class<?>... argTypes) { try {/*w w w . ja va 2 s .c om*/ Class<?> clazz = ReflectHelper.classForName(className); Constructor<?> constructor = clazz.getConstructor(argTypes); constructor.setAccessible(true); return constructor; } catch (ClassNotFoundException e) { ReflectionUtils.handleReflectionException(e); } catch (NoSuchMethodException e) { ReflectionUtils.handleReflectionException(e); } catch (SecurityException e) { ReflectionUtils.handleReflectionException(e); } return null; }
From source file:org.apache.tajo.master.rm.TajoResourceManager.java
protected synchronized AbstractQueryScheduler loadScheduler(String schedulerClassName) throws Exception { Class<? extends AbstractQueryScheduler> schedulerClass; if (SCHEDULER_CLASS_CACHE.containsKey(schedulerClassName)) { schedulerClass = SCHEDULER_CLASS_CACHE.get(schedulerClassName); } else {/*from ww w . j ava 2 s .c om*/ schedulerClass = (Class<? extends AbstractQueryScheduler>) Class.forName(schedulerClassName); SCHEDULER_CLASS_CACHE.put(schedulerClassName, schedulerClass); } Constructor<? extends AbstractQueryScheduler> constructor = schedulerClass .getDeclaredConstructor(new Class[] { TajoMaster.MasterContext.class }); constructor.setAccessible(true); return constructor.newInstance(new Object[] { masterContext }); }
From source file:org.seedstack.seed.core.internal.CorePlugin.java
@Override public Object nativeUnitModule() { Collection<Module> subModules = new HashSet<Module>(); for (Class<? extends Module> klazz : seedModules) { try {/*from www .j av a2 s . co m*/ Constructor<? extends Module> declaredConstructor = klazz.getDeclaredConstructor(); declaredConstructor.setAccessible(true); subModules.add(declaredConstructor.newInstance()); } catch (Exception e) { throw SeedException.wrap(e, CoreErrorCode.UNABLE_TO_INSTANTIATE_MODULE).put("module", klazz.getCanonicalName()); } } return new CoreModule(subModules, DIAGNOSTIC_MANAGER, diagnosticInfoCollectors); }
From source file:org.apache.solr.sentry.SentryIndexAuthorizationSingletonTest.java
@Test public void testNoBinding() throws Exception { // Use reflection to construct a non-singleton version of SentryIndexAuthorizationSingleton // in order to get an instance without a binding Constructor ctor = SentryIndexAuthorizationSingleton.class.getDeclaredConstructor(String.class); ctor.setAccessible(true); SentryIndexAuthorizationSingleton nonSingleton = (SentryIndexAuthorizationSingleton) ctor.newInstance(""); doExpectUnauthorized(nonSingleton, null, null, "binding"); // test getUserName try {//from w ww . j av a2 s .co m nonSingleton.getUserName(null); Assert.fail("Expected Solr exception"); } catch (SolrException ex) { assertEquals(ex.code(), SolrException.ErrorCode.UNAUTHORIZED.code); } Collection<String> groups = nonSingleton.getRoles("junit"); assertEquals(null, groups); }
From source file:edu.wustl.bulkoperator.action.BulkHandler.java
/** * This method will be called to get fileItem. * @param fileItem fileItem//from ww w. java2 s .c om * @return FormFile FormFile * @throws BulkOperationException BulkOperationException */ public FormFile getFormFile(FileItem fileItem) throws BulkOperationException { try { Class parentClass = Class.forName("org.apache.struts.upload.CommonsMultipartRequestHandler"); Class childClass = parentClass.getDeclaredClasses()[0]; Constructor constructor = childClass.getConstructors()[0]; constructor.setAccessible(true); return (FormFile) constructor.newInstance(new Object[] { fileItem }); } catch (Exception exp) { ErrorKey errorkey = ErrorKey.getErrorKey("bulk.operation.request.param.error"); throw new BulkOperationException(errorkey, exp, ""); } }
From source file:com.qmetry.qaf.automation.data.ElementInteractor.java
private QAFExtendedWebElement getElement(String loc, Class<? extends QAFExtendedWebElement> eleClass) { try {/*from ww w. j ava2 s .c o m*/ Constructor<? extends QAFExtendedWebElement> con = eleClass.getConstructor(String.class); con.setAccessible(true); QAFExtendedWebElement ele = con.newInstance(loc); ele.waitForVisible(); return ele; } catch (Exception e) { throw new AutomationError(e); } }
From source file:net.nicholaswilliams.java.licensing.encryption.TestKeyFileUtilities.java
@Test public void testConstructionForbidden() throws IllegalAccessException, InstantiationException, NoSuchMethodException { Constructor<KeyFileUtilities> constructor = KeyFileUtilities.class.getDeclaredConstructor(); constructor.setAccessible(true); try {/* w w w.j a v a 2 s. c o m*/ constructor.newInstance(); fail("Expected exception java.lang.reflect.InvocationTargetException, but got no exception."); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); assertNotNull("Expected cause for InvocationTargetException, but got no cause.", cause); assertSame("Expected exception java.lang.RuntimeException, but got " + cause.getClass(), RuntimeException.class, cause.getClass()); assertEquals("The message was incorrect.", "This class cannot be instantiated.", cause.getMessage()); } }