当前位置: 首页>>代码示例>>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;未经允许,请勿转载。