本文整理了Java中java.lang.reflect.Constructor.setAccessible()
方法的一些代码示例,展示了Constructor.setAccessible()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Constructor.setAccessible()
方法的具体详情如下:
包路径:java.lang.reflect.Constructor
类名称:Constructor
方法名:setAccessible
暂无
代码示例来源:origin: libgdx/libgdx
public void setAccessible (boolean accessible) {
constructor.setAccessible(accessible);
}
代码示例来源:origin: libgdx/libgdx
public void setAccessible (boolean accessible) {
constructor.setAccessible(accessible);
}
代码示例来源:origin: redisson/redisson
public AccessibleInstantiator(Class<T> type) {
super(type);
if(constructor != null) {
constructor.setAccessible(true);
}
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* 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);
}
}
代码示例来源:origin: apache/incubator-shardingsphere
@SneakyThrows
private static Constructor<JdbcXAConnection> getH2JdbcXAConstructor() {
Constructor<JdbcXAConnection> result = JdbcXAConnection.class.getDeclaredConstructor(JdbcDataSourceFactory.class, Integer.TYPE, JdbcConnection.class);
result.setAccessible(true);
return result;
}
代码示例来源:origin: spring-projects/spring-framework
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);
}
}
代码示例来源:origin: redisson/redisson
/**
* {@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;
}
代码示例来源:origin: redisson/redisson
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);
}
代码示例来源:origin: redisson/redisson
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);
}
代码示例来源:origin: alibaba/druid
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);
}
}
代码示例来源:origin: spring-projects/spring-framework
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;
}
代码示例来源:origin: google/guava
/**
* 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());
}
代码示例来源:origin: square/retrofit
@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);
}
代码示例来源:origin: spring-projects/spring-framework
@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);
}
}
}
代码示例来源:origin: apache/incubator-dubbo
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]);
}
}
}
代码示例来源:origin: jenkinsci/jenkins
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);
}
}
代码示例来源:origin: redisson/redisson
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());
}
代码示例来源:origin: redisson/redisson
public SunReflectionFactoryInstantiator(Class<T> type) {
Constructor<Object> javaLangObjectConstructor = getJavaLangObjectConstructor();
mungedConstructor = SunReflectionFactoryHelper.newConstructorForSerialization(
type, javaLangObjectConstructor);
mungedConstructor.setAccessible(true);
}
代码示例来源:origin: prestodb/presto
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);
}
}
代码示例来源:origin: ReactiveX/RxJava
/**
* 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;
}
}
内容来源于网络,如有侵权,请联系作者删除!