Search in sources :

Example 1 with FastMethod

use of net.sf.cglib.reflect.FastMethod 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 FastMethod

use of net.sf.cglib.reflect.FastMethod 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 FastMethod

use of net.sf.cglib.reflect.FastMethod 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 FastMethod

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

the class ReflectionUtil method copyProperties.

@SuppressWarnings({ "OverlyLongMethod", "OverlyNestedMethod" })
public static void copyProperties(Object source, Object target) {
    if (ObjectUtil.referenceEquals(source, target)) {
        return;
    }
    if (source == null) {
        throw new NullPointerException("Argument source can't be null (if target is not null).");
    }
    if (target == null) {
        throw new NullPointerException("Argument target can't be null (if source is not null).");
    }
    Map<String, FastMethod> sourceGetters = getGettersMap(source.getClass());
    Map<String, FastMethod> targetSetters = getSettersMap(target.getClass());
    for (Map.Entry<String, FastMethod> getterEntry : sourceGetters.entrySet()) {
        FastMethod getter = getterEntry.getValue();
        FastMethod setter = targetSetters.get(getterEntry.getKey());
        if (setter == null) {
            continue;
        }
        Class<?> getterReturnsClass = getter.getReturnType();
        Class<?> setterExpectsClass = setter.getParameterTypes()[0];
        Object value;
        try {
            value = getter.invoke(source, ArrayUtils.EMPTY_OBJECT_ARRAY);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(String.format("Can't get property '%s' from %s.", getterEntry.getKey(), source.getClass()), e);
        }
        if (setterExpectsClass.isAssignableFrom(getterReturnsClass)) {
            try {
                setter.invoke(target, new Object[] { value });
            } catch (InvocationTargetException e) {
                throw new RuntimeException(String.format("Can't copy assignable property '%s' from %s to %s.", getterEntry.getKey(), source.getClass(), target.getClass()), e);
            }
            continue;
        }
        if (setterExpectsClass.isAssignableFrom(String.class)) {
            try {
                setter.invoke(target, new Object[] { ObjectUtil.mapNotNull(value, Object::toString) });
            } catch (InvocationTargetException e) {
                throw new RuntimeException(String.format("Can't copy assignable property '%s' from %s to string assignable property of %s.", getterEntry.getKey(), source.getClass(), target.getClass()), e);
            }
            continue;
        }
        if (setterExpectsClass.isEnum()) {
            try {
                if (value == null) {
                    setter.invoke(target, new Object[] { null });
                } else {
                    String valueString = value.toString();
                    Object[] constants = setterExpectsClass.getEnumConstants();
                    for (Object constant : constants) {
                        if (constant.toString().equals(valueString)) {
                            setter.invoke(target, new Object[] { constant });
                            break;
                        }
                    }
                }
            } catch (InvocationTargetException e) {
                throw new RuntimeException(String.format("Can't copy enum property '%s' from %s to %s.", getterEntry.getKey(), source.getClass(), target.getClass()), e);
            }
            continue;
        }
        if (value != null) {
            try {
                Object valueCopy = setterExpectsClass.getConstructor().newInstance();
                copyProperties(value, valueCopy);
                setter.invoke(target, new Object[] { valueCopy });
            } catch (Exception e) {
                throw new RuntimeException(String.format("Can't copy object property '%s' from %s to %s.", getterEntry.getKey(), source.getClass(), target.getClass()), e);
            }
        }
    }
}
Also used : FastMethod(net.sf.cglib.reflect.FastMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 5 with FastMethod

use of net.sf.cglib.reflect.FastMethod 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

FastMethod (net.sf.cglib.reflect.FastMethod)5 FastClass (net.sf.cglib.reflect.FastClass)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)1