Skip to content

Commit

Permalink
Closes #130 - Add option to exclude lambdas from instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusoe committed Mar 13, 2019
1 parent 2e26914 commit 9b588c0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public class InstrumentationSettings {
@NotNull
private Map<@NotBlank String, @Valid DataSettings> data = Collections.emptyMap();

/**
* Defines whether lambda classes should be excluded from the declared scope.
*/
private boolean excludeLambdas = true;

/**
* Allows all nested configs to evaluate context sensitive config properties regarding their correctness.
* This is called by {@link InspectitConfig#performValidation(ViolationBuilder)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@

import javax.annotation.PostConstruct;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* This class is responsible for deriving the {@link InstrumentationConfiguration} from
Expand Down Expand Up @@ -224,7 +222,7 @@ boolean isIgnoredClass(Class<?> clazz, InstrumentationConfiguration config) {
return true;
}

if (isLambdaWithDefaultMethod(clazz)) {
if (config.getSource().isExcludeLambdas() && clazz.getName().contains("$$Lambda$")) {
return true;
}

Expand All @@ -247,8 +245,4 @@ boolean isIgnoredClass(Class<?> clazz, InstrumentationConfiguration config) {
}
return false;
}

private boolean isLambdaWithDefaultMethod(Class<?> clazz) {
return clazz.getName().contains("/") && Stream.of(clazz.getMethods()).anyMatch(Method::isDefault);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void setupInstrumentationMock() {
lenient().when(instrumentation.isModifiableClass(any())).thenReturn(true);
lenient().when(settings.getIgnoredBootstrapPackages()).thenReturn(Collections.emptyMap());
lenient().when(settings.getIgnoredPackages()).thenReturn(Collections.emptyMap());
lenient().when(settings.isExcludeLambdas()).thenReturn(true);

config = InstrumentationConfiguration.builder().source(settings).build();
}
Expand Down Expand Up @@ -283,7 +284,7 @@ void testDoNotInstrumentMarkerWorks() throws Exception {
}

@Test
void ignoreLambdaWithDefaultMethod() throws Exception {
void ignoreLambda() throws Exception {
DummyClassLoader dcl = new DummyClassLoader(getClass().getClassLoader(), LambdaTestProvider.class);
Class<?> lambdasProvider = dcl.loadClass(LambdaTestProvider.class.getName());

Expand All @@ -293,35 +294,16 @@ void ignoreLambdaWithDefaultMethod() throws Exception {
}

@Test
void ignoreLambdaWithInheritedDefaultMethod() throws Exception {
DummyClassLoader dcl = new DummyClassLoader(getClass().getClassLoader(), LambdaTestProvider.class);
Class<?> lambdasProvider = dcl.loadClass(LambdaTestProvider.class.getName());

Class<?> lambdaWithInheritedDefault = (Class<?>) lambdasProvider.getMethod("getLambdaWithInheritedDefaultMethod").invoke(null);

assertThat(resolver.isIgnoredClass(lambdaWithInheritedDefault, config)).isTrue();
}

@Test
void notIgnoreClassWithDefaultMethod() throws Exception {
DummyClassLoader dcl = new DummyClassLoader(getClass().getClassLoader(), LambdaTestProvider.class, LambdaTestProvider.getAnonymousClassWithDefaultMethod());
Class<?> lambdasProvider = dcl.loadClass(LambdaTestProvider.class.getName());
void notIgnoreLambda() throws Exception {
lenient().when(settings.isExcludeLambdas()).thenReturn(false);

Class<?> anonWithDefault = (Class<?>) lambdasProvider.getMethod("getAnonymousClassWithDefaultMethod").invoke(null);

assertThat(resolver.isIgnoredClass(anonWithDefault, config)).isFalse();
}

@Test
void notIgnoreClassWithInheritedDefaultMethod() throws Exception {
DummyClassLoader dcl = new DummyClassLoader(getClass().getClassLoader(), LambdaTestProvider.class, LambdaTestProvider.getAnonymousClassWithInheritedDefaultMethod());
DummyClassLoader dcl = new DummyClassLoader(getClass().getClassLoader(), LambdaTestProvider.class);
Class<?> lambdasProvider = dcl.loadClass(LambdaTestProvider.class.getName());

Class<?> anonWithInheritedDefault = (Class<?>) lambdasProvider.getMethod("getAnonymousClassWithInheritedDefaultMethod").invoke(null);
Class<?> lambdaWithDefault = (Class<?>) lambdasProvider.getMethod("getLambdaWithDefaultMethod").invoke(null);

assertThat(resolver.isIgnoredClass(anonWithInheritedDefault, config)).isFalse();
assertThat(resolver.isIgnoredClass(lambdaWithDefault, config)).isFalse();
}

}


Expand Down

0 comments on commit 9b588c0

Please sign in to comment.