/*
* JavaInspect - Utility to visualize java software
- * Copyright (C) 2013-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ * 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.
import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
-import java.io.*;
+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";
- private final List<Import> imports = new ArrayList<Import>();
+ public final List<Clazz> classes = new ArrayList<>();
+ String contents;
+ private final List<Import> imports = new ArrayList<>();
private final File file;
- public List<Clazz> classes = new ArrayList<Clazz>();
- StringBuffer contents = new StringBuffer();
private String packageName;
public JavaFile(final File file) throws IOException, InvalidSyntaxException {
public void parse() throws IOException, InvalidSyntaxException {
System.out.println("java file: " + file);
- readFile();
+ contents = getFileContentsAsString(file);
- final Tokenizer tokenizer = new Tokenizer(contents.toString());
+ final Tokenizer tokenizer = new Tokenizer(contents);
// empty space
- tokenizer.addTerminator(" ", DROP);
+ tokenizer.addTerminator(" ", DROP);
tokenizer.addTerminator("\t", DROP);
tokenizer.addTerminator("\n", DROP);
tokenizer.expectAndConsumeNextToken(";");
}
- private void readFile() throws IOException {
- InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), UTF_8);
-
- final BufferedReader bufferedReader = new BufferedReader(inputStream);
-
- while (true) {
- final String line = bufferedReader.readLine();
-
- if (line == null)
- break;
-
- contents.append(line);
- contents.append("\n");
- }
-
- bufferedReader.close();
- inputStream.close();
- }
-
public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException {
while (true) {
final TokenizerMatch token = tokenizer.getNextToken();