您的位置:首页 > 编程语言 > Java开发

删掉java注释

2010-01-05 15:36 274 查看
因为想用手机看源码,注释太多了不方便,所以把它给删掉了,最初用正则表达式来做的,但发现根本不可能,要匹配那么多东西,单空格和换行就让人倒,最后用JDT,这个好简单

参考资料:http://help.eclipse.org/ganymede/index.jsp?topic
http://www.techq.com/
代码:

package jdt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

/**
* 用JDT去掉java文件里的注释,并改为txt文件,注意导入eclipse.osgi包才能运行
*
*
* @author zhoufei
*/
public class DeleteAnnotate
{
//获得数据源
public String getSource(String str)
{
BufferedReader in;
StringBuffer sb = new StringBuffer();
try
{
in = new BufferedReader(new FileReader(new File(str)));
String s = null;
while ((s = in.readLine()) != null)
{
sb.append(s);
sb.append("/n");
}
in.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return sb.toString();
}

//格式化
public String format(String fileContent)
{
String sourceCode = fileContent;
Document doc = new Document(sourceCode);
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(JavaCore.getOptions());
TextEdit edits = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, sourceCode, 0, sourceCode.length(), 0,
null);
try
{
if (edits != null)
edits.apply(doc);
}
catch (MalformedTreeException e)
{
e.printStackTrace();
}
catch (BadLocationException e)
{
e.printStackTrace();
}
sourceCode = doc.get();
return sourceCode;
}

//入口,获得CompilationUnit对象
public CompilationUnit parser(String fileDir)
{
char[] source = getSource(fileDir).toCharArray();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(source);
CompilationUnit result = (CompilationUnit) parser.createAST(null);
return result;

}

//去掉注释
public String changFile(CompilationUnit cu)
{
cu.accept(new ASTVisitor()
{
@Override
public boolean visit(Javadoc node)
{
node.delete();
return super.visit(node);
}
});
return cu.toString();
// System.out.println(cu);
// TypeDeclaration type = (TypeDeclaration) cu.types().get(0);
// String thePackage = cu.getPackage().getName().getFullyQualifiedName();
// StringBuffer sf = new StringBuffer();
// sf.append("package ");
// sf.append(thePackage);
// sf.append(";/n");
// List imports = cu.imports();
// for (int i = 0; i < imports.size(); i++)
// sf.append(imports.get(i).toString());
// sf.append(type);

}

//重写入文件
public void writer(String str, String dir)
{
File file = new File(dir);
PrintWriter out = null;
try
{
out = new PrintWriter(new FileWriter(file));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write(str);
out.close();
String rename = dir.substring(0, dir.length() - 5);
File renameFile = new File(rename + ".txt");
file.renameTo(renameFile);
}

//应用于一个文件或文件夹
public void newJava(String fileDir)
{
File file = new File(fileDir);
File[] files = file.listFiles();
if (files == null)
{
String name = file.getName();
if ((name.substring(name.length() - 4, name.length()) == "java"))
{
String str = changFile(parser(fileDir));
writer(format(str), fileDir);
}
else
file.delete();
}
else
{
for (int i = 0; i < files.length; i++)
{
String dir = files[i].getAbsolutePath();
if (files[i].listFiles() != null)
newJava(dir);
else
{
String name = files[i].getName();
if ((name.substring(name.length() - 4, name.length()).equals("java")))
{
String str = changFile(parser(dir));
writer(format(str), dir);
}
else
files[i].delete();
}
}
}
}

public static void main(String[] args)
{
DeleteAnnotate t = new DeleteAnnotate();
t.newJava("d://java");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: