2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2017, 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 eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
13 import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
14 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
17 import java.util.ArrayList;
18 import java.util.List;
20 public class JavaFile {
22 public static final String UTF_8 = "UTF-8";
23 private final List<Import> imports = new ArrayList<Import>();
24 private final File file;
25 public List<Clazz> classes = new ArrayList<Clazz>();
26 StringBuffer contents = new StringBuffer();
27 private String packageName;
29 public JavaFile(final File file) throws IOException, InvalidSyntaxException {
34 public void parse() throws IOException, InvalidSyntaxException {
35 System.out.println("java file: " + file);
39 final Tokenizer tokenizer = new Tokenizer(contents.toString());
42 tokenizer.addTerminator(" ", true);
43 tokenizer.addTerminator("\t", true);
44 tokenizer.addTerminator("\n", true);
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 tokenizer.addTerminator(",", false);
56 tokenizer.addTerminator("@", false);
59 tokenizer.addTerminator("//", "\n", true);
60 tokenizer.addTerminator("/*", "*/", true);
62 final Modifiers modifiers = new Modifiers();
65 final TokenizerMatch match = tokenizer.getNextToken();
69 if (match.token.equals("package")) {
70 parsePackage(tokenizer);
74 if (match.token.equals("import")) {
75 parseImport(tokenizer);
79 final boolean wasModifier = modifiers.parseModifier(match.token);
83 if ("class".equals(match.token)) {
84 parseClass(tokenizer);
88 if ("interface".equals(match.token)) {
89 parseInterface(tokenizer);
93 if ("@".equals(match.token)) {
94 new Annotation(tokenizer);
98 System.out.println(" " + modifiers.toString() + " "
101 skipUntilSemicolon(tokenizer);
106 private void parseClass(final Tokenizer tokenizer)
107 throws InvalidSyntaxException {
109 final TokenizerMatch match = tokenizer.getNextToken();
110 final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
112 // System.out.println(clazz.toString());
117 private void parseImport(final Tokenizer tokenizer)
118 throws InvalidSyntaxException {
120 final Import imp = new Import();
122 final TokenizerMatch match = tokenizer.getNextToken();
124 if (match.token.equals("static")) {
126 imp.path = tokenizer.getNextToken().token;
128 imp.path = match.token;
132 tokenizer.expectNextToken(";");
135 private void parseInterface(final Tokenizer tokenizer)
136 throws InvalidSyntaxException {
138 final TokenizerMatch match = tokenizer.getNextToken();
139 final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
140 // System.out.println(clazz.toString());
144 private void parsePackage(final Tokenizer tokenizer)
145 throws InvalidSyntaxException {
147 final TokenizerMatch match = tokenizer.getNextToken();
149 packageName = match.token;
151 tokenizer.expectNextToken(";");
154 private void readFile() throws IOException {
155 InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), UTF_8);
157 final BufferedReader bufferedReader = new BufferedReader(inputStream);
160 final String line = bufferedReader.readLine();
165 contents.append(line);
166 contents.append("\n");
169 bufferedReader.close();
173 public void skipUntilSemicolon(final Tokenizer tokenizer) {
175 final TokenizerMatch token = tokenizer.getNextToken();
180 if (token.token.equals(";"))