X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finspector%2Fjava%2Fmethods%2FJavaFile.java;h=09469f80123d896886fff1f706c274543228a6d4;hb=7d1259aea992843c47f29c932434a88ea9364f7e;hp=d517537e94817988a4729a3a8db02cfb5c96d6e8;hpb=176eff8b9757d8097c5df58782bb4ba8be56b2a5;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 old mode 100644 new mode 100755 index d517537..09469f8 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java @@ -1,83 +1,188 @@ +/* + * 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 java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; +import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException; +import eu.svjatoslav.commons.string.tokenizer.Tokenizer; +import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch; + +import java.io.*; import java.util.ArrayList; import java.util.List; -import eu.svjatoslav.inspector.tokenizer.Tokenizer; -import eu.svjatoslav.inspector.tokenizer.TokenizerMatch; +import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP; +import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE; public class JavaFile { - private final List imports = new ArrayList(); + 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(); + + 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", 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 { - private final File file; + final Import imp = new Import(); - StringBuffer contents = new StringBuffer(); + final TokenizerMatch match = tokenizer.getNextToken(); - public JavaFile(final File file) throws IOException { - this.file = file; - parse(); - } + if (match.token.equals("static")) { + imp.isStatic = true; + imp.path = tokenizer.getNextToken().token; + } else + imp.path = match.token; - public void parse() throws IOException { - System.out.println("java file: " + file); + imports.add(imp); - readFile(); + tokenizer.expectAndConsumeNextToken(";"); + } - final Tokenizer tokenizer = new Tokenizer(contents.toString()); - tokenizer.addTerminator(" ", true); - tokenizer.addTerminator("\t", true); - tokenizer.addTerminator("\n", true); + private void parseInterface(final Tokenizer tokenizer) + throws InvalidSyntaxException { - tokenizer.addTerminator(";", false); + final TokenizerMatch match = tokenizer.getNextToken(); + final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true); + // System.out.println(clazz.toString()); + classes.add(clazz); + } - while (true) { - final TokenizerMatch match = tokenizer.getToken(); - if (match == null) - break; + private void parsePackage(final Tokenizer tokenizer) + throws InvalidSyntaxException { - if (match.token.equals("import")) - parseImport(tokenizer); - } + final TokenizerMatch match = tokenizer.getNextToken(); - } + packageName = match.token; - private void parseImport(final Tokenizer tokenizer) { - final Import imp = new Import(); + tokenizer.expectAndConsumeNextToken(";"); + } - final TokenizerMatch match = tokenizer.getToken(); + private void readFile() throws IOException { + InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), UTF_8); - if (match.token.equals("static")) { - imp.isStatic = true; - imp.path = tokenizer.getToken().token; - } else - imp.path = match.token; + final BufferedReader bufferedReader = new BufferedReader(inputStream); - imports.add(imp); - } + while (true) { + final String line = bufferedReader.readLine(); - private void readFile() throws FileNotFoundException, IOException { - final FileReader fileReader = new FileReader(file); + if (line == null) + break; - final BufferedReader bufferedReader = new BufferedReader(fileReader); + contents.append(line); + contents.append("\n"); + } - while (true) { - final String line = bufferedReader.readLine(); + bufferedReader.close(); + inputStream.close(); + } - if (line == null) - break; + public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException { + while (true) { + final TokenizerMatch token = tokenizer.getNextToken(); - contents.append(line); - contents.append("\n"); - } + if (token == null) + return; - bufferedReader.close(); - fileReader.close(); - } + if (token.token.equals(";")) + return; + } + } }