删掉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");
}
}
参考资料: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");
}
}
相关文章推荐
- 在picturebox上画一条线,然后在线上写字,把文字对应的线删掉
- 删掉客户机记住的密码
- 郭德纲被新浪删掉的博文《人在江湖》,骂功了得
- 双系统,怎么删掉VISTA??
- 找回你删掉并清空回收站的东西
- java注释
- 将字符串s1中任何与字符串s2匹配的字符都删掉
- wubi安装ubuntu,直接删掉安装文件夹,导致不能再次安装解决办法。
- 丹佛斯发生大幅地方士大夫删掉发生地发生地发生地发生地发生地发生地飞士大夫
- 手机储存卡被写保护怎样删掉
- 删掉错误:ORA-00942:表或视图不存在
- 黑马程序员__classpath配置和java注释
- 今天安装VisualSVN 不小心删掉了安装目录,再次安装 修改 移除提示UninstallWMISchemaExecute (0x8004401e) when updating VisualSV
- 删掉grub 恢复mbr
- HDU 1595 枚举+最短路(删掉边)
- Code Fragment-删掉那些认为有用而注释掉的code。
- 这样写角色权限的编辑,判断以前有的,现在没有了,删掉它。新加的,add.
- 由于插件问题导致iPhone无限重启且进不了安全模式,如何删掉问题插件
- 找回你删掉并清空回收站的东西
- oracle数据库中由于没有做任何约束,如何删掉表中重复的记录 推荐