• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Your usage of reflection

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the extent to which you have put java reflection to use in your projects?
I have'nt used it much beyond instantiating classes that implement the same interface. We used to keep the mapping (SomeKey:ClassName)in a properties file / database.
Has any one used dynamic class creation in their projects?. I did a quick search and found that hibernate, tapestry etc use libraries like javaassit etc to generate the framework specific classes.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, we do use it in our project. We do use it in some sort of factory pattern and with the proxy pattern. It is a very large project and usually when new people start we have to explain a bit more than with the usual straightforward coding. I think this is a bit of a drawback, because you cannot really see which class is actually created. Therefore debugging / finding the right classes for fixing bugs is more difficult.

Maybe the authors can reply on this drawback.

regards,
friso
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some usages:
- discovering/loading plugins
- code inspection for diagram generation (unfortunately i haven't finished this project to publish it)
- bytecode generation (using either BCEL or ASM)

--
./pope
[the_mindstorm]
 
author & internet detective
Posts: 41775
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some of our usages of reflection:
- Equality comparison (certain fields have special rules)
- Utility to turn null strings into spaces
- EasyMock matcher (similar to equality)
- Instantiate classes for validation rules (like dynamic proxy)
- Sorting implementation - uses reflection to see if comparable already implemented. If so, use that, else use own implementation
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
Some usages:
- bytecode generation (using either BCEL or ASM)
./pope



Ali,

What kind of requirement were you addressing using bcel/byte code generation
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AspectWerkz was based at different moments on BCEL and ASM. I was more involved using ASM than BCEL, so I can talk more about it than BCEL.

--
./pope
[the_mindstorm]
 
author
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many of my usages have become examples in the book. They include:

- persistance
- stubbing
- remote proxying
- logging
- plug-in loading
- deferred initialization

er... and probably some others that I'm forgetting. Many of the above have to do with applications of the Proxy pattern, or other synergies with delegation. (I'm a real patterns enthusiast.) If I think of some others that I've done more recently, I'll be sure to post...

Best Regards,

Nate
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ali, nate, friso, jeanne for sharing information
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used reflections to implement an XML mapping inside my web framework.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean serializing/deserializing objects to xml?

--
./pope
[the_mindstorm]
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Surasak Leenapongpanit:
I used reflections to implement an XML mapping inside my web framework.



Originally posted by Ali Pope:
You mean serializing/deserializing objects to xml?



Ali, I think Surasak is talking about creating classes on-the-fly using the elements specified in XML file... Or not? :roll:

If so, it is simple, isn't it? We just fetch the element values from the XML and then create the classes accordingly using reflection API...
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ko Ko Naing:

I think Surasak is talking about creating classes on-the-fly using the elements specified in XML file... Or not?



I do not think so. It would be kindda performance problem for a web application.


If so, it is simple, isn't it? We just fetch the element values from the XML and then create the classes accordingly using reflection API...



It should be easy, but I wanted to understand why reflection and not directly manipulation

--
./pope
[the_mindstorm]
 
Nate Forman
author
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remembered last night--sometimes I'll use setAccessible() to do things that really need to get done, but for some reason, are not available. For instance:

- I wanted to wrap a production applet in another applet as a decorator. I wanted to do this so I could hang development tools off the outer one and tweak the applet parameters on the way in. JApplet doesn't expose all of the things necessary to do this, so I inspected the outer applet using reflection and obtained and passed on the necessary data using setAccessible().

I may have another compelling example, but I need to check with a co-worker to see if what he did last night worked...

Best Regards,

Nate
 
Nate Forman
author
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another one that I just thought of...

I use the observer pattern all over the place in my application. However, it always drove me crazy to have to reimplement the add/remove methods and the iterative delivery, especially when there was multithreading involved.

A colleague observed that AWT handles multithreading by a linked list instead of by synchronization. I really liked that idea, but still was annoyed with the idea of having to reimplement it for each listener interface...

It's annoyances like that which seem to prompt reflective solutions for me. I used java.lang.reflect.Proxy to implement a linked list delegation chain like the AWT one, except it uses Proxy to receive its listener invocations and reflective invocation to call its contained listeners. I use this guy all over the place now.

Nate
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nate Forman:
Remembered last night--sometimes I'll use setAccessible() to do things that really need to get done, but for some reason, are not available.



Ah, yes, I have that pattern. I remember that we had to do that to write a patched subclass of a Borland DataExpress class, but I don't remember the specifics. BTW, if anyone thinks about using that library - think again... :roll:
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
EasyMock matcher (similar to equality)



Just recently, I've done the same! I was really frustrated that none of the JDK's Event objects seem to implement the equals method.

BTW, we were using the EqualsBuilder from Jakarta Commons Lang - nice little library...
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two more uses come to mind:

- as someone else already stated, loading of "plugins", as declared in an xml configuration file, and
- implementation of generic validators, so that you can write, for example

