Skip to content

Detect generated classes Closes #25 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/org/digma/MethodMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,20 @@ public static ElementMatcher<? super MethodDescription> create(TypeDescription t
return isMethod()
.and(isDeclaredBy(typeDescription))
.and(not(excludeNamesMatcher))
// .and(not(namedIgnoreCase("get"))) //todo: maybe add as default exclude filter
.and(not(methodsFilterByAnnotation()))
.and(not(isConstructor()))
.and(not(isSynthetic()))
.and(not(isBridge()))
.and(not(isSetter()))
.and(not(isGetter()))
.and(not(isMain()))
.and(not(isConstructor()))
.and(not(isFinalizer()))
.and(not(isHashCode()))
.and(not(isEquals()))
.and(not(isClone()))
.and(not(isToString()))
.and(not(isTypeInitializer()))
.and(not(isSetter()))
.and(not(isGetter()))
.and(not(isNative()))
.and(not(isSynthetic()))
.and(not(isBridge()))
.and(not(methodsFilterByAnnotation()))
.and(not(returns(named("kotlinx.coroutines.flow.Flow"))))
.and(not(nameContains("$")));
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/digma/TypeMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,22 @@ public static ElementMatcher<? super TypeDescription> digmaTypeMatcher(ElementMa
.and(not(hibernate6Types()))
.and(not(hibernate4Types()))
.and(not(isKotlinDataClass()))
.and(not(isSpringGeneratedClass()))
.and(not(nameContains("$")));
}


//todo: research and add more spring interfaces implemented by generated classes.
// see for example in spring data:
// org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator
// org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory
private static ElementMatcher<? super TypeDescription> isSpringGeneratedClass() {
return implementsInterface(namedOneOf(
"org.springframework.data.mapping.PersistentPropertyAccessor",
"org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator$ObjectInstantiator"));
}


private static ElementMatcher<? super TypeDescription> hibernate6Types() {
return implementsInterface(namedOneOf(
"org.hibernate.query.CommonQueryContract",
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/digma/WithSpanTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.bytebuddy.description.annotation.AnnotationDescription;
import org.digma.configuration.Configuration;
import org.digma.instrumentation.ExtendedObservability;
import org.digma.matchers.NotGeneratedClassMatcher;

import java.lang.annotation.Annotation;
import java.lang.instrument.Instrumentation;
Expand All @@ -20,6 +21,7 @@ public static void install(Instrumentation inst) {

new AgentBuilder.Default()
.type(TypeMatchers.create(Configuration.getInstance()))
.and(new NotGeneratedClassMatcher())
.transform((builder, typeDescription, classLoader, module, protectionDomain) -> {

try {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/digma/matchers/NotGeneratedClassMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.digma.matchers;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.utility.JavaModule;

import java.security.ProtectionDomain;

public class NotGeneratedClassMatcher implements AgentBuilder.RawMatcher {

@Override
public boolean matches(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, Class<?> classBeingRedefined, ProtectionDomain protectionDomain) {
if (protectionDomain != null && protectionDomain.getCodeSource() != null){
return protectionDomain.getCodeSource().getLocation() != null;
}
return true;
}
}
44 changes: 44 additions & 0 deletions src/test/java/org/digma/GeneratedClassMatchersTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.digma;

import com.example.testpkg.testclasses.MyTestClass;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
import org.digma.matchers.NotGeneratedClassMatcher;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class GeneratedClassMatchersTests {

@Test
public void testNotGeneratedClass() {

TypeDescription typeDescription = TypeDescription.ForLoadedType.of(MyTestClass.class);

assertTrue(new NotGeneratedClassMatcher().matches(typeDescription, MyTestClass.class.getClassLoader(), null, MyTestClass.class, MyTestClass.class.getProtectionDomain()));
}


@Test
public void testGeneratedClass() throws InstantiationException, IllegalAccessException {

Class<?> dynamicType = new ByteBuddy()
.subclass(Object.class)
.method(ElementMatchers.named("toString"))
.intercept(FixedValue.value("Hello World!"))
.make()
.load(getClass().getClassLoader())
.getLoaded();

assertEquals("Hello World!", dynamicType.newInstance().toString());


TypeDescription typeDescription = TypeDescription.ForLoadedType.of(dynamicType);

assertFalse(new NotGeneratedClassMatcher().matches(typeDescription, dynamicType.getClassLoader(), null, dynamicType, dynamicType.getProtectionDomain()));
}


}