如何读取JAVA中得注释内容 JAVA中是允许采用/***/这种注释形式的,并且可以其中加入参数.请问通过什么API或者第三方类可以得到这些注释内容 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以写个类专门分析java源码,碰到注释就读出可以考虑用正则表达式 源代码没问题,javadoc就是用这个读取的如果是.class则没戏了。你可以考虑用@Anoation 来解决 我一直接认为awk是文本处理最好的工具,简单实用,这是我用awk写的输出java注释的程序:BEGIN { stat=0; } /\/\// { gsub(".*//",""); print "//"$0;} /\/\*/ { x1=index($0,"/*"); x2=index($0,"*/"); if (x2) print substr($0,x1,x2-x1+2); else stat=1; next; }/\*\// { x2=index($0,"*/"); print substr($0,1,x2+2); stat=0;}{ if (stat) print $0;}测试程序如下:import java.util.*;class testv { /** * Method main * * * @param args * */ public static void main(String[] args) { // TODO: Add your code here class M { int a1; /* 测试...测试 */ } ; M m1,m=new M(); m.a1=100; Vector v = new Vector() ; v.add(0,m); /* 以下还是测试 ... 测试结束 */ m1=(M)v.elementAt(0); System.out.println(m1.a1); } }用awk -f b.awk testv.java结果如下:======================================== * Method main * * * @param args * */// TODO: Add your code here/* 测试...测试 */ ... 测试结束 */=====================================当然,我前几天刚写了一个去C/C++注释的awk程序(java的注释与它相同)BEGIN { stat=0; } { gsub(/\/\*.*\*\//,""); gsub(/\/\/.*/,""); s=$0; if (stat==0) if ((s~/\/\*/) && (s!~/\*\//)) { stat=1; gsub(/\/\*.*/,""); print $0; next; } if (stat==1) { if (s!~/\*\//) { next; } else { gsub(/.*\*\//,""); stat=0; } } print $0;}用上面的java程序测试:awk -f a.awk testv.java结果如下:import java.util.*;class testv { public static void main(String[] args) { class M { int a1; } ; M m1,m=new M(); m.a1=100; Vector v = new Vector() ; v.add(0,m); m1=(M)v.elementAt(0); System.out.println(m1.a1); }} 有些小问题/* 以下还是测试没出来,改进过的:BEGIN { stat=0; } /\/\// { gsub(".*//",""); print "//"$0;} /\/\*/ { x1=index($0,"/*"); x2=index($0,"*/"); if (x2) print substr($0,x1,x2-x1+2); else stat=1; }/\*\// { if (stat) { x2=index($0,"*/"); print substr($0,1,x2+2); stat=0; } }{ if (stat) print $0;} 正统的说,就是用java 自带的工具,javadoc 呀,生成的html文档,说明是相当详细的! 关于JTABLE的JcomBoBox的问题 为什么Hashmap的查询显示不出来,我这个问题出在哪?求教 求javax.media包 实习 JBossOpenTool .jar 这个文件倒底在用jbuilder + jboss 中用不用啊!!! class文件反编译出现乱码 急 急 急 !!! 求解释,语法不对,报错 查询数据库时,数据没有找到抛什么异常? 高手们帮我看看吧(javaBean的问题),先谢过各位了 Java集合将自定义对象存到ArrayList集合中,并去除重复元素 java中线程的暂停与恢复 java中暂停与恢复线程
可以考虑用正则表达式
@Anoation 来解决
BEGIN {
stat=0;
}
/\/\// {
gsub(".*//","");
print "//"$0;
}
/\/\*/ {
x1=index($0,"/*");
x2=index($0,"*/");
if (x2)
print substr($0,x1,x2-x1+2);
else
stat=1;
next;
}
/\*\// { x2=index($0,"*/");
print substr($0,1,x2+2);
stat=0;
}
{
if (stat)
print $0;
}
测试程序如下:import java.util.*;
class testv {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
class M {
int a1; /* 测试...测试 */
} ;
M m1,m=new M();
m.a1=100;
Vector v = new Vector() ;
v.add(0,m);
/* 以下还是测试
...
测试结束
*/
m1=(M)v.elementAt(0);
System.out.println(m1.a1);
}
}用awk -f b.awk testv.java
结果如下:
========================================
* Method main
*
*
* @param args
*
*/
// TODO: Add your code here
/* 测试...测试 */
...
测试结束
*/
=====================================
当然,我前几天刚写了一个去C/C++注释的awk程序(java的注释与它相同)
BEGIN {
stat=0;
}
{
gsub(/\/\*.*\*\//,"");
gsub(/\/\/.*/,"");
s=$0;
if (stat==0)
if ((s~/\/\*/) && (s!~/\*\//)) {
stat=1;
gsub(/\/\*.*/,"");
print $0;
next;
}
if (stat==1)
{
if (s!~/\*\//) {
next;
}
else
{
gsub(/.*\*\//,"");
stat=0;
}
}
print $0;
}
用上面的java程序测试:
awk -f a.awk testv.java
结果如下:import java.util.*;
class testv { public static void main(String[] args) { class M {
int a1;
} ;
M m1,m=new M();
m.a1=100;
Vector v = new Vector() ;
v.add(0,m);
m1=(M)v.elementAt(0);
System.out.println(m1.a1);
}
}
/* 以下还是测试
没出来,改进过的:
BEGIN {
stat=0;
}
/\/\// {
gsub(".*//","");
print "//"$0;
}
/\/\*/ {
x1=index($0,"/*");
x2=index($0,"*/");
if (x2)
print substr($0,x1,x2-x1+2);
else
stat=1;
}
/\*\// {
if (stat)
{
x2=index($0,"*/");
print substr($0,1,x2+2);
stat=0;
}
}
{
if (stat)
print $0;
}