diff --git a/Treasure.jar b/Treasure.jar index 9438843..d72484f 100644 Binary files a/Treasure.jar and b/Treasure.jar differ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 9b2422c..00237f9 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java; +import java.io.BufferedWriter; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; @@ -98,17 +99,20 @@ public class Class extends JavaElement{ // return this.vars; // } - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+Modifier.toString(modifier)+" "+this.name+"{"); - for(JavaElement jElement : this.childs){ - jElement.show(tab+1); - System.out.println(); - } - System.out.println(start+"}"); - } + @Override + public void build(BufferedWriter writer, int tab) throws Exception{ + super.build(writer, tab); + writer.write(Modifier.toString(modifier)+" "+this.name+"{\n"); + for(JavaElement child : this.childs){ + child.build(writer, tab+1); + writer.write("\n"); + } + + super.build(writer, tab); + writer.write("}\n"); + } + @Override public E find(java.util.function.Function search, java.util.function.Function, Boolean> deep, List trace){ if(search.apply(this)) return (E)this; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index 9983392..8351d71 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java; +import java.io.BufferedWriter; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Iterator; @@ -93,15 +94,18 @@ public class Function extends OperationContainer{ }while(quote); } - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; + @Override + public void build(BufferedWriter writer, int tab) throws Exception{ String param = ""; for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+""; if(!param.isEmpty()) param = param.substring(1); - System.out.println(start+Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+param+") "+exceptions+"{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + super.build(writer, tab); + writer.write(Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+param+") "+exceptions+"{\n"); + + for(JavaElement child : getChilds()) child.build(writer, tab+1); + + super.build(writer, tab); + writer.write("}\n"); } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java index c5a2f41..3136bd0 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java; +import java.io.BufferedWriter; import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; @@ -11,6 +12,9 @@ public abstract class JavaElement { public abstract E find(Function search, Function, Boolean> deep, List trace); public abstract E find(BiFunction, Boolean> search, List trace); - //Only for development - public abstract void show(int tab); + public void build(BufferedWriter writer, int tab) throws Exception { + String spacement = ""; + for(int i = 0; i < tab; i++) spacement+="\t"; + writer.write(spacement); + } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 3b98a36..768db83 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -21,8 +21,8 @@ public class JavaParser{ JavaParser parser = new JavaParser(reader); parser.parse(); - System.out.println("SHOW-----------------"); - parser.show(); + System.out.println("build-----------------"); + parser.build(new BufferedWriter(new FileWriter(new File("/home/buildClazzFromParser.txt")))); } public static void mainee(String[] args) throws Exception { @@ -44,7 +44,6 @@ public class JavaParser{ JavaParser parser = new JavaParser(reader); parser.parse(); System.out.println("SHOW-----------------"); - parser.show(); } private static void show(File dir) throws Exception{ @@ -131,14 +130,18 @@ public class JavaParser{ return this.clazz; } - public void show(){ - System.out.println("package "+this.pack.getName()+";"); - System.out.println(); - for(Import i : this.imports) System.out.println("import "+i.getName()+";"); - System.out.println(); - this.clazz.show(0); + public void build(BufferedWriter writer) throws Exception{ + writer.write("package "+this.pack.getName()+";\n"); + writer.write("\n"); + for(Import element : this.imports) writer.write("import "+element.getName()+";\n"); + writer.write("\n"); + + this.clazz.build(writer, 0); + writer.flush(); + writer.close(); } + public static int getModifier(String modifier){ switch(modifier){ case "public": return Modifier.PUBLIC; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java index 00189b2..76d423d 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.List; import java.util.function.BiFunction; import java.util.regex.Matcher; @@ -27,12 +28,11 @@ public class AssigmentOperation extends JavaElement{ return matcher.group(1).length(); } - + @Override - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+variable+" = "+value+";"); + public void build(BufferedWriter writer, int tab) throws Exception{ + super.build(writer, tab); + writer.write(variable+"= "+value+";\n"); } @Override diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java index 4c658c7..7ecff1e 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -55,14 +56,25 @@ public class ConditionalOperation extends OperationContainer{ return index+bodysize; } - + @Override - public void show(int tab) { - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"Condition??"+condition+"{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + public void build(BufferedWriter writer, int tab) throws Exception{ + boolean empty = getChilds().size() == 0; + if(empty){ + writer.write(";\n"); + return; + } + + boolean oneChild = getChilds().size() == 1; + if(oneChild) writer.write("\t"); + else writer.write("{\n"); + + for(JavaElement child : getChilds()) child.build(writer, oneChild ? 0 : tab+1); + + if(!oneChild){ + super.build(writer, tab); + writer.write("}\n"); + } } public static class IfOperation extends ConditionalOperation{ @@ -72,14 +84,15 @@ public class ConditionalOperation extends OperationContainer{ public IfOperation(){ super(PATTERN); } - + + @Override - public void show(int tab) { - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"if"+super.condition+"{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + public void build(BufferedWriter writer, int tab) throws Exception{ + String spacement = ""; + for(int i = 0; i < tab; i++) spacement+="\t"; + + writer.write(spacement+"if"+super.condition); + super.build(writer, tab+1); } } @@ -92,12 +105,12 @@ public class ConditionalOperation extends OperationContainer{ } @Override - public void show(int tab) { - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"for"+super.condition+"{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + public void build(BufferedWriter writer, int tab) throws Exception{ + String spacement = ""; + for(int i = 0; i < tab; i++) spacement+="\t"; + + writer.write(spacement+"for"+super.condition); + super.build(writer, tab+1); } } @@ -110,12 +123,12 @@ public class ConditionalOperation extends OperationContainer{ } @Override - public void show(int tab) { - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"while"+super.condition+"{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + public void build(BufferedWriter writer, int tab) throws Exception{ + String spacement = ""; + for(int i = 0; i < tab; i++) spacement+="\t"; + + writer.write(spacement+"while"+super.condition); + super.build(writer, tab+1); } } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java index d92dde5..e2bfac8 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -36,12 +37,14 @@ public class DoOperation extends OperationContainer{ } @Override - public void show(int tab) { - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"do{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + public void build(BufferedWriter writer, int tab) throws Exception{ + super.build(writer, tab); + writer.write("do{\n"); + + for(JavaElement child : getChilds()) child.build(writer, tab+1); + + super.build(writer, tab); + writer.write("}\n"); } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java index a15b9bf..ff91ddf 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -41,13 +42,22 @@ public class ElseOperation extends OperationContainer{ } @Override - public void show(int tab) { - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"else{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + public void build(BufferedWriter writer, int tab) throws Exception{ + super.build(writer, tab); + writer.write("else "); + + boolean oneOperation = getChilds().size() == 1; + if(oneOperation){ + getChilds().get(0).build(writer, 0); + return; + } + + writer.write("{\n"); + for(JavaElement child : getChilds()) child.build(writer, tab+1); + + super.build(writer, tab); + writer.write("}\n"); } - + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/LoopAffectOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/LoopAffectOperation.java index 30babaf..d9a9960 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/LoopAffectOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/LoopAffectOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.List; import java.util.function.BiFunction; import java.util.regex.Matcher; @@ -24,12 +25,6 @@ public class LoopAffectOperation extends JavaElement{ return matcher.group(1).length(); } - @Override - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"loop affect??;"); - } @Override public E find(java.util.function.Function search, java.util.function.Function, Boolean> deep, List trace){ @@ -51,11 +46,11 @@ public class LoopAffectOperation extends JavaElement{ } @Override - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"continue;"); + public void build(BufferedWriter writer, int tab) throws Exception { + super.build(writer, tab); + writer.write("continue;\n"); } + } public static class BreakOperation extends LoopAffectOperation{ @@ -67,10 +62,9 @@ public class LoopAffectOperation extends JavaElement{ } @Override - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"break;"); + public void build(BufferedWriter writer, int tab) throws Exception { + super.build(writer, tab); + writer.write("break;\n"); } } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java index aa891e2..94e023b 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.List; import java.util.function.BiFunction; import java.util.regex.Matcher; @@ -31,14 +32,13 @@ public class MethodCallOperation extends JavaElement{ public String getValue(){ return this.value; } - - @Override - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+value+";"); - } + @Override + public void build(BufferedWriter writer, int tab) throws Exception{ + super.build(writer, tab); + writer.write(value+";\n"); + } + @Override public E find(java.util.function.Function search, java.util.function.Function, Boolean> deep, List trace){ return search.apply(this) ? (E)this : null; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java index 07a9c44..5163dd8 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.List; import java.util.function.BiFunction; import java.util.regex.Matcher; @@ -35,19 +36,19 @@ public class ReturnOperation extends JavaElement{ return matcher.group(1).length(); } - + @Override - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; + public void build(BufferedWriter writer, int tab) throws Exception{ + super.build(writer, tab); if(value != null){ - System.out.println("return"); - value.show(tab+1); - System.out.println(";"); + writer.write("return"); + value.build(writer, 0); + writer.write(";\n"); return; } - System.out.println(start+"return "+toChange+";"); + writer.write("return "+toChange+";\n"); } + @Override public E find(java.util.function.Function search, java.util.function.Function, Boolean> deep, List trace){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java index d15ee88..25e22b8 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java @@ -1,5 +1,6 @@ package be.jeffcheasey88.peeratcode.parser.java.operations; +import java.io.BufferedWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -27,7 +28,7 @@ public class SynchronizedOperation extends OperationContainer{ matcher.matches(); content = matcher.group(1); - + index += content.length(); content = local.unzipOne(content, (s,p) -> s); content = content.substring(1, content.length()-1); @@ -36,14 +37,16 @@ public class SynchronizedOperation extends OperationContainer{ return index; } - + @Override - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+"synchronized"+this.include+"{"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); + public void build(BufferedWriter writer, int tab) throws Exception{ + super.build(writer, tab); + writer.write("synchronized"+this.include+"{\n"); + + for(JavaElement child : getChilds()) child.build(writer, tab+1); + + super.build(writer, tab); + writer.write("}\n"); } } \ No newline at end of file diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java index b1dc778..d0d4ef2 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java @@ -43,8 +43,6 @@ class OperationTest{ JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0) { return i; } return i; } }"); Class clazz = parser.getClazz(); - clazz.show(0); - assertEquals(1, clazz.getChilds().size()); Function function = (Function) clazz.getChilds().get(0); assertEquals(3, function.getChilds().size()); @@ -60,8 +58,6 @@ class OperationTest{ JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0); return i; } }"); Class clazz = parser.getClazz(); - clazz.show(0); - assertEquals(1, clazz.getChilds().size()); Function function = (Function) clazz.getChilds().get(0); assertEquals(3, function.getChilds().size()); @@ -77,8 +73,6 @@ class OperationTest{ JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; do{ System.out.println(\"Hello\"); }while(i == 0); return i; } }"); Class clazz = parser.getClazz(); - clazz.show(0); - assertEquals(1, clazz.getChilds().size()); Function function = (Function) clazz.getChilds().get(0); assertEquals(4, function.getChilds().size()); @@ -101,8 +95,6 @@ class OperationTest{ JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) continue; } }"); Class clazz = parser.getClazz(); - clazz.show(0); - assertEquals(1, clazz.getChilds().size()); Function function = (Function) clazz.getChilds().get(0); assertEquals(1, function.getChilds().size()); @@ -123,8 +115,6 @@ class OperationTest{ JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) break ; } }"); Class clazz = parser.getClazz(); - clazz.show(0); - assertEquals(1, clazz.getChilds().size()); Function function = (Function) clazz.getChilds().get(0); assertEquals(1, function.getChilds().size()); @@ -145,8 +135,6 @@ class OperationTest{ JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ synchronized(this) { this.none(); } } }"); Class clazz = parser.getClazz(); - clazz.show(0); - assertEquals(1, clazz.getChilds().size()); Function function = (Function) clazz.getChilds().get(0); assertEquals(1, function.getChilds().size());