如何以编程方式创建Java文件

如何以编程方式创建Java文件

本文介绍了如何以编程方式创建Java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个util-class,该类写入 .java文件,用作掩盖程序-生成器。

I am creating a util-class which writes .java Files that act as coverter - generator.

该实用程序类将生成AConverter.java'(请参见下面的示例)。



我想知道如何编写实用程序类。


我搜索了一下,发现推荐使用apache bcel。但是我找不到从 String 编写 .java文件并使其在我的程序中运行的示例

This util-class will generate AConverter.java' (refer below sample)

I want know how to write the util-class.
I googled, and found the recommendation to use apache bcel. But I couldn't find an example to write the .java File from a String and have it working in my program.



期望是...



The expectation is...

class ADTO
{
    private String empId;
    private String empName;
    private String dept;

    //setters and getters
}

class ABO
{
    private String loginId;
    private String userName;
    private String group;

    //setter and getter
}

class AConverter
{
    public void doConvertFromDB(ADTO source, ABO dest)
    {
        dest.setLoginId(source.getEmpId());
        ...
    }

    public void doConvertFromBO(ABO source, ADTO dest)
    {
        dest.setEmpId(source.getLoginId());
        ...

    public ADTO getSourceClass()
    {
        return ADTO.class;
    }

    public ABO getDestClass()
    {
        return ABO.class;
    }
}

上述AConverter类将由新的Util-

The above class AConverter will generated by the new Util-class.

推荐答案

几乎可以肯定的是,尝试以其他方式进行此操作将使您受益,该方案可能会失败的方法是令人担忧的大。以下是一些建议:

You would almost certainly benefit from trying to do this a different way, the number of ways this scheme could fail is worryingly large. Here are some suggestions:


  1. 添加某种类型的转换方法:

  1. Add a caster method of some sort:

class ADTO
{
    private String empId;
    private String empName;
    private String dept;

    // setters and getters

    public ABO toABO() // caster method (ABO will need a toADTO() as well)
    {
        ABO a = new ABO();

        a.setSomething(getSomethingEquivalent());
        ...

        return a;
    }
}


  • 代理类,也许是预期的课程。您将需要2个,每个类派生一个。

  • A proxy class, perhaps a subclass of the intended class. You would need 2, one derived from each class.

    class ADTO_Proxy extends ADTO
    {
        private ABO abo;
    
        public ADTO_Proxy(ABO a)
        {
            super();
            abo = a;
        }
    
        @override
        public String getEmployeeId()
        {
            return abo.getLoginId();
        }
    
        // other setters and getters
    }
    


  • 合并类,而不是尝试制作适配器。可以使用以下命令轻松完成:

  • Rather than trying to make an adapter, merge the classes. Could be easily accomplished with the following:

    class ADTO
    {
        private String empId;
        private String empName;
        private String dept;
    
        // getters and setters for each variable by each name
        public String getEmployeeId()
        { return empId; }
        public String getLoginId()
        { return empId; }
    
        public String getEmployeeName()
        { return empName; }
        public String getUsername()
        { return empName; }
    
        public String getDepartment()
        { return dept; }
        public String getGroup()
        { return dept; }
    
        // setters
    }
    

    这也可能是

    HateYourselfLater™评级:

    HateYourselfLater™ Ratings:

    第一种方法的排名为2,是三者中最好的。获得评级,是因为您永远不会发现自己在两者之间意外切换,并且无需更改其他代码。

    The first method ranks a 2, the best of the three. Rating earned because you won't ever find yourself accidentally switching between the two and not much other code has to be changed.

    第二种方法的排名为-3,三个中间。赢得评级是因为您有时可能会混淆另一种类型的对象,并可能产生意想不到的副作用。如果您从代理类中省略设置器,则可以将其降低到0级,但这会限制功能。

    The second method ranks a -3, in the middle of the three. Rating earned because you may occasionally mix up objects that are of one type of the other, with possible unintended side effects. You can reduce this to a rating of 0 if you omit setters from the proxy classes, but this limits functionality.

    第三种方法得到-5,这是最差的三。获得评级是因为存在很多副作用,并且冗余代码可能会在以后使您绊倒。但是,您可以通过重构所有内容以仅使用一个类来使比率为1,但这可能会花费很多工作,并且您现在会为此讨厌自己。

    The third method gets a -5, the worst of the three. Rating earned because there is a lot of possibility for side effects and the redundant code will probably trip you up later. However, you can make rate 1 by refactoring everything to properly use only one class, but that might take a lot of work, and you'll hate yourself for it now.

    也就是说,您最初的想法是即时生成一个类,以在两个等级之间转换大约-10,因为这将非常难以维护,并且对基础类的任何更改非常敏感,并且很可能会被打破。

    That said, your original idea of generating a class on the fly to convert between the two ranks about a -10 because it will be horribly difficult to maintain and very sensitive to any changes of the underlying classes, and will probably be easy to break.

    HateYourselfLater™比例从-10到10,其中10表示最大的赞,-10表示最大的恨。

    HateYourselfLater™ Scale ranges from -10 to 10, with 10 being the largest amount of like, and -10 being the largest amount of hate.

    您想要反编译器。有几种Java反编译器可供选择,我列出一些:

    You want a decompiler. There are several java decompilers available to pick from, I'll list a few:


    • Showmycode-易于使用,体面的反编译,(因此不适用于公司资料),搞砸了内置的类名和嵌套的匿名类

    • Jad-可下载,CLI,可以运行,但是会产生难看的代码,Linux版本已过时(如有必要,请在Windows版本中使用wine进行安装),枚举枚举,foreach循环和一些try / catching

    • Fernflower-CLI,很难找到,三者的最佳输出,作者是SO用户,看起来像样的输出,有时搞乱try / catching

    • Showmycode - easy to use, decent decompiling, online (thus unsuitable for corporate material), screws up built in class names and nested anonymous classes
    • Jad - downloadable, CLI, works but produces ugly code, linux version is out of date (use windows version with wine if necessary) , screws up enums, foreach loops, and some try/catches
    • Fernflower - CLI, hard to find, best output of the three, author is SO user, decent looking output, screws up try/catches sometimes

    它们都不是完美的,但这仅仅是由于某些数据在编译过程中丢失了,反编译器必须猜测程序的原始外观。

    None of them are perfect, but that's just a result of the fact that some data gets lost during compilation, and decompilers must guess what the program originally looked like.

    这篇关于如何以编程方式创建Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-21 14:26