本文整理汇总了Java中java.lang.reflect.Method.isAccessible方法的典型用法代码示例。如果您正苦于以下问题:Java Method.isAccessible方法的具体用法?Java Method.isAccessible怎么用?Java Method.isAccessible使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Method
的用法示例。
在下文中一共展示了Method.isAccessible方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
private Method getMethod(final Class<?> type, final String name, final Class<?>... parameterTypes) {
if (type == null) {
return null;
}
final FastField method = new FastField(type, name);
Method result = cache.get(method);
if (result == null) {
try {
result = type.getDeclaredMethod(name, parameterTypes);
if (!result.isAccessible()) {
result.setAccessible(true);
}
} catch (final NoSuchMethodException e) {
result = getMethod(type.getSuperclass(), name, parameterTypes);
}
cache.put(method, result);
}
return result;
}
示例2: findMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Locates a given method anywhere in the class inheritance hierarchy.
*
* @param instance an object to search the method into.
* @param name method name
* @param parameterTypes method parameter types
* @return a method object
* @throws NoSuchMethodException if the method cannot be located
*/
public static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
throws NoSuchMethodException {
for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
Method method = clazz.getDeclaredMethod(name, parameterTypes);
if (!method.isAccessible()) {
method.setAccessible(true);
}
return method;
} catch (NoSuchMethodException e) {
// ignore and search next
}
}
throw new NoSuchMethodException("Method "
+ name
+ " with parameters "
+ Arrays.asList(parameterTypes)
+ " not found in " + instance.getClass());
}
示例3: invokeMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
public static Object invokeMethod(Method method, Object methodReceiver, Object... methodParamValues) throws
InvocationTargetException, IllegalAccessException {
if (method != null) {
boolean acc = method.isAccessible();
if (!acc) {
method.setAccessible(true);
}
Object ret = method.invoke(methodReceiver, methodParamValues);
if (!acc) {
method.setAccessible(false);
}
return ret;
}
return null;
}
示例4: defineClass
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* 定义类
*
* @param loader 目标ClassLoader
* @param javaClassName 类名称
* @param classByteArray 类字节码数组
* @return 定义的类
* @throws InvocationTargetException 目标方法调用发生异常
* @throws IllegalAccessException 目标方法不可进入
*/
public static Class<?> defineClass(final ClassLoader loader,
final String javaClassName,
final byte[] classByteArray) throws InvocationTargetException, IllegalAccessException {
final Method defineClassMethod =
unCaughtGetClassDeclaredJavaMethod(ClassLoader.class, "defineClass", String.class, byte[].class, int.class, int.class);
synchronized (defineClassMethod) {
final boolean acc = defineClassMethod.isAccessible();
try {
defineClassMethod.setAccessible(true);
return (Class<?>) defineClassMethod.invoke(
loader,
javaClassName,
classByteArray,
0,
classByteArray.length
);
} finally {
defineClassMethod.setAccessible(acc);
}
}
}
示例5: getValue
import java.lang.reflect.Method; //导入方法依赖的package包/类
public static Object getValue(Object object, Method method, Object... params) throws ReflectorException, IllegalAccessException {
boolean originalAccessibility = method.isAccessible();
method.setAccessible(true);
try {
Object value = null;
if (params != null && params.length > 0) {
value = method.invoke(object, params);
} else {
value = method.invoke(object);
}
return value;
} catch (InvocationTargetException e) {
throw new ReflectorException("Unable to invoke selected method.", e);
} finally {
method.setAccessible(originalAccessibility);
}
}
示例6: findMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
private static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
throws NoSuchMethodException {
for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
Method method = clazz.getDeclaredMethod(name, parameterTypes);
if (!method.isAccessible()) {
method.setAccessible(true);
}
return method;
} catch (NoSuchMethodException e) {
// ignore and search next
}
}
throw new NoSuchMethodException("Method " + name + " with parameters " +
Arrays.asList(parameterTypes) + " not found in " + instance.getClass());
}
示例7: findMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Locates a given method anywhere in the class inheritance hierarchy.
*
* @param instance an object to search the method into.
* @param name method name
* @param parameterTypes method parameter types
* @return a method object
* @throws NoSuchMethodException if the method cannot be located
*/
private static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
throws NoSuchMethodException {
for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
Method method = clazz.getDeclaredMethod(name, parameterTypes);
if (!method.isAccessible()) {
method.setAccessible(true);
}
return method;
} catch (NoSuchMethodException e) {
// ignore and search next
}
}
throw new NoSuchMethodException("Method " + name + " with parameters " +
parameterTypes + " not found in " + instance.getClass());
}
示例8: makeOptionalFitsSystemWindows
import java.lang.reflect.Method; //导入方法依赖的package包/类
public static void makeOptionalFitsSystemWindows(View view) {
if (VERSION.SDK_INT >= 16) {
try {
Method method = view.getClass().getMethod("makeOptionalFitsSystemWindows", new Class[0]);
if (!method.isAccessible()) {
method.setAccessible(true);
}
method.invoke(view, new Object[0]);
} catch (NoSuchMethodException e) {
Log.d(TAG, "Could not find method makeOptionalFitsSystemWindows. Oh well...");
} catch (InvocationTargetException e2) {
Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e2);
} catch (IllegalAccessException e3) {
Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e3);
}
}
}
示例9: setProperty
import java.lang.reflect.Method; //导入方法依赖的package包/类
static void setProperty(PropertyDescriptor pd, Object value, Object source)
throws IllegalAccessException, InvocationTargetException, TransformException {
if (pd != null && pd.getWriteMethod() != null) {
Method m = pd.getWriteMethod();
if (!m.isAccessible())
m.setAccessible(true);
Class tClass = m.getParameterTypes()[0];
if (value == null || tClass.isAssignableFrom(value.getClass())) {
m.invoke(source, new Object[]{value});
if (log.isDebugEnabled())
log.debug("Set property '" + pd.getName() + '=' + value + "' on object '" + source.getClass().getName() + '\'');
} else if (DataTypeMapper.instance().isMappable(value.getClass(), tClass)) {
// See if there is a datatype mapper for these classes
value = DataTypeMapper.instance().map(value, tClass);
m.invoke(source, new Object[]{value});
if (log.isDebugEnabled())
log.debug("Translate+Set property '" + pd.getName() + '=' + value + "' on object '" + source.getClass().getName() + '\'');
} else {
// Data type mismatch
throw new TransformException(TransformException.DATATYPE_MISMATCH, source.getClass().getName() + '.' + m.getName(), tClass.getName(), value.getClass().getName());
}
} else {
TransformException me = new TransformException(TransformException.NO_SETTER, null,
pd == null ? "???" : pd.getName(), source.getClass().getName());
log.error(me.getLocalizedMessage());
throw me;
}
}
示例10: makeAccessible
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* 改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
*/
public static void makeAccessible(Method method) {
if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers()))
&& !method.isAccessible()) {
method.setAccessible(true);
}
}
示例11: getTreeItemForMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
private AssertionTreeItem getTreeItemForMethod(Object object, Method method) {
boolean accessible = method.isAccessible();
try {
method.setAccessible(true);
return new AssertionTreeItem(method.invoke(object, new Object[] {}), getPropertyName(method.getName()));
} catch (Throwable t) {
} finally {
method.setAccessible(accessible);
}
return null;
}
示例12: allMethodsThrowISE
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* All the methods of Disconnect should throw ISE.
* @throws Exception If something goes wrong.
*/
@Test
public void allMethodsThrowISE() throws Exception {
for(final Method method : Disconnected.class.getDeclaredMethods()) {
try {
if(!method.isAccessible()) {
continue;
}
Object[] params = new Object[method.getParameterCount()];
for(int i=0; i < method.getParameters().length; i++) {
if(method.getParameters()[i].getType().equals(int.class)) {
params[i] = 0;
} else if(method.getParameters()[i].getType().equals(boolean.class)) {
params[i] = false;
} else {
params[i] = null;
}
}
method.invoke(new Disconnected(), params);
Assert.fail("ISE should have been thrown by now!");
} catch (final InvocationTargetException ex) {
MatcherAssert.assertThat(
ex.getCause().getMessage(),
Matchers.equalTo(
"Not connected. Don't forget to "
+ "get a connected instance by calling #connect()"
)
);
}
}
}
示例13: makeAccessible
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* 改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
* @param method 要设置的方法
*/
public static void makeAccessible(Method method) {
if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers()))
&& !method.isAccessible()) {
method.setAccessible(true);
}
}
示例14: invokeMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
protected Object invokeMethod(Method method, Object[] args) throws Throwable {
try {
if (!method.isAccessible()) {
method.setAccessible(true);
}
return method.invoke(currentProxy.proxy, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
示例15: JavaMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
private JavaMethod(Method m) {
super( m.getParameterTypes(), m.getModifiers() );
this.method = m;
try {
if (!m.isAccessible())
m.setAccessible(true);
} catch (SecurityException s) {
}
}