• 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

Is it possible to create an Object from a source code String?

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, moose-people. I come in peace. This is my first post here and I have a question about dynamically creating objects. Let's say I have a String containing something like "new Date()".
Is there a way to evaluate that string and return an Object out of it? I have looked at the javax.tools package but I can't see any way to retrieve an object.
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure there is. It depends on the format of the String.

If you describe exactly how the String format generally looks, we might suggest some ideas.

And welcome to CodeRanch
 
Ranch Hand
Posts: 43
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

As correctly mentioned by Stephan, yes it is very much possible, but at the same time we would like to know more on two points,

a String containing something like "new Date()"


Need some more information on that. (Exactly how are going to do that.)

Is there a way to evaluate that string


What kind of evaluation (I aasume you mean String manupulation) is going to perform ?

Also I think the following link will be helpful for you,
https://coderanch.com/t/385084/java/java/creating-simple-java-objects-dynamically

Thanks
Joydeep






 
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
In dynamic programming languages, for example JavaScript or Python, you can easily do such a thing. But Java is a compiled language, it's not dynamically interpreted from source code, so it is not easy, and there is no built-in way, to evaluate arbitrary Java statements in a string at runtime.

You might be able to do something like what you want using BeanShell.
 
Rob Micah
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to create a collection of Objects that are created dynamically based on a set of Strings that contain Java expressions. These objects will be represented by a select list in a web interface. I'm sort of playing the middle man in this operation. Does that make sense or should I go into more detail?
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe if you tell us the purpose of it.

This seems like a very complicated and probably unnecessary way of doing things. Like Jesper hinted, there are languages better suited for this sort of thing.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there are a finite number of possibilities that will be presented in a dropdown, it might just make sense to have all of them pre-evaluated (and perhaps stored in a Map) and just choose the one that matches the dropdown selection.
 
Rob Micah
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my situation in detail. I am using JasperReports in a web application. Jasper lets users set default values for its parameters using Java expressions. Unfortunately it has no built-in support for providing a pre-defined list of values for a parameter. That's where I'm trying to apply this technique; by letting users specify in a list of properties Java expressions just like they can do for the default value.

So the flow looks like this:

  • A developer creates a report, compiles it, and places it in a pre-defined directory for the web application to read.
  • A user sends a request through a browser for a certain report
  • My web application reads the compiled report parameters and dynamically generates a page for the user to enter parameters.
  • The web app runs the report and returns its output


  • So step 3 is where I need this support. Granted this is ambitious and I have somewhat accomplished this by now using the ScriptEngine class with Javascript but it would be nice to keep things consistent for the people who have to design these reports.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic