zoukankan      html  css  js  c++  java
  • 使用JDT转java代码为AST

    maven依赖
    <dependency>
    <groupId>org.eclipse.jdt</groupId>
    <artifactId>org.eclipse.jdt.core</artifactId>
    <version>3.13.0</version>
    </dependency>

    代码:
    import org.apache.commons.io.FileUtils;
    import org.eclipse.jdt.core.JavaCore;
    import org.eclipse.jdt.core.dom.AST;
    import org.eclipse.jdt.core.dom.ASTParser;
    import org.eclipse.jdt.core.dom.CompilationUnit;

    import java.io.File;
    import java.util.Map;

    import static org.eclipse.jdt.core.dom.AST.JLS8;

    public class JdtTest {

    public static void main(String[] args) {
    ASTParser parser = ASTParser.newParser(AST.JLS8); //设置Java语言规范版本
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    parser.setCompilerOptions(null);
    parser.setResolveBindings(true);

    Map<String, String> compilerOptions = JavaCore.getOptions();
    compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); //设置Java语言版本
    compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
    compilerOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
    parser.setCompilerOptions(compilerOptions); //设置编译选项

    String src = null;
    try {
    src = FileUtils.readFileToString(new File("LoginController.java"),"UTF-8"); //要解析的文件
    } catch (Exception e) {
    e.printStackTrace();
    }
    parser.setSource(src.toCharArray());
    CompilationUnit cu = (CompilationUnit) parser.createAST(null); //下个断点可以看看cu的types成员就是整个语法树
    System.out.println(cu);
    }
    }
  • 相关阅读:
    Android--多线程之Handler
    webkit-transition-
    结构体直接赋值
    shell 俄罗斯方块 杂记
    debian 开启daytime等服务 "xinetd"
    Linux中 etc/init.d “服务"
    <iOS>关于Xcode上的Other linker flags
    多线程总结
    git的使用与分支管理
    -ios项目中安装和使用CocoaPods
  • 原文地址:https://www.cnblogs.com/fsqsec/p/7845222.html
Copyright © 2011-2022 走看看