Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default Need help on writing Java source code

    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...

    Java Code:
    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;
       String attributeType;
        
       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;
            } else if (token.equalsIgnoreCase( "STRING" )) {
            	scanState = SCANNED_ATTRIBUTE;
            	continue;
            } else if (token.equalsIgnoreCase( "INT" )) {
            	scanState = SCANNED_ATTRIBUTE;
            	continue;
            } else if (token.equalsIgnoreCase( "FLOAT" )) {
            	scanState = SCANNED_ATTRIBUTE;
            	continue;
            } else if (token.equalsIgnoreCase( "DOUBLE" )) {
            	scanState = SCANNED_ATTRIBUTE;
            	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;
         	  	          
         	  case Java2UML.SCANNED_METHOD:
         	  
         	  	     
         	}
         
            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 NAME IS : " + attributeName );
      	System.out.println( "---------------------------" );
      }
      
      
      public static void main (String args [])throws Exception{
    	Java2UML x; 
    	x = new Java2UML ( "Java2UML.java" ); 
    	x.openFile();
    	x.readFile();
        
      }
      
    }
    Last edited by k4it0xtr3me; 01-22-2011 at 07:30 AM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    How to use Code Tags
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Can anyone help me...i know need to add more case...but i dunno how to extract out the attribute names...

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Regarding your question.

    Actually what you really want to do is, kind of a parser to read the file and extract data. There are plenty of ways to do that, and use of regular expressions is one of them.

    When considering methods, do you know how many ways you've / you can define? Based on that you should develop the pattern.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by k4it0xtr3me View Post
    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.
    Use the JavaCompiler API; you can find its documentation if you open the API start page; you'll see a big block listing all the Java technologies; the second block from the left in the top row shows 'javac'. All you have to do is implement a Visitor that visits the AST (Abstract Syntax Tree). You'll find classes, methods and everything that was just parsed by the compiler. You'll need the classes and interfaces in the packages com.sun.source.tree and com.sun.source.util.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    thx guys for the reply..but if anyhow can help me to write the coding..i m very stuck..i know the concept well, but anyhow i cant write the code...

  8. #8
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    codetags??

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by k4it0xtr3me View Post
    codetags??
    Yes, code tags. They are used to surround your code when you post it here. In that way, the code can be more readable for other people who are willing to help you. And that's why I have sent that link to you, so that you can make the necessary changes.

    I hope you are getting me,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #10
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Yes, code tags. They are used to surround your code when you post it here. In that way, the code can be more readable for other people who are willing to help you. And that's why I have sent that link to you, so that you can make the necessary changes.

    I hope you are getting me,

    Goldest
    thx goldest, i will do it now =D

  11. #11
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    I have edited, pls experts..i really need ur help..i been stuck for 1 week..if continue like that...there will be no progress...pls...help me...

  12. #12
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Bumpz Pls bring up my post....

  13. #13
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    guess no one can help..

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I think Jos' post was helpful.

    You don't say whether you followed his advice, and, if you did, with what results. If you have some question about his post ask him (or others).

    ------------------------------

    Part of the problem is the extremely varied (syntactic) way in which methods might be declared (- including methods belonging to classes that don't have a name!). I anticipate "but I just..." followed by whatever restrictions are needed to remove the syntactic complications and allow some sort of rough and ready by-hand parsing. I still think building the AST and visiting its nodes is the way to go.

  15. #15
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    guess no one can help..
    Well, your solution is regular expressions. Look at the regex package in the java api. I wrote a compiler to convert PL/SQL into intel assembly, which required this exact functionality. I wrote a single Regex to parse the input code and convert every element into tokens ignoring whitespace (parens, semi colons, params, names, etc...).

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    Well, your solution is regular expressions.
    Nope, regular expressions aren't the answer; REs can recognize regular languages ('type 3 languages') while Java is a 'type 2 language', i.e. it has context free structures, e.g. nested parentheses, nested curly brackets etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    I think is it ok for me to re-edit the code, i think i now i manage to pull out the attribute name already..but there is something that should not be produce as output...can anyone help me check it...? Has edited the 1st post... pls help me to fix it..i think for too long..

  18. #18
    k4it0xtr3me is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    My new problem is get rid of ATTRIBUTE NAME IS : name ..

    this not suppose to be attribute...

    and have to pull out attribute type next....

    can anyone help..

  19. #19
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Nope, regular expressions aren't the answer; REs can recognize regular languages ('type 3 languages') while Java is a 'type 2 language', i.e. it has context free structures, e.g. nested parentheses, nested curly brackets etc.
    Thats fine/doesn't matter if you're only using regex to tokenize. Being able to work with type 2 languages is the job of the compiler (with a stack) in a LALR compiler. If you only need to parse a sequence of code elements (parens, operators, operands, comments, scope modifiers, declaration, semi colons and the like) then a regex will do all of that just fine! You of course still need stacks, derivation tables, a grammar, etc... to actually do anything with the code. But tokenizing it can be done completely with one regex.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    Thats fine/doesn't matter if you're only using regex to tokenize. Being able to work with type 2 languages is the job of the compiler (with a stack) in a LALR compiler. If you only need to parse a sequence of code elements (parens, operators, operands, comments, scope modifiers, declaration, semi colons and the like) then a regex will do all of that just fine! You of course still need stacks, derivation tables, a grammar, etc... to actually do anything with the code. But tokenizing it can be done completely with one regex.
    Sure, but what would be the value of recognizing a literal String in a mess such as this: ((;"foo))"

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. java compiler source code
    By vkolluru in forum New To Java
    Replies: 13
    Last Post: 02-24-2010, 07:28 AM
  2. [SOLVED] Source code of Java API
    By Gudradain in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 05:17 AM
  3. Can we Obtain Java Source Code?
    By tornado in forum New To Java
    Replies: 7
    Last Post: 12-10-2008, 07:23 PM
  4. Replies: 1
    Last Post: 11-28-2008, 06:27 PM
  5. open source java code
    By reena in forum Advanced Java
    Replies: 1
    Last Post: 04-19-2008, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •