forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add invokeDefaultMethod utils for JDK dynamic proxy created by Spring…
… AOP Closes spring-projectsgh-28495
- Loading branch information
Showing
4 changed files
with
106 additions
and
14 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...-aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2024-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.aop.framework; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
|
||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Utility methods for {@link ReflectiveMethodInvocation}. | ||
* | ||
* @author Injae Kim | ||
* @since 6.2 | ||
*/ | ||
public abstract class ReflectiveMethodInvocationUtils { | ||
|
||
/** | ||
* Invoke default method in {@link ReflectiveMethodInvocation}. | ||
* @return {@code null} if method is not default method or invocation is not {@link ReflectiveMethodInvocation} | ||
*/ | ||
@Nullable | ||
public static Object invokeDefaultMethod(MethodInvocation invocation) throws Throwable { | ||
Method method = invocation.getMethod(); | ||
if (method.isDefault() && invocation instanceof ReflectiveMethodInvocation reflectiveMethodInvocation) { | ||
Object proxy = reflectiveMethodInvocation.getProxy(); | ||
return InvocationHandler.invokeDefault(proxy, method, invocation.getArguments()); | ||
} | ||
return null; | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...src/test/java/org/springframework/aop/framework/ReflectiveMethodInvocationUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2024-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.aop.framework; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link ReflectiveMethodInvocationUtils}. | ||
* | ||
* @author Injae Kim | ||
* @since 6.2 | ||
*/ | ||
public class ReflectiveMethodInvocationUtilsTests { | ||
|
||
@Test | ||
public void invokeDefaultMethod() { | ||
Service service = ProxyFactory.getProxy(Service.class, (MethodInterceptor) ReflectiveMethodInvocationUtils::invokeDefaultMethod); | ||
|
||
assertThat(service.getDefaultMethodValue()).isEqualTo("default method value"); | ||
assertThat(service.get()).isNull(); | ||
} | ||
|
||
private interface Service { | ||
|
||
String get(); | ||
|
||
default String getDefaultMethodValue() { | ||
return "default method value"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters