Skip to content

Add Proxy.newProxyInstance detection for automatic package imports#7

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-classforname-detection-issue
Draft

Add Proxy.newProxyInstance detection for automatic package imports#7
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-classforname-detection-issue

Conversation

Copy link

Copilot AI commented Nov 6, 2025

When Proxy.newProxyInstance is called with interface types, bnd should automatically import packages referenced by those interface methods (return types, parameters). Currently only Class.forName has this detection.

Changes

Core Detection Logic (Clazz.java)

  • Track Proxy.newProxyInstance method reference alongside existing Class.forName detection
  • Detect bytecode pattern: anewarray (Class[]) → ldc/ldc_waastoreinvokestatic newProxyInstance
  • Process detected interfaces by loading class definitions and referencing all method signature types
  • Controlled by existing -noclassforname flag

Test Coverage (ClassParserTest.java)

  • Verify inline array pattern detection works
  • Verify -noclassforname flag disables detection
  • Verify field-based arrays are NOT detected (conservative safety)

Limitations

Only detects interfaces when the Class[] array is created inline:

// ✓ Detected - inline array creation
Proxy.newProxyInstance(loader, new Class<?>[] { ServletContext.class }, handler);

// ✗ Not detected - array from field/variable/parameter
private static final Class<?>[] INTERFACES = { ServletContext.class };
Proxy.newProxyInstance(loader, INTERFACES, handler);

Cannot reliably determine array contents from bytecode when sourced from fields, variables, or parameters. Conservative by design.

Example

Given:

interface MyInterface {
    javax.servlet.descriptor.JspConfigDescriptor getDescriptor();
}

Proxy.newProxyInstance(loader, new Class<?>[] { MyInterface.class }, handler);

Previously: javax.servlet.descriptor package not imported → runtime NoClassDefFoundError

Now: Package automatically detected and imported via interface method analysis

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • bndtools.jfrog.io
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED -Xms1024m -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.14-bin/38aieal9i53h9rfe7vjup95b9/gradle-8.14/lib/gradle-daemon-main-8.14.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.14-bin/38aieal9i53h9rfe7vjup95b9/gradle-8.14/lib/agents/gradle-instrumentation-agent-8.14.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.14 (dns block)
  • scans-in.gradle.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED -Xms1024m -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.14-bin/38aieal9i53h9rfe7vjup95b9/gradle-8.14/lib/gradle-daemon-main-8.14.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.14-bin/38aieal9i53h9rfe7vjup95b9/gradle-8.14/lib/agents/gradle-instrumentation-agent-8.14.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.14 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Propose a fix for bndtools#6920

It should work similar to how the classforname detection is implemented and we need a testcase for this new feature as well.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits November 6, 2025 05:52
Add detection for Proxy.newProxyInstance calls to automatically include packages referenced by proxy interface methods. This works similarly to the existing Class.forName detection.

Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Add test case to verify that proxy interfaces are NOT detected when the Class[] array comes from a field/variable. Add comments explaining that detection only works for inline array creation pattern.

Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Only create the proxyInterfaces ArrayList when proxy tracking is actually needed, avoiding unnecessary allocations for methods that don't use Proxy.newProxyInstance.

Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix detection issue similar to classforname implementation Add Proxy.newProxyInstance detection for automatic package imports Nov 6, 2025
Copilot AI requested a review from laeubi November 6, 2025 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants