15d5169da2f53c5fab57fc014fbbaf3fd9c1e7f9
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / methods / JavaFile.java
1 package eu.svjatoslav.inspector.java.methods;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
12 import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
13 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
14
15 public class JavaFile {
16
17         private final List<Import> imports = new ArrayList<Import>();
18
19         private String packageName;
20
21         private final File file;
22
23         StringBuffer contents = new StringBuffer();
24
25         public List<Clazz> classes = new ArrayList<Clazz>();
26
27         public JavaFile(final File file) throws IOException, InvalidSyntaxException {
28                 this.file = file;
29                 parse();
30         }
31
32         public void parse() throws IOException, InvalidSyntaxException {
33                 System.out.println("java file: " + file);
34
35                 readFile();
36
37                 final Tokenizer tokenizer = new Tokenizer(contents.toString());
38
39                 // empty space
40                 tokenizer.addTerminator(" ", true);
41                 tokenizer.addTerminator("\t", true);
42                 tokenizer.addTerminator("\n", true);
43
44                 tokenizer.addTerminator(";", false);
45                 tokenizer.addTerminator("{", false);
46                 tokenizer.addTerminator("}", false);
47                 tokenizer.addTerminator("(", false);
48                 tokenizer.addTerminator(")", false);
49                 tokenizer.addTerminator("[", false);
50                 tokenizer.addTerminator("]", false);
51                 tokenizer.addTerminator("<", false);
52                 tokenizer.addTerminator(">", false);
53                 tokenizer.addTerminator(",", false);
54                 tokenizer.addTerminator("@", false);
55
56                 // comments
57                 tokenizer.addTerminator("//", "\n", true);
58                 tokenizer.addTerminator("/*", "*/", true);
59
60                 final Modifiers modifiers = new Modifiers();
61
62                 while (true) {
63                         final TokenizerMatch match = tokenizer.getNextToken();
64                         if (match == null)
65                                 break;
66
67                         if (match.token.equals("package")) {
68                                 parsePackage(tokenizer);
69                                 continue;
70                         }
71
72                         if (match.token.equals("import")) {
73                                 parseImport(tokenizer);
74                                 continue;
75                         }
76
77                         final boolean wasModifier = modifiers.parseModifier(match.token);
78                         if (wasModifier)
79                                 continue;
80
81                         if ("class".equals(match.token)) {
82                                 parseClass(tokenizer);
83                                 continue;
84                         }
85
86                         if ("interface".equals(match.token)) {
87                                 parseInterface(tokenizer);
88                                 continue;
89                         }
90
91                         if ("@".equals(match.token)) {
92                                 final Annotation annotation = new Annotation(tokenizer);
93                                 continue;
94                         }
95
96                         System.out.println("    " + modifiers.toString() + " "
97                                         + match.token);
98                         modifiers.reset();
99                         skipUntilSemicolon(tokenizer);
100                 }
101
102         }
103
104         private void parseClass(final Tokenizer tokenizer)
105                         throws InvalidSyntaxException {
106
107                 final TokenizerMatch match = tokenizer.getNextToken();
108                 final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
109                                 false);
110                 // System.out.println(clazz.toString());
111                 classes.add(clazz);
112
113         }
114
115         private void parseImport(final Tokenizer tokenizer)
116                         throws InvalidSyntaxException {
117
118                 final Import imp = new Import();
119
120                 final TokenizerMatch match = tokenizer.getNextToken();
121
122                 if (match.token.equals("static")) {
123                         imp.isStatic = true;
124                         imp.path = tokenizer.getNextToken().token;
125                 } else
126                         imp.path = match.token;
127
128                 imports.add(imp);
129
130                 tokenizer.expectNextToken(";");
131         }
132
133         private void parseInterface(final Tokenizer tokenizer)
134                         throws InvalidSyntaxException {
135
136                 final TokenizerMatch match = tokenizer.getNextToken();
137                 final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
138                 // System.out.println(clazz.toString());
139                 classes.add(clazz);
140         }
141
142         private void parsePackage(final Tokenizer tokenizer)
143                         throws InvalidSyntaxException {
144
145                 final TokenizerMatch match = tokenizer.getNextToken();
146
147                 packageName = match.token;
148
149                 tokenizer.expectNextToken(";");
150         }
151
152         private void readFile() throws FileNotFoundException, IOException {
153                 final FileReader fileReader = new FileReader(file);
154
155                 final BufferedReader bufferedReader = new BufferedReader(fileReader);
156
157                 while (true) {
158                         final String line = bufferedReader.readLine();
159
160                         if (line == null)
161                                 break;
162
163                         contents.append(line);
164                         contents.append("\n");
165                 }
166
167                 bufferedReader.close();
168                 fileReader.close();
169         }
170
171         public void skipUntilSemicolon(final Tokenizer tokenizer) {
172                 while (true) {
173                         final TokenizerMatch token = tokenizer.getNextToken();
174
175                         if (token == null)
176                                 return;
177
178                         if (token.token.equals(";"))
179                                 return;
180                 }
181         }
182
183 }