2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013-2020, 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.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
21 import static eu.svjatoslav.commons.file.IOHelper.getFileContentsAsString;
22 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
23 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
25 public class JavaFile {
27 public static final String UTF_8 = "UTF-8";
28 public final List<Clazz> classes = new ArrayList<>();
30 private final List<Import> imports = new ArrayList<>();
31 private final File file;
32 private String packageName;
34 public JavaFile(final File file) throws IOException, InvalidSyntaxException {
39 public void parse() throws IOException, InvalidSyntaxException {
40 System.out.println("java file: " + file);
42 contents = getFileContentsAsString(file);
44 final Tokenizer tokenizer = new Tokenizer(contents);
47 tokenizer.addTerminator(" ", DROP);
48 tokenizer.addTerminator("\t", DROP);
49 tokenizer.addTerminator("\n", DROP);
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);
60 tokenizer.addTerminator(",", PRESERVE);
61 tokenizer.addTerminator("@", PRESERVE);
64 tokenizer.addTerminator("//", "\n", DROP);
65 tokenizer.addTerminator("/*", "*/", DROP);
67 final Modifiers modifiers = new Modifiers();
70 final TokenizerMatch match = tokenizer.getNextToken();
74 if (match.token.equals("package")) {
75 parsePackage(tokenizer);
79 if (match.token.equals("import")) {
80 parseImport(tokenizer);
84 final boolean wasModifier = modifiers.parseModifier(match.token);
88 if ("class".equals(match.token)) {
89 parseClass(tokenizer);
93 if ("interface".equals(match.token)) {
94 parseInterface(tokenizer);
98 if ("@".equals(match.token)) {
99 new Annotation(tokenizer);
103 System.out.println(" " + modifiers.toString() + " "
106 skipUntilSemicolon(tokenizer);
111 private void parseClass(final Tokenizer tokenizer)
112 throws InvalidSyntaxException {
114 final TokenizerMatch match = tokenizer.getNextToken();
115 final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
117 // System.out.println(clazz.toString());
122 private void parseImport(final Tokenizer tokenizer)
123 throws InvalidSyntaxException {
125 final Import imp = new Import();
127 final TokenizerMatch match = tokenizer.getNextToken();
129 if (match.token.equals("static")) {
131 imp.path = tokenizer.getNextToken().token;
133 imp.path = match.token;
137 tokenizer.expectAndConsumeNextToken(";");
140 private void parseInterface(final Tokenizer tokenizer)
141 throws InvalidSyntaxException {
143 final TokenizerMatch match = tokenizer.getNextToken();
144 final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
145 // System.out.println(clazz.toString());
149 private void parsePackage(final Tokenizer tokenizer)
150 throws InvalidSyntaxException {
152 final TokenizerMatch match = tokenizer.getNextToken();
154 packageName = match.token;
156 tokenizer.expectAndConsumeNextToken(";");
159 public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException {
161 final TokenizerMatch token = tokenizer.getNextToken();
166 if (token.token.equals(";"))