java.lang.invoke.SerializedLambda Java Examples

The following examples show how to use java.lang.invoke.SerializedLambda. 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: SerialFilterTest.java    From TencentKona-8 with GNU General Public License v2.0 7 votes vote down vote up
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
    Class<?> serialClass = filter.serialClass();
    System.out.printf("     checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
            serialClass, filter.arrayLength(), filter.references(),
            filter.depth(), filter.streamBytes());
    count++;
    if (serialClass != null) {
        if (serialClass.getName().contains("$$Lambda$")) {
            // TBD: proper identification of serialized Lambdas?
            // Fold the serialized Lambda into the SerializedLambda type
            classes.add(SerializedLambda.class);
        } else if (Proxy.isProxyClass(serialClass)) {
            classes.add(Proxy.class);
        } else {
            classes.add(serialClass);
        }

    }
    this.maxArray = Math.max(this.maxArray, filter.arrayLength());
    this.maxRefs = Math.max(this.maxRefs, filter.references());
    this.maxDepth = Math.max(this.maxDepth, filter.depth());
    this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
    return ObjectInputFilter.Status.UNDECIDED;
}
 
Example #2
Source File: SerialFilterTest.java    From jdk8u-jdk with GNU General Public License v2.0 7 votes vote down vote up
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
    count++;
    if (filter.serialClass() != null) {
        if (filter.serialClass().getName().contains("$$Lambda$")) {
            // TBD: proper identification of serialized Lambdas?
            // Fold the serialized Lambda into the SerializedLambda type
            classes.add(SerializedLambda.class);
        } else if (Proxy.isProxyClass(filter.serialClass())) {
            classes.add(Proxy.class);
        } else {
            classes.add(filter.serialClass());
        }

    }
    this.maxArray = Math.max(this.maxArray, filter.arrayLength());
    this.maxRefs = Math.max(this.maxRefs, filter.references());
    this.maxDepth = Math.max(this.maxDepth, filter.depth());
    this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
    return ObjectInputFilter.Status.UNDECIDED;
}
 
Example #3
Source File: SerialFilterTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
    Class<?> serialClass = filter.serialClass();
    System.out.printf("     checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
            serialClass, filter.arrayLength(), filter.references(),
            filter.depth(), filter.streamBytes());
    count++;
    if (serialClass != null) {
        if (serialClass.getName().contains("$$Lambda$")) {
            // TBD: proper identification of serialized Lambdas?
            // Fold the serialized Lambda into the SerializedLambda type
            classes.add(SerializedLambda.class);
        } else if (Proxy.isProxyClass(serialClass)) {
            classes.add(Proxy.class);
        } else {
            classes.add(serialClass);
        }

    }
    this.maxArray = Math.max(this.maxArray, filter.arrayLength());
    this.maxRefs = Math.max(this.maxRefs, filter.references());
    this.maxDepth = Math.max(this.maxDepth, filter.depth());
    this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
    return ObjectInputFilter.Status.UNDECIDED;
}
 
Example #4
Source File: FunctionFactoryUtils.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
private static String[] getSerializedLambdaParameterizedTypeNames(Object source) {
	Method method = ReflectionUtils.findMethod(source.getClass(), "writeReplace");
	if (method == null) {
		return null;
	}
	ReflectionUtils.makeAccessible(method);
	SerializedLambda serializedLambda = (SerializedLambda) ReflectionUtils
			.invokeMethod(method, source);
	String signature = serializedLambda.getImplMethodSignature().replaceAll("[()]",
			"");

	List<String> typeNames = Stream.of(signature.split(";"))
			.map(t -> t.substring(1).replace('/', '.')).collect(Collectors.toList());

	return typeNames.toArray(new String[typeNames.size()]);
}
 
