tinycodinglm / lang /lang-0026.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
5.43 kB
opencode trace
System You are an expert Java agent developer specializing in bytecode instrumentation, runtime agents, and the Java Instrumentation API.

You are an expert Java agent developer specializing in bytecode instrumentation, runtime agents, and the Java Instrumentation API.

User

How should I filter classes in a Java agent to avoid instrumenting JDK classes, bootstrap-loaded classes, and specific packages?

Assistant

Class Filtering Strategies

1. Package Name Filtering

java
import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;
import java.util.Set;

public class FilteredTransformer implements ClassFileTransformer {
    private final String includePrefix;
    private final Set<String> excludePrefixes;

    public FilteredTransformer(String includePrefix, Set<String> excludePrefixes) {
        this.includePrefix = includePrefix;
        this.excludePrefixes = excludePrefixes;
    }

    @Override
    public byte[] transform(ClassLoader loader, String className,
            Class<?> classBeingRedefined,
            ProtectionDomain protectionDomain,
            byte[] classfileBuffer) {
        if (className == null) return null;
        if (!className.startsWith(includePrefix)) return null;
        for (String exclude : excludePrefixes) {
            if (className.startsWith(exclude)) return null;
        }
        return instrument(classfileBuffer, className);
    }
}

Set<String> excludes = Set.of(
    "java/", "javax/", "sun/", "com/sun/", "jdk/",
    "org/xml/", "org/w3c/", "org/ietf/"
);
FilteredTransformer transformer = new FilteredTransformer("com/example/", excludes);

2. ClassLoader-Based Filtering

java
public class ClassLoaderFilter implements ClassFileTransformer {
    @Override
    public byte[] transform(ClassLoader loader, String className,
            Class<?> classBeingRedefined,
            ProtectionDomain protectionDomain,
            byte[] classfileBuffer) {
        // Bootstrap loader is null
        if (loader == null) return null;

        String loaderName = loader.getClass().getName();
        if (loaderName.equals("java.lang.ApplicationClassLoader")) {
            // Application code - instrument
        } else if (loaderName.contains("PluginClassLoader")
                || loaderName.contains("DelegatingClassLoader")) {
            // Framework/plugin classloader - maybe instrument
        } else {
            return null;
        }

        if (className != null && isBootstrapClass(className)) return null;
        return instrument(classfileBuffer, className);
    }

    private boolean isBootstrapClass(String className) {
        return className.startsWith("java/") || className.startsWith("javax/")
            || className.startsWith("sun/") || className.startsWith("com/sun/")
            || className.startsWith("jdk/");
    }
}

3. Efficient Composite Filter

java
public class CompositeClassFilter {
    private final String[] includePrefixes;
    private final String[] excludePrefixes;
    private final boolean excludeBootstrap;

    public boolean shouldInstrument(String className, ClassLoader loader) {
        if (className == null) return false;
        if (excludeBootstrap && loader == null) return false;

        boolean included = false;
        for (String prefix : includePrefixes) {
            if (className.startsWith(prefix)) { included = true; break; }
        }
        if (!included) return false;

        for (String prefix : excludePrefixes) {
            if (className.startsWith(prefix)) return false;
        }
        return true;
    }

    public static class Builder {
        private String[] includePrefixes = new String[0];
        private String[] excludePrefixes = new String[0];
        private boolean excludeBootstrap = true;

        public Builder include(String... prefixes) {
            this.includePrefixes = prefixes; return this;
        }
        public Builder exclude(String... prefixes) {
            this.excludePrefixes = prefixes; return this;
        }
        public CompositeClassFilter build() {
            return new CompositeClassFilter(this);
        }
    }
}

4. Common Pitfalls

java
// PITFALL 1: Not checking for null className
// WRONG: will NPE on anonymous classes
if (!className.startsWith("com.example.")) return null;
// RIGHT:
if (className == null || !className.startsWith("com.example.")) return null;

// PITFALL 2: Instrumenting proxy classes
if (className.contains("$") && !className.contains("$Lambda")) return null;

// PITFALL 3: Lambda classes have generated names
if (className.contains("$$Lambda")) return null;

// PITFALL 4: Module system (Java 9+)
try {
    Module module = Class.forName(className.replace('/', '.')).getModule();
    if (module.isNamed() && module.getName().startsWith("java.")) return null;
} catch (ClassNotFoundException e) { }

Key points: Filter by className prefix first (cheapest), check for null className, filter bootstrap-loaded classes (loader == null), and handle proxy/lambda classes specifically.