
Hi, sir/madam.
I am new here and currently developing a program whereby it can read the Java source file and then display all the String into tokens. Class Name , attribute and methods are being extracted out respectively throughout the execution. However, I now face the problem to retrieve the attribute name and method name. I have manage to pull out the Class name but now I am stuck at pull out the attribute name. (For example, type: String, attribute name = variable name)
Below are my source code. Hope the expert can help me to solve this problem and pull out the attribute name. Can sir/madam help me check where is the problem...and help me to fix it..thanks a lot..
import java.util.*;
import java.io.*;
public class Java2UML {
private static final int INITIAL = 0;
private static final int COMMENT = 1;
private static final int SCANNED_CLASS = 2;
private static final int SCANNED_ATTRIBUTE = 3;
private static final int SCANNED_METHOD = 4;
private static final int READING_CURLYBRACKET = 5;
private static final int READING_ACCESS_SPECIFIER = 6;
private static final int READING_ATTRIBUTES = 7;
private String filename; //declare the variable filename of type String
private Scanner sc;
String className;
String attributeName;
Java2UML ( String name ) {
filename = name;
}
public void openFile() throws IOException {
System.out.println( "File: " + filename );
sc = new Scanner( new File ( filename ) );
}
public void readFile() {
while (sc.hasNextLine() ) {
String line;
line = sc.nextLine();
tokenize(line);
}
}
/************************
* tokenizes a single line, which is passed in as a parameter.
*/
public void tokenize(String line) {
String token;
int scanState = INITIAL;
StringTokenizer st = new StringTokenizer( line, "; " );
while (st.hasMoreTokens()) {
token = st.nextToken();
if (token.equalsIgnoreCase( "//" )) {
// we've seen the beginning of comment.
scanState = COMMENT;
} else if (token.equalsIgnoreCase( "CLASS" )) {
scanState = SCANNED_CLASS;
continue;
} else if (token.equalsIgnoreCase( "PRIVATE" )) {
scanState = READING_ACCESS_SPECIFIER;
continue;
} else if (token.equalsIgnoreCase( "PUBLIC" )) {
scanState = READING_ACCESS_SPECIFIER;
continue;
} else if (token.equalsIgnoreCase( "PROTECTED" )) {
scanState = READING_ACCESS_SPECIFIER;
continue;
} else if (token.equalsIgnoreCase( "PRIVATE" )) {
scanState = READING_ACCESS_SPECIFIER;
continue;
}
switch (scanState) {
case Java2UML.COMMENT: continue;
case Java2UML.SCANNED_CLASS:
className = token;
drawClass(className);
scanState = READING_CURLYBRACKET;
continue;
case Java2UML.SCANNED_ATTRIBUTE:
attributeName = token;
drawAttribute(attributeName);
scanState = READING_ATTRIBUTES;
continue;
}
System.out.println("This is a token <" + token + ">");
}
}
public void drawClass(String className) {
System.out.println( "---------------------------" );
System.out.println( "CLASS NAME IS : " + className );
System.out.println( "---------------------------" );
}
public void drawAttribute(String attributeName) {
System.out.println( "---------------------------" );
System.out.println( "ATTRIBUTE IS : " + attributeName );
System.out.println( "---------------------------" );
}
public static void main (String args [])throws Exception{
Java2UML x;
x = new Java2UML ( "Java2UML.java" );
x.openFile();
x.readFile();
}
}