Search in sources :

Example 1 with FastClass

use of net.sf.cglib.reflect.FastClass in project codeforces-commons by Codeforces.

the class MethodArgumentsFormatUtil method getPropertyMethod.

private static FastMethod getPropertyMethod(Class<?> clazz, String property) {
    ClassAndProperty classAndProperty = new ClassAndProperty(clazz, property);
    FastMethod method = methodByProperty.get(classAndProperty);
    if (method == null) {
        FastClass cglibClazz = FastClass.create(clazz);
        try {
            method = cglibClazz.getMethod("get" + StringUtils.capitalize(property), ArrayUtils.EMPTY_CLASS_ARRAY);
        } catch (NoSuchMethodError ignored) {
        // No operations.
        }
        if (method == null) {
            try {
                method = cglibClazz.getMethod("is" + StringUtils.capitalize(property), ArrayUtils.EMPTY_CLASS_ARRAY);
            } catch (NoSuchMethodError ignored) {
            // No operations.
            }
        }
        if (method == null) {
            try {
                method = cglibClazz.getMethod(property, ArrayUtils.EMPTY_CLASS_ARRAY);
            } catch (NoSuchMethodError ignored) {
            // No operations.
            }
        }
        if (method != null) {
            methodByProperty.put(classAndProperty, method);
        } else {
            throw new IllegalArgumentException("Unable to find getter for property `" + property + "` in " + clazz + '.');
        }
    }
    return method;
}
Also used : FastClass(net.sf.cglib.reflect.FastClass) FastMethod(net.sf.cglib.reflect.FastMethod)

Example 2 with FastClass

use of net.sf.cglib.reflect.FastClass in project swift by luastar.

the class RpcServerChannelHandler method handleRequest.

protected Object handleRequest(RpcRequest request) throws Exception {
    // 获取服务对象
    String serviceName = request.getInterfaceName();
    String serviceVersion = request.getServiceVersion();
    if (StringUtils.isNotEmpty(serviceVersion)) {
        serviceName += "-" + serviceVersion;
    }
    Object serviceBean = handlerMap.get(serviceName);
    if (serviceBean == null) {
        throw new RuntimeException(String.format("can not find service bean by key: %s", serviceName));
    }
    // 获取反射调用所需的参数
    Class<?> serviceClass = serviceBean.getClass();
    String methodName = request.getMethodName();
    Class<?>[] parameterTypes = request.getParameterTypes();
    Object[] parameters = request.getParameters();
    // 执行反射调用
    /*
        Method method = serviceClass.getMethod(methodName, parameterTypes);
        method.setAccessible(true);
        return method.invoke(serviceBean, parameters);
        */
    // 使用 CGLib 执行反射调用
    FastClass serviceFastClass = FastClass.create(serviceClass);
    FastMethod serviceFastMethod = serviceFastClass.getMethod(methodName, parameterTypes);
    return serviceFastMethod.invoke(serviceBean, parameters);
}
Also used : FastClass(net.sf.cglib.reflect.FastClass) FastClass(net.sf.cglib.reflect.FastClass) FastMethod(net.sf.cglib.reflect.FastMethod)

Example 3 with FastClass

use of net.sf.cglib.reflect.FastClass in project codeforces-commons by Codeforces.

the class ReflectionUtil method buildSettersMap.

private static Map<String, FastMethod> buildSettersMap(Class<?> clazz) {
    Map<String, FastMethod> result = new HashMap<>();
    FastClass fastClass = getFastClass(clazz);
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (!Modifier.isStatic(method.getModifiers())) {
            String property = getSetterProperty(method);
            if (property != null) {
                result.put(property, fastClass.getMethod(method));
            }
        }
    }
    return result;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FastClass(net.sf.cglib.reflect.FastClass) FastMethod(net.sf.cglib.reflect.FastMethod) FastMethod(net.sf.cglib.reflect.FastMethod)

Example 4 with FastClass

use of net.sf.cglib.reflect.FastClass in project codeforces-commons by Codeforces.

the class ReflectionUtil method getFastClass.

private static FastClass getFastClass(Class<?> clazz) {
    FastClass result = fastClassCache.get(clazz);
    while (result == null) {
        FastClass fastClass = FastClass.create(clazz);
        fastClassCache.putIfAbsent(clazz, fastClass);
        result = fastClassCache.get(clazz);
    }
    return result;
}
Also used : FastClass(net.sf.cglib.reflect.FastClass)

Example 5 with FastClass

use of net.sf.cglib.reflect.FastClass in project codeforces-commons by Codeforces.

the class ReflectionUtil method buildGettersMap.

private static Map<String, FastMethod> buildGettersMap(Class<?> clazz) {
    Map<String, FastMethod> result = new HashMap<>();
    FastClass fastClass = getFastClass(clazz);
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (!Modifier.isStatic(method.getModifiers())) {
            String property = getGetterProperty(method);
            if (property != null) {
                result.put(property, fastClass.getMethod(method));
            }
        }
    }
    return result;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FastClass(net.sf.cglib.reflect.FastClass) FastMethod(net.sf.cglib.reflect.FastMethod) FastMethod(net.sf.cglib.reflect.FastMethod)

Aggregations

FastClass (net.sf.cglib.reflect.FastClass)5 FastMethod (net.sf.cglib.reflect.FastMethod)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2