-/*
- * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.inspector.java.methods;
-
-import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
-import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
-import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import static eu.svjatoslav.commons.file.IOHelper.getFileContentsAsString;
-import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
-import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
-
-public class JavaFile {
-
- public static final String UTF_8 = "UTF-8";
- public final List<Clazz> classes = new ArrayList<>();
- String contents;
- private final List<Import> imports = new ArrayList<>();
- private final File file;
- private String packageName;
-
- public JavaFile(final File file) throws IOException, InvalidSyntaxException {
- this.file = file;
- parse();
- }
-
- public void parse() throws IOException, InvalidSyntaxException {
- System.out.println("java file: " + file);
-
- contents = getFileContentsAsString(file);
-
- final Tokenizer tokenizer = new Tokenizer(contents);
-
- // empty space
- tokenizer.addTerminator(" ", DROP);
- tokenizer.addTerminator("\t", DROP);
- tokenizer.addTerminator("\n", DROP);
-
- tokenizer.addTerminator(";", PRESERVE);
- tokenizer.addTerminator("{", PRESERVE);
- tokenizer.addTerminator("}", PRESERVE);
- tokenizer.addTerminator("(", PRESERVE);
- tokenizer.addTerminator(")", PRESERVE);
- tokenizer.addTerminator("[", PRESERVE);
- tokenizer.addTerminator("]", PRESERVE);
- tokenizer.addTerminator("<", PRESERVE);
- tokenizer.addTerminator(">", PRESERVE);
- tokenizer.addTerminator(",", PRESERVE);
- tokenizer.addTerminator("@", PRESERVE);
-
- // comments
- tokenizer.addTerminator("//", "\n", DROP);
- tokenizer.addTerminator("/*", "*/", DROP);
-
- final Modifiers modifiers = new Modifiers();
-
- while (true) {
- final TokenizerMatch match = tokenizer.getNextToken();
- if (match == null)
- break;
-
- if (match.token.equals("package")) {
- parsePackage(tokenizer);
- continue;
- }
-
- if (match.token.equals("import")) {
- parseImport(tokenizer);
- continue;
- }
-
- final boolean wasModifier = modifiers.parseModifier(match.token);
- if (wasModifier)
- continue;
-
- if ("class".equals(match.token)) {
- parseClass(tokenizer);
- continue;
- }
-
- if ("interface".equals(match.token)) {
- parseInterface(tokenizer);
- continue;
- }
-
- if ("@".equals(match.token)) {
- new Annotation(tokenizer);
- continue;
- }
-
- System.out.println(" " + modifiers.toString() + " "
- + match.token);
- modifiers.reset();
- skipUntilSemicolon(tokenizer);
- }
-
- }
-
- private void parseClass(final Tokenizer tokenizer)
- throws InvalidSyntaxException {
-
- final TokenizerMatch match = tokenizer.getNextToken();
- final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
- false);
- // System.out.println(clazz.toString());
- classes.add(clazz);
-
- }
-
- private void parseImport(final Tokenizer tokenizer)
- throws InvalidSyntaxException {
-
- final Import imp = new Import();
-
- final TokenizerMatch match = tokenizer.getNextToken();
-
- if (match.token.equals("static")) {
- imp.isStatic = true;
- imp.path = tokenizer.getNextToken().token;
- } else
- imp.path = match.token;
-
- imports.add(imp);
-
- tokenizer.expectAndConsumeNextToken(";");
- }
-
- private void parseInterface(final Tokenizer tokenizer)
- throws InvalidSyntaxException {
-
- final TokenizerMatch match = tokenizer.getNextToken();
- final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
- // System.out.println(clazz.toString());
- classes.add(clazz);
- }
-
- private void parsePackage(final Tokenizer tokenizer)
- throws InvalidSyntaxException {
-
- final TokenizerMatch match = tokenizer.getNextToken();
-
- packageName = match.token;
-
- tokenizer.expectAndConsumeNextToken(";");
- }
-
- public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException {
- while (true) {
- final TokenizerMatch token = tokenizer.getNextToken();
-
- if (token == null)
- return;
-
- if (token.token.equals(";"))
- return;
- }
- }
-
-}