简体   繁体   English

使用javaparse解析for循环时出错

[英]Error while parsing for-loop using javaparse

It's continuous of my previous question: How to parse a for-loop using Javaparser? 这是我先前的问题的延续: 如何使用Javaparser解析for循环? . I'm trying to parse this incorrect for-loop: FOR(j, k, 15, >, -); 我试图解析这个不正确的for循环: FOR(j, k, 15, >, -); to the correct form: for (var j = k; j > 15; j--){} . 格式正确: for (var j = k; j > 15; j--){} It means to parse Class.java to the class ClassAltered.java. 这意味着将Class.java解析为ClassAltered.java类。 My code should create the new class ClassAltered.java with the correct loop-for. 我的代码应使用正确的循环创建新类ClassAltered.java。 Now my method to parse the for-loop is commented (I haven't finished it), but anyway I have the same error. 现在,注释了我分析for循环的方法(我尚未完成),但是无论如何我都遇到了同样的错误。 I'm sure that javaparser could parse incorrect statements, but I have problems with the sights ">", "<=", "+", "-". 我确定javaparser可以解析不正确的语句,但是我对“>”,“ <=”,“ +”,“-”的外观有疑问。 If I delete this sights everything works. 如果我删除此景点,一切正常。 I use javaparser-core-3.1.0.jar 我使用javaparser-core-3.1.0.jar

Here is my error: 这是我的错误:

在此处输入图片说明

Here is my Class.java which I want to parse: 这是我要解析的Class.java:

public class Class {
public static void main(String[] args) {
    method1();
    method2();
}

public static void method1() {
FOR(j, k, 15, >, -);
System.out.println("FOR(j, k, 15, >, -) test");
}

public static void method2() {
FOR(j, 0, 10, <=, +);
System.out.println("FOR(j, 0, 10, <=, +) test");
}
}

Here is my Main.class: 这是我的Main.class:

 import com.github.javaparser.JavaParser;
 import com.github.javaparser.ast.CompilationUnit;
 import com.github.javaparser.ast.expr.*;
 import com.github.javaparser.ast.stmt.ForStmt;

 import javax.tools.*;

 import java.io.*;
 import java.util.*;

 public class Main {

 public static void main(String[] args) throws IOException {
    final String fileName = "Class.java";
    final String alteredFileName = "src\\ClassAltered.java";
    CompilationUnit cu;
    try(FileInputStream in = new FileInputStream(fileName)){
        cu = JavaParser.parse(in);
    }

    //cu.getChildNodesByType(ForStmt.class)
    //.forEach(Main::setForStatement);

    cu.getClassByName("Class").get().setName("ClassAltered");

    try(FileWriter output = new FileWriter(new File(alteredFileName), false)) {
        output.write(cu.toString());
    }

    File[] files = {new File(alteredFileName)};
    String[] options = { "-d", "out//production//Synthesis" };

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
        Iterable<? extends JavaFileObject> compilationUnits =
            fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files));
        compiler.getTask(
            null,
            fileManager,
            diagnostics,
            Arrays.asList(options),
            null,
            compilationUnits).call();

        diagnostics.getDiagnostics().forEach(d ->    System.out.println(d.getMessage(null)));
    }
}

 /*private static void setForStatement(ForStmt forStmt) {

    MethodCallExpr initializer = (MethodCallExpr) forStmt.getInitialization().get(0);

    if (initializer.getArguments().size() == 5
            && initializer.getArgument(1) instanceof IntegerLiteralExpr
            && initializer.getArgument(2) instanceof IntegerLiteralExpr) {
        IntegerLiteralExpr a1 = (IntegerLiteralExpr) initializer.getArgument(1);
        IntegerLiteralExpr a2 = (IntegerLiteralExpr) initializer.getArgument(2);
    }
}*/
}

I do not think you should relay on the way JavaParser deal with incorrect statements. 我不认为您应该中继JavaParser处理错误语句的方式。 Parsing incorrect statements is something complicate and brittle. 解析不正确的语句是一件复杂而又脆弱的事情。 There is no "right" way to parse something that is not Java in a Java file. 没有“正确”的方法来解析Java文件中非Java的内容。

I would wonder how that code ended up in a JavaParser file: was it generated by some faulty code generator? 我想知道代码是如何最终存储在JavaParser文件中的:它是由错误的代码生成器生成的吗? Any chance you can fix that generator instead of reverse engineering an incorrect file? 您是否有可能修复该生成器,而不是对错误的文件进行反向工程?

Disclaimer: I am a JavaParser contributor 免责声明:我是JavaParser贡献者

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

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