public void setAccessible (boolean accessible) { constructor.setAccessible(accessible); }
public void setAccessible (boolean accessible) { constructor.setAccessible(accessible); }
public AccessibleInstantiator(Class<T> type) { super(type); if(constructor != null) { constructor.setAccessible(true); } } }
/** * Make the given constructor accessible, explicitly setting it accessible * if necessary. The {@code setAccessible(true)} method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active). * @param ctor the constructor to make accessible * @see java.lang.reflect.Constructor#setAccessible */ @SuppressWarnings("deprecation") // on JDK 9 public static void makeAccessible(Constructor<?> ctor) { if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { ctor.setAccessible(true); } }
@SneakyThrows private static Constructor<JdbcXAConnection> getH2JdbcXAConstructor() { Constructor<JdbcXAConnection> result = JdbcXAConnection.class.getDeclaredConstructor(JdbcDataSourceFactory.class, Integer.TYPE, JdbcConnection.class); result.setAccessible(true); return result; }
public static Constructor getConstructor(Class type, Class[] parameterTypes) { try { Constructor constructor = type.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor; } catch (NoSuchMethodException e) { throw new CodeGenerationException(e); } }
/** * {@inheritDoc} */ public Dispatcher run() { // This is safe even in a multi-threaded environment as all threads set the instances accessible before invoking any methods. // By always setting accessibility, the security manager is always triggered if this operation was illegal. methodInfo.setAccessible(true); getName.setAccessible(true); getDeclaringClass.setAccessible(true); getReferenceKind.setAccessible(true); getMethodType.setAccessible(true); return this; }
private <T> T instantiate(Class<T> cls, Object id) { try { for (Constructor<?> constructor : cls.getDeclaredConstructors()) { if (constructor.getParameterTypes().length == 0) { constructor.setAccessible(true); return (T) constructor.newInstance(); } } } catch (Exception e) { throw new IllegalArgumentException(e); } throw new IllegalArgumentException("Can't find default constructor for " + cls); }
private <T> T instantiate(Class<T> cls, Object id) { try { for (Constructor<?> constructor : cls.getDeclaredConstructors()) { if (constructor.getParameterTypes().length == 0) { constructor.setAccessible(true); return (T) constructor.newInstance(); } } } catch (Exception e) { throw new IllegalArgumentException(e); } throw new IllegalArgumentException("Can't find default constructor for " + cls); }
public static XAConnection createXAConnection(Object factory, Connection physicalConn) throws SQLException { try { if (constructor == null) { constructor = JdbcXAConnection.class.getDeclaredConstructor(JdbcDataSourceFactory.class, int.class, JdbcConnection.class); constructor.setAccessible(true); } int id = getNextId(XA_DATA_SOURCE); return constructor.newInstance(factory, id, physicalConn); } catch (Exception e) { throw new SQLException("createXAConnection error", e); } }
public WebSphereClassLoaderAdapter(ClassLoader classLoader) { Class<?> wsCompoundClassLoaderClass; try { wsCompoundClassLoaderClass = classLoader.loadClass(COMPOUND_CLASS_LOADER_NAME); this.cloneConstructor = classLoader.getClass().getDeclaredConstructor(wsCompoundClassLoaderClass); this.cloneConstructor.setAccessible(true); this.wsPreProcessorClass = classLoader.loadClass(CLASS_PRE_PROCESSOR_NAME); this.addPreDefinePlugin = classLoader.getClass().getMethod("addPreDefinePlugin", this.wsPreProcessorClass); this.transformerList = wsCompoundClassLoaderClass.getDeclaredField(PLUGINS_FIELD); this.transformerList.setAccessible(true); } catch (Throwable ex) { throw new IllegalStateException( "Could not initialize WebSphere LoadTimeWeaver because WebSphere API classes are not available", ex); } if (!wsCompoundClassLoaderClass.isInstance(classLoader)) { throw new IllegalArgumentException( "ClassLoader must be an instance of [" + COMPOUND_CLASS_LOADER_NAME + "]: " + classLoader); } this.classLoader = classLoader; }
/** * Verifies that {@code ctor} produces a {@link NullPointerException} or {@link * UnsupportedOperationException} when the parameter in position {@code paramIndex} is null. If * this parameter is marked nullable, this method does nothing. */ public void testConstructorParameter(Constructor<?> ctor, int paramIndex) { ctor.setAccessible(true); testParameter(null, Invokable.from(ctor), paramIndex, ctor.getDeclaringClass()); }
@Override Object invokeDefaultMethod(Method method, Class<?> declaringClass, Object object, @Nullable Object... args) throws Throwable { // Because the service interface might not be public, we need to use a MethodHandle lookup // that ignores the visibility of the declaringClass. Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, int.class); constructor.setAccessible(true); return constructor.newInstance(declaringClass, -1 /* trusted */) .unreflectSpecial(method, declaringClass) .bindTo(object) .invokeWithArguments(args); }
@SuppressWarnings("deprecation") // on JDK 9 public static Object newInstance(final Constructor cstruct, final Object[] args) { boolean flag = cstruct.isAccessible(); try { if (!flag) { cstruct.setAccessible(true); } Object result = cstruct.newInstance(args); return result; } catch (InstantiationException e) { throw new CodeGenerationException(e); } catch (IllegalAccessException e) { throw new CodeGenerationException(e); } catch (InvocationTargetException e) { throw new CodeGenerationException(e.getTargetException()); } finally { if (!flag) { cstruct.setAccessible(flag); } } }
public BeanDeserializer(Class cl) { _type = cl; _methodMap = getMethodMap(cl); _readResolve = getReadResolve(cl); Constructor[] constructors = cl.getConstructors(); int bestLength = Integer.MAX_VALUE; for (int i = 0; i < constructors.length; i++) { if (constructors[i].getParameterTypes().length < bestLength) { _constructor = constructors[i]; bestLength = _constructor.getParameterTypes().length; } } if (_constructor != null) { _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]); } } }
private void swap(StandardOutputStream stdout) throws IOException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException { // duplicate the OS file descriptor and create FileOutputStream around it int out = GNUCLibrary.LIBC.dup(1); if (out<0) throw new IOException("Failed to dup(1)"); Constructor<FileDescriptor> c = FileDescriptor.class.getDeclaredConstructor(int.class); c.setAccessible(true); FileOutputStream fos = new FileOutputStream(c.newInstance(out)); // swap it into channel so that it'll use the new file descriptor stdout.swap(fos); // close fd=1 (stdout) and duplicate fd=2 (stderr) into fd=1 (stdout) GNUCLibrary.LIBC.close(1); GNUCLibrary.LIBC.dup2(2,1); } }
protected Object createInstance(ScalarNode node, CompactData data) throws Exception { Class<?> clazz = getClassForName(data.getPrefix()); Class<?>[] args = new Class[data.getArguments().size()]; for (int i = 0; i < args.length; i++) { // assume all the arguments are Strings args[i] = String.class; } java.lang.reflect.Constructor<?> c = clazz.getDeclaredConstructor(args); c.setAccessible(true); return c.newInstance(data.getArguments().toArray()); }
public SunReflectionFactoryInstantiator(Class<T> type) { Constructor<Object> javaLangObjectConstructor = getJavaLangObjectConstructor(); mungedConstructor = SunReflectionFactoryHelper.newConstructorForSerialization( type, javaLangObjectConstructor); mungedConstructor.setAccessible(true); }
private static Constructor<? extends RecordWriter> getOrcWriterConstructor() { try { Constructor<? extends RecordWriter> constructor = OrcOutputFormat.class.getClassLoader() .loadClass(ORC_RECORD_WRITER) .asSubclass(RecordWriter.class) .getDeclaredConstructor(Path.class, WriterOptions.class); constructor.setAccessible(true); return constructor; } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } }
/** * Validates that the given class, when forcefully instantiated throws * an IllegalArgumentException("No instances!") exception. * @param clazz the class to test, not null */ public static void checkUtilityClass(Class<?> clazz) { try { Constructor<?> c = clazz.getDeclaredConstructor(); c.setAccessible(true); try { c.newInstance(); fail("Should have thrown InvocationTargetException(IllegalStateException)"); } catch (InvocationTargetException ex) { assertEquals("No instances!", ex.getCause().getMessage()); } } catch (Exception ex) { AssertionError ae = new AssertionError(ex.toString()); ae.initCause(ex); throw ae; } }