Re: Reflection, how to understand



John_Woo wrote:
Hi,

I'm trying understand jdk 5 reflection.

I know it's for loading/invoking method on the fly. I'm wondering, any
simple core-java example (not sort of EJB, rmi) illuestrated how an app
re-compile then invoke the newly classes/methods, and why need to do
that (recompile)?

AFAIK, it doesn't have to do with recompiling - it has to do with, as
you said, dynamically accessing to classes.

For example:

String classname = "org.some.class.TestClass";
Object[] parameters;
Object obj =
Class.forName(classname).getConstructor(Object[].class).newInstance(new
Object[] { parameters });

What this code snippet does is:

Get the class named "org.some.class.TestClass".
(Class.forName(classname)).
Then it proceeds to get a constructor for said class, one that requires
an object array as parameter ( getConstructor(Object[].class)).
Then it proceeds to instantiate the object using said constructor,
passing the Object array "parameters" as the parameters.

There you have it - I have loaded an instance of the class
org.some.class.TestClass - without having to know that name in the code
- the String classname could have been loaded from any source (in
particular, I use that code snippet for persistence of simple objects
from a database - load the className and the constructor parameters
from the database, execute the reflection voodoo to instantiate)

.



Relevant Pages

  • Re: Reflection
    ... Alternatively you can use Reflection, ... > constructor, how do i go about instantiating it. ... > but for some odd reason i never am able to instantiate my objects ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Calling an internal constructor
    ... there is nothing you can do to call this constructor from ... your constructor in code (even through reflection) so that your class can ... > internal constructor (The CurrencyManager class). ... > derived from BindingManagerBase, and though the documentation has a "Note ...
    (microsoft.public.dotnet.languages.csharp)
  • Generics and reflection
    ... I have this really ugly thing regarding generics and reflection (and ... doStuffWith() ), which means I have to check the ... may still throw some kind of access violation exception (from ... I don't know how to catch that in the constructor (the ...
    (comp.lang.java.programmer)
  • Re: default values
    ... reflection and call through reflection. ... >> or not the type derives from ValueType in some way. ... If it does not derive from ValueType, then it is a reference>> type, and the default value is null. ... >>> I'd have hoped that the boxed equivalent of the value type would have a>>> constructor, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Prevent DLL usage
    ... "Pavel Minaev" wrote in message ... and it seems to do the trick too. ... FullTrust assembly from using reflection to instantiate your classes ... I'd neglected to consider instantiation through reflection. ...
    (microsoft.public.dotnet.languages.csharp)