Java 使用输入字符串生成对象和函数

Java 使用输入字符串生成对象和函数,java,reflection,Java,Reflection,假设我有一个名为file.txt的文件,它由java中反射方法的脚本组成。假设其中一些是: new <id> <class> <arg0> <arg1> … creates a new instance of <class> by using a constructor that takes the given argument types and stores the result in <id>. call <i

假设我有一个名为file.txt的文件,它由java中反射方法的脚本组成。假设其中一些是:

new <id> <class> <arg0> <arg1> … creates a new instance of <class> by using 
a constructor that takes the given argument types and stores the result in <id>.
call <id> <method> <arg0> <arg1> …  invokes the specified <method> that 
takes the given arguments on the instance specified by <id> and prints the answer. 
print <id>  prints detailed information about the instance specified by <id>. See 
below for Object details.
new…使用创建的新实例
采用给定参数类型并将结果存储在中的构造函数。
调用…调用指定的
在指定的实例上获取给定参数并打印答案。
打印打印指定的实例的详细信息。看见
下面是对象详细信息。

文件中的脚本将作为字符串在程序中拾取。如何将其转换为上面指定的参数进行反射。我对这件事视而不见!如果我是java新手,请提供一些代码帮助

首先,这是一个解析问题。你需要做的第一件事是把你的输入分解成可管理的块。因为您似乎在使用空格分隔组件,所以这应该是一件相当容易的事情

因为每行有一个命令,所以要做的第一件事是将它们分成行,然后根据空格分成单独的字符串。解析是一个足够大的主题,值得提出自己的问题

然后逐行执行,在行中的第一个单词上使用if语句来确定应该执行什么命令,然后根据对其他单词的处理来解析它们

大概是这样的:

public void execute(List<String> lines){
    for(String line : lines){
        // This is a very simple way to perform the splitting. 
        // You may need to write more code based on your needs.
        String[] parts = lines.split(" ");

        if(parts[0].equalsIgnoreCase("new")){
            String id = parts[1];
            String className = parts[2];
            // Etc...
        } else if(parts[0].equalsIgnoreCase("call")){
            String id = parts[1];
            String methodName = parts[2];
            // Etc...
        }
    }
}
public void execute(列表行){
用于(字符串行:行){
//这是执行拆分的一种非常简单的方法。
//您可能需要根据需要编写更多代码。
字符串[]部分=行。拆分(“”);
if(部件[0].equalsIgnoreCase(“新”)){
字符串id=部件[1];
字符串className=parts[2];
//等等。。。
}else if(部件[0].equalsIgnoreCase(“调用”)){
字符串id=部件[1];
字符串methodName=parts[2];
//等等。。。
}
}
}