new MandatoryPropertyValidator("propertyName", "error message").validate(myBean);
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use reflection all the time in my GUI code. I've subclassed button and written a generic Action Handler. The button has two (relevent) extra parameters: targetObject and targetMethod. I do all of my screen design using a BeanBox (NetBeans...), and this prevents me from having to write all the handler code. I just tell the button what it's target is and the method that it is to execute. This is especially useful when targeting some of my other GUI components (datastore-aware Panels, for example.) I can put the whole screen together in the BeanBox and not have to worry about it.

(Note that I could also acheive a similar effect by implementing the javax.swing.Action method and assigning the button that action. However, I don't always have control over the code that I necessarily want to target in this fashion, so reflection works great for me in this manner.)
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so what are the design patterns that can make use of Reflection API:
1.)Proxy
2.)Factory
3.)Observer

I think,we will be using Reflection API as a basis of building blocks of an application.
Looking at the advantages of reflection :
1.)Get to know/instantiate the class without knowing much about it and
2.)Accessing/invoking its methods.

Disadvantages:
1.)Hard to debug->This can kill too much of time in case of a big project.

There must be some solution to it still unknown to me though.
 
Jeanne Boyarsky
author & internet detective
Posts: 41775
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
I do not think so. It would be kindda performance problem for a web application.


Struts does something similar to creating classes on the fly with DynaForms. In an XML file, you say what fields are needed. I don't think Struts actually builds a class, but it is pseudo-reflection.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
Struts does something similar to creating classes on the fly with DynaForms. In an XML file, you say what fields are needed. I don't think Struts actually builds a class, but it is pseudo-reflection.



Hi Jeanne,
I'm not quite clear about the word "pseudo-reflection"... Does the fact, that Struts creates classes on-the-fly without actually building a class, mean "pseudo-reflection"? What is the main difference between reflection that we have been discussing with Nate, one of the authors, and "pseudo-reflection" that you talked about in the above post?

Thanks...
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ko Ko Naing:
I'm not quite clear about the word "pseudo-reflection"... Does the fact, that Struts creates classes on-the-fly without actually building a class, mean "pseudo-reflection"? What is the main difference between reflection that we have been discussing with Nate, one of the authors, and "pseudo-reflection" that you talked about in the above post?


The difference is that Struts doesn't make use of the Reflection API in implementing the DynaActionForm class. It just uses a Map to store an arbitrary set of "parameters" and only validates them according to the struts-config.xml. So, there's no Reflection involved at all but the outcome is similar to what could be achieved with Reflection as well -- no Java code needs to be written in order to support one more parameter.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:

Struts does something similar to creating classes on the fly with DynaForms. In an XML file, you say what fields are needed. I don't think Struts actually builds a class, but it is pseudo-reflection.



Jeanne afaik DynaForms is just a different name for DynaBeans (dynamic beans). Almost always DynaBeans implementation is based on a Map where the bean properties and values are stored with the addition of a validation against the set of available properties. So I wouldn't say here is something related to reflection.

--
./pope
[the_mindstorm]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jitesh Sinha:

Disadvantages:
1.)Hard to debug->This can kill too much of time in case of a big project.



I'm not sure I understand why you think the size of the project is an issue...

Using reflection pays back when you can use it to code generic code that otherwise had to be repeated over and over again. By reducing that duplication, you can actually make a system easier to understand (at least to someone who understands reflection) and to maintain - for example because a bug only has to be fixed in one place instead of ten or hundreds.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just remembered another use of reflection in our systems.

Our system is composed of a number of modules, and every module can optionally have a XML configuration file. The structure of the file is defined usign a schema and we use JAXB to generate beans from them and parse the configuration files.

Now we had a feature request for being able to "inherit" configurations. That is, you should be able to have a configuration file refer to a different one and only override specific values.

So, what we have after JAXB parsed the configuration is a bean filled with all the values of the parent configuration, and another bean filled with only the alternative values.

What I did was writing a reflection algorithm which traversed the bean and copied the values to the parent bean. It's a rather complex thing, because it needs to take care of a nested bean structure, including collections, and there are some specifics to how JAXB generates those beans, and I couldn't have made it without writing an extensive suite of unit tests for it.

But now that it works (for some months now), it works like a charm. Need a new configuration property? Just adjust the schema. A new module needs configuration. Just write a new schema. Run Ant, and everything else works automagically.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja some questions:


[...] generate beans from them and parse the configuration files.



Is this refering to populating/setting beans values or really creating new classes? (i guess the 1st one).


What I did was writing a reflection algorithm which traversed the bean and copied the values to the parent bean.

.

Is this solution somehow similar to Castor solution?

cheers,
--
./pope
[the_mindstorm]
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:

The difference is that Struts doesn't make use of the Reflection API in implementing the DynaActionForm class. It just uses a Map to store an arbitrary set of "parameters" and only validates them according to the struts-config.xml. So, there's no Reflection involved at all but the outcome is similar to what could be achieved with Reflection as well -- no Java code needs to be written in order to support one more parameter.



Then, Lasse, I guess Struts' way is more efficient than Sun's reflection... Is there any disadvantage that Struts is not able to fulfil a requirement, while Sun's reflection has some ways to do?

