Java Code Examples for com.esotericsoftware.reflectasm.MethodAccess#getIndex()

The following examples show how to use com.esotericsoftware.reflectasm.MethodAccess#getIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodAccessBenchmark.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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");
}
 
Example 2
Source File: DefaultRpcServerInvoker.java    From craft-atom with MIT License 5 votes vote down vote up
@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();
	}
}
 
Example 3
Source File: CacheableAccessors.java    From godaddy-logger with MIT License 4 votes vote down vote up
/**
 * Builds the method sorted cache. Sorts the methods alphabetically by name.
 */
private static void buildMethodCache(Class<?> clazz, MethodAccess methodAccess) {

    if(!_methodSortCache.containsKey(clazz)) {
        LogCache[] sortedLogCache = new LogCache[methodAccess.getMethodNames().length];

        String[] sortedMethodNames = Arrays.copyOf(methodAccess.getMethodNames(), sortedLogCache.length);

        Arrays.sort(sortedMethodNames);

        for(int i = 0; i < sortedLogCache.length; i++) {
            sortedLogCache[i] = new LogCache(methodAccess.getIndex(sortedMethodNames[i]),
                                             getMethodLogScope(clazz, sortedMethodNames[i]));
        }

        _methodSortCache.put(clazz, sortedLogCache);
    }

}