-
Type:
CSR
-
Resolution: Approved
-
Priority:
P3
-
Component/s: core-svc
-
None
-
behavioral
-
low
-
-
Java API
-
SE
Summary
Align the Instrumentation implementation with the java.lang.instrument spec which requires the premain and agentmain methods to be defined as public.
Problem
The java.lang.instrument spec clearly states:
The agent class must implement a public static premain method
similar in principle to the main application entry point.
. . .
The agent class must implement a public static agentmain method.
The current implementation of sun/instrument/InstrumentationImpl.java violates the specification as follows:
- the implementation allows
premainandagentmainmethods to be non-public but the spec clearly states "public static premain/agentmain" method. - if the agent class does not define the
premain/agentmainmethod, the implementation would invoke thepremain/agentmainfrom its superclasses if present but the spec clearly states that the agent class must implement "public static premain/agentmain" method.
Solution
The solution is to align implementation with the java.lang.instrument spec:
- the agent class must implement a public static
premainmethod if the agent is started from the command-line - the agent class must implement a public static
agentmainmethod if the agent is started after VM startup.
It will find the premain and agentmain method from the agent class but not its superclasses.
This change will break any existing java agent with a non-public static premain or agentmain methods.
Such agents will fail to load with an exception like below:
java.lang.IllegalAccessException: method <fully-qualified-class-name>.agentmain must be declared public
In addition, this change will break any existing agent which does not implement premain/agentmain but its superclass does.
The agent class in an unnamed module can still be non-public same as the Main class declaring public static void main allows. Support for modular java agent is a future enhancement ( https://bugs.openjdk.java.net/browse/JDK-6932391)
Also, see the GitHub Pull Request: https://github.com/openjdk/jdk/pull/1694
The latest webrev is: https://openjdk.github.io/cr/?repo=jdk&pr=1694&range=08
Specification
The implementation is being aligned with the java.lang.instrument spec which remains unchanged:
https://docs.oracle.com/en/java/javase/15/docs/api/java.instrument/java/lang/instrument/package-summary.html
- csr of
-
JDK-8165276 Spec states to invoke the premain method in an agent class if it's public but implementation differs
-
- Resolved
-