Commit 979b9413 authored by Joachim Müssig's avatar Joachim Müssig
Browse files

clear up code, javadoc

parent da14fc5e
...@@ -132,24 +132,15 @@ public class IfThenElseListener extends Java8BaseListener{ ...@@ -132,24 +132,15 @@ public class IfThenElseListener extends Java8BaseListener{
public void rewriteFileWithAssertions() throws IOException { public void rewriteFileWithAssertions() throws IOException {
insertAssertions(); insertAssertions();
List<String> lines = readLinesOfFile(pathToJavaFile); List<String> lines = readLinesOfFile(pathToJavaFile);
File javaFile = new File(pathToJavaFile); File javaFile = new File(pathToJavaFile);
PrintWriter pw = new PrintWriter(new FileWriter(javaFile, false)); PrintWriter pw = new PrintWriter(new FileWriter(javaFile, false));
for(int i = 0; i < lines.size(); i++) { for(int i = 0; i < lines.size(); i++) {
if(insertions.containsKey(i)) { if(insertions.containsKey(i)) {
pw.println(insertions.get(i)); pw.println(insertions.get(i));
} }
pw.println(lines.get(i)); pw.println(lines.get(i));
} }
pw.close(); pw.close();
} }
...@@ -166,7 +157,6 @@ public class IfThenElseListener extends Java8BaseListener{ ...@@ -166,7 +157,6 @@ public class IfThenElseListener extends Java8BaseListener{
@Override @Override
public void enterIfThenElseStatement(Java8Parser.IfThenElseStatementContext ctx) { public void enterIfThenElseStatement(Java8Parser.IfThenElseStatementContext ctx) {
if(ctx.statementNoShortIf()!=null && ctx.statement() != null) { if(ctx.statementNoShortIf()!=null && ctx.statement() != null) {
int line = ctx.start.getLine(); int line = ctx.start.getLine();
if(analysis.containsKey(line)) { if(analysis.containsKey(line)) {
......
...@@ -6,14 +6,20 @@ import java.io.FileNotFoundException; ...@@ -6,14 +6,20 @@ import java.io.FileNotFoundException;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import org.antlr.runtime.BufferedTokenStream;
import org.antlr.runtime.TokenSource;
import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import antlr.TokenStreamBasicFilter;
import joanakey.antlr.java8.Java8BaseListener; import joanakey.antlr.java8.Java8BaseListener;
import joanakey.antlr.java8.Java8Lexer; import joanakey.antlr.java8.Java8Lexer;
public class PreProcessor extends Java8BaseListener { public class PreProcessor extends Java8BaseListener {
//TODO: dont remove comments
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
String testFilePath = "testdata/plusMinusFalsePositive"; String testFilePath = "testdata/plusMinusFalsePositive";
...@@ -23,10 +29,10 @@ public class PreProcessor extends Java8BaseListener { ...@@ -23,10 +29,10 @@ public class PreProcessor extends Java8BaseListener {
private static CommonTokenStream getTokenStream(String path) throws FileNotFoundException, IOException { private static CommonTokenStream getTokenStream(String path) throws FileNotFoundException, IOException {
FileInputStream in = new FileInputStream(path); FileInputStream in = new FileInputStream(path);
Java8Lexer java8Lexer = new Java8Lexer(new ANTLRInputStream(in));
Java8Lexer java8Lexer = new Java8Lexer(new ANTLRInputStream(in));
CommonTokenStream tokens = new CommonTokenStream(java8Lexer); CommonTokenStream tokens = new CommonTokenStream(java8Lexer);
// org.antlr.v4.runtime.BufferedTokenStream tokens = new org.antlr.v4.runtime.BufferedTokenStream(java8Lexer);
// t.getTokens();
tokens.getText(); tokens.getText();
return tokens; return tokens;
} }
...@@ -38,7 +44,6 @@ public class PreProcessor extends Java8BaseListener { ...@@ -38,7 +44,6 @@ public class PreProcessor extends Java8BaseListener {
//System.out.println("tokens: "+); //System.out.println("tokens: "+);
for(Token t : tokens.getTokens()){ for(Token t : tokens.getTokens()){
if(t.getText().equals("{")){ if(t.getText().equals("{")){
buffer.append("\n"+generateTab(depth)+t.getText()+"\n"); buffer.append("\n"+generateTab(depth)+t.getText()+"\n");
depth++; depth++;
br =true; br =true;
...@@ -46,9 +51,7 @@ public class PreProcessor extends Java8BaseListener { ...@@ -46,9 +51,7 @@ public class PreProcessor extends Java8BaseListener {
else if(t.getText().equals("}")){ else if(t.getText().equals("}")){
depth--; depth--;
buffer.append(generateTab(depth)+t.getText()+"\n"); buffer.append(generateTab(depth)+t.getText()+"\n");
br =true; br =true;
} }
else if(t.getText().equals(";")){ else if(t.getText().equals(";")){
buffer.append(t.getText()+"\n"); buffer.append(t.getText()+"\n");
...@@ -67,9 +70,7 @@ public class PreProcessor extends Java8BaseListener { ...@@ -67,9 +70,7 @@ public class PreProcessor extends Java8BaseListener {
} }
public void formatJavaSourceFolder(String path) throws FileNotFoundException, IOException { public void formatJavaSourceFolder(String path) throws FileNotFoundException, IOException {
File file = new File(path); File file = new File(path);
if(file.isDirectory()) { if(file.isDirectory()) {
for(File sub : file.listFiles()) { for(File sub : file.listFiles()) {
formatJavaSourceFolder(sub.getAbsolutePath()); formatJavaSourceFolder(sub.getAbsolutePath());
...@@ -84,13 +85,9 @@ public class PreProcessor extends Java8BaseListener { ...@@ -84,13 +85,9 @@ public class PreProcessor extends Java8BaseListener {
} }
public void formatJavaFile(String path) throws FileNotFoundException, IOException { public void formatJavaFile(String path) throws FileNotFoundException, IOException {
CommonTokenStream tokens = getTokenStream(path); CommonTokenStream tokens = getTokenStream(path);
String formatted = formatJavaSource(tokens); String formatted = formatJavaSource(tokens);
writeContentsToFile(path, formatted); writeContentsToFile(path, formatted);
} }
private void writeContentsToFile(String path, String formatted) throws IOException { private void writeContentsToFile(String path, String formatted) throws IOException {
...@@ -105,7 +102,7 @@ public class PreProcessor extends Java8BaseListener { ...@@ -105,7 +102,7 @@ public class PreProcessor extends Java8BaseListener {
private static String generateTab(int len){ private static String generateTab(int len){
String result = ""; String result = "";
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
result += " "; result += " ";
} }
return result; return result;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment