▆▆▆▆▆▆ Java代码注释的问题 ▆▆▆▆▆▆

ruanjiantaotao 2007-11-03 07:11:26
请教代码注释的问题(Java语言环境下):
1.如何用程序的方式去除一个文件中的所有注释?
2.当在源代码中匹配一个字符串时,如何判断它是否已被注释? 如/* this.setName("name"); */
当我匹配“Name”字符串判断那一行是不是被注释掉?

不知道有何解决办法?每个问题50分,最好有相关代码呃,3Q!
...全文
302 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruanjiantaotao 2007-11-18
  • 打赏
  • 举报
回复
正则表达式如何应用于Java中呃
kulin3422 2007-11-16
  • 打赏
  • 举报
回复
正则匹配肯定能行
coolzyt 2007-11-16
  • 打赏
  • 举报
回复
正则啊
匹配//开始 换行结尾的
以及/*开始 */结尾的
guoxyj 2007-11-14
  • 打赏
  • 举报
回复
up
sdd330 2007-11-13
  • 打赏
  • 举报
回复
用这个看看, Reader是输入流, CodeOutput是代码输出, Comment是注释输出
public void SplitCodeComment(LineNumberReader p_Reader, PrintStream p_CodeOutput, PrintStream p_Comment){
while(true){
try{
String _Line = p_Reader.readLine();
if(_Line == null)break;

boolean _Muti = false;
if(_Line.startsWith("//")){
p_Comment.println(_Line);
continue;
}
else if(_Line.startsWith("/*")){
if(!_Line.endsWith("*/"))_Muti = true;
p_Comment.println(_Line);
continue;
}else if(_Line.endsWith("*/")){
_Muti = false;
p_Comment.println(_Line);
continue;
}

if(_Muti)
p_Comment.println(_Line);
else
p_CodeOutput.println(_Line);

}catch(Exception ex){
ex.printStackTrace();
break;
}
}
ruanjiantaotao 2007-11-10
  • 打赏
  • 举报
回复
有没有相关的开源包哦~
ruanjiantaotao 2007-11-09
  • 打赏
  • 举报
回复

// 判断 关键字 所在行 是否被注释掉
public List<String> isComment(String filename, String keyword) {
if (filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
List<String> list = Util.readFile(filename,false);
List<String> flag = new ArrayList<String>();
boolean isMultiLineComment = false;
for (int i = 0, k = list.size(); i < k; i++) {
String code = list.get(i);

if (isMultiLineComment) {
if (code.indexOf(keyword) >= 0)
flag.add("Line " + (i + 1) + " : " + keyword
+ " is comment");
if (code.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
continue;
}

int index = code.indexOf("/*");
if (index >= 0) {
if (code.indexOf(keyword) >= 0)
flag.add("Line " + (i + 1) + " : " + keyword
+ " is comment");
if (code.indexOf("*/") < 0) {
isMultiLineComment = true;
}
continue;
}

index = code.indexOf("//");
if (index >= 0) {
if (code.indexOf(keyword) >= 0)
flag.add("Line " + (i + 1) + " : " + keyword
+ " is comment");
continue;
}
}
return flag;
}


Many thanks to bao110908 and I have added new function about how to judge the line which has the keyword is commented or not.

Another question is how can I compare the two xml files and calculate the delta-values of the nodes in those two xml files. Any help is welcome!

The samle xml file:
XML1

<project name="bean1.lst">
<package name="bean">
<type kind="class" name="Demo">
<metric name="LOCC" value="54"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="1"/>
<metric name="CBM" value="1"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="0"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="0"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="9"/>
<metric name="RFM" value="9"/>
<metric name="RFP" value="0"/>
</type>
<type kind="class" name="Point">
<metric name="LOCC" value="46"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="0"/>
<metric name="CBM" value="0"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="0"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="0"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="12"/>
<metric name="RFM" value="12"/>
<metric name="RFP" value="0"/>
</type>
</package>
</project>



XML2
<project name="bean2.lst">
<package name="bean">
<type kind="class" name="Demo">
<metric name="LOCC" value="54"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="1"/>
<metric name="CBM" value="1"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="1"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="1"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="10"/>
<metric name="RFM" value="9"/>
<metric name="RFP" value="1"/>
</type>
<type kind="class" name="Point">
<metric name="LOCC" value="26"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="0"/>
<metric name="CBM" value="0"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="1"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="1"/>
<metric name="CAI" value="1"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="8"/>
<metric name="RFM" value="7"/>
<metric name="RFP" value="1"/>
</type>
<type kind="aspect" name="BoundPoint"> // Be careful, this is the additional content!!!
<metric name="LOCC" value="37"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="1"/>
<metric name="CMC" value="3"/>
<metric name="CBM" value="3"/>
<metric name="CDA" value="2"/>
<metric name="CDP" value="2"/>
<metric name="CDI" value="1"/>
<metric name="CAE" value="0"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="0"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="2"/>
<metric name="RFA" value="6"/>
<metric name="RFM" value="6"/>
<metric name="RFP" value="0"/>
</type>
</package>
</project>



And what I want to get is the delta-value about the two xml files according to the node(package,name).

The similar code maybe better! 3Q~
千里冰封820 2007-11-05
  • 打赏
  • 举报
回复
Netbeans源代码里面有这个的分析
alphax 2007-11-05
  • 打赏
  • 举报
回复
做个词法分析就可以了
qiuqiupeng 2007-11-05
  • 打赏
  • 举报
回复
remark
  • 打赏
  • 举报
回复
写一个,没有经过很严格的测试。

测试文件:abc.java

// Test File
package color;

/**
* My firat Java program: Hello World
* @author bao110908
*/
public class Test{

/*
main method
method of automatic startup
*/

/* abnormal comment 1 */
/** abnormal comment 2 **/

// normal comment
public static void main(String[] args) {
System.out.println("Hello World"); // print 'Hello World'
System.out.println("Hello World"); /* abnormal comment 3 */
}
}



代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
Comment comment = new Comment();
int count = comment.countCommentLine("abc.java");
System.out.println("总共的注释行数:" + count);

System.out.println();
List<String> codes = comment.removeComment("abc.java");
System.out.println("没有注释的源代码");
for(String str : codes) {
System.out.println(str);
}
}
}

class Comment {

/**
* 计算某个 Java 文件中注释的行数
*/
public int countCommentLine(String filename) {
if(filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
int count = 0;
List<String> list = Util.readFile(filename);
boolean isMultiLineComment = false;
for(String str : list) {
// 当为多行注释时,直接将计算注释行
if(isMultiLineComment) {
count++;
// 直到碰到 */ 为止,当然 */ 这一行也算注释
if(str.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
}
// 为 // 直接计数
if(str.indexOf("//") >= 0) {
count++;
}

// 当碰到 /* 时,开始计数
if(str.indexOf("/*") >= 0) {
count++;
// 并且在该行中没 */ 时,可以看作是多行注释的开始
if(str.indexOf("*/") < 0) {
isMultiLineComment = true;
}
}
}
return count;
}

/**
* 除去文件中的注释,与 countCommentLine 方法类似
*/
public List<String> removeComment(String filename) {
if(filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
List<String> list = Util.readFile(filename);
List<String> codes = new ArrayList<String>();
boolean isMultiLineComment = false;
for(int i = 0, k = list.size(); i < k; i++) {
String code = list.get(i);

if(isMultiLineComment) {
if(code.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
continue;
}

int index = code.indexOf("/*");
if(index >= 0) {
String s = code.substring(0, index);
if(s.trim().length() > 0) {
codes.add(s);
}
if(code.indexOf("*/") < 0) {
isMultiLineComment = true;
}
continue;
}

index = code.indexOf("//");
if(index >= 0) {
String s = code.substring(0, index);
if(s.trim().length() > 0) {
codes.add(s);
}
continue;
}
codes.add(code);
}
return codes;
}
}

class IllegalJavaFile extends RuntimeException {
private static final long serialVersionUID = 1L;
public IllegalJavaFile(String filename){
super("\"" + filename + "\", the file is not a java file.");
}
}

class Util {
public static List<String> readFile(String filename) {
List<String> list = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
String str = "";
while((str = br.readLine()) != null) {
// 忽略空行
if(str.trim().length() > 0) {
list.add(str);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}

public static void writeFile(List<String> codes, String filename) {
BufferedWriter br = null;
try {
br = new BufferedWriter(new FileWriter(filename));
String line = System.getProperty("line.separator");
for(int i = 0, k = codes.size(); i < k; i++) {
br.write(codes.get(i));
br.write(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ruanjiantaotao 2007-11-04
  • 打赏
  • 举报
回复
To weijiepeng: That's wrong! If the statement is like that below:
/*
the first line
the second line
*/

the middle statements are not what I need.


Maybe the 反射或者正则表达式 is ok. But how can I implement it? Some examples are better!
Many thanks!
johnsoncr 2007-11-04
  • 打赏
  • 举报
回复
正则表达式
weijiepeng 2007-11-03
  • 打赏
  • 举报
回复
文件流, 转成字符串 str ,
应该可以实现了吧
str.replace("//", " ")
str.replace("/*","")
str.repalec("*/","")
adverse 2007-11-03
  • 打赏
  • 举报
回复
不清楚楼主要实现什么功能,可以考虑用反射或者正则表达式来试试.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