开发者

Java - accessing strings in non-compiled .java files during runtime

开发者 https://www.devze.com 2023-03-20 16:00 出处:网络
I\'ve got a problem for you. I\'ve got a bunch of Java files (.java) sitting around and they all contain a class declaration and an array of strings. I need to do stuff with the array. What is the be

I've got a problem for you.

I've got a bunch of Java files (.java) sitting around and they all contain a class declaration and an array of strings. I need to do stuff with the array. What is the best way to access it?

I tried using JavaCompiler class, but that didn't seem to work - so should I use regex or something?

Here's a sample of what the files look like:

package com.mypack.costmgr;

public class Cost_en extends java.util.ListResourceBundle {

 static final Object[][] contents = new String[][] {
    {"ACTIVE", "ACTIVE"},
    {"Joe", "asfag"},
    {"lolcats", "cheezburger"},
    {"HELP", "OH GOD NOT THE BEES"},
    {"asdfffff", "hacks"}
 };

 public Object[][] getContents() {
     return contents;
 }
}

And there's probably a hundred of these f开发者_开发百科iles.

So, to summarize: what is the best way to gain access to that data?

(Obviously, I cannot simply compile them with my project.)


You have to compile the .java files and make them .class files. Then you put those .class files on your classpath. At this point you can now make a reference to the contents of each of those files. Since contents is static you can get a reference to it by doing the following:

class MyAwesomeClass  
 {  
    Object[][] myArray = Cost_en.contents;  
 }  

Resource bundles


Spring Roo has an interesting Java language parser and manipulation framework for their plugins. It's used to extract info from user created .java files as part of the code supporting AspectJ. Maybe you can create a Roo plugin to handle what you're trying to do?

0

精彩评论

暂无评论...
验证码 换一张
取 消