通过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 + "\t" + methodName + "\t" + 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;
}
}
通过jdt解析spring mvc中url-类-方法的对应关系的更多相关文章
- Spring MVC中一般类使用service
在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...
- spring mvc中的控制器方法中的参数从哪里传进来?
编写控制器方法的时候很奇怪,spring是怎么知道你控制器方法的参数类型,并且注入正确的对象呢? 比如下面这样 @RequestMapping(value="/register", ...
- spring mvc中DispatcherServlet如何得到ModelAndView的
首先看下面这种张图,这张图说明了spring mvc整体的流程. 本文讲的就是如何从DispatcherServlet中得到ModerAndView的过程. 首先看DispatherServlet这个 ...
- Spring MVC中一般 普通类调用service
在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...
- Spring MVC 中 @ModelAttribute 注解的妙用
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
- Spring MVC:DispatchServlet类
Spring MVC架构 Spring Web MVC是基于Servlet API构建的原始Web框架,从一开始就已包含在Spring框架中.传统的模型层被拆分为了业务层(Service)和数据访问层 ...
- spring mvc中使用freemark的一点心得
参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...
- Spring MVC 中的基于注解的 Controller【转】
原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
随机推荐
- PHP-深入理解Opcode缓存
1.什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译, ...
- C# DateTime的11种构造函数
别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Globalization; using Syste ...
- mac使用git管理Github
工欲善其事,必先利其器. 在OS X Yosemite 10.10.3安装最新版本号Xcode.在terminal下能够发现git已经被安装. ~ mesut$ git --version git v ...
- Enum,int,string类型互转
举例:enum Colors { Red, Green, Blue, Yellow }; Enum-->String (1)利用Object.ToString()方法:如Colors.Green ...
- silverlight RadGridView总结二(转载)
系列二 实现RadGridView行中添加不同控件,并在控件中绑定不同的数据源 先上一段前台代码 <telerik:RadGridView Grid.Row="2" ...
- SQL 子查询 EXISTS 和 NOT EXISTS
MySQL EXISTS 和 NOT EXISTS 子查询语法如下: SELECT … FROM table WHERE EXISTS (subquery) 该语法可以理解为:将主查询的数据,放到子查 ...
- ModelSim6.2 설치에 관한(About the Installation problem of ModelSim 6.2)
ModelSim 설치는 PC OS 따라서 호환성 문제가 발생한다. !!!!!! Vista OS에서는 ModelSim 설치가 안됨(호환성 문제) XP, Win7에서는 호환성 문제 없 ...
- C语言变长參数的认识以及宏实现
1.认识 变长參数是C语言的特殊參数形式.比如例如以下函数声明: int printf(const char *format, ....); 如此的声明表明,printf函数除了第一个參数类型为con ...
- 【转载】IE下利用滤镜实现背景颜色渐变
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- c++ why can't class template hide its implementation in cpp file?
类似的问题还有: why can't class template use Handle Class Pattern to hide its implementation? || why there ...