當前位置: 首頁>>代碼示例>>Java>>正文


Java Method.isAccessible方法代碼示例

本文整理匯總了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;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:21,代碼來源:SerializationMethodInvoker.java

示例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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:32,代碼來源:ShareReflectUtil.java

示例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;
}
 
開發者ID:wangyupeng1-iri,項目名稱:springreplugin,代碼行數:22,代碼來源:ReflectUtils.java

示例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);
        }
    }

}
 
開發者ID:alibaba,項目名稱:jvm-sandbox,代碼行數:35,代碼來源:SandboxReflectUtils.java

示例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);
    }
}
 
開發者ID:iFanie,項目名稱:ChocoPie,代碼行數:21,代碼來源:Reflector.java

示例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());
}
 
開發者ID:coding-dream,項目名稱:TPlayer,代碼行數:21,代碼來源:DelegateApplication64Bit.java

示例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());
}
 
開發者ID:inferjay,項目名稱:r8,代碼行數:30,代碼來源:MultiDex.java

示例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);
        }
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:18,代碼來源:ViewUtils.java

示例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;
    }
}
 
開發者ID:jaffa-projects,項目名稱:jaffa-framework,代碼行數:29,代碼來源:TransformerUtils.java

示例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);
	}
}
 
開發者ID:zhangjunfang,項目名稱:util,代碼行數:10,代碼來源:ClassUtil.java

示例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;
}
 
開發者ID:jalian-systems,項目名稱:marathonv5,代碼行數:12,代碼來源:AssertionTreeView.java

示例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()"
                )
            );
        }
    }
}
 
開發者ID:amihaiemil,項目名稱:comdor,代碼行數:35,代碼來源:DisconnectedTestCase.java

示例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);
	}
}
 
開發者ID:javahaohao,項目名稱:gen_code,代碼行數:11,代碼來源:AnalysisObject.java

示例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();
  }
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:11,代碼來源:RetryInvocationHandler.java

示例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) {
	}
}
 
開發者ID:nekocode,項目名稱:Hubs,代碼行數:10,代碼來源:JavaMethod.java


注:本文中的java.lang.reflect.Method.isAccessible方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。