JDK-6526540 : Language change: Class method to get pointer to method
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2007-02-20
  • Updated: 2011-02-16
  • Resolved: 2007-02-20
Related Reports
Duplicate :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
It would be very useful if Java supported a method in Class that allowed one to get a pointer to a method without using a String for the name:

  Class<X> cls = X.class;
  Method meth = cls.getMethod(getColor);

This could then be compile-time checked to see that the method 'getColor' actually exists for the class X.

The example that I have is as follows: my application uses 'DataAccessor' objects to contain things stored in the database.  A Person object might have methods 'getFirstName', 'getLastName', 'getPhone', etc.

I have a SortTable class that can display collections of DataAccessors given a List<String> of method names.  I would like the compiler to tell me that I misspelled 'getFristName', instead of waiting until runtime to give me a NoSuchMethodException.

JUSTIFICATION :
See above

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
See above

Comments
EVALUATION A language-level construct to obtain method (and field) references has long been requested elsewhere.
20-02-2007