Javac

package com.leon;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.sun.tools.javac.tree.TreeScanner;

public class Main {
	public static void main(String[] args) throws IOException {
		TreeScanner ms = new MethodScanner();
		ScanFile sf = new ScanFile(ms);

		Collection list = FileUtils.listFiles(new File("D:/llsp/src"), new String[] { "java" }, true);
		System.out.println(list.size());
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			File object = iterator.next();
			sf.scan(object.getAbsolutePath());
		}
		List rs = Statistics.getInstance().getTop10();
		for (int i = 0; i < rs.size(); i++) {
			System.out.println(rs.get(i));
		}
	}

}
package com.leon;

import java.util.Iterator;

import javax.lang.model.element.Modifier;

import org.apache.log4j.Logger;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.util.List;

public class MethodScanner extends TreeScanner {
	Logger logger = Logger.getLogger(MethodScanner.class);

	public void visitApply(JCTree.JCMethodInvocation paramJCMethodInvocation) {
		Symbol sym = TreeInfo.symbol(paramJCMethodInvocation.meth);
		if (filter(sym)) {
			String returnType = getShortType(paramJCMethodInvocation.type);
			StringBuffer sb = new StringBuffer();
			sb.append(sym.outermostClass().className() + "``");
			Iterator it = sym.getModifiers().iterator();
			while (it.hasNext()) {
				sb.append(it.next() + " ");
			}
			sb.append(returnType + " ");
			sb.append(sym.name);
			List list = paramJCMethodInvocation.args;
			if (list != null && !list.isEmpty()) {
				sb.append(genArs(list));
			} else {
				sb.append("()");
			}
			Statistics.getInstance().put(sb.toString());
			logger.info(sb.toString());
		}
		super.visitApply(paramJCMethodInvocation);
	}

	private String genArs(List list) {
		StringBuffer args = new StringBuffer();
		args.append("(");
		for (int i = 0; i < list.size(); i++) {
			String type = getShortType(list.get(i).type);
			args.append(type + " " + "arg" + i + ",");
		}
		args.replace(args.length() - 1, args.length(), ")");
		return args.toString();
	}

	public boolean filter(Symbol sym) {
		if (sym.isConstructor()) {
			return false;
		}
		String clazz = sym.outermostClass().className();
		if (canPass(clazz)) {
			return true;
		}
		return false;
	}

	private boolean canPass(String clazz) {
		String[] pass = new String[] { "com.citi", "com.citigroup", "com.leon" };
		for (int i = 0; i < pass.length; i++) {
			if (getPackage(clazz).startsWith(pass[i])) {
				return true;
			}
		}
		return false;
	}

	private String getPackage(String owner) {
		int i = owner.lastIndexOf(46);
		if (i != -1) {
			String str = owner.substring(0, i);
			return str;
		}
		return null;
	}

	private String getShortType(Type type) {
		// if (type.isPrimitive() || type.getKind() == TypeKind.VOID) {
		// return type.toString();
		// } else {
		// String rs = type.toString();
		// return rs.substring(getPackage(rs).length() + 1);
		// }
		return type.toString();
	}
}

package com.leon;

import java.io.IOException;

import javax.tools.JavaFileObject;

import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;

public class ScanFile {
	private JavaCompiler javaCompiler;
	private TreeScanner scanner;

	public ScanFile(TreeScanner scanner) {
		Context context = init();
		this.scanner = scanner;
		javaCompiler = JavaCompiler.instance(context);
	}

	private Context init() {
		Context context = new Context();
		JavacFileManager.preRegister(context);
		return context;
	}

	@SuppressWarnings("deprecation")
	public JCTree.JCCompilationUnit parse(String paramString)
			throws IOException {
		return javaCompiler.parse(paramString);
	}

	public JCTree.JCCompilationUnit parse(JavaFileObject paramJavaFileObject) {
		return javaCompiler.parse(paramJavaFileObject);
	}

	public List envTrees(JCTree.JCCompilationUnit unit) {
		return javaCompiler.enterTrees(List.of(unit));
	}

	public Env annotateTypeInfo(Env paramEnv) {
		return javaCompiler.attribute(paramEnv);
	}

	public void scan(String paramString) throws IOException {
		envTrees(parse(paramString));
		while (!javaCompiler.todo.isEmpty()) {
			Env paramEnv = javaCompiler.todo.remove();
			scanner.scan(annotateTypeInfo(paramEnv).tree);
		}
	}
}

package com.leon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Statistics {
	private Map map = new HashMap();
	public static Statistics instance;

	private Statistics() {

	}

	public static Statistics getInstance() {
		if (instance == null) {
			instance = new Statistics();
		}
		return instance;
	}

	public void put(String key) {
		if (map.containsKey(key)) {
			map.put(key, map.get(key) + 1);
		} else {
			map.put(key, 1);
		}
	}

	public List getTop10() {
		List> list = new ArrayList>(map.entrySet());
		Collections.sort(list, new Comparator>() {
			public int compare(Map.Entry o1, Map.Entry o2) {
				return o2.getValue().compareTo(o1.getValue());
			}
		});
		List rs = new ArrayList();
		for (int i = 0; i < 10; i++) {
			String[] strs = list.get(i).getKey().split("``");
			rs.add(new Node(strs[0], strs[1], list.get(i).getValue()));
		}
		return rs;
	}
}

class Node {
	public String className;
	public String methodName;
	public int count;

	public Node(String className, String methodName, int count) {
		super();
		this.className = className;
		this.methodName = methodName;
		this.count = count;
	}

	@Override
	public String toString() {
		return "[className=" + className + ", methodName=" + methodName + ", count=" + count + "]";
	}

}


你可能感兴趣的:(Javac)