Java中的语法检查 [英] Syntax Checking in Java

查看:195
本文介绍了Java中的语法检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个具有嵌入式文本编辑器的程序。用户应该在编辑器中键入java代码。然后将编入编辑器的代码转换为字符串。我只想要一些可以检查缺少括号或没有捕获的尝试等等。它不需要编译。我已经看了很多,但我还是新编程,不能实现一些更难的东西。

I am currently working on a program that has an embedded text editor. The users are supposed to type java code in the editor. The code typed into the editor is then made into a string. I just want something that would check for missing parenthesis or a try without a catch, etc. It doesn't need to be compiled. I've looked around quite a bit, but I'm still new to programming and can't implement some of the harder stuff.

所以要缩短它:我我正在寻找一些可以分析语法错误代码的java包。

So to make it shorter: I'm looking for some java package that will analyze code for syntax errors.

推荐答案

从Java 6开始,你可以使用 JavaCompiler 编译文本并返回诊断对象,告诉您文件有什么问题(如果有的话)。因此,对于您的示例,您需要获取编辑器的内容并将其传递给JavaCompiler,运行它,并报告任何问题。下面的示例假定编辑器文本写出文件。

As of Java 6 you can use JavaCompiler to compile the text and get back Diagnostic objects that tell you what problems the file has (if any). So for your example you'd need to take the content of the editor and pass it to the JavaCompiler, run it, and report back any problems. Example that follows assumes editor text written out to a file.

示例代码:

要检查的文件

public class HelloBuggyWorld {
    String test // missing a semicolon

    public static void main (String [] args) {
        System.out.println('Hello World!');  // should be double quoted
    }
}

检查器

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class JavaSyntaxChecker {
    public static void main(String[] args) {
        System.out.println(JavaSyntaxChecker.check("/path/to/HelloBuggyWorld.java"));
    }

    public static List<String> check(String file) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> compilationUnits =
                fileManager.getJavaFileObjectsFromStrings(Arrays.asList(file));

        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call();

        List<String> messages = new ArrayList<String>();
        Formatter formatter = new Formatter();
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            messages.add(diagnostic.getKind() + ":\t Line [" + diagnostic.getLineNumber() + "] \t Position [" + diagnostic.getPosition() + "]\t" + diagnostic.getMessage(Locale.ROOT) + "\n");
        }

        return messages;
    }
}

输出

运行 main 方法。

[ERROR:  Line [5]    Position [124] HelloBuggyWorld.java:5: unclosed character literal
, ERROR:     Line [5]    Position [126] HelloBuggyWorld.java:5: ';' expected
, ERROR:     Line [5]    Position [131] HelloBuggyWorld.java:5: not a statement
, ERROR:     Line [5]    Position [136] HelloBuggyWorld.java:5: ';' expected
, ERROR:     Line [5]    Position [137] HelloBuggyWorld.java:5: unclosed character literal
]

这篇关于Java中的语法检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