Example #5
Source File: SerialFilterTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
    Class<?> serialClass = filter.serialClass();
    System.out.printf("     checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
            serialClass, filter.arrayLength(), filter.references(),
            filter.depth(), filter.streamBytes());
    count++;
    if (serialClass != null) {
        if (serialClass.getName().contains("$$Lambda$")) {
            // TBD: proper identification of serialized Lambdas?
            // Fold the serialized Lambda into the SerializedLambda type
            classes.add(SerializedLambda.class);
        } else if (Proxy.isProxyClass(serialClass)) {
            classes.add(Proxy.class);
        } else {
            classes.add(serialClass);
        }

    }
    this.maxArray = Math.max(this.maxArray, filter.arrayLength());
    this.maxRefs = Math.max(this.maxRefs, filter.references());
    this.maxDepth = Math.max(this.maxDepth, filter.depth());
    this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
    return ObjectInputFilter.Status.UNDECIDED;
}
 
Example #6
Source File: SerialFilterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
    Class<?> serialClass = filter.serialClass();
    System.out.printf("     checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
            serialClass, filter.arrayLength(), filter.references(),
            filter.depth(), filter.streamBytes());
    count++;
    if (serialClass != null) {
        if (serialClass.getName().contains("$$Lambda$")) {
            // TBD: proper identification of serialized Lambdas?
            // Fold the serialized Lambda into the SerializedLambda type
            classes.add(SerializedLambda.class);
        } else if (Proxy.isProxyClass(serialClass)) {
            classes.add(Proxy.class);
        } else {
            classes.add(serialClass);
        }

    }
    this.maxArray = Math.max(this.maxArray, filter.arrayLength());
    this.maxRefs = Math.max(this.maxRefs, filter.references());
    this.maxDepth = Math.max(this.maxDepth, filter.depth());
    this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
    return ObjectInputFilter.Status.UNDECIDED;
}
 
Example #7
Source File: SerialFilterTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
    Class<?> serialClass = filter.serialClass();
    System.out.printf("     checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
            serialClass, filter.arrayLength(), filter.references(),
            filter.depth(), filter.streamBytes());
    count++;
    if (serialClass != null) {
        if (serialClass.getName().contains("$$Lambda$")) {
            // TBD: proper identification of serialized Lambdas?
            // Fold the serialized Lambda into the SerializedLambda type
            classes.add(SerializedLambda.class);
        } else if (Proxy.isProxyClass(serialClass)) {
            classes.add(Proxy.class);
        } else {
            classes.add(serialClass);
        }

    }
    this.maxArray = Math.max(this.maxArray, filter.arrayLength());
    this.maxRefs = Math.max(this.maxRefs, filter.references());
    this.maxDepth = Math.max(this.maxDepth, filter.depth());
    this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
    return ObjectInputFilter.Status.UNDECIDED;
}
 
Example #8
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 #9
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 #10
Source File: DeserializeMethodTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
    try {
        Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
        if (!expectedPresent)
            fail("Unexpected $deserializeLambda$ in " + clazz);
    }
    catch (NoSuchMethodException e) {
        if (expectedPresent)
            fail("Expected to find $deserializeLambda$ in " + clazz);
    }
}
 
Example #11
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 #12
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 #13
Source File: MyBatisUtil.java    From summerframework with Apache License 2.0 6 votes vote down vote up
public static <T> String getFieldName(EntityGetterMethod<T, Object> expression) {
    if (expression == null)
        throw new IllegalArgumentException("Expression should not be null");
    try {
        Method method = expression.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(Boolean.TRUE);
        SerializedLambda serializedLambda = (SerializedLambda)method.invoke(expression);
        String fieldName = StringUtils.resolveFieldName(serializedLambda.getImplMethodName());
        String className = serializedLambda.getImplClass().replace("/", ".");
        Field field = ReflectionUtils.findField(Class.forName(className), fieldName);
        String columnName = field.getName();
        TableField[] tableField = field.getAnnotationsByType(TableField.class);
        if (null != tableField && tableField.length == 1) {
            if (!StringUtils.isEmpty(tableField[0].value())) {
                columnName = tableField[0].value();
            }
        }
        String ret = StringUtils.camelToUnderline(columnName);
        return ret;
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException("This will never happen!", e);
    }
}
 
