本文整理汇总了Java中com.esotericsoftware.reflectasm.MethodAccess.invoke方法的典型用法代码示例。如果您正苦于以下问题:Java MethodAccess.invoke方法的具体用法?Java MethodAccess.invoke怎么用?Java MethodAccess.invoke使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.esotericsoftware.reflectasm.MethodAccess
的用法示例。
在下文中一共展示了MethodAccess.invoke方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: invokeMethod
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
/**
* 执行某对象方法
*
* @param owner
* 对象
* @param methodName
* 方法名
* @param args
* 参数
* @return 方法返回值
*/
public static final Object invokeMethod(Object owner, String methodName, Object... args) {
Class<?> ownerClass = owner.getClass();
String key = null;
if (args != null) {
Class<?>[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
if (args[i] != null) {
argsClass[i] = args[i].getClass();
}
}
key = ownerClass + "_" + methodName + "_" + StringUtils.join(argsClass, ","); // 用于区分重载的方法
} else {
key = ownerClass + "_" + methodName; // 用于区分重载的方法
}
MethodAccess methodAccess = methodMap.get(key);
if (methodAccess == null) {
methodAccess = MethodAccess.get(ownerClass);
methodMap.put(key, methodAccess); // 缓存Method对象
}
return methodAccess.invoke(owner, methodName, args);
}
示例2: invokeMethod
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
/**
* 执行某对象方法
*
* @param owner
* 对象
* @param methodName
* 方法名
* @param args
* 参数
* @return 方法返回值
*/
public static final Object invokeMethod(Object owner, String methodName, Object... args) {
Class<?> ownerClass = owner.getClass();
String key = null;
if (args != null) {
Class<?>[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
if (args[i] != null) {
argsClass[i] = args[i].getClass();
}
}
key = ownerClass + "_" + methodName + "_" + StringUtils.join(argsClass, ","); // 用于区分重载的方法
} else {
key = ownerClass + "_" + methodName; // 用于区分重载的方法
}
MethodAccess methodAccess = methodMap.get(key);
if (methodAccess == null) {
methodAccess = MethodAccess.get(ownerClass);
methodMap.put(key, methodAccess); // 缓存Method对象
}
return methodAccess.invoke(owner, methodName, args);
}
示例3: copyProperties
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
public static void copyProperties(Object desc, Object src) {
MethodAccess descMethodAccess = METHOD_MAP.get(desc.getClass());
if (descMethodAccess == null) {
descMethodAccess = cache(desc);
}
MethodAccess srcMethodAccess = METHOD_MAP.get(src.getClass());
if (srcMethodAccess == null) {
srcMethodAccess = cache(src);
}
List<String> fieldList = FIELD_MAP.get(src.getClass());
for (String field : fieldList) {
String getKey = src.getClass().getName() + ".get" + field;
String setKey = desc.getClass().getName() + ".set" + field;
Integer setIndex = METHOD_INDEX_MAP.get(setKey);
if (setIndex != null) {
int getIndex = METHOD_INDEX_MAP.get(getKey);
descMethodAccess.invoke(desc, setIndex, srcMethodAccess.invoke(src, getIndex));
}
}
}
示例4: doMethodCall
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
private Object doMethodCall(Code code) {
Object[] params = new Object[code.b];
for (int i = 0; i < code.b; ++i) {
params[i] = regs[code.a + i + 2];
}
Object methodName = regs[code.a + 1];
Object object = regs[code.a];
try {
MethodAccess access = methodAccessCache.get(object.getClass());
if (access == null) {
access = MethodAccess.get(object.getClass());
methodAccessCache.put(object.getClass(), access);
}
Object retval = access
.invoke(object, methodName.toString(), params);
return retval;
} catch (IllegalArgumentException e) {
warn(e.toString());
return null;
}
}
示例5: invoke
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
@Override
public Object invoke(RemotingProtocol msg) throws Exception {
Stopwatch watch = Stopwatch.createStarted();
String methodName = msg.getMethod();
Object[] args = msg.getArgs();
Class<?>[] mParamsType = msg.getmParamsTypes();
Class<?> implClass = GuiceDI.getInstance(ServiceInfosHolder.class).getImplClass(msg.getDefineClass(), msg.getImplClass());
if (implClass == null) {
logger.error(String.format("define:%s impl:%s", msg.getDefineClass(), msg.getImplClass()));
throw new NoImplClassException(msg.getDefineClass().getName());
}
String mkey = String.format("%s_%s", implClass.getSimpleName(), methodName);
MethodAccess access = methodCache.get(mkey);
if (access == null) {
logger.info(String.format("method:%s args:%s", methodName, JSON.toJSONString(mParamsType)));
access = MethodAccess.get(implClass);
if (access != null) {
methodCache.putIfAbsent(mkey, access);
}
}
if (access == null) {
throw new NoImplMethodException(implClass.getName(), methodName);
}
Object target = GuiceDI.getInstance(ServiceInfosHolder.class).getOjbectFromClass(implClass.getName());
Object retval = null;
//此步反射 非常耗时
retval = access.invoke(target, methodName, args);
if (logger.isDebugEnabled())
logger.debug(String.format("jdk invoke use:%s", watch.toString()));
return retval;
}
示例6: handleRequest
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
public void handleRequest(TransportChannel channel, Request request) {
Response response = new Response();
Class<?> interfaceClass = request.getInterfaceClass();
response.setRequestId(request.getRequestId());
Object providerInstance = providerContainer.getProvider(interfaceClass);
if (providerInstance == null) {
response.setResult(new ProviderNotFoundException("No Provider For " + request.getClass().getName()));
channel.write(response);
return;
}
MethodAccess methodAccess = methodAccessCache.computeIfAbsent(interfaceClass, key -> MethodAccess.get(key));
try {
Object result = methodAccess.invoke(providerInstance, request.getMethodName(), request.getArgs());
response.setResult(result);
} catch (Throwable t) {
response.setResult(t);
}
channel.write(response);
return;
}
示例7: handle
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
private Object handle(Request req) throws Throwable {
String className = req.getClassName();
String methodName = req.getMethodName();
Class<?>[] parameterTypes = req.getParameterTypes();
Object[] parameters = req.getParameters();
//new object
Class<?> clazz = Class.forName(className);
ConstructorAccess<?> constructorAccess = ConstructorAccess.get(clazz);
Object invokeObject = constructorAccess.newInstance();
//get method access
MethodAccess methodAccess = MethodAccess.get(clazz);
Object result = methodAccess.invoke(invokeObject, methodName, parameterTypes, parameters);
return result;
}
示例8: main
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
public static void main(String[] args) {
User user = new User();
//使用reflectasm生产User访问类
MethodAccess access = MethodAccess.get(User.class);
//invoke setName方法name值
access.invoke(user, "setName", "张三");
//invoke getName方法 获得值
String name = (String) access.invoke(user, "getName", null);
System.out.println(name);
}
示例9: MethodAccessBenchmark
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
public MethodAccessBenchmark () throws Exception {
int count = 100000;
Object[] dontCompileMeAway = new Object[count];
Object[] args = new Object[0];
MethodAccess access = MethodAccess.get(SomeClass.class);
SomeClass someObject = new SomeClass();
int index = access.getIndex("getName");
Method method = SomeClass.class.getMethod("getName");
// method.setAccessible(true); // Improves reflection a bit.
for (int i = 0; i < 100; i++) {
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = access.invoke(someObject, index, args);
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = method.invoke(someObject, args);
}
warmup = false;
for (int i = 0; i < 100; i++) {
start();
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = access.invoke(someObject, index, args);
end("MethodAccess");
}
for (int i = 0; i < 100; i++) {
start();
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = method.invoke(someObject, args);
end("Reflection");
}
chart("Method Call");
}
示例10: invoke
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
@Override
public RpcMessage invoke(RpcMessage req) throws RpcException {
String rpcId = req.getBody().getRpcId();
Class<?> rpcInterface = req.getBody().getRpcInterface();
RpcMethod rpcMethod = req.getBody().getRpcMethod();
Class<?>[] paramTypes = rpcMethod.getParameterTypes();
Object[] params = rpcMethod.getParameters();
String methodName = rpcMethod.getName();
RpcApi api = registry.lookup(new DefaultRpcApi(rpcId, rpcInterface, rpcMethod));
if (api == null) { throw new RpcException(RpcException.SERVER_ERROR, "No exported api mapping"); }
Object rpcObject = api.getRpcObject();
try {
// Set rpc context
RpcContext ctx = RpcContext.getContext();
ctx.setClientAddress(req.getClientAddress());
ctx.setServerAddress(req.getServerAddress());
ctx.setAttachments(req.getAttachments());
LOG.debug("[CRAFT-ATOM-RPC] Rpc server invoker is invoking, |rpcContext={}|", ctx);
// Reflect invoke
MethodAccess ma = MethodAccess.get(rpcInterface);
int methodIndex = ma.getIndex(methodName, paramTypes);
Object returnObject = ma.invoke(rpcObject, methodIndex, params);
return RpcMessages.newRsponseRpcMessage(req.getId(), returnObject);
} catch (Exception e) {
LOG.warn("[CRAFT-ATOM-RPC] Rpc server invoker error", e);
if (isDeclaredException(e, rpcInterface, methodName, paramTypes)) {
return RpcMessages.newRsponseRpcMessage(req.getId(), e);
} else {
throw new RpcException(RpcException.SERVER_ERROR, "server error");
}
} finally {
RpcContext.removeContext();
}
}
示例11: invoke
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
@SafeVarargs
public final <V> V invoke(String methodName, Object... args) {
final MethodAccess methodAccess = getMethodAccess(cls);
return (V) methodAccess.invoke(target, methodName, args);
}
示例12: test4
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
@Test
public void test4() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Person person = new Person("xxx");
int count = 10000;
String name;
// direct invoke
long startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
name = person.getName();
}
System.out.println("total = " + (System.nanoTime() - startTime));
// field invoke
startTime = System.nanoTime();
Field field = person.getClass().getDeclaredField("name");
field.setAccessible(true);
for (int i = 0; i < count; i++) {
name = (String) field.get(person);
}
System.out.println("total = " + (System.nanoTime() - startTime));
// field invoke
startTime = System.nanoTime();
FieldAccess fieldAccess = FieldAccess.get(Person.class);
for (int i = 0; i < count; i++) {
name = (String) fieldAccess.get(person, "name");
}
System.out.println("total = " + (System.nanoTime() - startTime));
// method invoke - 1
startTime = System.nanoTime();
Method method = person.getClass().getDeclaredMethod("getName");
for (int i = 0; i < count; i++) {
name = (String) method.invoke(person);
}
System.out.println("total = " + (System.nanoTime() - startTime));
// method invoke - 2
startTime = System.nanoTime();
MethodAccess access = MethodAccess.get(Person.class);
for (int i = 0; i < count; i++) {
name = (String) access.invoke(person, "getName");
}
System.out.println("total = " + (System.nanoTime() - startTime));
}
示例13: action
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
@Override
protected void action(Event event) {
MethodAccess access = MethodAccess.get(getNode().getClass());
access.invoke(getNode(), handlerMethod);
}
示例14: execute
import com.esotericsoftware.reflectasm.MethodAccess; //导入方法依赖的package包/类
public void execute(JobExecutionContext context) throws JobExecutionException {
long start = System.currentTimeMillis();
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
String taskType = jobDataMap.getString("taskType");
String targetObject = jobDataMap.getString("targetObject");
String targetMethod = jobDataMap.getString("targetMethod");
logger.info("定时任务开始执行: [{}.{}]", targetObject, targetMethod);
try {
ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext()
.get("applicationContext");
if (TaskType.local.equals(taskType)) {
Object object = applicationContext.getBean(targetObject);
MethodAccess methodAccess = MethodAccess.get(object.getClass());
methodAccess.invoke(object, targetMethod);
} else if (TaskType.dubbo.equals(taskType)) {
// Object object = DubboUtil.refer(applicationContext, targetObject);
// MethodAccess methodAccess = MethodAccess.get(object.getClass());
// methodAccess.invoke(object, targetMethod);
}
double time = (System.currentTimeMillis() - start) / 1000.0;
logger.info("定时任务[{}.{}]用时:{}s", targetObject, targetMethod, time);
} catch (Exception e) {
throw new JobExecutionException(e);
}
}