Compare commits

...

2 commits

18 changed files with 253 additions and 146 deletions

Binary file not shown.

View file

@ -0,0 +1,28 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Array;
import java.util.Iterator;
public class ArrayBuffer<E>{
private E[] elements;
public ArrayBuffer(java.lang.Class<?> type){
this.elements = (E[]) Array.newInstance(type, 0);
}
public void add(E e){
E[] copy = (E[]) Array.newInstance(e.getClass(), this.elements.length+1);
System.arraycopy(elements, 0, copy, 0, elements.length);
copy[elements.length] = e;
this.elements = copy;
}
public void append(java.util.function.Function<E, E> modifier){
this.elements[elements.length-1] = modifier.apply(this.elements[elements.length-1]);
}
public Iterator<E> iterator(){
return new ArrayIterator<E>(elements);
}
}

View file

@ -1,5 +1,6 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Array;
import java.util.Iterator;
public class ArrayIterator<E> implements Iterator<E>{
@ -8,9 +9,11 @@ public class ArrayIterator<E> implements Iterator<E>{
private int index;
public ArrayIterator(E[] elements){
this.elements = elements;
E[] copy = (E[]) Array.newInstance(elements.getClass().getComponentType(), elements.length);
System.arraycopy(elements, 0, copy, 0, elements.length);
this.elements = copy;
}
@Override
public boolean hasNext(){
return index < elements.length;

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,17 +99,22 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+this.name+"{");
buffer.add("");
for(JavaElement child : this.childs){
child.build(buffer, tab+1);
buffer.add("");
}
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
@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

@ -93,15 +93,23 @@ 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(ArrayBuffer<String> buffer, 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(buffer, tab);
final String paramMod = param;
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+paramMod+") "+exceptions+"{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}

View file

@ -11,6 +11,10 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception {
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modifier = spacement;
buffer.append((s) -> s+=modifier);
}
}

View file

@ -7,6 +7,7 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
@ -19,10 +20,10 @@ public class JavaParser{
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Class.java");
BufferedReader reader = new BufferedReader(new FileReader(file));
JavaParser parser = new JavaParser(reader);
parser.parse();
System.out.println("SHOW-----------------");
parser.show();
JavaParser parser = new JavaParser();
parser.parse(reader);
System.out.println("build-----------------");
parser.build(new BufferedWriter(new FileWriter(new File("/home/buildClazzFromParser.txt"))));
}
public static void mainee(String[] args) throws Exception {
@ -41,10 +42,9 @@ public class JavaParser{
BufferedReader reader = new BufferedReader(new FileReader(new File("/home/tmp.txt")));
JavaParser parser = new JavaParser(reader);
parser.parse();
JavaParser parser = new JavaParser();
parser.parse(reader);
System.out.println("SHOW-----------------");
parser.show();
}
private static void show(File dir) throws Exception{
@ -54,8 +54,8 @@ public class JavaParser{
BufferedReader reader = new BufferedReader(new FileReader(dir));
try {
JavaParser parser = new JavaParser(reader);
parser.parse();
JavaParser parser = new JavaParser();
parser.parse(reader);
Class clazz = parser.getClazz();
System.out.println(clazz.getName());
@ -72,17 +72,15 @@ public class JavaParser{
private List<Import> imports;
private Class clazz;
private BufferedReader reader;
private CleanerPool cleaner;
public JavaParser(BufferedReader reader){
this.reader = reader;
}
public JavaParser(){}
public void parse() throws Exception{
public void parse(BufferedReader reader) throws Exception{
String content = "";
int index;
CleanerPool cleaner = new CleanerPool(
cleaner = new CleanerPool(
new Cleaner("CONSTANT_STRING",'"','"'),
new Cleaner("CONSTANT_CHAR",'\'','\''));
@ -131,14 +129,28 @@ 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");
ArrayBuffer<String> buffer = new ArrayBuffer<String>(String.class);
buffer.add("");
this.clazz.build(buffer, 0);
Iterator<String> lines = buffer.iterator();
while(lines.hasNext()){
String line = lines.next();
line = this.cleaner.unzip(line, (s,p) -> s);
writer.write(line+"\n");
}
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;
import java.io.BufferedWriter;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
@ -126,10 +127,12 @@ public class Variable extends JavaElement{
return this.value;
}
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
buffer.add("");
}
@Override
@ -188,16 +191,21 @@ public class Variable extends JavaElement{
}
@Override
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String sMod = spacement;
String vars = "";
for(int i = 0; i < this.names.size(); i++){
Variable value = this.values.get(i);
vars+=","+this.names.get(i)+((value == null) ? "" : " = "+value);
}
vars = vars.substring(1);
System.out.println(start+Modifier.toString(getModifier())+" "+getType()+" "+vars+";");
String varMod = vars;
buffer.append((s) -> s+=sMod+Modifier.toString(getModifier())+" "+getType()+" "+varMod+";");
buffer.add("");
}
}
}

View file

@ -5,6 +5,7 @@ import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -27,12 +28,12 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=variable+" = "+value+";");
buffer.add("");
}
@Override

View file

@ -1,8 +1,10 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.io.BufferedWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -55,14 +57,30 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
boolean empty = getChilds().size() == 0;
if(empty){
buffer.append((s) -> s+=";");
buffer.add("");
return;
}
boolean oneChild = getChilds().size() == 1;
if(oneChild) buffer.append((s) -> s+=" ");
else{
buffer.append((s) -> s+="{");
buffer.add("");
}
for(JavaElement child : getChilds()) child.build(buffer, oneChild ? 0 : tab+1);
if(!oneChild){
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
public static class IfOperation extends ConditionalOperation{
@ -72,14 +90,16 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modSpace = spacement;
buffer.append((s) -> s+=modSpace+"if"+super.condition);
super.build(buffer, tab);
}
}
@ -92,12 +112,13 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modSpace = spacement;
buffer.append((s) -> s+=modSpace+"for"+super.condition);
super.build(buffer, tab);
}
}
@ -110,12 +131,13 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modSpace = spacement;
buffer.append((s) -> s+=modSpace+"while"+super.condition);
super.build(buffer, tab);
}
}
}

