GH-106 - Avoid intercepting Object and proxy-related methods for module interaction tracing.
We now skip Object methods and ones declared on Spring's Advised or TargetClassAware types when intercepting invocations to trigger a module entry.
This commit is contained in:
@@ -245,6 +245,23 @@ public class ApplicationModule {
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link ArchitecturallyEvidentType} for the given type.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @return will never be {@literal null}.
|
||||
* @throws IllegalArgumentException if the given type is not a module type.
|
||||
*/
|
||||
public ArchitecturallyEvidentType getArchitecturallyEvidentType(Class<?> type) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
return getType(type.getName())
|
||||
.map(it -> ArchitecturallyEvidentType.of(it, getSpringBeansInternal()))
|
||||
.orElseThrow(() -> new IllegalArgumentException("Couldn't find type %s in module %s!".formatted(
|
||||
FormatableType.of(type).getAbbreviatedFullName(this), getName())));
|
||||
}
|
||||
|
||||
public boolean contains(JavaClass type) {
|
||||
return basePackage.contains(type);
|
||||
}
|
||||
|
||||
@@ -47,6 +47,13 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.experimental</groupId>
|
||||
<artifactId>spring-modulith-test</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
|
||||
@@ -23,10 +23,11 @@ import java.util.Arrays;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.aop.ProxyMethodInvocation;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.modulith.model.FormatableType;
|
||||
import org.springframework.modulith.model.ApplicationModule;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
import org.springframework.modulith.model.FormatableType;
|
||||
import org.springframework.modulith.model.SpringBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.tngtech.archunit.core.domain.JavaClass;
|
||||
|
||||
@@ -98,6 +99,9 @@ class DefaultObservedModule implements ObservedModule {
|
||||
*/
|
||||
@Override
|
||||
public boolean exposes(JavaClass type) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
return module.isExposed(type);
|
||||
}
|
||||
|
||||
@@ -107,6 +111,9 @@ class DefaultObservedModule implements ObservedModule {
|
||||
*/
|
||||
@Override
|
||||
public boolean isObservedModule(ApplicationModule module) {
|
||||
|
||||
Assert.notNull(module, "AppliucationModule must not be null!");
|
||||
|
||||
return this.module.equals(module);
|
||||
}
|
||||
|
||||
@@ -116,6 +123,9 @@ class DefaultObservedModule implements ObservedModule {
|
||||
*/
|
||||
public ObservedModuleType getObservedModuleType(Class<?> type, ApplicationModules modules) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
Assert.notNull(modules, "ApplicationModules must not be null!");
|
||||
|
||||
return module.getSpringBeans().stream()
|
||||
.filter(it -> it.getFullyQualifiedTypeName().equals(type.getName()))
|
||||
.map(SpringBean::toArchitecturallyEvidentType)
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.modulith.observability;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.modulith.model.ApplicationModule;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
|
||||
@@ -53,9 +54,10 @@ interface ObservedModule {
|
||||
/**
|
||||
* Returns the {@link ObservedModuleType} for the given type and {@link ApplicationModules}.
|
||||
*
|
||||
* @param type
|
||||
* @param modules
|
||||
* @return
|
||||
* @param type must not be {@literal null}.
|
||||
* @param modules must not be {@literal null}.
|
||||
* @return the {@link ObservedModuleType} for the given type or {@literal null} if the type is not to be observed.
|
||||
*/
|
||||
@Nullable
|
||||
ObservedModuleType getObservedModuleType(Class<?> type, ApplicationModules modules);
|
||||
}
|
||||
|
||||
@@ -18,12 +18,17 @@ package org.springframework.modulith.observability;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.aop.TargetClassAware;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
import org.springframework.modulith.model.ArchitecturallyEvidentType;
|
||||
import org.springframework.modulith.model.ArchitecturallyEvidentType.ReferenceMethod;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Represents a type in an {@link ObservedModule}.
|
||||
@@ -33,6 +38,8 @@ import org.springframework.modulith.model.ApplicationModules;
|
||||
@RequiredArgsConstructor
|
||||
public class ObservedModuleType {
|
||||
|
||||
private static Collection<Class<?>> IGNORED_TYPES = List.of(Advised.class, TargetClassAware.class);
|
||||
|
||||
private final ApplicationModules modules;
|
||||
private final ObservedModule module;
|
||||
private final ArchitecturallyEvidentType type;
|
||||
@@ -59,7 +66,7 @@ public class ObservedModuleType {
|
||||
public Predicate<Method> getMethodsToIntercept() {
|
||||
|
||||
if (!type.isEventListener()) {
|
||||
return it -> true;
|
||||
return it -> !(ReflectionUtils.isObjectMethod(it) || IGNORED_TYPES.contains(it.getDeclaringClass()));
|
||||
}
|
||||
|
||||
return candidate -> type.getReferenceMethods() //
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2023 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 example.sample;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@Component
|
||||
public class SampleComponent {
|
||||
|
||||
void someMethod() {}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2023 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.modulith.observability;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import example.sample.SampleComponent;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.modulith.model.ApplicationModule;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
import org.springframework.modulith.model.ArchitecturallyEvidentType;
|
||||
import org.springframework.modulith.test.TestApplicationModules;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ObservedModuleType}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public class ObservedModuleTypeUnitTests {
|
||||
|
||||
static final ApplicationModules modules = TestApplicationModules.of("example");
|
||||
|
||||
ApplicationModule module = modules.getModuleByName("sample").orElseThrow();
|
||||
ArchitecturallyEvidentType type = module.getArchitecturallyEvidentType(SampleComponent.class);
|
||||
|
||||
ObservedModuleType observedType = new ObservedModuleType(modules, new DefaultObservedModule(module), type);
|
||||
|
||||
@Test // GH-106
|
||||
void onlyExposesUserMethodsAsToBeIntercepted() {
|
||||
|
||||
assertThat(observedType.getMethodsToIntercept()).satisfies(it -> {
|
||||
|
||||
assertThat(it.test(ReflectionUtils.findMethod(SampleComponent.class, "someMethod"))).isTrue();
|
||||
|
||||
assertThat(it.test(ReflectionUtils.findMethod(Object.class, "toString"))).isFalse();
|
||||
assertThat(it.test(ReflectionUtils.findMethod(Advised.class, "getTargetClass"))).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test // GH-106
|
||||
void considersExposedTypeAsToBeIntercepted() {
|
||||
assertThat(observedType.shouldBeTraced()).isTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user