1 package eu.svjatoslav.inspector.java.methods;
3 import java.io.BufferedReader;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
11 import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
12 import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
13 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
15 public class JavaFile {
17 private final List<Import> imports = new ArrayList<Import>();
19 private String packageName;
21 private final File file;
23 StringBuffer contents = new StringBuffer();
25 public List<Clazz> classes = new ArrayList<Clazz>();
27 public JavaFile(final File file) throws IOException, InvalidSyntaxException {
32 public void parse() throws IOException, InvalidSyntaxException {
33 System.out.println("java file: " + file);
37 final Tokenizer tokenizer = new Tokenizer(contents.toString());
40 tokenizer.addTerminator(" ", true);
41 tokenizer.addTerminator("\t", true);
42 tokenizer.addTerminator("\n", true);
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);
57 tokenizer.addTerminator("//", "\n", true);
58 tokenizer.addTerminator("/*", "*/", true);
60 final Modifiers modifiers = new Modifiers();
63 final TokenizerMatch match = tokenizer.getNextToken();
67 if (match.token.equals("package")) {
68 parsePackage(tokenizer);
72 if (match.token.equals("import")) {
73 parseImport(tokenizer);
77 final boolean wasModifier = modifiers.parseModifier(match.token);
81 if ("class".equals(match.token)) {
82 parseClass(tokenizer);
86 if ("interface".equals(match.token)) {
87 parseInterface(tokenizer);
91 if ("@".equals(match.token)) {
92 final Annotation annotation = new Annotation(tokenizer);
96 System.out.println(" " + modifiers.toString() + " "
99 skipUntilSemicolon(tokenizer);
104 private void parseClass(final Tokenizer tokenizer)
105 throws InvalidSyntaxException {
107 final TokenizerMatch match = tokenizer.getNextToken();
108 final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
110 // System.out.println(clazz.toString());
115 private void parseImport(final Tokenizer tokenizer)
116 throws InvalidSyntaxException {
118 final Import imp = new Import();
120 final TokenizerMatch match = tokenizer.getNextToken();
122 if (match.token.equals("static")) {
124 imp.path = tokenizer.getNextToken().token;
126 imp.path = match.token;
130 tokenizer.expectNextToken(";");
133 private void parseInterface(final Tokenizer tokenizer)
134 throws InvalidSyntaxException {
136 final TokenizerMatch match = tokenizer.getNextToken();
137 final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
138 // System.out.println(clazz.toString());
142 private void parsePackage(final Tokenizer tokenizer)
143 throws InvalidSyntaxException {
145 final TokenizerMatch match = tokenizer.getNextToken();
147 packageName = match.token;
149 tokenizer.expectNextToken(";");
152 private void readFile() throws FileNotFoundException, IOException {
153 final FileReader fileReader = new FileReader(file);
155 final BufferedReader bufferedReader = new BufferedReader(fileReader);
158 final String line = bufferedReader.readLine();
163 contents.append(line);
164 contents.append("\n");
167 bufferedReader.close();
171 public void skipUntilSemicolon(final Tokenizer tokenizer) {
173 final TokenizerMatch token = tokenizer.getNextToken();
178 if (token.token.equals(";"))