Example #14
Source File: SerialFilterTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
    Class<?> serialClass = filter.serialClass();
    System.out.printf("     checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
            serialClass, filter.arrayLength(), filter.references(),
            filter.depth(), filter.streamBytes());
    count++;
    if (serialClass != null) {
        if (serialClass.getName().contains("$$Lambda$")) {
            // TBD: proper identification of serialized Lambdas?
            // Fold the serialized Lambda into the SerializedLambda type
            classes.add(SerializedLambda.class);
        } else if (Proxy.isProxyClass(serialClass)) {
            classes.add(Proxy.class);
        } else {
            classes.add(serialClass);
        }

    }
    this.maxArray = Math.max(this.maxArray, filter.arrayLength());
    this.maxRefs = Math.max(this.maxRefs, filter.references());
    this.maxDepth = Math.max(this.maxDepth, filter.depth());
    this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
    return ObjectInputFilter.Status.UNDECIDED;
}
 
Example #15
Source File: LambdaCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
LambdaCodec() {
  try {
    this.readResolveMethod = SerializedLambda.class.getDeclaredMethod("readResolve");
  } catch (NoSuchMethodException e) {
    throw new IllegalStateException(e);
  }
  readResolveMethod.setAccessible(true);
}
 
Example #16
Source File: LambdaTest.java    From demo-junit-5 with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
default Method method() {
	SerializedLambda lambda = serialized();
	Class<?> containingClass = getContainingClass();
	return stream(containingClass.getDeclaredMethods())
			.filter(method -> Objects.equals(method.getName(), lambda.getImplMethodName()))
			.findFirst()
			.orElseThrow(UnableToGuessMethodException::new);
}
 
Example #17
Source File: LambdaCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public Serializable deserialize(DeserializationContext context, CodedInputStream codedIn)
    throws SerializationException, IOException {
  SerializedLambda serializedLambda = context.deserialize(codedIn);
  try {
    return (Serializable) readResolveMethod.invoke(serializedLambda);
  } catch (ReflectiveOperationException e) {
    throw new IllegalStateException("Error read-resolving " + serializedLambda, e);
  }
}
 
Example #18
Source File: Reflection.java    From openCypher with Apache License 2.0 5 votes vote down vote up
/**
 * Get the parameter name of a ({@linkplain Serializable serializable}) lambda with a single parameter.
 * <p>
 * Getting the parameter requires the source to be compiled with the {@code -parameters} flag passed to {@code
 * javac} and JDK {@code 1.8.0_60} or newer.
 *
 * @param lambda the ({@linkplain Serializable serializable}) lambda to get the parameter name from.
 * @return the name of the sole parameter of the lambda.
 */
public static String lambdaParameterName( Serializable lambda )
{
    SerializedLambda serialized = serializedLambda( lambda );
    Parameter[] parameters = lambdaMethod( serialized ).getParameters();
    int bound;
    switch ( serialized.getImplMethodKind() )
    {
    case REF_invokeStatic:
        bound = serialized.getCapturedArgCount();
        break;
    case REF_invokeSpecial:
        bound = serialized.getCapturedArgCount() - 1;
        break;
    default:
        throw new IllegalArgumentException( "Unsupported method kind: " + serialized.getImplMethodKind() );
    }
    if ( parameters == null || (parameters.length - bound) != 1 )
    {
        throw new IllegalArgumentException(
                "Must have exactly one parameter, not " + (parameters == null ? 0 : parameters.length) +
                "; " + Arrays.toString( parameters ) + ", bound: " + bound );
    }
    Parameter parameter = parameters[bound];
    if ( !parameter.isNamePresent() )
    {
        throw new IllegalStateException(
                "No parameter name present, compile with '-parameters', and use JDK 1.8.0_60 or newer. " +
                "Your JDK version is " + System.getProperty( "java.version" ) );
    }
    return parameter.getName();
}
 
Example #19
Source File: Reflection.java    From openCypher with Apache License 2.0 5 votes vote down vote up
/**
 * Get the implementation method of a ({@linkplain Serializable serializable}) lambda.
 *
 * @param serialized the serialized form of a lambda.
 * @return the {@link Method} that implements the lambda.
 */
