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


Java Method.invoke方法代碼示例

本文整理匯總了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;
    }
}
 
開發者ID:kawasima,項目名稱:goku-jdbc,代碼行數:19,代碼來源:GokuInvocationHandler.java

示例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);
	}
}
 
開發者ID:crazysnailboy,項目名稱:Halloween,代碼行數:23,代碼來源:ReflectionUtils.java

示例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;
            }
        }
    }
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:20,代碼來源:CloseableHttpResponseProxy.java

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

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

}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:21,代碼來源:InitializerTest.java

示例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);
		}
	}
}
 
開發者ID:youngMen1,項目名稱:JAVA-,代碼行數:28,代碼來源:Request2ModelUtil.java

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

示例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.
		}
	}
}
 
開發者ID:eclipse,項目名稱:scanning,代碼行數:27,代碼來源:Bean.java

示例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;
}
 
開發者ID:DomKing,項目名稱:springbootWeb,代碼行數:32,代碼來源:MapConverter.java

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

示例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
    }
}
 
開發者ID:lucko,項目名稱:helper,代碼行數:12,代碼來源:HelperEventListener.java

示例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;
}
 
開發者ID:ximsfei,項目名稱:Android-skin-support,代碼行數:21,代碼來源:SkinCompatManager.java

示例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);
    }
}
 
開發者ID:alibaba,項目名稱:jvm-sandbox,代碼行數:14,代碼來源:SandboxReflectUtils.java

示例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 */);
    }
}
 
開發者ID:google,項目名稱:mobly-bundled-snippets,代碼行數:12,代碼來源:AudioSnippet.java

示例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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:34,代碼來源:IndexerCache.java


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