本文整理匯總了Java中java.lang.reflect.Method.invoke方法的典型用法代碼示例。如果您正苦於以下問題:Java Method.invoke方法的具體用法?Java Method.invoke怎麽用?Java Method.invoke使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Method
的用法示例。
在下文中一共展示了Method.invoke方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: invoke
import java.lang.reflect.Method; //導入方法依賴的package包/類
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Object ret = method.invoke(orig, args);
Class<?> returnType = method.getReturnType();
if (WRAP_TARGET_CLASSES.contains(returnType)) {
ret = Proxy.newProxyInstance(returnType.getClassLoader(), new Class[]{returnType},
new GokuInvocationHandler(ret));
}
return ret;
} catch (InvocationTargetException ex) {
Throwable targetEx = ex.getTargetException();
if (targetEx instanceof SQLException) {
targetEx = new GokuSQLException((SQLException) targetEx);
}
throw targetEx;
}
}
示例2: invokeMethod
import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
* Invoke Method methodToAccess on an Object instance.
*/
public static final <T, E> T invokeMethod(final Method methodToAccess, E instance, Object... args)
{
try
{
if (methodToAccess.getReturnType().equals(Void.TYPE))
{
methodToAccess.invoke(instance, args);
return null;
}
else
{
return (T)methodToAccess.invoke(instance, args);
}
}
catch (Exception ex)
{
throw new UnableToInvokeMethodException(methodToAccess, ex);
}
}
示例3: invoke
import java.lang.reflect.Method; //導入方法依賴的package包/類
public Object invoke(
final Object proxy, final Method method, final Object[] args) throws Throwable {
final String mname = method.getName();
if (mname.equals("close")) {
close();
return null;
} else {
try {
return method.invoke(original, args);
} catch (final InvocationTargetException ex) {
final Throwable cause = ex.getCause();
if (cause != null) {
throw cause;
} else {
throw ex;
}
}
}
}
示例4: play_damageEffect
import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
* Play effect of player getting damaged for all players.
*/
@SuppressWarnings("all")
public void play_damageEffect() {
Class craftPlayer = ReflectionUtils.getClazz(ReflectionUtils.cbclasses, "CraftPlayer");
Class entityPlayer = ReflectionUtils.getClazz(ReflectionUtils.nmsclasses, "EntityPlayer");
Class playerConnection = ReflectionUtils.getClazz(ReflectionUtils.nmsclasses, "PlayerConnection");
Class packet = ReflectionUtils.getClazz(ReflectionUtils.nmsclasses, "PacketPlayOutEntityStatus");
for (Player p1 : Bukkit.getOnlinePlayers()) {
try {
Object nmsPlayer = craftPlayer.getDeclaredMethod("getHandle").invoke(craftPlayer.cast(p1));
Object conn = entityPlayer.getDeclaredField("playerConnection").get(nmsPlayer);
Method sendPacket = playerConnection.getDeclaredMethod("sendPacket", ReflectionUtils.getClazz(ReflectionUtils.nmsclasses, "Packet"));
sendPacket.invoke(conn, packet.getConstructor(entityPlayer, byte.class).newInstance(craftPlayer.getDeclaredMethod("getHandle").invoke(craftPlayer.cast(bukkit)), (byte) 2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例5: testLoggingWrongFileType
import java.lang.reflect.Method; //導入方法依賴的package包/類
@Test
public void testLoggingWrongFileType() throws Exception {
File log4jFile = createLog4jFile(LOG4J_CONFIG1);
try {
// Set path of log4j properties
String log4jPath = log4jFile.getCanonicalPath();
setSysSetting(log4jPath);
// Invoke "private" method :)
Method method = testElm.getClass().getDeclaredMethod(
"postConstruct");
method.setAccessible(true);
method.invoke(testElm);
} finally {
log4jFile.delete();
resetSysSetting();
}
}
示例6: covertObj
import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
* 根據傳遞的參數修改數據
*
* @param o
* @param parameterMap
*/
public static final void covertObj(Object o, Map<String, String[]> parameterMap) {
Class<?> clazz = o.getClass();
Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String[]> entry = iterator.next();
String key = entry.getKey().trim();
String value = entry.getValue()[0].trim();
try {
Method method = setMethod(key, clazz);
if (method != null) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (method != null) {
Object[] param_value = new Object[] { TypeParseUtil.convert(value, parameterTypes[0], null) };
method.invoke(o, param_value);
}
}
} catch (Exception e) {
logger.error("", e);
}
}
}
示例7: call
import java.lang.reflect.Method; //導入方法依賴的package包/類
public static Object call(ReflectorMethod p_call_0_, Object... p_call_1_)
{
try
{
Method method = p_call_0_.getTargetMethod();
if (method == null)
{
return null;
}
else
{
Object object = method.invoke((Object)null, p_call_1_);
return object;
}
}
catch (Throwable throwable)
{
handleException(throwable, (Object)null, p_call_0_, p_call_1_);
return null;
}
}
示例8: init
import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
* Method reflects the fields into the map of field values.
* @param theBean
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
private void init(Object theBean) throws Exception {
Collection<FieldValue> fields = FieldUtils.getModelFields(theBean);
for (FieldValue field : fields) {
String fieldName = field.getName();
try {
String getterName = getGetterName(fieldName);
Method getter = theBean.getClass().getMethod(getterName);
Object value = getter.invoke(theBean);
if (value!=null) {
data.put(fieldName, value);
}
} catch (NoSuchMethodException nsme) {
continue; // It is legal to have a field with no getter value.
}
}
}
示例9: toMap
import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
* 將一個 JavaBean 對象轉化為一個 Map
*
* @param bean 要轉化的JavaBean 對象
* @return 轉化出來的 Map 對象
*/
public static Map<String, Object> toMap(Object bean) {
Map<String, Object> returnMap = null;
try {
Class<?> type = bean.getClass();
returnMap = new HashMap<>();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, null);
}
}
}
} catch (Exception e) {
returnMap = null;
logger.error(ExceptionUtil.parseException(e));
}
return returnMap;
}
示例10: call
import java.lang.reflect.Method; //導入方法依賴的package包/類
@Override
public Object call(Object who, Method method, Object... args) throws Throwable {
boolean slice = ParceledListSliceCompat.isReturnParceledListSlice(method);
int userId = VUserHandle.myUserId();
List<ResolveInfo> appResult = VPackageManager.get().queryIntentContentProviders((Intent) args[0], (String) args[1],
(Integer) args[2], userId);
Object _hostResult = method.invoke(who, args);
List<ResolveInfo> hostResult = slice ? ParceledListSlice.getList.call(_hostResult)
: (List) _hostResult;
if (hostResult != null) {
Iterator<ResolveInfo> iterator = hostResult.iterator();
while (iterator.hasNext()) {
ResolveInfo info = iterator.next();
if (info == null || info.providerInfo == null || !isVisiblePackage(info.providerInfo.applicationInfo)) {
iterator.remove();
}
}
appResult.addAll(hostResult);
}
if (ParceledListSliceCompat.isReturnParceledListSlice(method)) {
return ParceledListSliceCompat.create(appResult);
}
return appResult;
}
示例11: unregisterListener
import java.lang.reflect.Method; //導入方法依賴的package包/類
@SuppressWarnings("JavaReflectionMemberAccess")
private static void unregisterListener(Class<? extends Event> eventClass, Listener listener) {
try {
// unfortunately we can't cache this reflect call, as the method is static
Method getHandlerListMethod = eventClass.getMethod("getHandlerList");
HandlerList handlerList = (HandlerList) getHandlerListMethod.invoke(null);
handlerList.unregister(listener);
} catch (Throwable t) {
// ignored
}
}
示例12: getSkinResources
import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
* 獲取皮膚包資源{@link Resources}.
*
* @param skinPkgPath sdcard中皮膚包路徑.
* @return
*/
@Nullable
public Resources getSkinResources(String skinPkgPath) {
try {
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPath.invoke(assetManager, skinPkgPath);
Resources superRes = mAppContext.getResources();
return new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例13: unCaughtInvokeMethod
import java.lang.reflect.Method; //導入方法依賴的package包/類
public static <T> T unCaughtInvokeMethod(final Method method,
final Object target,
final Object... parameterArray) {
final boolean isAccessible = method.isAccessible();
try {
method.setAccessible(true);
return (T) method.invoke(target, parameterArray);
} catch (Throwable e) {
throw new UnCaughtException(e);
} finally {
method.setAccessible(isAccessible);
}
}
示例14: muteAll
import java.lang.reflect.Method; //導入方法依賴的package包/類
@Rpc(description = "Silences all audio streams.")
public void muteAll() throws Exception {
/* Get numStreams from AudioSystem through reflection. If for some reason this fails,
* calling muteAll will throw. */
Class<?> audioSystem = Class.forName("android.media.AudioSystem");
Method getNumStreamTypes = audioSystem.getDeclaredMethod("getNumStreamTypes");
int numStreams = (int) getNumStreamTypes.invoke(null /* instance */);
for (int i = 0; i < numStreams; i++) {
mAudioManager.setStreamVolume(i /* audio stream */, 0 /* value */, 0 /* flags */);
}
}
示例15: C
import java.lang.reflect.Method; //導入方法依賴的package包/類
public C() {
Map<String, Integer> order = null;
Method getMIMETypesMethod = null;
try {
getMIMETypesMethod = MIMEResolver.class.getDeclaredMethod("getMIMETypes"); //NOI18N
} catch (Exception ex) {
// ignore
}
if (getMIMETypesMethod != null) {
Collection<? extends MIMEResolver> resolvers = Lookup.getDefault().lookupAll(MIMEResolver.class);
order = new HashMap<String, Integer>();
int idx = 0;
for(MIMEResolver r : resolvers) {
String [] mimeTypes = null;
try {
mimeTypes = (String []) getMIMETypesMethod.invoke(r);
} catch (Exception e) {
// ignore;
}
if (mimeTypes != null) {
for(String mimeType : mimeTypes) {
order.put(mimeType, idx);
}
}
idx++;
}
}
orderByResolvers = order != null && order.size() > 0 ? order : null;
}