项目里需要扫代码的sql,主要是想找出所有用到的sql。 将相关的sql 提交给DBA 来分析,希望在最早的时间发现潜在的查询性能问题。

想想eclipse 里面用到的JDT 能分析java 源代码, 如果我们能分析项目里的源代码利用ASTParser 就可以找到相关的SQL 定义了。

其实已经有人想到这个 http://www.programcreek.com/2011/01/a-complete-standalone-example-of-astparser

,已经自己整理过所有会依赖的jar,机器重装本地的 repository给丢了。  所有就直接用这个链接里面给的一个zip 包, 但是在我现在用到的eclipse 还需要 一个 org.eclipse.text_3.5.0.jar 。

为了这个代码不要弄丢了,直接放一份 当然没有项目里面的信息

package com.bwang.jdt;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Writer;

import java.util.ArrayList;

import java.util.List;

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

import org.eclipse.jdt.core.dom.ASTNode;

import org.eclipse.jdt.core.dom.ASTParser;

import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;

import org.eclipse.jdt.core.dom.BodyDeclaration;

import org.eclipse.jdt.core.dom.CompilationUnit;

import org.eclipse.jdt.core.dom.FieldDeclaration;

public class SqlExtractor {

private BufferedWriter writer;

private long counter = 0;

private final String fileRoot;

private final String logFileName;

public static void main (String[] args) {

if (args.length < 2) {

System.out.println("Please provide the parameters:");

System.out.println("SqlExtractor codeOfRoot logFileName");

System.exit(-1);

}

SqlExtractor sqlExtractor = new SqlExtractor(args);

sqlExtractor.extractSql();

}

SqlExtractor(String[] args) {

fileRoot = args[0];

logFileName = args[1];

try {

writer = new BufferedWriter(new FileWriter(new File(logFileName)));

} catch(IOException e) {

}

}

private void analyzeOneFile(String sourceFile, Writer writer) {

String source = getFileContent(sourceFile);

ASTParser parser = ASTParser.newParser(AST.JLS3);

parser.setSource(source.toCharArray());

CompilationUnit unit = (CompilationUnit)parser.createAST(null);

unit.recordModifications();

StringBuilder sb = new StringBuilder();

List types = unit.types();

for (AbstractTypeDeclaration type : types) {

if (type.getNodeType() == ASTNode.TYPE_DECLARATION) {

// Class def found

List bodies = type.bodyDeclarations();

for (BodyDeclaration body : bodies) {

if (body.getNodeType() == ASTNode.FIELD_DECLARATION /*&&

((FieldDeclaration)body).getType().equals(Type.S) */) {

FieldDeclaration field = (FieldDeclaration)body;

if(field.toString().toLowerCase().indexOf("select") >=0) {

sb.append(field.toString());

counter++;

}

}

}

}

}

if (sb.length() > 0) {

try {

if (counter > 0) {

writer.append("\n\n");

}

sourceFile = sourceFile.replace(fileRoot, "");

sourceFile = sourceFile.replace("\\", "/");

writer.append(sourceFile + "\n");

String content = sb.toString();

content = content.replace("\" +", "\"\n\t\t+ ");

content = content.replace("\"+", "\"\n\t\t+ ");

writer.append(content);

}catch(IOException ioe) {

}

}

}

private void extractSql() {

File folder = new File(fileRoot);

List poms = new ArrayList();

handleOneFolder(folder, poms);

System.out.println("begin to analyze files of :" + poms.size());

try {

int index = 1;

for(File f : poms) {

System.out.println("begin to analyze file : " + index + " " + f.getName());

analyzeOneFile(f.getPath(), writer);

index++;

}

}

finally {

try {

writer.flush();

writer.close();

} catch(IOException e) {

}

}

System.out.println("successfully find sql count:" + counter);

}

private void handleOneFolder(File folder, List poms) {

if(!folder.isDirectory()) {

return;

}

String[] files = folder.list();

for(String oneFile : files) {

String fullFileName = folder.getPath() + "/" + oneFile;

if (fullFileName.endsWith(".java") && fullFileName.indexOf("src\\test\\java\\") < 0) {

poms.add(new File(fullFileName));

continue;

}

File f = new File(fullFileName);

if (f.isDirectory()) {

handleOneFolder(f, poms);

}

}

}

private String getFileContent(String fName) {

try {

File file = new File(fName);

BufferedReader in = new BufferedReader(new FileReader(file));

StringBuffer buffer = new StringBuffer();

String line = null;

while (null != (line = in.readLine())) {

buffer.append("\t" + line);

buffer.append("\n");

}

return buffer.toString();

}

catch (IOException e) {

throw new RuntimeException(e);

}

}

}

