type-safe Methods

Behrang Saeedzadeh behrangsa at gmail.com
Mon Jun 20 05:52:45 PDT 2011


Juan,

Right now in order to invoke a method on an object via reflection we
have to write something like:

final Integer aNumber = 1;
final Class clazz = Integer.class;
Method toString = clazz.getDeclaredMethod("toString");
System.out.println("toString = " + toString.invoke(aNumber));

This is not type-safe because we pass in the method name as a string
to the getDeclaredMethod.

Now let's pretend that Java had a method referencing construct symbol
such as #. Then we could have achieved the same thing in a more
concise and type-safe way:

final Integer aNumber = 1;
Method<String> toString = Integer#toString();
System.out.println("toString = " + toString.invoke(aNumber));

Cheers,
Behrang Saeedzadeh
http://www.behrang.org



On Mon, Jun 20, 2011 at 10:14 PM,  <juan at juanreza.com> wrote:
> Behrang,
> Please explain your idea. I am sorry that the JDK team did not understand
> it. If you wish to discuss it, I might be able to help you.
>
> Juan
>
> -------- Original Message --------
> Subject: Re: Any plans to support type-safe access to java.lang.Method?
> From: Erik Trimble <erik.trimble at oracle.com&gt;
> Date: Sun, June 19, 2011 4:53 pm
> To: Behrang Saeedzadeh <behrangsa at gmail.com>
> Cc: jdk8-dev at openjdk.java.net
>
> On 6/19/2011 5:07 AM, Behrang Saeedzadeh wrote:
>> Hi all,
>>
>> Are there any plans to support type-safe access to methods?
>>
>> For example something like:
>>
>> java.lang.Method toString = Object#toString();
>>
>> It would make writing APIs like JPA 2's type-safe criteria builder
>> more concise and readable.
>>
>>
>> Cheers,
>> Behrang Saeedzadeh
>> http://www.behrang.org
>
> Behrang,
>
>
>


More information about the jdk8-dev mailing list