本文整理匯總了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) {
}
}