Compare commits

..

No commits in common. "97aa0ac39196b58e68b082cb4036e8297f17023d" and "6909ca513c5ee2aaf202e2094dddec7e7718d268" have entirely different histories.

18 changed files with 145 additions and 252 deletions

Binary file not shown.

View file

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

View file

@ -1,6 +1,5 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.io.BufferedWriter;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
@ -99,20 +98,15 @@ public class Class extends JavaElement{
// return this.vars;
// }
@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("");
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();
}
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
System.out.println(start+"}");
}
@Override

View file

@ -93,23 +93,15 @@ public class Function extends OperationContainer{
}while(quote);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
String param = "";
for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
if(!param.isEmpty()) param = param.substring(1);
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("");
System.out.println(start+Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+param+") "+exceptions+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}

View file

@ -11,10 +11,6 @@ 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);
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);
}
//Only for development
public abstract void show(int tab);
}

View file

@ -7,7 +7,6 @@ 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;
@ -20,10 +19,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();
parser.parse(reader);
System.out.println("build-----------------");
parser.build(new BufferedWriter(new FileWriter(new File("/home/buildClazzFromParser.txt"))));
JavaParser parser = new JavaParser(reader);
parser.parse();
System.out.println("SHOW-----------------");
parser.show();
}
public static void mainee(String[] args) throws Exception {
@ -42,9 +41,10 @@ public class JavaParser{
BufferedReader reader = new BufferedReader(new FileReader(new File("/home/tmp.txt")));
JavaParser parser = new JavaParser();
parser.parse(reader);
JavaParser parser = new JavaParser(reader);
parser.parse();
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();
parser.parse(reader);
JavaParser parser = new JavaParser(reader);
parser.parse();
Class clazz = parser.getClazz();
System.out.println(clazz.getName());
@ -72,15 +72,17 @@ public class JavaParser{
private List<Import> imports;
private Class clazz;
private CleanerPool cleaner;
private BufferedReader reader;
public JavaParser(){}
public JavaParser(BufferedReader reader){
this.reader = reader;
}
public void parse(BufferedReader reader) throws Exception{
public void parse() throws Exception{
String content = "";
int index;
cleaner = new CleanerPool(
CleanerPool cleaner = new CleanerPool(
new Cleaner("CONSTANT_STRING",'"','"'),
new Cleaner("CONSTANT_CHAR",'\'','\''));
@ -129,28 +131,14 @@ public class JavaParser{
return this.clazz;
}
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");
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);
}
writer.flush();
writer.close();
}
public static int getModifier(String modifier){
switch(modifier){
case "public": return Modifier.PUBLIC;

View file

@ -1,6 +1,5 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.io.BufferedWriter;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
@ -127,12 +126,10 @@ public class Variable extends JavaElement{
return this.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("");
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
@ -191,21 +188,16 @@ public class Variable extends JavaElement{
}
@Override
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;
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
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);
String varMod = vars;
buffer.append((s) -> s+=sMod+Modifier.toString(getModifier())+" "+getType()+" "+varMod+";");
buffer.add("");
}
System.out.println(start+Modifier.toString(getModifier())+" "+getType()+" "+vars+";");
}
}
}

View file

@ -5,7 +5,6 @@ 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;
@ -30,10 +29,10 @@ public class AssigmentOperation extends JavaElement{
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=variable+" = "+value+";");
buffer.add("");
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+variable+" = "+value+";");
}
@Override

View file

@ -1,10 +1,8 @@
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;
@ -59,28 +57,12 @@ public class ConditionalOperation extends OperationContainer{
}
@Override
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 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 static class IfOperation extends ConditionalOperation{
@ -91,15 +73,13 @@ public class ConditionalOperation extends OperationContainer{
super(PATTERN);
}
@Override
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);
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+"}");
}
}
@ -112,13 +92,12 @@ public class ConditionalOperation extends OperationContainer{
}
@Override
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);
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+"}");
}
}
@ -131,13 +110,12 @@ public class ConditionalOperation extends OperationContainer{
}
@Override
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);
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+"}");
}
}
}

View file

@ -1,10 +1,8 @@
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;
@ -38,16 +36,12 @@ public class DoOperation extends OperationContainer{
}
@Override
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("");
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+"}");
}
}

View file

@ -1,10 +1,8 @@
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;
@ -43,23 +41,12 @@ public class ElseOperation extends OperationContainer{
}
@Override
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("");
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+"}");
}
}

View file

@ -1,12 +1,10 @@
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;
@ -26,6 +24,12 @@ 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){
@ -47,12 +51,11 @@ public class LoopAffectOperation extends JavaElement{
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="continue;");
buffer.add("");
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"continue;");
}
}
public static class BreakOperation extends LoopAffectOperation{
@ -64,10 +67,10 @@ public class LoopAffectOperation extends JavaElement{
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="break;");
buffer.add("");
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"break;");
}
}
}

View file

@ -1,12 +1,10 @@
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,10 +33,10 @@ public class MethodCallOperation extends JavaElement{
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=value+";");
buffer.add("");
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+value+";");
}
@Override

View file

@ -1,12 +1,10 @@
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;
@ -39,19 +37,18 @@ public class ReturnOperation extends JavaElement{
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
if(value != null){
buffer.append((s) -> s+="return ");
value.build(buffer, 0);
buffer.add("");
System.out.println("return");
value.show(tab+1);
System.out.println(";");
return;
}
buffer.append((s) -> s+="return "+toChange+";");
buffer.add("");
System.out.println(start+"return "+toChange+";");
}
@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,10 +1,8 @@
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;
@ -40,16 +38,12 @@ public class SynchronizedOperation extends OperationContainer{
}
@Override
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("");
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+"}");
}
}

View file

@ -32,8 +32,8 @@ class OperationTest{
}
};
JavaParser parser = new JavaParser();
parser.parse(reader);
JavaParser parser = new JavaParser(reader);
parser.parse();
return parser;
}
@ -43,6 +43,8 @@ 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());
@ -58,6 +60,8 @@ 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());
@ -73,6 +77,8 @@ 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());
@ -95,6 +101,8 @@ 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());
@ -115,6 +123,8 @@ 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());
@ -135,6 +145,8 @@ 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();
parser.parse(reader);
JavaParser parser = new JavaParser(reader);
parser.parse();
return parser;
}