hi, I am new here... I am currently facing some trouble in pulling out the Attribute as in its types and names.

My program purpose is to read the Java file and display all the String into tokens. Then, it must be able to pull out the class name, attribute names and methods name. I manage to pull out the class name but now i dont know how to pull out (retrieve) attributes and methods.

Below are my source code so far...hope any expert can help me solve this problem...

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_ATTRIBUTES = 6;

private String filename; //declare the variable filename of type String
private Scanner sc;
String className;

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(); 


* 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;


switch (scanState) {
case Java2UML.COMMENT: continue;

className = token;



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 static void main (String args [])throws Exception{
Java2UML x; 
x = new Java2UML ( "Java2UML.java" ); 



Recommended Answers

All 9 Replies

any help is appreciated...I now very desperate.... I know have to add in more case...but i just dunno how to get it right and produce the correct output as in to get the attribute name just like how I get the class name..=(

I don't know if this helps... but, if you really are desperate, here are some random thoughts, although I can't guarantee any of this ...
Language Ref 3.8:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7).

So if you find all the strings that start with a letter and just contain letters and digits, and are not a keyword (see language ref 3.9), "false", "true" or "null" then these should be identifiers. These will be class, interface, enum, variable and method names (I may have missed a couple? (types?)). You already know the class names; enum and interface names can be found the same way. The remaining names are vars and methods, but method declarations always have a ( after the name, and var names do not.

Thx james for ur reply. So, does that mean in order to pull out attribute names..is it the same way as i did for pull out the class name..? Any more other suggestion.. if can, can anyone help me improve the coding..

My idea was first to find all the names, separate out the class (interface/enum) names (name preceeded by "class", "interface" or "enum", separate out the method names (name followed by "("), and the remaining names are variables.
It's just an idea, I don't know if it's a very good one, but you did say you were desperate!

if you know the standard used for naming methods and attributes, then it's actually easier. you tokenize the entire input file, and as James suggested, everything that's not a key word, is a class, method, or attribute name. now, you know class name follows keyword class. now, perhaps, method names start with a lower case letter, and attribute names contain only upper case letters. if you set the conventions for naming attributes and methods, your job to identify them is easier. just an idea.

thx james and bibiki.. i get the concept, but if can, can help me write the coding..just wanna get the reference..because I m still stuck of figuring out...my java programming skills are weak..

Can suggest some ways of research regarding my problem.. so i can use google to search for it...

I suggest u to look at the java reflection API.
Reflection is = u are looking in ur java program in the mirror, u can retrieve enythin, fields , methods , constructor, u can also load classes at runtime.
We use also reflection for java code generation. Is a powerful API.
Please , if u can read the book "reflection in action" 10/10

for ur problem, for getting the fields of ur class (i will for exp, choose the name Car for the class) :

Car car = new Car();
Class cls = car.getClass();
List accum = new LinkedList();
while (cls != null) {
Field[] f = cls.getDeclaredFields();// u have now all the fields in this array
for (int i=0; i<f.length; i++) {
cls = cls.getSuperclass();

And for method the same code but just usethe method getDeclaredMethods() in the place of the getDecalredFields() method and the return type is Method[]

I hope that is usefulfor you

thx onepiece. But still that is not a way to do it in my program. i just need to know which case to be added, follow by my if else statement than switch statement..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.