Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2006

Tip: Looking for answers? Try searching our database.

Possible to use reflection to get constructor params/args

Thread view: 
Petterson Mikael - 28 Jun 2006 14:31 GMT
Hi,

Is it possible to use reflection to get the param/arg names i and d in
second constructor? ( I am using jdk 1.4).

cheers,

//mikael

public class Constructor1 {
      public Constructor1()
      {
      }

      protected Constructor1(int i, double d)
      {
      }
}
Hendrik Maryns - 28 Jun 2006 14:50 GMT
Petterson Mikael schreef:
> Hi,
>
[quoted text clipped - 14 lines]
>       }
> }

The names are absolutely meaningless, except in the inners of the
method, so there is absolutely no reason to want to get them.  What do
you REALLY want?

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Thomas Hawtin - 28 Jun 2006 17:27 GMT
> Is it possible to use reflection to get the param/arg names i and d in
> second constructor? ( I am using jdk 1.4).

No, the information is not present in the source file.

There are a number of workarounds:

 o APT (introduced in 1.5, part of javac in 1.6) gives access to
parameter names (it works on source). For instance see:
http://weblogs.java.net/blog/emcmanus/archive/2006/06/intermxbean_ref.html

 o Run some form of parser on your source to store the information in a
convenient format. You could use a complete parser (like APT), or add
tags within comments to the source.

 o Compile with variable name debugging enabled, and use ASM (or
similar) to read the class file.

 o Compile with line number and file name debugging. Read the
information for the constructors with either ASM or arrange for an
exception to be thrown/created (and parse the stack trace). Use a regex
to extract the information from the source (which will need to be easy
to regex). See: http://www.jroller.com/page/cpurdy?entry=yaul_trace

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.