Java - 在运行时访问未编译的 .java 文件中的字符串

发布于 2024-11-19 21:43:14 字数 635 浏览 4 评论 0原文

我有个问题想请教你。

我有一堆 Java 文件 (.java),它们都包含一个类声明和一个字符串数组。我需要对数组做一些事情。访问它的最佳方式是什么?

我尝试使用 JavaCompiler 类,但这似乎不起作用 - 那么我应该使用正则表达式还是其他东西?

以下是这些文件的示例:

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;
 }
}

可能有一百个这样的文件。

那么,总结一下:访问该数据的最佳方式是什么?

(显然,我不能简单地将它们与我的项目一起编译。)

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 files.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-11-26 21:43:15

您必须编译 .java 文件并将其制作为 .class 文件。然后将这些 .class 文件放入您的类路径中。此时,您现在可以引用每个文件的内容。由于 contents静态,因此您可以通过执行以下操作来获取对其的引用:

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

资源包

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

是你 2024-11-26 21:43:15

Spring Roo 为其插件提供了一个有趣的 Java 语言解析器和操作框架。它用于从用户创建的 .java 文件中提取信息,作为支持 AspectJ 的代码的一部分。也许您可以创建一个 Roo 插件来处理您想要做的事情?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文