思路也简单就是遍历 codeOfRoot 下面的所有java 文件, 找到 所有的field 定义的值里面包含 select 关键字的 field,  然后将所有这些满足条件的都写到一个log 文件。

到时候将log 文件发给DBA 就行了。 当然还可以更进一步来个CI,  可以定期去扫项目的代码, 然后比较产生的log 文件的变化。 然后DBA 定期看 变化的SQL 就好了, 是不是很酷。

jdt 解析java语句,利用JDT 回分析java 源代码相关推荐

  1. 利用java虚拟机的工具jmap分析java内存情况

    2019独角兽企业重金招聘Python工程师标准>>> 有时候碰到性能问题,比如一个java application出现out of memory,出现内存泄漏的情况,再去修改bug ...

  2. java jmap 分析_利用java虚拟机的工具jmap分析java内存情况

    有时候碰到性能问题,比如一个java application出现out of memory,出现内存泄漏的情况,再去修改bug可能会变得异常复杂,利用工具去分析整个java application 内 ...

  3. 如何分析java程序_如何利用 JConsole观察分析Java程序的运行,进行排错调优

    一.JConsole是什么 从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行.您可以轻松地使用 JCons ...

  4. 如何利用 JConsole观察分析Java程序的运行,进行排错调优

    原文链接:http://jiajun.iteye.com/blog/810150 一.JConsole是什么 从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能 ...

  5. 如何利用 JConsole观察分析Java程序的运行,进行排错调优(转)

    一.JConsole是什么 从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行.您可以轻松地使用 JCons ...

  6. java lam表达式_详细分析Java Lambda表达式

    在了解Lambda表达式之前我们先来区分一下面向对象的思想和函数式编程思想的区别 面向对象的思想: 做一件事情,找一个能解决这个事情的对象,调用他的方法来解决 函数时编程思想: 只要能获取到结果,谁去 ...

  7. spring boot java app_利用spring boot创建java app

    利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...

  8. Java虚拟机:深入详细分析Java ClassLoader原理与源码

    一.什么是ClassLoader? ClassLoader就是类加载器,当我们写好一个Java程序之后,都是由若干个.class文件组成的一个完整的Java应用程序,当程序在运行时,即会调用该程序的一 ...

  9. java 怎样 thread dump_怎样分析 JAVA 的 Thread Dumps

    展开全部 当有障碍,或者是一个基于 JAVA 的 WEB 应用运行的比预期32313133353236313431303231363533e58685e5aeb931333337623537慢的时候, ...

最新文章

  1. sobel算子原理以及运用
  2. Algorithms_二叉树的前序遍历、中序遍历、后续遍历(深度优先)
  3. Java黑皮书课后题第5章:*5.20(打印2到1000之间的素数)修改程序清单5-15,打印2到1000之间(包括2和1000)的所有素数。每1行显示8个素数,数字之间用一个空格字符隔开
  4. POJ 1077 Eight
  5. 最先进单插槽专业绘图解决方案
  6. Centos6.6部署Redis集群
  7. 领域驱动系列五模型驱动设计的构造块
  8. Opencv 视频转为图像序列
  9. Omega network
  10. 层次分析法简述即其MATLAB代码
  11. 亚马逊AWS学习——EC2实例无法正确加载EBS卷问题的解决
  12. 揭秘大厂的物联网关键技术
  13. UVM学习笔记--sequence和sequencer
  14. SMSAlarm短信猫语音猫快速连接
  15. PTA 02-线性结构3 Reversing Linked List 题目解析
  16. 2011年度中国地区安全威胁大事记
  17. 免费的mysql空间
  18. 昆山市地方税务局异地复制及备份系统询价采购招标公告
  19. 全站最全面的Python 基础入门必备知识大全,学完即就业!【建议收藏仔细学习】
  20. 40款非常酷的国外创意名片设计欣赏(下)

热门文章

  1. 海光服务器型号,中科海光CPU的首次评测:基于AMD架构,覆盖桌面服务器端
  2. c语言编写坦克大战设计报告,c语言编写坦克大战源代码
  3. 关于数学基础的研究现状
  4. NetSuite2.0 Restlet脚本 时间初始化脚本
  5. mybatis-plus配置(包含分页插件)
  6. 济南软件测试行业提出千亿级市场的战略布局
  7. matlab中contourm,MATLAB 中contour函数的使用
  8. 网路学员面试常见问题:
  9. 岩板铺地好吗_**岩板铺地「泉州泰亨石材供应」
  10. Hive数据仓库中历史拉链表实践