2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2018, 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 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
21 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
23 public class JavaFile {
25 public static final String UTF_8 = "UTF-8";
26 public final List<Clazz> classes = new ArrayList<>();
27 final StringBuffer contents = new StringBuffer();
28 private final List<Import> imports = new ArrayList<>();
29 private final File file;
30 private String packageName;
32 public JavaFile(final File file) throws IOException, InvalidSyntaxException {
37 public void parse() throws IOException, InvalidSyntaxException {
38 System.out.println("java file: " + file);
42 final Tokenizer tokenizer = new Tokenizer(contents.toString());
45 tokenizer.addTerminator(" ", DROP);
46 tokenizer.addTerminator("\t", DROP);
47 tokenizer.addTerminator("\n", DROP);
49 tokenizer.addTerminator(";", PRESERVE);
50 tokenizer.addTerminator("{", PRESERVE);
51 tokenizer.addTerminator("}", PRESERVE);
52 tokenizer.addTerminator("(", PRESERVE);
53 tokenizer.addTerminator(")", PRESERVE);
54 tokenizer.addTerminator("[", PRESERVE);
55 tokenizer.addTerminator("]", PRESERVE);
56 tokenizer.addTerminator("<", PRESERVE);
57 tokenizer.addTerminator(">", PRESERVE);
58 tokenizer.addTerminator(",", PRESERVE);
59 tokenizer.addTerminator("@", PRESERVE);
62 tokenizer.addTerminator("//", "\n", DROP);
63 tokenizer.addTerminator("/*", "*/", DROP);
65 final Modifiers modifiers = new Modifiers();
68 final TokenizerMatch match = tokenizer.getNextToken();
72 if (match.token.equals("package")) {
73 parsePackage(tokenizer);
77 if (match.token.equals("import")) {
78 parseImport(tokenizer);
82 final boolean wasModifier = modifiers.parseModifier(match.token);
86 if ("class".equals(match.token)) {
87 parseClass(tokenizer);
91 if ("interface".equals(match.token)) {
92 parseInterface(tokenizer);
96 if ("@".equals(match.token)) {
97 new Annotation(tokenizer);
101 System.out.println(" " + modifiers.toString() + " "
104 skipUntilSemicolon(tokenizer);
109 private void parseClass(final Tokenizer tokenizer)
110 throws InvalidSyntaxException {
112 final TokenizerMatch match = tokenizer.getNextToken();
113 final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
115 // System.out.println(clazz.toString());
120 private void parseImport(final Tokenizer tokenizer)
121 throws InvalidSyntaxException {
123 final Import imp = new Import();
125 final TokenizerMatch match = tokenizer.getNextToken();
127 if (match.token.equals("static")) {
129 imp.path = tokenizer.getNextToken().token;
131 imp.path = match.token;
135 tokenizer.expectAndConsumeNextToken(";");
138 private void parseInterface(final Tokenizer tokenizer)
139 throws InvalidSyntaxException {
141 final TokenizerMatch match = tokenizer.getNextToken();
142 final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
143 // System.out.println(clazz.toString());
147 private void parsePackage(final Tokenizer tokenizer)
148 throws InvalidSyntaxException {
150 final TokenizerMatch match = tokenizer.getNextToken();
152 packageName = match.token;
154 tokenizer.expectAndConsumeNextToken(";");
157 private void readFile() throws IOException {
158 InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), UTF_8);
160 final BufferedReader bufferedReader = new BufferedReader(inputStream);
163 final String line = bufferedReader.readLine();
168 contents.append(line);
169 contents.append("\n");
172 bufferedReader.close();
176 public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException {
178 final TokenizerMatch token = tokenizer.getNextToken();
183 if (token.token.equals(";"))