简体   繁体   English

从Java应用程序生成Java类

[英]Generate A Java Class From A Java Application

I am trying to create a data validation program that checks if a specific data type is valid by running each "type" (eg. name, age, gender) through its own validation method (each method is unique to its data type), the only problem is I want the application to be flexible to different data types and different amounts of data types. 我正在尝试创建一个数据验证程序,通过使用其自己的验证方法(每种方法对其数据类型都是唯一的)运行每个“类型”(例如,姓名,年龄,性别)来检查特定数据类型是否有效。唯一的问题是我希望应用程序对不同的数据类型和不同数量的数据类型具有灵活性。

Is there a way I can actually generate an entirely new Java application with methods for each type from the running Java application (for example "writing" a new class) during runtime once I have all the types? 一旦我拥有所有类型,有没有一种方法可以实际上生成一个全新的Java应用程序,其中包含运行中的Java应用程序中每种类型的方法(例如“编写”新类)?

Example Code: 示例代码:

public class JavaGen(){
    public static void main(String[] args){

        int dataLength = Integer.parseInt(JOptionPane.showInputDialog("Amount Of Data Types:"));
        String[] dataTypeList = new String[dataLength];

        //Fill up dataTypeList with user input
        writeJavaFile(dataTypeList);
    }

    public void writeJavaFile(String[] dataTypes){
        //Create the new class and its methods here using the array of dataTypes
    }
}

Which, for example, will then create a class of methods similar to this: 例如,然后将创建类似于以下内容的方法类:

public class ActualClass(){
    public String validate'What The dataTypes[x] was'(String infoToValidate){
        if(etc etc){
            return "etc etc";
        }else{
            return "";
        }
    }
}

You can create, compile and instantiate a java class in runtime. 您可以在运行时创建,编译和实例化Java类。

See this: http://www.java-tips.org/java-se-tips/java.lang/create-a-java-source-dynamically-compile-and.html 参见: http : //www.java-tips.org/java-se-tips/java.lang/create-a-java-source-dynamically-compile-and.html

Some libraries: cglib Codemodel 一些库: cglib Codemodel

Or you could use Nashorn. 或者您可以使用Nashorn。 (javascript interpreter) I can imagine a use case where user defines complex validation in javascript and then this would be a good approach. (JavaScript解释器)我可以想象一个用例,其中用户在javascript中定义了复杂的验证,然后这将是一个好方法。

Although in your case I would not recommend any of the above and just write it in the code. 尽管在您的情况下,我不建议您使用上述任何一种方法,只需将其写在代码中。 Data types validation sounds like something you want to have hard-coded. 数据类型验证听起来像是您想要硬编码的东西。

I can tell you right off that bat you are approaching a simple problem with an incredibly complex solution... 我可以马上告诉您,您正在通过一个极其复杂的解决方案来解决一个简单的问题...

I would guess you are looking for the instanceof keyword, or perhaps an abstract validation class. 您正在寻找instanceof关键字,或者也许是抽象验证类。

If you could be more specific with your intended functionality we could help you come up with a reasonable design pattern. 如果您可以对预期的功能进行更具体的介绍,我们可以帮助您提出合理的设计模式。

Directly to your question: Yes, you can generate and load classes in Java on the fly. 直接针对您的问题:是的,您可以即时生成和加载Java类。 It involves invoking the defineClass(String, byte[], int, int) method in the reflection package. 它涉及在反射包中调用defineClass(String, byte[], int, int)方法。 As input to this function you have to provide a byte array of the class you are instantiating. 作为此函数的输入,您必须提供要实例化的类的字节数组。 This doesn't sound like the appropriate approach to this problem. 这听起来不像是解决此问题的适当方法。

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

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