本文整理汇总了Java中java.lang.reflect.Constructor.setAccessible方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.setAccessible方法的具体用法?Java Constructor.setAccessible怎么用?Java Constructor.setAccessible使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Constructor
的用法示例。
在下文中一共展示了Constructor.setAccessible方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private final Constructor<?> generateConstructor(Class<?> cl,
Constructor<?> constructorToCall) {
ConstructorAccessor acc = new MethodAccessorGenerator().
generateSerializationConstructor(cl,
constructorToCall.getParameterTypes(),
constructorToCall.getExceptionTypes(),
constructorToCall.getModifiers(),
constructorToCall.getDeclaringClass());
Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
constructorToCall.getParameterTypes(),
constructorToCall.getExceptionTypes(),
constructorToCall.getModifiers(),
langReflectAccess().
getConstructorSlot(constructorToCall),
langReflectAccess().
getConstructorSignature(constructorToCall),
langReflectAccess().
getConstructorAnnotations(constructorToCall),
langReflectAccess().
getConstructorParameterAnnotations(constructorToCall));
setConstructorAccessor(c, acc);
c.setAccessible(true);
return c;
}
示例2: newInstance
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static Object newInstance(Class clazz, Class[] constructors_class, Object[] constructors_args) {
Object instance = null;
try {
Constructor constructor = clazz.getDeclaredConstructor(constructors_class);
constructor.setAccessible(true);
instance = constructor.newInstance(constructors_args);
} catch (NoSuchMethodException e) {
JLog.i("clf", "NoSuchMethodException..e=" + e.getMessage());
} catch (IllegalArgumentException e2) {
JLog.i("clf", "IllegalArgumentException..e=" + e2.getMessage());
} catch (InstantiationException e3) {
JLog.i("clf", "InstantiationException..e=" + e3.getMessage());
} catch (IllegalAccessException e4) {
JLog.i("clf", "IllegalAccessException..e=" + e4.getMessage());
} catch (InvocationTargetException e5) {
JLog.i("clf", "InvocationTargetException..e=" + e5.getMessage());
}
return instance;
}
示例3: createView
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
示例4: testExecutingConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public void testExecutingConstructor() throws Exception {
ClassLoader l = new L();
Class<?> c = l.loadClass(CAPI.class.getName());
Member m = c.getDeclaredConstructor(int.class);
Constructor ctor = (Constructor)m;
ctor.setAccessible(true);
Object o = ctor.newInstance(5);
assertSame(c, o.getClass());
assertTrue("Invalid API superclass", Superclazz.class.isInstance(o));
assertEquals("@ConstructorDelegate method did not execute", 5, ((Superclazz)o).val);
Field f = o.getClass().getField("otherVal");
Object v = f.get(o);
assertEquals("Patched API constructor did not execute", v, 1);
}
示例5: addMethodProxy
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private void addMethodProxy(Class<?> hookType) {
try {
Constructor<?> constructor = hookType.getDeclaredConstructors()[0];
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
MethodProxy methodProxy;
if (constructor.getParameterTypes().length == 0) {
methodProxy = (MethodProxy) constructor.newInstance();
} else {
methodProxy = (MethodProxy) constructor.newInstance(this);
}
mInvocationStub.addMethodProxy(methodProxy);
} catch (Throwable e) {
throw new RuntimeException("Unable to instance Hook : " + hookType + " : " + e.getMessage());
}
}
示例6: get
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static Block get(int id, Integer meta, Position pos) {
Block block;
try {
Class<? extends Block> c = list[id];
if (c != null) {
Constructor<? extends Block> constructor = c.getDeclaredConstructor(int.class);
constructor.setAccessible(true);
block = (Block) constructor.newInstance(meta);
} else {
block = new BlockUnknown(id, meta);
}
} catch (Exception e) {
block = new BlockUnknown(id, meta);
}
if (pos != null) {
block.x = pos.x;
block.y = pos.y;
block.z = pos.z;
block.level = pos.level;
}
return block;
}
示例7:
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* isChangedメソッドでMemcachedからキャッシュを取得できた場合falseを返すこと.
* @throws Exception テスト中の例外
*/
@SuppressWarnings("unchecked")
@Test
public void isChangedメソッドでMemcachedからキャッシュを取得できた場合falseを返すこと() throws Exception {
String nodeId = "nodeId";
Map<String, Object> data = new HashMap<String, Object>();
data.put("testKey", "testValue");
// MemcachedClientのモックを作成(getしたときにMemchachedClientExceptionをthrowするモック)
Constructor<MemcachedClient> c = MemcachedClient.class.getDeclaredConstructor();
c.setAccessible(true);
MemcachedClient mockMemcachedClient = Mockito.spy(c.newInstance());
Mockito.doReturn(data).when(mockMemcachedClient).get(Mockito.anyString(), Mockito.any(Class.class));
// UserDataSchemaCacheのモックを作成(MemcachedClientのモックを使用するモック)
PowerMockito.spy(UserDataSchemaCache.class);
PowerMockito.when(UserDataSchemaCache.class, "getMcdClient").thenReturn(mockMemcachedClient);
// キャッシュの設定を有効にする
PowerMockito.spy(PersoniumUnitConfig.class);
PowerMockito.when(PersoniumUnitConfig.class, "isSchemaCacheEnabled").thenReturn(true);
// isChangedメソッドのテスト
boolean isChanged = UserDataSchemaCache.isChanged(nodeId, data);
assertFalse(isChanged);
}
示例8: jskLogTest
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@Test(expected = JSKException.class)
public void jskLogTest() throws Exception {
Constructor<JSKLog> constructor = JSKLog.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
try {
constructor.setAccessible(true);
constructor.newInstance();
}
catch(Exception exception) {
throw new JSKException(ERROR_UTILITY_CLASS);
}
}
示例9: createPipeStream
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private Pair<InputStream, OutputStream> createPipeStream() throws Exception {
Class<?> pipeStreamClass = Class.forName("jdk.jshell.execution.PipeInputStream");
Constructor<?> c = pipeStreamClass.getDeclaredConstructor();
c.setAccessible(true);
Object pipeStream = c.newInstance();
Method createOutputStream = pipeStreamClass.getDeclaredMethod("createOutput");
createOutputStream.setAccessible(true);
return Pair.of((InputStream) pipeStream, (OutputStream) createOutputStream.invoke(pipeStream));
}
示例10: getConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private Constructor<T> getConstructor(Class[] classes) throws NoSuchMethodException {
Constructor<T> constructor = type.getDeclaredConstructor(classes);
if (!Modifier.isPublic(constructor.getModifiers())) {
constructor.setAccessible(true);
}
return constructor;
}
示例11: allocateDirectBuffer
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Uses internal JDK APIs to allocate a DirectByteBuffer while ignoring the JVM's
* MaxDirectMemorySize limit (the default limit is too low and we do not want to require users
* to increase it).
*/
@SuppressWarnings("unchecked")
public static ByteBuffer allocateDirectBuffer(int size) {
try {
Class cls = Class.forName("java.nio.DirectByteBuffer");
Constructor constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE);
constructor.setAccessible(true);
Field cleanerField = cls.getDeclaredField("cleaner");
cleanerField.setAccessible(true);
final long memory = allocateMemory(size);
ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size);
Cleaner cleaner = Cleaner.create(buffer, new Runnable() {
@Override
public void run() {
freeMemory(memory);
}
});
cleanerField.set(buffer, cleaner);
return buffer;
} catch (Exception e) {
throwException(e);
}
throw new IllegalStateException("unreachable");
}
示例12: newInstance
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
static PreferenceManager newInstance(Activity activity, int firstRequestCode) {
try {
Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
c.setAccessible(true);
return c.newInstance(activity, firstRequestCode);
} catch (Exception e) {
Log.w(TAG, "Couldn't call constructor PreferenceManager by reflection", e);
}
return null;
}
示例13: instantiateException
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private IOException instantiateException(Class<? extends IOException> cls)
throws Exception {
Constructor<? extends IOException> cn = cls.getConstructor(String.class);
cn.setAccessible(true);
String firstLine = this.getMessage();
int eol = firstLine.indexOf('\n');
if (eol>=0) {
firstLine = firstLine.substring(0, eol);
}
IOException ex = cn.newInstance(firstLine);
ex.initCause(this);
return ex;
}
示例14: load
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@Override
public CachedConstructor load(final Class<?> type) throws Exception {
try {
validateType(type);
Constructor<?> constructor = selectConstructor(type);
constructor.setAccessible(true);
return CachedConstructor.of(constructor);
} catch (Throwable e) {
return CachedConstructor.of(e);
}
}
示例15: jskStringTest
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@Test(expected = JSKException.class)
public void jskStringTest() throws Exception {
Constructor<JSKString> constructor = JSKString.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
try {
constructor.setAccessible(true);
constructor.newInstance();
}
catch(Exception exception) {
throw new JSKException(ERROR_UTILITY_CLASS);
}
}