Friday, March 18, 2011

Reflection API gripes

java.lang.Method and java.lang.Constructor are strikingly close in concept. For all intents and purposes Constructors are little more than syntactic sugar. Yet there is no API acknowledgement of many their similarities.

Yes, they share some common interfaces and the same super class, but when you peal away the methods that are not accounted for by interfaces and superclasses, here's what you get:

java.lang.reflect.Constructor
public T newInstance(Object ... initargs)
public Annotation[][] getParameterAnnotations()
public Class[] getExceptionTypes()
public Class[] getParameterTypes()
public String toGenericString()
public Type[] getGenericExceptionTypes()
public Type[] getGenericParameterTypes()
public boolean isVarArgs()
java.lang.reflect.Method
public Object getDefaultValue()
public Object invoke(Object obj, Object... args)
public Class getReturnType()
public boolean isBridge()
public Type getGenericReturnType()
public Annotation[][] getParameterAnnotations()
public Class[] getExceptionTypes()
public Class[] getParameterTypes()
public String toGenericString()
public Type[] getGenericExceptionTypes()
public Type[] getGenericParameterTypes()
public boolean isVarArgs()
Of the methods, few are actually truly unique to that class type. Those would be the following methods

java.lang.reflect.Constructor
public T newInstance(Object ... initargs)
java.lang.reflect.Method
public Object getDefaultValue()
public Object invoke(Object obj, Object... args)
public Class getReturnType()
public boolean isBridge()
public Type getGenericReturnType()
A far smaller number, especially for Constructor. The remaining methods shared, but not accounted for in any superclass or interface, are:

Identical
public Annotation[][] getParameterAnnotations()
public Class[] getExceptionTypes()
public Class[] getParameterTypes()
public String toGenericString()
public Type[] getGenericExceptionTypes()
public Type[] getGenericParameterTypes()
public boolean isVarArgs()
These methods are just begging for an interface. Perhaps ParameterizedMember would be a good name?

Granted most Java development doesn't involve the reflection api, but those of us that do use it would really appreciate being thrown a bone. Especially with the growing amount of code and APIs that involve annotations. An interface similar to AnnotatedElement that can contain this critical getParameterAnnotations method would just be wonderful.

I'm quite certain there must have been some kind of debate. Likely one that ended in, "it's not really that important." Would love to hear from anyone involved.

I personally can't think of any downside to allowing us developers who deal with the guts of supporting annotation based APIs to get a little polymorphism in this are of the reflection API.

A bottle of my favorite rum to whomever can get this into Java 7.

12 comments:

Michael Campbell said...

"intense purposes"? =)

David Blevins said...

@Michael Wow, looks like I've been saying that incorrectly for years. Thanks, Michael!

Gunnar Morling said...

Seems like your wish might become reality :)

--Gunnar

Narender Godara said...

Packers and movers Faridabad
Packers and movers Gandhidham
Packers and movers Gandhinagar
Packers and movers Ghaziabad

amine lahragui said...

Your work is very good and I appreciate you and hopping for some more informative posts. happy new year 2016 new year 2016 bonne année 2016 happy new year 2016 images feliz año nuevo 2016 imagenes de año nuevo 2016 feliz año 2016

Deepak Negi said...

Hey Guys Must Check these :-

I can do that live show
I can do that live telecast
I can do that Show live streaming
muharram 2015 Images
muharram 2015 Hd Wallpapers
Happy muharram 2015 Quotes
Muharram 2015 Slogans
muharram 2015 Karbala sms
Muharram 2015 Quotes
10 muharram 2015 Talwar Matam Videos
Muharram 2015 Zanjeer Juloos Videos
muharram 2015 Poems
Hazrat Hussain Poetry 2015
muharram 2015 Qawwali Mp3
10 Muharram 2015 shayari
Muharram 2015 Status
Labbaik ya hussain mp3 Songs
Hazrat Hussain Hd Images
Happy Childrens Day 2015 Images
Mastizaade Box Office Collection
Mastizaade Funny Scenes
Mastizaade hd Videos
Mastizaade Hot Scenes
Mastizaade Movie Review
Mastizaade Romantic Scenes
Mastizaade Songs
Mastizaade trailer film
World Aids Day 2015 Quotes
World Aids Day 2015 Posters
World Aids Day 2015 Themes
World Aids Day 2015 Slogans

Mary Brown said...

Java EE article is practical oriented and real time examples. How Java EE address the enterprise development is very important. for that you need a practical orieneted Java Training you need.

Online Java Training
Online Java Training from India
Java Training Institutes in Chennai
Java Training in Chennai
Java Training

The Ahir said...


Happy New Year 2016 Messages

Happy New Year 2016 Images

New Year Wishes

IPL 2016 Schedule

IPL Schedule 2016

IPL 9 Schedule

IPL 2016

IPL 2016 Schedule

IPL Fixtures 2016

IPL 2016 Live Score

IPL 2016 Live

2016 IPL Schedule

ICC T20 World Cup 2016 Schedule

T20 World Cup 2016 Live Streaming

T20 World Cup 2016

Cricket World Cup 2016 Schedule

T20 Cricket World Cup 2016 Schedule

National Flag of India

indian national flag

indian flag pictures

national flag images

indian flags

Govt Jobs Jobs Recruitment Govt Jobs

Latest Jobs Jobs Latest Recruitment Govt Jobs

Ranveer Patil said...

This is great do you have a catologue if so I would love one to share with friends and family.
ipl 2016 live matches watch online here
watch cricket stream on cricbuzz ipl matches
ipl live streaming 2016 score watch online

rana pratap singh said...

Valentine Week List 2016 calendar
Cute Hug Day Images

jawad zaib said...

Your Site Is Very Good and The Post is Well On Topic, Thanks for Sharing it with us.

New Songs 2016
Minor League Wisconsin Baseball Player
New Sad Urdu Poetry 2016
Best Happy Valentine Day 2016 Wallpapers
apple iphone 7 hands on
Karachi Kings PSL 2016 Live Streaming
sad urdu poetry 2016
New HD Songs 2016
copyright free classical music 2016

Dalton Tyson said...

Valentines Day Free Heart Pictures HD
Romantic Valentines Day Poems
Romantic Valentines Day Sayings

Valentines 2016 Cute Poems
Free Valentines Day Picture Download