• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with understanding reflection

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Hi. As you can see i am trying to understand how reflection works.

Can someone help with me out with the process "reflection" or any "Java application startup" goes through?

Have been reading the docs at webpage to understand the jvm process. The docs out there explain reflection and not the jvm process of "class loading" which i think plays a crucial role before reflection kicks in.

Any help to find appropriate docs or explanation of how a jvm starts up an application would be appreciated.

Ciao.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure exactly what you want to know - are you not clear about what the concept of reflection is, or are you just asking what it is useful for?

Normally, when you write a Java program, and you use classes that are for example in libraries (including the standard Java API), the compiler has to know about those classes - you have to have the class files for all those classes somewhere (in a directory or JAR file) and they have to be on the classpath or somewhere else so that the compiler can find them.

Suppose that you are writing a system that can be extended via plug-ins. There are many real systems which have this ability - for example, IDEs such as Eclipse and NetBeans have a plug-in architecture.

Your system needs to be able to load plug-ins and call methods in the classes of a plug-in. But note that those plug-ins are not available when you are writing your system, so you can't write your code so that you can directly call methods in plug-ins. In fact, it's most likely that the plug-ins won't even exist yet when you are writing your system, and you don't even know the class names of the classes in the plug-ins.

What you could then do is define an interface for plug-ins, for example something like this:

And then require that people who write plug-ins implement that interface.

In your system, you would use reflection to load a plug-in dynamically. You could get the name of the class that implements the PlugIn interface from a configuration file. You can then load that class with Class.forName(className); and use reflection to call the methods.
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it safe to say that the purpose of reflection is to allow you to create dynamic applications using a plug-in architecture?

Also is reflection then used only for creating instances at runtime and then invoking methods? Can it be used to modify the behavior of a method at runtime.

What i mean to ask is can i use reflection to change the order of method calls
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, though this would tie whatever you are writing very much into the implementation of whatever you are reflecting over. Not a very safe thing to do. For example, if you call a private method via reflection that relies on state initialized in the method that accesses it you'll get unexpected behaviour.
 
reply
    Bookmark Topic Watch Topic
  • New Topic