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:org.solenopsis.checkstyle.PackageObjectFactory.java
/** * Creates a new instance of a named class. * @param className the name of the class to instantiate. * @return the {@code Object} created by loader or null. *//*from ww w . java 2s. c o m*/ private Object createObject(String className) { Object instance = null; try { final Class<?> clazz = Class.forName(className, true, moduleClassLoader); final Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(); declaredConstructor.setAccessible(true); instance = declaredConstructor.newInstance(); } catch (final ReflectiveOperationException | NoClassDefFoundError exception) { LOG.debug(IGNORING_EXCEPTION_MESSAGE, exception); } return instance; }
From source file:org.springframework.data.neo4j.support.AbstractConstructorEntityInstantiator.java
protected <T> Constructor<T> getDeclaredConstructor(Class<T> c) { try {/*from w w w . j ava 2 s . com*/ final Constructor<T> declaredConstructor = c.getDeclaredConstructor(); declaredConstructor.setAccessible(true); return declaredConstructor; } catch (NoSuchMethodException e) { return null; } }
From source file:com.aionlightning.commons.services.CronServiceTest.java
@BeforeClass public void init() throws Exception { Constructor<CronService> constructor = CronService.class.getDeclaredConstructor(); constructor.setAccessible(true); cronService = constructor.newInstance(); cronService.init(CurrentThreadRunnableRunner.class); }
From source file:com.puppycrawl.tools.checkstyle.PackageObjectFactory.java
/** * Creates a new instance of a named class. * @param className the name of the class to instantiate. * @return the {@code Object} created by loader. * @throws CheckstyleException if an error occurs. *///from ww w . ja va2s . com private Object createObject(String className) throws CheckstyleException { try { final Class<?> clazz = Class.forName(className, true, moduleClassLoader); final Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(); declaredConstructor.setAccessible(true); return declaredConstructor.newInstance(); } catch (final ReflectiveOperationException exception) { throw new CheckstyleException("Unable to find class for " + className, exception); } }
From source file:com.buaa.cfs.fs.AbstractFileSystem.java
/** * Create an object for the given class and initialize it from conf. * * @param theClass class of which an object is created * @param conf Configuration//from www. ja v a 2 s . co m * * @return a new object */ @SuppressWarnings("unchecked") static <T> T newInstance(Class<T> theClass, URI uri, Configuration conf) { T result; try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(URI_CONFIG_ARGS); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(uri, conf); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.nebula.framework.workflow.WorkflowInstance.java
private boolean createInstanceWithNoArgumentConstructor() { try {//from ww w .j av a2s . c om Constructor constructor = processDefinition.getWorkflowImplementation().getDeclaredConstructor(); constructor.setAccessible(true); instance = constructor.newInstance(); return true; } catch (Exception e) { log.error("Failed to create instance", e); throw new RuntimeException("Failed to create instance"); } }
From source file:org.onosproject.yang.compiler.utils.io.impl.YangIoUtilsTest.java
/** * A private constructor is tested.//from w ww.j a v a 2 s . co m * * @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 = { YangIoUtils.class }; for (Class<?> clazz : classesToConstruct) { Constructor<?> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); assertThat(null, not(constructor.newInstance())); } }
From source file:com.adaptris.util.TestURLString.java
private URLString newInstance() throws Exception { Constructor[] ctors = URLString.class.getDeclaredConstructors(); Constructor ctor = noArg(ctors); ctor.setAccessible(true); return (URLString) ctor.newInstance(); }
From source file:com.glaf.core.util.ReflectUtils.java
public static Object newInstance(final Constructor<?> cstruct, final Object[] args) { boolean flag = cstruct.isAccessible(); try {//from ww w . java2s.com cstruct.setAccessible(true); Object result = cstruct.newInstance(args); return result; } catch (Exception e) { throw new RuntimeException(e); } finally { cstruct.setAccessible(flag); } }
From source file:com.puppycrawl.tools.checkstyle.PackageNamesLoaderTest.java
@Test @SuppressWarnings("unchecked") public void testPackagesWithDots() throws Exception { Constructor<PackageNamesLoader> constructor = PackageNamesLoader.class.getDeclaredConstructor(); constructor.setAccessible(true); PackageNamesLoader loader = constructor.newInstance(); Attributes attributes = mock(Attributes.class); when(attributes.getValue("name")).thenReturn("coding."); loader.startElement("", "", "package", attributes); loader.endElement("", "", "package"); Field field = PackageNamesLoader.class.getDeclaredField("packageNames"); field.setAccessible(true);//from w ww.j a va 2 s .c om Set<String> list = (Set<String>) field.get(loader); assertEquals("coding.", list.iterator().next()); }