JDK-5050261 : java.lang.reflect.Method instance from a method name, similar to MyClass.class
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-05-20
  • Updated: 2006-11-14
  • Resolved: 2006-11-14
Related Reports
Duplicate :  
Description
Name: rmT116609			Date: 05/19/2004


A DESCRIPTION OF THE REQUEST :
  Today we can get a java.lang.Class instance directly from the class name using MyClass.class

A similar method should exist to get java.lang.reflect.Method instances from methods and java.lang.reflect.Field instances from fields.


JUSTIFICATION :
Users, specifically if they use

EventHandler.create(ActionListener.class, this, "myMethod")

can get a benefit if instead of using the name of the method, they use a constructor that directly references method names, for example, instead of the previous example, an IDE can generate:

EventHandler.create(ActionListener.class, this, MyClass.myMethod.method)

Using this way is checked at compilation time, and if the users renames the class or the method using IDE builtin reractoring, the EventHandler code will still be correct.

Of course for the last example to work java.beans.EventHandler will need overloaded methods requiring Method arguments instead of method names.

JDO and Hibernate also requires text strings that should contain class names and method names. I always use MyClass.class.getName() for classes, but can't solve the problem with methods.

Compiler safety is the big plus.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Some possible variants:
<MyClass>.<myMethod>.method <-- new reserved word? also field and constructor?
<MyClass>.<myMethod>.class <-- utilizing the same word as <MyClass>.class
<MyClass>.<myMethod>.instanceof <-- utilizing an existing reserved word
(Incident Review ID: 270429) 
======================================================================