So Struts' implementation is called pseudo-reflection and normal reflection refers to Sun's Java Reflection... isn't it?

Thanks a lot for your explanation...
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
Is this refering to populating/setting beans values or really creating new classes?



JAXB both generates Java source code for the beans and provides an API for binding them to the XML files (i.e. populating/persisting).

Is this solution somehow similar to Castor solution?



From the little I know about Castor, I'd say that its XML binding is somewhat similar to JAXB, though probably more flexible, as it allows mappings between arbitrary existing beans and XML files.

I'm not aware of Castor providing a way to populate beans from other beans, but that might just show my lack of knowledge about it.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guess Ali has made it clear that there is no reflection involved wrt struts dyna beans. As for 'pseudo-reflection', i think jeanne just came up with that term.
 
Nate Forman
author
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:


Jeanne afaik DynaForms is just a different name for DynaBeans (dynamic beans). Almost always DynaBeans implementation is based on a Map where the bean properties and values are stored with the addition of a validation against the set of available properties. So I wouldn't say here is something related to reflection.

--
./pope
[the_mindstorm]



It seems like the program is storing program metadata and querying it later to make decisions at runtime. While this does not utilize the java core reflection API, it is the same idea. If this metadata was stored by the programming language itself, it could be accessed via reflection. I'm not saying that this is reflection, but it certainly isn't completely unrelated...
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hehe. Unfortunately i still do not agree. I do not consider that a piece of data that is used at runtime verification can be seen as related to reflection.

--
./pope
[the_mindstorm]

ps: maybe i am wrong about dyma beans but as far as i can imagine here it is:


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

Originally posted by Nate Forman:
Many of my usages have become examples in the book. They include:

- persistance
- stubbing
- remote proxying
- logging
- plug-in loading
- deferred initialization

er... and probably some others that I'm forgetting. Many of the above have to do with applications of the Proxy pattern, or other synergies with delegation. (I'm a real patterns enthusiast.) If I think of some others that I've done more recently, I'll be sure to post...

Best Regards,

Nate




Can u be more specific about how plug-in loading can use the reflection.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kishore Dandu:
Can u be more specific about how plug-in loading can use the reflection.

Dynamic class loading.

1) Read a JAR file from somewhere using java.util.jar.JarFile
2) Ask the JarFile for an entry like "plugin.xml"
3) If such entry is found, read/parse the plugin descriptor
4) Register the plugin descriptor's information somewhere
5) Initialize a ClassLoader of some sort to give the "core" of your application access to the plugin JAR's classes (if they're not already in classpath)
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read Matt's description (of the same algo) here

--
./pope
[the_mindstorm]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
You can read Matt's description (of the same algo) here


Ah. I must've missed that one.

I once implemented a simple ClassLoader-based plugin loader framework and it only took a couple of hours to get the plugin loader working properly. I wonder if it would be significantly more difficult to do the same for a J2EE application... Any idea about that?
 
Jeanne Boyarsky
author & internet detective
Posts: 41775
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:

Jeanne afaik DynaForms is just a different name for DynaBeans (dynamic beans). Almost always DynaBeans implementation is based on a Map where the bean properties and values are stored with the addition of a validation against the set of available properties. So I wouldn't say here is something related to reflection.


Yes, DynaForms and DynaBeans are the same thing. I didn't mean to imply that they used reflection. I just meant that they build a data structure (alebit a Map) at runtime from an XML file.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:

I wonder if it would be significantly more difficult to do the same for a J2EE application... Any idea about that?



JBoss is using this mechanism to add functionality to their container. The core is very simple (hehe - not quite), every other part of the spec being added by a different sar.

--
./pope
[the_mindstorm]

ps: probably i have exagerated a bit on the above lines.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:

From the little I know about Castor, I'd say that its XML binding is somewhat similar to JAXB, though probably more flexible, as it allows mappings between arbitrary existing beans and XML files.



Unfortunately, somehow I have missed the JAXB part of your post. Thought everything is happening at runtime. Thanks for clarification.

Originally posted by Ilja Preuss:

I'm not aware of Castor providing a way to populate beans from other beans, but that might just show my lack of knowledge about it.



This part is indeed missing from Castor. You are right. Most probably the full solution was a combination of Castor and BeanUtils, in which case taken into account at least the size of Castor lib, a custom solution is probably more appropriate.

--
./pope
[the_mindstorm]
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
JBoss is using this mechanism to add functionality to their container. The core is very simple (hehe - not quite), every other part of the spec being added by a different sar.



FYI, ATG Dynamo 7.0 is also using this mechanism in their container... But u know, it's pretty complicated to get an idea of how things are done in ATG Dynamo, (which are pretty famous for its unuser-friendliness )

Just my 2 cents...
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
JBoss is using this mechanism to add functionality to their container. The core is very simple (hehe - not quite)


"Very simple" is, in my opinion, far from the truth. The architecture itself may very well be simple but the implementation is not. At least not simple enough for me to figure out how they've implemented remoting for the EJB container -- I looked in there once and found a cryptic mess that looked like Java
 
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic