Java Code Examples for java.lang.invoke.SerializedLambda#getImplMethodName()

The following examples show how to use java.lang.invoke.SerializedLambda#getImplMethodName() . 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: PropertyWrap.java    From weed3 with Apache License 2.0 6 votes vote down vote up
/** 将 Property 转为 PropertyWrap  */
private static <C> PropertyWrap wrap(Property<C, ?> p) {
    try {
        Method fun = p.getClass().getDeclaredMethod("writeReplace");
        fun.setAccessible(Boolean.TRUE);
        SerializedLambda slambda = (SerializedLambda) fun.invoke(p);
        String method = slambda.getImplMethodName();
        String attr = null;
        if (method.startsWith("get")) {
            attr = method.substring(3);
        } else {
            attr = method.substring(2);//is
        }
        return new PropertyWrap(p, slambda.getImplClass(), attr);
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: PropertyTest.java    From weed3 with Apache License 2.0 6 votes vote down vote up
private static  <C> String getName(Property<C, ?> property) {
    try {
        Method declaredMethod = property.getClass().getDeclaredMethod("writeReplace");
        declaredMethod.setAccessible(Boolean.TRUE);
        SerializedLambda serializedLambda = (SerializedLambda) declaredMethod.invoke(property);
        String method = serializedLambda.getImplMethodName();
        String attr = null;
        if (method.startsWith("get")) {
            attr = method.substring(3);
        } else {
            attr = method.substring(2);
        }
        return attr;
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: BladeCache.java    From blade with Apache License 2.0 6 votes vote down vote up
public static String getLambdaFieldName(SerializedLambda serializedLambda) {
    String name = CACHE_LAMBDA_NAME.get(serializedLambda);
    if (null != name) {
        return name;
    }
    String className  = serializedLambda.getImplClass().replace("/", ".");
    String methodName = serializedLambda.getImplMethodName();
    String fieldName  = methodToFieldName(methodName);
    try {
        Field field = Class.forName(className).getDeclaredField(fieldName);
        name = field.getName();
        CACHE_LAMBDA_NAME.put(serializedLambda, name);
        return name;
    } catch (NoSuchFieldException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: Reflections.java    From Mapper with MIT License 6 votes vote down vote up
public static String fnToFieldName(Fn fn) {
    try {
        Method method = fn.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(Boolean.TRUE);
        SerializedLambda serializedLambda = (SerializedLambda) method.invoke(fn);
        String getter = serializedLambda.getImplMethodName();
        if (GET_PATTERN.matcher(getter).matches()) {
            getter = getter.substring(3);
        } else if (IS_PATTERN.matcher(getter).matches()) {
            getter = getter.substring(2);
        }
        return Introspector.decapitalize(getter);
    } catch (ReflectiveOperationException e) {
        throw new ReflectionOperationException(e);
    }
}
 
Example 5
Source File: ExpressionUtil.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public static String expressionToMethodName(Expression expression) {
    if (expression == null)
        return "";
    try {
        Method method = expression.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(Boolean.TRUE);
        SerializedLambda serializedLambda = (SerializedLambda)method.invoke(expression);
        return serializedLambda.getImplMethodName();
    } catch (ReflectiveOperationException e) {
        return "";
    }
}
 
Example 6
Source File: TypeFunction.java    From GyJdbc with Apache License 2.0 5 votes vote down vote up
/**
 * 获取列名称
 * @param lambda lamda表达式
 * @return String 列名称
 */
static String getLambdaColumnName(Serializable lambda) {
    try {
        Method method = lambda.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(Boolean.TRUE);
        SerializedLambda serializedLambda = (SerializedLambda) method.invoke(lambda);
        String getter = serializedLambda.getImplMethodName();
        String fieldName = Introspector.decapitalize(getter.replace("get", ""));
        return EntityTools.transferColumnName(fieldName);
    } catch (ReflectiveOperationException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 7
Source File: CommonsHelper.java    From mybatis-dynamic-query with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("squid:S00112")
public static <T, R extends Comparable> PropertyInfo getPropertyInfo(GetPropertyFunction<T, R> fn) {
    try {
        Method method = fn.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(true);
        SerializedLambda serializedLambda = (SerializedLambda) method.invoke(fn);
        String methodName = serializedLambda.getImplMethodName();
        String className = serializedLambda.getImplClass();
        String propertyName;
        String getString = "get";
        String isString = "is";
        if (methodName.startsWith(getString)) {
            propertyName = java.beans.Introspector.decapitalize(methodName.substring(3));
        } else if (methodName.startsWith(isString)) {
            propertyName = java.beans.Introspector.decapitalize(methodName.substring(2));
        } else {
            propertyName = methodName;
        }

        Class ownerClass;
        if (classMap.containsKey(className)) {
            ownerClass = classMap.get(className);
        } else {
            ownerClass = Class.forName(className.replace('/', '.'));
            classMap.put(className, ownerClass);
        }

        PropertyInfo propertyInfo = new PropertyInfo();
        propertyInfo.setPropertyName(propertyName);
        propertyInfo.setOwnerClass(ownerClass);
        return propertyInfo;
    } catch (ReflectiveOperationException e) {
        throw new InternalRuntimeException(e);
    }
}
 
Example 8
Source File: ControllerInspector.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public ControllerInspector(Controller controller) {
    Class<?> controllerClass = controller.getClass();

    try {
        if (controller instanceof LambdaController) {
            Method writeReplace = controllerClass.getDeclaredMethod("writeReplace");
            if (!writeReplace.trySetAccessible()) {
                throw new Error("failed to inspect controller, cannot access writeReplace");
            }
            SerializedLambda lambda = (SerializedLambda) writeReplace.invoke(controller);
            Class<?> targetClass = Class.forName(lambda.getImplClass().replace('/', '.'));
            String targetMethodName = lambda.getImplMethodName();
            controllerInfo = targetClass.getCanonicalName() + "." + targetMethodName;
            if (targetMethodName.contains("$")) {   // for lambda
                this.targetClass = controllerClass;
                targetMethod = controllerClass.getMethod("execute", Request.class);
            } else {    // for method reference
                this.targetClass = targetClass;
                targetMethod = targetClass.getDeclaredMethod(targetMethodName, Request.class);
            }
        } else {
            targetClass = controllerClass;
            targetMethod = controllerClass.getMethod("execute", Request.class);
            controllerInfo = controllerClass.getCanonicalName() + ".execute";
        }
    } catch (ReflectiveOperationException e) {
        throw new Error("failed to inspect controller", e);
    }
}
 
Example 9
Source File: DescribablePredicate.java    From java-8-matchers with MIT License 5 votes vote down vote up
default String getResultDescription() {
    SerializedLambda lambda = asSerializedLambda();
    if (! lambda.getImplMethodName().startsWith("lambda$")) {
        return lambda.getImplMethodName();
    }
    return "boolean";
}
 
Example 10
Source File: DescribableFunction.java    From java-8-matchers with MIT License 5 votes vote down vote up
default String getResultDescription() {
    SerializedLambda lambda = asSerializedLambda();
    MethodType lambdaMethodType = MethodType.fromMethodDescriptorString(lambda.getImplMethodSignature(), getClass().getClassLoader());
    String resultType = lambdaMethodType.returnType().getSimpleName();
    if (! lambda.getImplMethodName().startsWith("lambda$")) {
        return lambda.getImplMethodName() + " (" + withPrefixedArticle(resultType) + ")";
    }
    return resultType;
}