Create a class dynamically

Hi experts!
I am confroted to the following situation... I am building a search engine where a user can customise his search criteria fields and therefore I need to dynamically create a java bean class that will contain the input info if I do not want create one with all the available options (more than 40!!)
Is there such a thing in java
Any input is much appreciated...
Thanks!
bakameAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ucoolConnect With a Mentor Commented:
Well, I am not clear about your form bean class. The key thing is how you design your application.  if use reflection, you can define an interface class has a target/invoke method with its passing parameters. Then all your calling classes implement this interface. This base on you define your class, not the bytecode from third party or other vendor.
0
 
aozarovCommented:
You can use JBoss javaassits: http://sourceforge.net/project/showfiles.php?group_id=22866&package_id=80766&release_id=297879
You can also call Runtime.exec for Javac or call it directly thru com.sun.javac.Main (has a static main method that gets the same arguments
as if you provide them in the command line)
0
 
aozarovCommented:
BTW, did you check  lucene (as an alternative to your search engine) it is very powerfull text search engine: http://lucene.apache.org/java/docs/index.html
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
aozarovCommented:
For more about JavaAssist (the old page, before it became part JBoss company): http://www.csg.is.titech.ac.jp/~chiba/javassist/
0
 
aozarovConnect With a Mentor Commented:
There are other alternatives (with the first you can do just dynamic bytecode manipulation [you can create a base bean and then add/remove methods to it]):
http://cglib.sourceforge.net/ (more complex then javaassist)
http://jakarta.apache.org/bcel/ (probably the most flexible but is the most complex and very low level)
0
 
aozarovCommented:
>> am building a search engine where a user can customise his search criteria fields and therefore I need to dynamically
Also, if you only need to add/create new fields as string value (and not to dynamically create logic) then why not to store it in a Map or similar
instead of a Bean and avoid the hassel of bean modification/creation.
0
 
objectsConnect With a Mentor Commented:
Instead of creating a bean that contains all available options look to define a generic class that defines a single option, you may need to to create subclasses to implement the specific implementations of some options. You search options would then become a collection of these options.
Let me know if you need help implementing this.
0
 
ucoolCommented:
As objects said you can "define a generic class that defines a single option", one way to do this you can use Abstract Factory design pattern.
Another way you can define an implementation class to read a XML file which is define all the option type with its execute class.  Use java.lang.reflect.Method to invoke the executed class method.
0
 
bakameAuthor Commented:
>> You can use JBoss javaassits:

Never heard of it! Will follow your recommendations and read about it!

>> BTW, did you check  lucene...... why not to store it in a Map or similar instead of a Bean and avoid the hassel of bean modification/creation

I called it a search engine but actually it is a simple web app that queries my database for info based on the input provided thru a jsp page. Hence the need of a bean that will "remember" the input info and populates the text fields.

>>  "define a generic class that defines a single option".... you can define an implementation class to read a XML file which is define all the option type with its execute class.

This option looks the most appealing in my case... can you please elaborate more and give me more hints... I have little knowledge of java.lang.reflect.
I am using BEA workshop and using XML/XSD to convert to a java class is very simple...

Thank you all for your input!
0
 
aozarovCommented:
>> Hence the need of a bean that will "remember" the input info and populates the text fields.
Can't your jsp populate a HashMap instead of a Bean? You can save the map in the session or request the same way.
0
 
bakameAuthor Commented:
>> Can't your jsp populate a HashMap instead of a Bean? You can save the map in the session or request the same way.

A HashMap is not an option because it does not guarantee the order of the map.
0
 
ucoolCommented:
For using reflect and xml, here are hints: (I also prefer this way, since you can add 10 more criteria later and no need to modify your previous code. Very flexiable! just Plug and play!)

1. you have xml file like:

<searchOption>
      <criteriaName>criteriaA</criteriaName>
      <callClass>ClassA</callClass>
      ...
</searchOption>

-- all ClassA, ClassB ... have one same method getCriteria, but implementation is different.

2. you have parser to read xml and save the content in vector or whatever you like.

3. in execution class you define something like this:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

             try
                  {
                              // name is the class name read from xml which is match the target criteria
                        Class target = Class.forName(name);  
                        String output = "resultPath";
                        String result = null;
                        Class[] parameterTypes = new Class[] {String.class,String.class};
                         
                        Method aClassMethod;
                        Object[] arguments = new Object[] {strCriteria,output};
                        
                        aClassMethod = target.getMethod("getCriteria", parameterTypes);
                        aClassMethod.invoke(null, arguments);
                                                             
                        } catch (NoSuchMethodException e) {
                              System.out.println(e);
                              e.printStackTrace();
                        }
                  catch (ClassNotFoundException e)
                  {
                        System.out.println(e);
                        e.printStackTrace();
                  }
                  catch (IllegalAccessException e)
                  {
                        System.out.println(e);
                        e.printStackTrace();
                  }
                  catch (InvocationTargetException e) {
                        System.out.println(e);
                        e.printStackTrace();
                  }
            }
0
 
aozarovCommented:
>> A HashMap is not an option because it does not guarantee the order of the map.
You can use LinkedHashMap which does guarenty the order: see http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedHashMap.html
How does a Bean guarenty order?
0
 
bakameAuthor Commented:
Hi Experts!
Sorry it took me so long to get back to you but I was very busy with another project...

>> To Aozarov:
Thanks again for the links... I have plenty of meat to chew on! That's what I love about Java...There is so much going on!!
As to the idea of using a map, I agree with you, a bean does not guarantee the order either! It is just that I really wanted to take advantage of the tools provided by my environment (BEA workshop!) and form beans are so easy to use...

>> To Ucool:
I investigated the idea of using xml and reflection... But reflection is used to call existing methods and fields. In my case I want to create fields and methods on the fly... I will still need to use bytecode manipulation (as Aozarov suggested) to add or remove methods to my form bean class, right?
0
All Courses

From novice to tech pro — start learning today.