Reflection syntax sugar

Matthew Adams matthew at matthewadams.me
Fri Nov 18 13:03:57 PST 2011


Interesting slides, thanks.

Not a big deal, but why the switch from "#" to operator "::"?
Regardless of that answer, using the "::" operator for member access
syntax sugar would lead to the following (taken from my comments in
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5043025).  I think
these are pretty promising.  Your thoughts?

// field syntax
assert Foo::someField instanceof Field;

// method syntax
assert Foo::someMethod(String,int) instanceof Method;

// constructor syntax
assert Foo::() instanceof Constructor; // no-arg ctor
assert Foo::(String,Bar) instanceof Constructor; // ctor w/args

// package?
assert package::org.example instanceof Package; // maybe?

// class annotation
assert Foo::@SomeAnnotation instanceof Annotation;

// field annotation
assert Foo::someField at SomeAnnotation

// method annotation
assert Foo::someMethod(String,int)@SomeAnnotation instanceof Annotation;

// method parameter annotation
assert Foo::someMethod(String at SomeAnnotation,int) instanceof Annotation;

// constructor parameter annotation
assert Foo::(String at SomeAnnotation,int) instanceof Annotation;

// package annotation?
assert package::org.example at SomeAnnotation instanceof Annotation; // ?


On Fri, Nov 18, 2011 at 1:32 PM, Rémi Forax <forax at univ-mlv.fr> wrote:
>
> JDK8 will include a method reference syntax which can be seen
> as a typesafe way to handle a reference to a method.
>
> By example,
>  Runnable runnable = "foo"::length;
> should compile.
>
> The slide from Brian Goetz explaining lambda and method reference is available here:
> http://blogs.oracle.com/briangoetz/entry/slides_from_devoxx_talk_on
>
> You can test a prototype here:
>  http://jdk8.java.net/lambda/
> (the syntax implemented in the prototype for method reference use '#' instead of '::')
>
> Compared to reflection a method reference/lambda call through
> a functional interface should be faster than using the reflection
> (at least until the reflection is rewritten to use the same mechanism
> as the one used for implementing lambda).
>
> No syntax for typesafe property will be not included in jdk8,
> perhaps in 9 when JavaFX will be merged in the jdk.
>
> cheers,
> Rémi
>
> On 11/18/2011 07:16 PM, Matthew Adams wrote:
>>
>> Hi all,
>>
>> I'm hoping that reflection syntax sugar enhancements can make it into JDK8.
>>
>> It appears the consolidated issue, referencing several others&  which looks
>> the most promising IMHO, is at
>> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5043025
>>
>> It uses the javadoc-like syntax, primary the # operator, with some other
>> suggestions by me&  others.  I entered my own issue that was closed as a
>> duplicate of 5043025:
>> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6915224
>>
>> One thing about my request that deserves discussion is whether it's a
>> compile-time error if the fields, annotations, constructors&  methods
>> referred to are not present.  I was thinking that it should be selectable
>> by the developer, but then it would require a new @SupressErrors annotation
>> (like @SupressWarnings).  That might open up a can of worms, though.
>>
>> Please review the issues above and those reachable from them.  Is this
>> feasible?  It has good application in the area of query in JDO, JPA&
>> others.
>>
>> Thanks,
>> Matthew
>



--
@matthewadams12
mailto:matthew at matthewadams.me
skype:matthewadams12
yahoo:matthewadams
aol:matthewadams12
google-talk:matthewadams12 at gmail.com
msn:matthew at matthewadams.me
http://matthewadams.me
http://www.linkedin.com/in/matthewadams


More information about the jdk8-dev mailing list