X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finspector%2Fjava%2Fmethods%2FJavaFile.java;h=1ddc5943053db65efa25bc7fe8ca10a1153a13be;hb=d4780f4855f42272779c424b0d5e88f4e4621418;hp=e8cbe6b8c4e5aed3b09e533e40cc34acfe078a76;hpb=9d9b65fb1909e12606e76ed0625706e446f588fb;p=javainspect.git diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java b/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java index e8cbe6b..1ddc594 100755 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java @@ -1,7 +1,7 @@ /* * JavaInspect - Utility to visualize java software - * Copyright (C) 2013-2015, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2013-2018, 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. @@ -9,184 +9,180 @@ package eu.svjatoslav.inspector.java.methods; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException; import eu.svjatoslav.commons.string.tokenizer.Tokenizer; import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch; -public class JavaFile { - - private final List imports = new ArrayList(); - - private String packageName; - - private final File file; - - StringBuffer contents = new StringBuffer(); - - public List classes = new ArrayList(); - - public JavaFile(final File file) throws IOException, InvalidSyntaxException { - this.file = file; - parse(); - } - - public void parse() throws IOException, InvalidSyntaxException { - System.out.println("java file: " + file); +import java.io.*; +import java.util.ArrayList; +import java.util.List; - readFile(); +import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP; +import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE; - final Tokenizer tokenizer = new Tokenizer(contents.toString()); +public class JavaFile { - // empty space - tokenizer.addTerminator(" ", true); - tokenizer.addTerminator("\t", true); - tokenizer.addTerminator("\n", true); + public static final String UTF_8 = "UTF-8"; + public final List classes = new ArrayList<>(); + final StringBuffer contents = new StringBuffer(); + private final List 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); + + readFile(); - tokenizer.addTerminator(";", false); - tokenizer.addTerminator("{", false); - tokenizer.addTerminator("}", false); - tokenizer.addTerminator("(", false); - tokenizer.addTerminator(")", false); - tokenizer.addTerminator("[", false); - tokenizer.addTerminator("]", false); - tokenizer.addTerminator("<", false); - tokenizer.addTerminator(">", false); - tokenizer.addTerminator(",", false); - tokenizer.addTerminator("@", false); + final Tokenizer tokenizer = new Tokenizer(contents.toString()); + + // 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", true); - tokenizer.addTerminator("/*", "*/", true); + // comments + tokenizer.addTerminator("//", "\n", DROP); + tokenizer.addTerminator("/*", "*/", DROP); - final Modifiers modifiers = new Modifiers(); + final Modifiers modifiers = new Modifiers(); - while (true) { - final TokenizerMatch match = tokenizer.getNextToken(); - if (match == null) - break; + while (true) { + final TokenizerMatch match = tokenizer.getNextToken(); + if (match == null) + break; - if (match.token.equals("package")) { - parsePackage(tokenizer); - continue; - } + if (match.token.equals("package")) { + parsePackage(tokenizer); + continue; + } - if (match.token.equals("import")) { - parseImport(tokenizer); - continue; - } + if (match.token.equals("import")) { + parseImport(tokenizer); + continue; + } - final boolean wasModifier = modifiers.parseModifier(match.token); - if (wasModifier) - continue; + final boolean wasModifier = modifiers.parseModifier(match.token); + if (wasModifier) + continue; - if ("class".equals(match.token)) { - parseClass(tokenizer); - continue; - } + if ("class".equals(match.token)) { + parseClass(tokenizer); + continue; + } - if ("interface".equals(match.token)) { - parseInterface(tokenizer); - continue; - } + if ("interface".equals(match.token)) { + parseInterface(tokenizer); + continue; + } - if ("@".equals(match.token)) { - final Annotation annotation = new Annotation(tokenizer); - continue; - } + if ("@".equals(match.token)) { + new Annotation(tokenizer); + continue; + } - System.out.println(" " + modifiers.toString() + " " - + match.token); - modifiers.reset(); - skipUntilSemicolon(tokenizer); - } + System.out.println(" " + modifiers.toString() + " " + + match.token); + modifiers.reset(); + skipUntilSemicolon(tokenizer); + } - } + } - private void parseClass(final Tokenizer tokenizer) - throws InvalidSyntaxException { + 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); + 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 { + private void parseImport(final Tokenizer tokenizer) + throws InvalidSyntaxException { - final Import imp = new Import(); + final Import imp = new Import(); - final TokenizerMatch match = tokenizer.getNextToken(); + final TokenizerMatch match = tokenizer.getNextToken(); - if (match.token.equals("static")) { - imp.isStatic = true; - imp.path = tokenizer.getNextToken().token; - } else - imp.path = match.token; + if (match.token.equals("static")) { + imp.isStatic = true; + imp.path = tokenizer.getNextToken().token; + } else + imp.path = match.token; - imports.add(imp); + imports.add(imp); - tokenizer.expectNextToken(";"); - } + tokenizer.expectAndConsumeNextToken(";"); + } - private void parseInterface(final Tokenizer tokenizer) - throws InvalidSyntaxException { + 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); - } + 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 { + private void parsePackage(final Tokenizer tokenizer) + throws InvalidSyntaxException { - final TokenizerMatch match = tokenizer.getNextToken(); + final TokenizerMatch match = tokenizer.getNextToken(); - packageName = match.token; + packageName = match.token; - tokenizer.expectNextToken(";"); - } + tokenizer.expectAndConsumeNextToken(";"); + } - private void readFile() throws FileNotFoundException, IOException { - final FileReader fileReader = new FileReader(file); + private void readFile() throws IOException { + InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), UTF_8); - final BufferedReader bufferedReader = new BufferedReader(fileReader); + final BufferedReader bufferedReader = new BufferedReader(inputStream); - while (true) { - final String line = bufferedReader.readLine(); + while (true) { + final String line = bufferedReader.readLine(); - if (line == null) - break; + if (line == null) + break; - contents.append(line); - contents.append("\n"); - } + contents.append(line); + contents.append("\n"); + } - bufferedReader.close(); - fileReader.close(); - } + bufferedReader.close(); + inputStream.close(); + } - public void skipUntilSemicolon(final Tokenizer tokenizer) { - while (true) { - final TokenizerMatch token = tokenizer.getNextToken(); + public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException { + while (true) { + final TokenizerMatch token = tokenizer.getNextToken(); - if (token == null) - return; + if (token == null) + return; - if (token.token.equals(";")) - return; - } - } + if (token.token.equals(";")) + return; + } + } }