The Artima Developer Community
Sponsored Link

Java Answers Forum
How to write a Java Program to analyse codes.

2 replies on 1 page. Most recent reply: Jan 14, 2003 11:15 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Melvin

Posts: 6
Nickname: melvin
Registered: Jan, 2003

How to write a Java Program to analyse codes. Posted: Jan 13, 2003 7:02 PM
Reply to this message Reply
Advertisement
Can anyone help me with this problem:

For example if I have a file like this:

class machine {.....}
class Inventories {....}

and I would like to locate and retrieve all the Class names, Machine, Inventories etc. Can anyone kindly give me an idea how to go about doing it. Thank you.


Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: How to write a Java Program to analyse codes. Posted: Jan 14, 2003 2:25 AM
Reply to this message Reply
//the sample code only solve the condition which the class keyword and the class name in one line.

import java.util.regex.*;
import java.util.*;
import java.io.*;


public class GetClassName{
private List classNameList;
private String srcFileName;
private BufferedReader in;

public GetClassName(String srcFileName){
this.srcFileName = srcFileName;
classNameList = new ArrayList();
try{
in = new BufferedReader(
new FileReader(this.srcFileName));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}

public List getClassName(){
String line;
Pattern classPattern = Pattern.compile("class[ \\t]+([_$\\w]+)", Pattern.MULTILINE);
Pattern commentBeginLikeC = Pattern.compile("[ \\t]*/\\*");
Pattern commentLikeCPP = Pattern.compile("[ \\t]*//");

Matcher m;

try{
while((line = in.readLine()) != null){
m = commentLikeCPP.matcher(line);

//skip comments like CPP
if(m.find())
continue;

m = commentBeginLikeC.matcher(line);
//skip comments like C
if(m.find())
skipCommentLikeC(line);

//replace string literal with empty string
line = replaceWithEmptyString(line);

m = classPattern.matcher(line);

//find a class , get the name of the class
if(m.find()){
String className = m.group(1);
classNameList.add(className);
}
}
}
catch(IOException e){
e.printStackTrace();
}

return classNameList;
}

public void skipCommentLikeC(String oneLine){
Pattern commentEndLikeC = Pattern.compile("\\*/[ \\t]*");
Matcher m = commentEndLikeC.matcher(oneLine);
if(m.find())
return;
else{
String line;
try{

while((line = in.readLine()) != null){
m = commentEndLikeC.matcher(line);
if(m.find())
return;
}
}
catch(IOException e){
e.printStackTrace();
}
}
}


public String replaceWithEmptyString(String line){
return line.replaceAll("\".*\"", "\"\"");
}

}

class TestGetClassName{
public static void main(String [] args){
GetClassName gn = new GetClassName("GetClassName.java");

System.out.println(gn.getClassName());
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How to write a Java Program to analyse codes. Posted: Jan 14, 2003 11:15 AM
Reply to this message Reply
You might want to check out something like JavaCC for a more thorough solution: http://www.webgain.com/products/java_cc/

Flat View: This topic has 2 replies on 1 page
Topic: Swings-jsp-servlets Previous Topic   Next Topic Topic: Vectors

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use