简体   繁体   English

java:使用变量的值作为对象名称(不是eval()方式)

[英]java: using a variable's value as an object name (not the eval() way)

Ok, so coming from a background of mostly perl, and mostly writing dirty little apps to automate my tasks, I've read the pages about the evils of eval(), and I always use a hash (in perl). 好的,因此,我来​​自一个以Perl为主的背景,并且主要是写一些肮脏的小应用程序来自动化我的任务,因此,我已经阅读了有关eval()弊端的页面,并且我总是在perl中使用哈希。 I'm currently working on a little project (mostly for me and a couple of other technical people at work), for creating "canned response" e-mails. 我目前正在做一个小项目(主要是给我和其他几个工作中的技术人员),用于创建“罐头响应”电子邮件。 To allow for additions, subtractions, edits, etc., I'd like to essentially describe the response form(s) in XML, and have my app parse the XML and create the response forms at runtime. 为了允许增加,减少,编辑等,我想本质上以XML描述响应形式,并让我的应用程序解析XML并在运行时创建响应形式。 I want to use Java (to integrate it into an existing Java tool that I created), and boiled down to a trivial example, what I'm trying to do is take some XML like: 我想使用Java(将其集成到我创建的现有Java工具中),并归纳为一个简单的示例,我想要做的就是采用一些XML,例如:

<Form Name="first" Title="Title!">
  <Label Name="before">Your Request:</Label>
  <Textbox Name="input"/>
  <Label Name="after">has been completed.</Label>
  <Output>%before%%input%%after%</Output>
</Form>
<Form Name="second">
...

and from parsing that, I want to create a JFrame named first, which contains a JLabel named before with the obvious text, then a textbox, then another JLabel... you get the idea (I eventually want to use the output tag to control exactly how the response is formatted). 然后从解析开始,我想创建一个名为JFrame的JFrame,它包含一个以明显的文本命名为before的JLabel,然后是一个文本框,然后是另一个JLabel ...您明白了(我最终想使用输出标签来控制准确地格式化响应)。

I can parse the XML, and get the element name and such, but I don't know how to instantiate the Objects with a name that is the value of a variable, effectively: 我可以解析XML,并获取元素名称等,但是我不知道如何有效地使用作为变量值的名称实例化对象:

JFrame $(thisNode.getAttributes().getNamedItem("Name").getNodeValue()) = new JFrame(thisNode.getAttributes().getNamedItem("Title").getNodeValue());

I've read basically the whole first page of google results on java reflection, but I haven't come across anyone doing quite what I'm looking for (at least not that I could tell). 我基本上已经阅读了有关Java反射的Google搜索结果的整个第一页,但是我没有遇到任何正在做我所寻找的事情(至少不是我能说的)。 Having basically zero experience with reflection, I'm curious if this is something that can be accomplished using it, or if I should take the same approach as I would in Perl, and create a HashMap or HashTable of Objects, and tie them to a entry in a Hash of JFrames. 有了反射的经验基本上为零,我很好奇这是否可以使用反射实现,还是我应该采用与Perl中相同的方法,并创建一个HashMap或HashTable of Objects,并将它们绑定到JFrame哈希中的条目。 Or, I'm open to ideas that don't fall into those two categories. 或者,我对不属于这两类的想法持开放态度。 The Hash is sort of my stand-by answer, because I've done it in Perl plenty of times, and I'm sure I can make it work in Java, but if there's a feature (like reflection) that's made to do this task, then why not do it the way it was intended to be done? 哈希是我的备用答案,因为我已经在Perl中做过很多次了,而且我确定我可以使其在Java中工作,但是如果有一个功能(如反射)可以做到这一点任务,那么为什么不按照预期的方式去做呢?

What you're asking isn't possible in Java. 您要的内容在Java中是不可能的。 It doesn't work that way and these sorts of tricks, which are common in dynamic languages, aren't the Java way. 这种方式行不通,动态语言中常见的这些技巧不是Java方式。 You can certainly do: 您当然可以:

JFrame frame = JFrameBuilder.buildFromTemplate("frame.xml");

where you create a JFrameBuilder class that reads the XML and creates an object from it but the variable name can't be dynamic. 在这里创建一个JFrameBuilder类,该类读取XML并从中创建一个对象,但变量名不能是动态的。 You have to remember that there are two steps in Java. 您必须记住,Java有两个步骤。

  1. Java source files are compiled into bytecode; Java源文件被编译成字节码;
  2. The bytecode is read by a Java interpreter (JVM) and executed. 字节码由Java解释器(JVM)读取并执行。

What you want is essentially asking to execute code in step (1). 您实际上想要的是在步骤(1)中要求执行代码。 Now annotations can do things in a compile step (like adding interfaces, implementing methods and so on) but local variable naming is not one of those things. 现在,批注可以在编译步骤中完成(例如添加接口,实现方法等),但是局部变量命名不是其中之一。

You could (not necessarily that you should) generate Java source based on your XML, compile the generated code, and finally, execute the compiled code. 您可以(不一定要这样做)基于XML生成Java源代码,编译生成的代码,最后执行编译后的代码。 This could be more efficient if you saved the generated .class files and reused them instead of parsing the XML every time the program is run (it can check the timestamp on the XML and only generate and compile if it's been modified since the last code generation). 如果您保存生成的.class文件并重用它们,而不是在每次运行程序时都对XML进行解析(这样可以检查XML的时间戳,并且仅在上次代码生成后对其进行了修改,则仅生成和编译),这样可能会更有效。 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM