• 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

returning a String containing the code from a class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -
Does anyone know how I can create a method which returns a String or StringBuilder object containing the code from a class? For example:



The output of this program would simply be a text string (with appropriate newline characters) which contains the text code of the class.

Thanks,

Jonathan Glass
[ July 25, 2007: Message edited by: Jonathan Glass ]
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can do that without hardcoding!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Java is a compiled language, the source code isn't available at runtime. And some very tricky programming is needed to hardcode code that contains its own code! (In fact, it can only be done for very specific programs.) So either you hardcode the code into some other class, and just allow this one to fetch and return it, or, more likely, you need to make the *.java file available at runtime, so this class can read the file and return its contents.

Now, all that said -- what are you actually trying to accomplish? Maybe there's a more sensible alternative.
 
Jonathan Glass
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I'm trying to do. I have a program which runs tests on lots of historical price data. The big testing program is in one package, and a second package contains one class per test (each class defines the rules of a separate test). The program runs and writes the test results for the specified test class to a text file; in the file are various sorts of quantitative results of the the test. The text results file also contains the name of the class tested (which I obtain via reflection) and the values of parameters used for testing, etc. Typically the user than modifies the test class a little and runs the test again, and repeats this process many times. I thought it would be nice for each test results file to have the complete code of the testing algorithm printed at the bottom so there would be a very clear permanant record of exactly which test the results represent. I looked in the java reflection package but could only find ways of outputting method names and signatures but not the actual method code. I see what you mean, Ernest - the .class files don't contain source code, so how can one expect the program to output the source code?

There's a limit to how many hoops I'm willing to jump through to get this to work. For example I know there are programs that reverse engineer compiled code to produce source code, but I don't think I'd want to go there. As it is I keep the source files, and the text files with the test results provide reference to those source files (because they output the name of the class) so they can be looked up. It seems the user will have to rename each test class each time, though, in order to keep a discrete record of each test unless the source code can be stored elsewhere, e.g. in the results file. If anyone has a fairly simple solution, great; otherwise I'll have to make due.

Thanks for your consideration.

Jonathan
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might consider coding those rules in a more dynamic language, which would allow them to be inspected and modified at runtime (Hint: see my signature.)
 
Jonathan Glass
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest - Thanks for your feedback. I actually looked with interest at JESS after you replied to a different post of mine the other day. I've only been programming in Java for about six months. I know nothing of "rules-based" or "expert" systems, but the name "rule-based" intrigued me because the nature of my program is that the user writes rules to test trading systems over very detailed (i.e. second by second) historical price data of all sorts of financial instruments. Reading George Rudolph's piece on your site made me think that I should know what my specific need for JESS would be (besides the problem delineated in my latest post) before diverting my attention from Java itself which I'm just learning. I'll keep it in mind for down the road.
 
reply
    Bookmark Topic Watch Topic
  • New Topic