Skip to content

Commit

Permalink
Introduce isLambdaClass() as a public utility in ClassUtils
Browse files Browse the repository at this point in the history
This commit extracts isLambda() from AopProxyUtils and makes it
publicly available as ClassUtils.isLambdaClass().

This is a prerequisite for gh-28209.
  • Loading branch information
sbrannen committed Apr 9, 2022
1 parent 35de7e1 commit 5f6d8df
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static Class<?>[] completeProxiedInterfaces(AdvisedSupport advised, boolean deco
if (targetClass.isInterface()) {
advised.setInterfaces(targetClass);
}
else if (Proxy.isProxyClass(targetClass) || isLambda(targetClass)) {
else if (Proxy.isProxyClass(targetClass) || ClassUtils.isLambdaClass(targetClass)) {
advised.setInterfaces(targetClass.getInterfaces());
}
specifiedInterfaces = advised.getProxiedInterfaces();
Expand Down Expand Up @@ -245,18 +245,4 @@ static Object[] adaptArgumentsIfNecessary(Method method, @Nullable Object[] argu
return arguments;
}

/**
* Determine if the supplied {@link Class} is a JVM-generated implementation
* class for a lambda expression or method reference.
* <p>This method makes a best-effort attempt at determining this, based on
* checks that work on modern, main stream JVMs.
* @param clazz the class to check
* @return {@code true} if the class is a lambda implementation class
* @since 5.3.16
*/
static boolean isLambda(Class<?> clazz) {
return (clazz.isSynthetic() && (clazz.getSuperclass() == Object.class) &&
(clazz.getInterfaces().length > 0) && clazz.getName().contains("$$Lambda"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.springframework.aop.SpringProxy;
import org.springframework.core.NativeDetector;
import org.springframework.util.ClassUtils;

/**
* Default {@link AopProxyFactory} implementation, creating either a CGLIB proxy
Expand Down Expand Up @@ -60,7 +61,7 @@ public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass) || AopProxyUtils.isLambda(targetClass)) {
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass) || ClassUtils.isLambdaClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -134,61 +133,4 @@ public void testProxiedUserInterfacesWithNoInterface() {
AopProxyUtils.proxiedUserInterfaces(proxy));
}

@Test
void isLambda() {
assertIsLambda(AopProxyUtilsTests.staticLambdaExpression);
assertIsLambda(AopProxyUtilsTests::staticStringFactory);

assertIsLambda(this.instanceLambdaExpression);
assertIsLambda(this::instanceStringFactory);
}

@Test
void isNotLambda() {
assertIsNotLambda(new EnigmaSupplier());

assertIsNotLambda(new Supplier<String>() {
@Override
public String get() {
return "anonymous inner class";
}
});

assertIsNotLambda(new Fake$$LambdaSupplier());
}

private static void assertIsLambda(Supplier<String> supplier) {
assertThat(AopProxyUtils.isLambda(supplier.getClass())).isTrue();
}

private static void assertIsNotLambda(Supplier<String> supplier) {
assertThat(AopProxyUtils.isLambda(supplier.getClass())).isFalse();
}

private static final Supplier<String> staticLambdaExpression = () -> "static lambda expression";

private final Supplier<String> instanceLambdaExpression = () -> "instance lambda expressions";

private static String staticStringFactory() {
return "static string factory";
}

private String instanceStringFactory() {
return "instance string factory";
}

private static class EnigmaSupplier implements Supplier<String> {
@Override
public String get() {
return "enigma";
}
}

private static class Fake$$LambdaSupplier implements Supplier<String> {
@Override
public String get() {
return "fake lambda";
}
}

}
14 changes: 14 additions & 0 deletions spring-core/src/main/java/org/springframework/util/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,20 @@ public static boolean isInnerClass(Class<?> clazz) {
return (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers()));
}

/**
* Determine if the supplied {@link Class} is a JVM-generated implementation
* class for a lambda expression or method reference.
* <p>This method makes a best-effort attempt at determining this, based on
* checks that work on modern, mainstream JVMs.
* @param clazz the class to check
* @return {@code true} if the class is a lambda implementation class
* @since 5.3.19
*/
public static boolean isLambdaClass(Class<?> clazz) {
return (clazz.isSynthetic() && (clazz.getSuperclass() == Object.class) &&
(clazz.getInterfaces().length > 0) && clazz.getName().contains("$$Lambda"));
}

/**
* Check whether the given object is a CGLIB proxy.
* @param object the object to check
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -408,6 +409,29 @@ void isPrimitiveOrWrapperWithWrapper(Class<?> type) {
assertThat(ClassUtils.isPrimitiveOrWrapper(type)).isTrue();
}

@Test
void isLambda() {
assertIsLambda(ClassUtilsTests.staticLambdaExpression);
assertIsLambda(ClassUtilsTests::staticStringFactory);

assertIsLambda(this.instanceLambdaExpression);
assertIsLambda(this::instanceStringFactory);
}

@Test
void isNotLambda() {
assertIsNotLambda(new EnigmaSupplier());

assertIsNotLambda(new Supplier<String>() {
@Override
public String get() {
return "anonymous inner class";
}
});

assertIsNotLambda(new Fake$$LambdaSupplier());
}


@Nested
class GetStaticMethodTests {
Expand Down Expand Up @@ -500,4 +524,38 @@ void print(String header, String[] messages, String footer) {
}
}

private static void assertIsLambda(Supplier<String> supplier) {
assertThat(ClassUtils.isLambdaClass(supplier.getClass())).isTrue();
}

private static void assertIsNotLambda(Supplier<String> supplier) {
assertThat(ClassUtils.isLambdaClass(supplier.getClass())).isFalse();
}

private static final Supplier<String> staticLambdaExpression = () -> "static lambda expression";

private final Supplier<String> instanceLambdaExpression = () -> "instance lambda expressions";

private static String staticStringFactory() {
return "static string factory";
}

private String instanceStringFactory() {
return "instance string factory";
}

private static class EnigmaSupplier implements Supplier<String> {
@Override
public String get() {
return "enigma";
}
}

private static class Fake$$LambdaSupplier implements Supplier<String> {
@Override
public String get() {
return "fake lambda";
}
}

}

0 comments on commit 5f6d8df

Please sign in to comment.