private static Method lambdaMethod( SerializedLambda serialized )
{
    Class<?> implClass = lambdaClass( serialized );
    return Stream.of( implClass.getDeclaredMethods() )
                 .filter( method -> Objects.equals( method.getName(), serialized.getImplMethodName() ) )
                 .reduce( ( l, r ) -> {
                     throw new IllegalArgumentException( "Too many implementation methods." );
                 } )
                 .orElseThrow( () -> new IllegalStateException( "Unable to find implementation method." ) );
}
 
Example #20
Source File: Reflection.java    From openCypher with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize a ({@linkplain Serializable serializable}) lambda.
 *
 * @param lambda the ({@linkplain Serializable serializable}) lambda to serialize.
 * @return the serialized form of the given lambda.
 */
private static SerializedLambda serializedLambda( Serializable lambda )
{
    try
    {
        Method replaceMethod = lambda.getClass().getDeclaredMethod( "writeReplace" );
        replaceMethod.setAccessible( true );
        return (SerializedLambda) replaceMethod.invoke( lambda );
    }
    catch ( Exception e )
    {
        throw new IllegalStateException( "Reflection failed." );
    }
}
 
Example #21
Source File: Reflection.java    From openCypher with Apache License 2.0 5 votes vote down vote up
private static Class<?> lambdaClass( SerializedLambda serialized )
{
    try
    {
        return Class.forName( serialized.getImplClass().replaceAll( "/", "." ) );
    }
    catch ( ClassNotFoundException e )
    {
        throw new IllegalStateException( e );
    }
}
 
Example #22
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 #23
Source File: SerializedLambdaResolvable.java    From java-8-matchers with MIT License 5 votes vote down vote up
default SerializedLambda asSerializedLambda() {
    try {
        Method replaceMethod = getClass().getDeclaredMethod("writeReplace");
        replaceMethod.setAccessible(true);
        return (SerializedLambda) replaceMethod.invoke(this);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new RuntimeException(e.getClass().getSimpleName() + ": '" + e.getMessage() + "'", e);
    }
}
 
Example #24
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 #25
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;
}
 
Example #26
Source File: DeserializeMethodTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
    try {
        Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
        if (!expectedPresent)
            fail("Unexpected $deserializeLambda$ in " + clazz);
    }
    catch (NoSuchMethodException e) {
        if (expectedPresent)
            fail("Expected to find $deserializeLambda$ in " + clazz);
    }
}
 
Example #27
Source File: DeserializeMethodTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
    try {
        Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
        if (!expectedPresent)
            fail("Unexpected $deserializeLambda$ in " + clazz);
    }
    catch (NoSuchMethodException e) {
        if (expectedPresent)
            fail("Expected to find $deserializeLambda$ in " + clazz);
    }
}
 
Example #28
Source File: MethodFinder.java    From lambda-type-references with Apache License 2.0 5 votes vote down vote up
default SerializedLambda serialized() {
    try {
        Method replaceMethod = getClass().getDeclaredMethod("writeReplace");
        replaceMethod.setAccessible(true);
        return (SerializedLambda) replaceMethod.invoke(this);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #29
Source File: MethodFinder.java    From lambda-type-references with Apache License 2.0 5 votes vote down vote up
default Method method() {
    SerializedLambda lambda = serialized();
    Class<?> containingClass = getContainingClass();
    return asList(containingClass.getDeclaredMethods())
            .stream()
            .filter(method -> Objects.equals(method.getName(), lambda.getImplMethodName()))
            .findFirst()
            .orElseThrow(UnableToGuessMethodException::new);
}
 
Example #30
Source File: ColumnChecker.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
default Method method() {
  SerializedLambda lambda = serialized();
  Class containingClass = getContainingClass();
  return Arrays.stream(containingClass.getDeclaredMethods())
    .filter(method -> Objects.equals(method.getName(), lambda.getImplMethodName()))
    .findFirst()
    .orElseThrow(MethodReferenceReflection.UnableToGuessMethodException::new);
}