zoukankan      html  css  js  c++  java
  • 通过jdt解析spring mvc中url-类-方法的对应关系

    依赖
    <dependencies>

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

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
    </dependency>

    </dependencies>


    代码
    package com.xxx.fsq;

    import org.apache.commons.io.FileUtils;
    import org.eclipse.jdt.core.JavaCore;
    import org.eclipse.jdt.core.dom.*;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    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("XXXController.java"), "UTF-8"); //要解析的文件
    } catch (Exception e) {
    e.printStackTrace();
    }
    parser.setSource(src.toCharArray());
    CompilationUnit cu = (CompilationUnit) parser.createAST(null); //下个断点可以看看cu的types成员就是整个语法树


    System.out.println(cu);

    //get url
    String classUrl = null;
    String methodUrl = null;
    String className = null;
    String methodName = null;

    List types = cu.types();
    for (Object type : types) {
    TypeDeclaration typeDeclaration = (TypeDeclaration) type;
    className = typeDeclaration.getName().getIdentifier();
    List<Expression> expressionsClass = getExpressions(typeDeclaration);

    if (expressionsClass == null) {
    System.out.println("x");
    }

    for (Expression expression : expressionsClass) {
    classUrl = expression.toString().replace(""", "");
    }

    MethodDeclaration[] methods = typeDeclaration.getMethods();
    for (MethodDeclaration method : methods) {

    methodName = method.getName().getIdentifier();
    List<Expression> expressionsMethod = getExpressions(method);
    if (expressionsMethod == null) {
    continue;
    }

    for (Expression expression : expressionsMethod) {
    methodUrl = expression.toString().replace(""", "");
    System.out.println(className + " " + methodName + " " + classUrl + methodUrl);
    }

    }

    }
    }


    public static List<Expression> getExpressions(BodyDeclaration bodyDeclaration) {

    List methodModifiers = bodyDeclaration.modifiers();
    for (Object methodModifier : methodModifiers) {

    if (methodModifier instanceof SingleMemberAnnotation) {
    SingleMemberAnnotation modifier = (SingleMemberAnnotation) methodModifier;
    String typeName = modifier.getTypeName().getFullyQualifiedName();
    if (!typeName.equals("RequestMapping")) {
    continue;
    }
    Expression v = modifier.getValue();
    ArrayList<Expression> list = new ArrayList<Expression>();
    list.add(v);
    return list;


    } else if (methodModifier instanceof NormalAnnotation) {
    NormalAnnotation modifier = (NormalAnnotation) methodModifier;
    String typeName = modifier.getTypeName().getFullyQualifiedName();
    if (!typeName.equals("RequestMapping")) {
    continue;
    }

    List values = modifier.values();
    for (Object value : values) {
    MemberValuePair memberValuePair = (MemberValuePair) value;
    SimpleName simapleName = memberValuePair.getName();
    String name = simapleName.getIdentifier();
    if (!name.equals("value")) {
    continue;
    }

    Expression v = memberValuePair.getValue();
    if (v instanceof ArrayInitializer) {

    ArrayInitializer ev = (ArrayInitializer) v;
    List expressions = ev.expressions();
    return expressions;

    } else if (v instanceof Expression) {
    ArrayList<Expression> list = new ArrayList<Expression>();
    list.add(v);
    return list;
    }
    }
    }
    }
    return null;
    }
    }


  • 相关阅读:
    [NM]打开NetworkManager和wpa_supplicant的DEBUG接口
    TI am335x am437x PRU
    Ansible and FileBeta
    [gpio]devm_gpiod_get_optional用法
    TCP连接
    STM32云平台连接培训20180814
    select理解
    TypeScript躬行记(1)——数据类型
    React躬行记(15)——React Hooks
    React躬行记(14)——测试框架
  • 原文地址:https://www.cnblogs.com/fsqsec/p/8409510.html
Copyright © 2011-2022 走看看