Parseur -> build clazz (simple replace of show method)

This commit is contained in:
jeffcheasey88 2023-06-05 11:26:46 +02:00
parent 6909ca513c
commit 0159f9c5fc
14 changed files with 147 additions and 120 deletions

Binary file not shown.

View file

@ -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,15 +99,18 @@ 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();
@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");
}
System.out.println(start+"}");
super.build(writer, tab);
writer.write("}\n");
}
@Override

View file

@ -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");
}
}

View file

@ -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 extends JavaElement> E find(Function<JavaElement, Boolean> search, Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace);
public abstract <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> 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);
}
}

View file

@ -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;

View file

@ -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;
@ -29,10 +30,9 @@ public class AssigmentOperation extends JavaElement{
}
@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

View file

@ -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;
@ -57,12 +58,23 @@ 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+"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{
@ -73,13 +85,14 @@ public class ConditionalOperation extends OperationContainer{
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);
}
}
}

View file

@ -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");
}
}

View file

@ -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,12 +42,21 @@ 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");
}
}

View file

@ -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 extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> 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");
}
}
}

View file

@ -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;
@ -33,10 +34,9 @@ public class MethodCallOperation extends JavaElement{
}
@Override
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+value+";");
public void build(BufferedWriter writer, int tab) throws Exception{
super.build(writer, tab);
writer.write(value+";\n");
}
@Override

View file

@ -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;
@ -37,18 +38,18 @@ public class ReturnOperation extends JavaElement{
}
@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 extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
if(search.apply(this)) return (E)this;

View file

@ -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;
@ -38,12 +39,14 @@ public class SynchronizedOperation extends OperationContainer{
}
@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");
}
}

View file

@ -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());