Results 1 to 20 of 30
- 01-21-2011, 07:10 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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.
- 01-21-2011, 07:37 AM #2
How to use Code Tags
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-21-2011, 11:57 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
Can anyone help me...i know need to add more case...but i dunno how to extract out the attribute names...
- 01-21-2011, 12:14 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you read what goldest post above?
- 01-21-2011, 12:17 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-21-2011, 12:51 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-21-2011, 02:03 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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...
- 01-21-2011, 02:07 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
codetags??
- 01-21-2011, 02:38 PM #9
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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-21-2011, 03:27 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
- 01-21-2011, 03:29 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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...
- 01-22-2011, 01:00 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
Bumpz Pls bring up my post....
- 01-22-2011, 05:22 AM #13
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
guess no one can help..
- 01-22-2011, 05:54 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 01-22-2011, 06:06 AM #15
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...).guess no one can help..
- 01-22-2011, 07:23 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-22-2011, 07:28 AM #17
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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..
- 01-22-2011, 07:31 AM #18
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
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..
- 01-23-2011, 12:26 AM #19
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.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.
- 01-23-2011, 08:35 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
java compiler source code
By vkolluru in forum New To JavaReplies: 13Last Post: 02-24-2010, 07:28 AM -
[SOLVED] Source code of Java API
By Gudradain in forum New To JavaReplies: 3Last Post: 01-01-2009, 05:17 AM -
Can we Obtain Java Source Code?
By tornado in forum New To JavaReplies: 7Last Post: 12-10-2008, 07:23 PM -
MavenJava - browse source code of all open source projects online
By jirkacelak in forum Java SoftwareReplies: 1Last Post: 11-28-2008, 06:27 PM -
open source java code
By reena in forum Advanced JavaReplies: 1Last Post: 04-19-2008, 06:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks