Skip to content

Commit

Permalink
Add invokeDefaultMethod utils for JDK dynamic proxy created by Spring…
Browse files Browse the repository at this point in the history
  • Loading branch information
injae-kim committed Jan 12, 2024
1 parent c868bc5 commit 3db7627
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 14 deletions.
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;
}

}
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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.messaging.rsocket.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -29,7 +28,7 @@
import org.aopalliance.intercept.MethodInvocation;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocationUtils;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.AnnotatedElementUtils;
Expand All @@ -46,6 +45,7 @@
* {@link Builder Builder}.
*
* @author Rossen Stoyanchev
* @author Injae Kim
* @since 6.0
*/
public final class RSocketServiceProxyFactory {
Expand Down Expand Up @@ -253,11 +253,9 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
if (serviceMethod != null) {
return serviceMethod.invoke(invocation.getArguments());
}
if (method.isDefault()) {
if (invocation instanceof ReflectiveMethodInvocation reflectiveMethodInvocation) {
Object proxy = reflectiveMethodInvocation.getProxy();
return InvocationHandler.invokeDefault(proxy, method, invocation.getArguments());
}
Object invokeDefaultMethod = ReflectiveMethodInvocationUtils.invokeDefaultMethod(invocation);
if (invokeDefaultMethod != null) {
return invokeDefaultMethod;
}
throw new IllegalStateException("Unexpected method invocation: " + method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.web.service.invoker;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -29,7 +28,7 @@
import org.aopalliance.intercept.MethodInvocation;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocationUtils;
import org.springframework.core.KotlinDetector;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.ReactiveAdapterRegistry;
Expand All @@ -49,6 +48,7 @@
* {@link Builder Builder}.
*
* @author Rossen Stoyanchev
* @author Injae Kim
* @since 6.0
* @see org.springframework.web.client.support.RestClientAdapter
* @see org.springframework.web.reactive.function.client.support.WebClientAdapter
Expand Down Expand Up @@ -302,11 +302,9 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
resolveCoroutinesArguments(invocation.getArguments()) : invocation.getArguments();
return httpServiceMethod.invoke(arguments);
}
if (method.isDefault()) {
if (invocation instanceof ReflectiveMethodInvocation reflectiveMethodInvocation) {
Object proxy = reflectiveMethodInvocation.getProxy();
return InvocationHandler.invokeDefault(proxy, method, invocation.getArguments());
}
Object invokeDefaultMethod = ReflectiveMethodInvocationUtils.invokeDefaultMethod(invocation);
if (invokeDefaultMethod != null) {
return invokeDefaultMethod;
}
throw new IllegalStateException("Unexpected method invocation: " + method);
}
Expand Down

0 comments on commit 3db7627

Please sign in to comment.