2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.inspector.java.methods;
12 import java.io.BufferedReader;
14 import java.io.FileNotFoundException;
15 import java.io.FileReader;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.List;
20 import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
21 import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
22 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
24 public class JavaFile {
26 private final List<Import> imports = new ArrayList<Import>();
28 private String packageName;
30 private final File file;
32 StringBuffer contents = new StringBuffer();
34 public List<Clazz> classes = new ArrayList<Clazz>();
36 public JavaFile(final File file) throws IOException, InvalidSyntaxException {
41 public void parse() throws IOException, InvalidSyntaxException {
42 System.out.println("java file: " + file);
46 final Tokenizer tokenizer = new Tokenizer(contents.toString());
49 tokenizer.addTerminator(" ", true);
50 tokenizer.addTerminator("\t", true);
51 tokenizer.addTerminator("\n", true);
53 tokenizer.addTerminator(";", false);
54 tokenizer.addTerminator("{", false);
55 tokenizer.addTerminator("}", false);
56 tokenizer.addTerminator("(", false);
57 tokenizer.addTerminator(")", false);
58 tokenizer.addTerminator("[", false);
59 tokenizer.addTerminator("]", false);
60 tokenizer.addTerminator("<", false);
61 tokenizer.addTerminator(">", false);
62 tokenizer.addTerminator(",", false);
63 tokenizer.addTerminator("@", false);
66 tokenizer.addTerminator("//", "\n", true);
67 tokenizer.addTerminator("/*", "*/", true);
69 final Modifiers modifiers = new Modifiers();
72 final TokenizerMatch match = tokenizer.getNextToken();
76 if (match.token.equals("package")) {
77 parsePackage(tokenizer);
81 if (match.token.equals("import")) {
82 parseImport(tokenizer);
86 final boolean wasModifier = modifiers.parseModifier(match.token);
90 if ("class".equals(match.token)) {
91 parseClass(tokenizer);
95 if ("interface".equals(match.token)) {
96 parseInterface(tokenizer);
100 if ("@".equals(match.token)) {
101 final Annotation annotation = new Annotation(tokenizer);
105 System.out.println(" " + modifiers.toString() + " "
108 skipUntilSemicolon(tokenizer);
113 private void parseClass(final Tokenizer tokenizer)
114 throws InvalidSyntaxException {
116 final TokenizerMatch match = tokenizer.getNextToken();
117 final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
119 // System.out.println(clazz.toString());
124 private void parseImport(final Tokenizer tokenizer)
125 throws InvalidSyntaxException {
127 final Import imp = new Import();
129 final TokenizerMatch match = tokenizer.getNextToken();
131 if (match.token.equals("static")) {
133 imp.path = tokenizer.getNextToken().token;
135 imp.path = match.token;
139 tokenizer.expectNextToken(";");
142 private void parseInterface(final Tokenizer tokenizer)
143 throws InvalidSyntaxException {
145 final TokenizerMatch match = tokenizer.getNextToken();
146 final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
147 // System.out.println(clazz.toString());
151 private void parsePackage(final Tokenizer tokenizer)
152 throws InvalidSyntaxException {
154 final TokenizerMatch match = tokenizer.getNextToken();
156 packageName = match.token;
158 tokenizer.expectNextToken(";");
161 private void readFile() throws FileNotFoundException, IOException {
162 final FileReader fileReader = new FileReader(file);
164 final BufferedReader bufferedReader = new BufferedReader(fileReader);
167 final String line = bufferedReader.readLine();
172 contents.append(line);
173 contents.append("\n");
176 bufferedReader.close();
180 public void skipUntilSemicolon(final Tokenizer tokenizer) {
182 final TokenizerMatch token = tokenizer.getNextToken();
187 if (token.token.equals(";"))