本文整理汇总了Java中com.esotericsoftware.reflectasm.MethodAccess类的典型用法代码示例。如果您正苦于以下问题:Java MethodAccess类的具体用法?Java MethodAccess怎么用?Java MethodAccess使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MethodAccess类属于com.esotericsoftware.reflectasm包,在下文中一共展示了MethodAccess类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: wrap
import com.esotericsoftware.reflectasm.MethodAccess; //导入依赖的package包/类
private void wrap(String route, Receiver<RequestType> handler, Method method, RoleType[] role) {
MethodAccess access = MethodAccess.get(handler.getClass());
int index = access.getIndex(method.getName());
use(route, request -> {
try {
access.invoke(handler, index, request);
} catch (Throwable e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
}
}, role);
}
示例4: buildMethodNameMap
import com.esotericsoftware.reflectasm.MethodAccess; //导入依赖的package包/类
private Map<String, List<Object[]>> buildMethodNameMap(MethodAccess methodAccess) {
Map<String, List<Object[]>> map = new HashMap<String, List<Object[]>>();
String[] methodNames = methodAccess.getMethodNames();
Class[][] parameterTypes = methodAccess.getParameterTypes();
for (int i = 0; i < methodNames.length; i++) {
String name = methodNames[i];
Class[] pTypes = parameterTypes[i];
List<Object[]> paramIndexList = map.get(name);
if (paramIndexList == null) {
paramIndexList = new ArrayList<Object[]>();
map.put(name, paramIndexList);
}
Object[] paramIndex = new Object[2];
List<Class[]> paramTypes = (List<Class[]>) paramIndex[0];
if (parameterTypes == null) {
paramTypes = new ArrayList<Class[]>();
paramIndex[0] = paramTypes;
}
paramIndexList.add(paramIndex);
}
return map;
}
示例5: 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));
}
}
}
示例6: 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;
}
}
示例7: 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;
}
示例8: 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;
}
示例9: invokeObject
import com.esotericsoftware.reflectasm.MethodAccess; //导入依赖的package包/类
static <T> T invokeObject(
final Object instance,
final String name,
final Object... args) {
return Fn.get(() -> {
final MethodAccess access = MethodAccess.get(instance.getClass());
// Direct invoke, multi overwrite for unbox/box issue still existing.
// TODO: Unbox/Box type issue
Object result;
try {
result = access.invoke(instance, name, args);
} catch (final Throwable ex) {
ex.printStackTrace();
// Could not call, re-find the method by index
// Search method by argument index because could not call directly
final int index;
final List<Class<?>> types = new ArrayList<>();
for (final Object arg : args) {
types.add(Types.toPrimary(arg.getClass()));
}
index = access.getIndex(name, types.toArray(new Class<?>[]{}));
result = access.invoke(instance, index, args);
}
final Object ret = result;
return Fn.get(() -> (T) ret, ret);
}, instance, name);
}
示例10: addTag
import com.esotericsoftware.reflectasm.MethodAccess; //导入依赖的package包/类
public void addTag(String tag, Method method) {
if (tagMap.containsKey(tag)) {
logger.warn(String.format("cid: %s, 存在相同的tag : %s", cid, tag));
}
Class<?> cls = method.getDeclaringClass();
final Tag t = new Tag();
t.setInvokeCls(cls);
t.setMethodName(method.getName());
Reconsume mReconsume = method.getAnnotation(Reconsume.class);
if (null != mReconsume) {
t.setReconsume(mReconsume.value());
} else {
Reconsume cReconsume = cls.getAnnotation(Reconsume.class);
if (null != cReconsume) {
t.setReconsume(cReconsume.value());
}
}
MethodAccess methodAccess = check.get(cls);
if (null == methodAccess) {
methodAccess = MethodAccess.get(cls);
check.put(cls, methodAccess);
}
t.setMethodAccess(methodAccess);
t.setArgumentExtractors(ArgumentExtractors.getArgumentExtractor(method, method.getParameterTypes(), beanFactory));
Stream.of(tag.split("\\|\\|")).forEach(tag_ -> tagMap.put(tag_.trim(), t));
}
示例11: 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;
}
示例12: 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);
}
示例13: JATOInstanceActor
import com.esotericsoftware.reflectasm.MethodAccess; //导入依赖的package包/类
public JATOInstanceActor(JATOActorFactory actorFactory, T instance, Channel<MethodMessage> mailbox, Fiber fiber) {
super(actorFactory, mailbox, fiber);
this.instance = instance;
this.clazz = (Class<T>) instance.getClass();
methodAccess = MethodAccess.get(clazz);
methodNameMap = buildMethodNameMap(methodAccess);
}
示例14: getMethodAccess
import com.esotericsoftware.reflectasm.MethodAccess; //导入依赖的package包/类
private MethodAccess getMethodAccess(final Class<?> cls) {
MethodAccess methodAccess = clsMethodPool.get(cls);
if (methodAccess == null) {
methodAccess = MethodAccess.get(cls);
clsMethodPool.put(cls, methodAccess);
}
return methodAccess;
}
示例15: cache
import com.esotericsoftware.reflectasm.MethodAccess; //导入依赖的package包/类
private static MethodAccess cache(Object obj) {
synchronized (obj.getClass()) {
MethodAccess methodAccess = MethodAccess.get(obj.getClass());
Field[] fields = null;
try {
// 这里查找了包括父类的属性
fields = ReflectionUtils.findFields(obj.getClass());
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
List<String> fieldList = new ArrayList<String>(fields.length);
for (Field field : fields) {
if (Modifier.isPrivate(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
//非公共私有变量
String fieldName = capitalize(field.getName());
int getIndex = methodAccess.getIndex("get" + fieldName);
int setIndex = methodAccess.getIndex("set" + fieldName);
METHOD_INDEX_MAP.put(obj.getClass().getName() + ".get" + fieldName, getIndex);
METHOD_INDEX_MAP.put(obj.getClass().getName() + ".set" + fieldName, setIndex);
fieldList.add(fieldName);
}
}
FIELD_MAP.put(obj.getClass(), fieldList);
METHOD_MAP.put(obj.getClass(), methodAccess);
return methodAccess;
}
}