View file

@ -1,8 +1,10 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.io.BufferedWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -36,12 +38,16 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="do{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}

View file

@ -1,8 +1,10 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.io.BufferedWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -41,13 +43,24 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="else ");
boolean oneOperation = getChilds().size() == 1;
if(oneOperation){
getChilds().get(0).build(buffer, 0);
return;
}
buffer.append((s) -> s+="{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}

View file

@ -1,10 +1,12 @@
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;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -24,12 +26,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 +47,12 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="continue;");
buffer.add("");
}
}
public static class BreakOperation extends LoopAffectOperation{
@ -67,10 +64,10 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="break;");
buffer.add("");
}
}
}

View file

@ -1,10 +1,12 @@
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;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -31,14 +33,14 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=value+";");
buffer.add("");
}
@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){
return search.apply(this) ? (E)this : null;

View file

@ -1,10 +1,12 @@
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;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -35,19 +37,20 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
if(value != null){
System.out.println("return");
value.show(tab+1);
System.out.println(";");
buffer.append((s) -> s+="return ");
value.build(buffer, 0);
buffer.add("");
return;
}
System.out.println(start+"return "+toChange+";");
buffer.append((s) -> s+="return "+toChange+";");
buffer.add("");
}
@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){

View file

@ -1,8 +1,10 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.io.BufferedWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
@ -27,7 +29,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 +38,18 @@ 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(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="synchronized"+this.include+"{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}

View file

@ -32,8 +32,8 @@ class OperationTest{
}
};
JavaParser parser = new JavaParser(reader);
parser.parse();
JavaParser parser = new JavaParser();
parser.parse(reader);
return parser;
}
@ -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());

View file

@ -31,8 +31,8 @@ class SearchTest{
}
};
JavaParser parser = new JavaParser(reader);
parser.parse();
JavaParser parser = new JavaParser();
parser.parse(reader);
return parser;
}