JDK-8322886 : Executable#getParameters returns Receivers only for Constructors
  • Type: Bug
  • Component: core-libs
  • Sub-Component: javax.lang.model
  • Affected Version: 17,21,22
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2023-12-29
  • Updated: 2024-05-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
a Receivers is basicly a way to annotate "this" in a method or non static inner class constructor. it is a parameter, but not a formal one. the distinction between parameters and formal parameters is blury and inconsistent in the relfection api. 
the doc for java.lang.reflect.Executable#getParameters only mentions parameters and not formal parameters, while returning formal parameters for methods and parameters for constructors.

annotation processing is clear in its doc. javax.lang.model.element.ExecutableElement#getParameters is documented to return formal parameters and it does. 

spec:
https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.8

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See zip file
run org.example.Main.main

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
warning: Annotation Processing MethodExample
warning: parameter count: 0
warning: Annotation Processing ConstructorExample
warning: parameter count: 0
4 warnings
Reflection MethodExample
parameter count: 0
Reflection ConstructorExample
parameter count: 0
ACTUAL -
warning: Annotation Processing MethodExample
warning: parameter count: 0
warning: Annotation Processing ConstructorExample
warning: parameter count: 0
4 warnings
Reflection MethodExample
parameter count: 0
Reflection ConstructorExample
parameter count: 1
parameters are: [ConstructorExample arg0]

---------- BEGIN SOURCE ----------
See the attached zip file
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
none. the Receiver support is very limited. via reflection it is impossible to know if a receiver is present. its only possible to construct one and see if it is annotated. yes -> receiver is present; no -> receiver is not annotated or not present. therefore it is not reliable possible to add a receiver to the parameters for methods only when it should be present or remove the receiver for construtors when it should not be present.

FREQUENCY : always



Comments
It is possible to filter receivers for javac-generated inner class constructors in core reflection. The receiver parameter can be checked by `Parameter::isImplicit()`. Other methods don't declare receivers as their receiver type annotations have their own storage category, and constructors without outer class instances don't have an enclosing instance/receiver in the first place. In fact, the receiver type annotations on inner class constructors have type path as receiver, not on the first formal parameter.
10-03-2024

The observations on Windows 11: JDK 11: N.A. JDK 17: Failed, Reflection ConstructorExample gets parameter count: 1 JDK 21: Failed. JDK 22ea+16: Failed.
03-01-2024