From 2c3400e7768f77ae39ce83d53688b4f5d7cf5c22 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Tue, 24 Oct 2023 10:29:35 +0200 Subject: [PATCH] [Builder] base rebuild --- .../peeratcode/parser/ElementBuilder.java | 33 ++++++++++++++++ .../peeratcode/parser/java/Annotation.java | 16 ++++++++ .../peeratcode/parser/java/Class.java | 28 +++++++++++++ .../peeratcode/parser/java/Function.java | 36 +++++++++++++++++ .../peeratcode/parser/java/JavaElement.java | 4 +- .../peeratcode/parser/java/JavaFile.java | 39 ++++++++++++++----- .../peeratcode/parser/java/Value.java | 25 ++++++++++++ .../peeratcode/parser/java/Variable.java | 21 ++++++++++ .../java/operation/AssignOperation.java | 7 ++++ .../parser/java/operation/BreakOperation.java | 5 +++ .../java/operation/ContinueOperation.java | 5 +++ .../java/operation/MethodCallOperation.java | 8 +++- .../parser/java/operation/OperationBag.java | 5 +++ .../java/operation/ReturnOperation.java | 6 +++ .../java/operation/SwitchOperation.java | 4 ++ .../parser/java/operation/ThrowOperation.java | 6 +++ 16 files changed, 236 insertions(+), 12 deletions(-) create mode 100644 src/be/jeffcheasey88/peeratcode/parser/ElementBuilder.java diff --git a/src/be/jeffcheasey88/peeratcode/parser/ElementBuilder.java b/src/be/jeffcheasey88/peeratcode/parser/ElementBuilder.java new file mode 100644 index 0000000..60b96d1 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/ElementBuilder.java @@ -0,0 +1,33 @@ +package be.jeffcheasey88.peeratcode.parser; + +import java.io.BufferedReader; +import java.util.LinkedList; +import java.util.List; + +public interface ElementBuilder{ + + void build(Builder builder) throws Exception; + + public static class Builder{ + + private List tokens; + + public Builder(){ + this.tokens = new LinkedList<>(); + this.tokens.add(new Token(1,1,"", TokenType.GROUP)); + } + + public void append(Token token){ + this.tokens.add(token); + } + + public void append(String value){ + Token last = tokens.get(tokens.size()-1); + append(new Token(last.getLineNumber(), last.getCharacterNumber()+last.getValue().length(), value, TokenType.GROUP)); + } + + public void build(BufferedReader writer) throws Exception{ + + } + } +} \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java b/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java index e3908ec..8d69881 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.function.Function; import be.jeffcheasey88.peeratcode.parser.Token; @@ -35,6 +36,21 @@ public class Annotation extends JavaElement{ public Map getParameters(){ return this.values; } + + @Override + public void build(Builder builder) throws Exception{ + builder.append(name); + if(values != null && values.size() > 0){ + builder.append("("); + for(Entry entry : values.entrySet()){ + builder.append(entry.getKey()); + builder.append("="); + entry.getValue().build(builder); + builder.append(","); //replace by iterator splitter method + } + builder.append(")"); + } + } @Override public E find(Function finder){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 486a4b0..503c5f9 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -20,6 +20,7 @@ public class Class extends JavaElement implements AnnotableBuffer, ClassContaine private List annotations; + //where mod ???? private Token name; private Token extend; private Token implement; @@ -84,6 +85,33 @@ public class Class extends JavaElement implements AnnotableBuffer, ClassContaine this.annotationBuffer = null; return list; } + + @Override + public void build(Builder builder) throws Exception{ + if(annotations != null){ + for(Annotation annotation : this.annotations){ + annotation.build(builder); + } + } + + builder.append("class"); + builder.append(name); + if(extend != null){ + builder.append(" extends "); + builder.append(extend); + } + if(implement != null){ + builder.append(" implements "); + builder.append(implement); + } + builder.append("{"); + if(elements != null){ + for(JavaElement element : this.elements){ + element.build(builder); + } + } + builder.append("}"); + } @Override public E find(java.util.function.Function finder){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index 630b58c..35f133d 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -1,9 +1,11 @@ package be.jeffcheasey88.peeratcode.parser.java; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import be.jeffcheasey88.peeratcode.parser.Token; +import be.jeffcheasey88.peeratcode.parser.TokenType; import be.jeffcheasey88.peeratcode.parser.java.Operation.OperationContainer; import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer; @@ -90,6 +92,40 @@ public class Function extends JavaElement implements VariableContainer, Operatio public List getElements(){ return this.elements; } + + @Override + public void build(Builder builder) throws Exception{ + if(annotations != null){ + for(Annotation annotation : this.annotations){ + annotation.build(builder); + } + } + String mod = Modifier.toString(this.mod); + builder.append(new Token(type.getLineNumber(), type.getCharacterNumber()-(mod.length()+1), mod, TokenType.GROUP)); + if(generic != null) builder.append(generic); + builder.append(type); + builder.append(name); + builder.append("("); + if(parameters != null){ + for(Variable param : this.parameters){ + param.build(builder); + } + } + builder.append(")"); + if(throwables != null){ + builder.append("throws"); + for(Token token : throwables){ + builder.append(token); + } + } + builder.append("{"); + if(elements != null){ + for(JavaElement content : this.elements){ + content.build(builder); + } + } + builder.append("}"); + } @Override public E find(java.util.function.Function finder){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java index c76e151..92b4a08 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java @@ -1,9 +1,11 @@ package be.jeffcheasey88.peeratcode.parser.java; import be.jeffcheasey88.peeratcode.parser.Bag; +import be.jeffcheasey88.peeratcode.parser.ElementBuilder; + import java.util.function.Function; -public abstract class JavaElement{ +public abstract class JavaElement implements ElementBuilder{ Bag bag; //to remove diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java index 98d5003..0722553 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java @@ -53,16 +53,7 @@ public class JavaFile extends JavaElement implements ClassContainer, AnnotableBu public List getSubClasses(){ return this.subClazz; } - - @Override - public E find(Function finder){ - if(finder.apply(mainClazz)) return (E) mainClazz; - for(Class clazz : subClazz){ - if(finder.apply(clazz)) return (E) clazz; - } - return null; - } - + @Override public void addAnnotationBuffer(Annotation annotation){ if(annotationBuffer == null) annotationBuffer = new ArrayList<>(); @@ -74,4 +65,32 @@ public class JavaFile extends JavaElement implements ClassContainer, AnnotableBu this.annotationBuffer = null; return list; } + + @Override + public void build(Builder builder) throws Exception { + builder.append("package "); + builder.append(pack); + builder.append(";"); + + for(Import imp : imports){ + builder.append("import "); + if(imp.isStatic()) builder.append("static "); + builder.append(imp.getValue()); + } + + mainClazz.build(builder); + for(Class clazz : subClazz){ + clazz.build(builder); + } + } + + @Override + public E find(Function finder){ + if(finder.apply(mainClazz)) return (E) mainClazz; + for(Class clazz : subClazz){ + if(finder.apply(clazz)) return (E) clazz; + } + return null; + } + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Value.java b/src/be/jeffcheasey88/peeratcode/parser/java/Value.java index 5c10a91..3bfe457 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Value.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Value.java @@ -32,6 +32,15 @@ public class Value extends JavaElement{ return "Value[token="+token+", content="+content+"]"; } + @Override + public void build(Builder builder) throws Exception{ + if(token != null){ + builder.append(token); + }else{ + content.build(builder); + } + } + @Override public E find(Function finder){ return content != null ? finder.apply(content) ? (E) content : null : null; @@ -66,6 +75,13 @@ public class Value extends JavaElement{ return left+" "+action+" "+right; } + @Override + public void build(Builder builder) throws Exception{ + left.build(builder); + builder.append(action); + right.build(builder); + } + @Override public E find(Function finder){ return finder.apply(left) ? (E) left : finder.apply(right) ? (E) right : null; @@ -92,6 +108,15 @@ public class Value extends JavaElement{ return fail; } + @Override + public void build(Builder builder) throws Exception { + check.build(builder); + builder.append("?"); + success.build(builder); + builder.append(":"); + fail.build(builder); + } + @Override public E find(Function finder){ return finder.apply(check) ? (E) check : finder.apply(success) ? (E) success : finder.apply(fail) ? (E) fail : null; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index 33ed184..8d216fb 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -1,10 +1,12 @@ package be.jeffcheasey88.peeratcode.parser.java; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import java.util.function.Function; import be.jeffcheasey88.peeratcode.parser.Token; +import be.jeffcheasey88.peeratcode.parser.TokenType; public class Variable extends JavaElement{ @@ -68,6 +70,24 @@ public class Variable extends JavaElement{ return this.value; } + @Override + public void build(Builder builder) throws Exception{ + if(annotations != null){ + for(Annotation annotation : this.annotations){ + annotation.build(builder); + } + } + String mod = Modifier.toString(this.mod); + builder.append(new Token(type.getLineNumber(), type.getCharacterNumber()-(mod.length()+1), mod, TokenType.GROUP)); + builder.append(type); + if(elips) builder.append("..."); + builder.append(name); + if(value != null){ + builder.append("="); + value.build(builder); + } + } + @Override public E find(Function finder){ if(annotations != null){ @@ -77,4 +97,5 @@ public class Variable extends JavaElement{ } return value != null ? finder.apply(value) ? (E)value : null : null; } + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/AssignOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/AssignOperation.java index f1cba58..e660998 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/AssignOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/AssignOperation.java @@ -23,6 +23,13 @@ public class AssignOperation extends Operation{ public Value right(){ return right; } + + @Override + public void build(Builder builder) throws Exception{ + left.build(builder); + builder.append("="); + right.build(builder); + } @Override public E find(Function finder){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/BreakOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/BreakOperation.java index 925912b..29453c5 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/BreakOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/BreakOperation.java @@ -8,6 +8,11 @@ import be.jeffcheasey88.peeratcode.parser.java.Operation; public class BreakOperation extends Operation{ public BreakOperation(){} + + @Override + public void build(Builder builder) throws Exception{ + builder.append("break;"); + } @Override public E find(Function finder){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/ContinueOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/ContinueOperation.java index 8768590..37d1676 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/ContinueOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/ContinueOperation.java @@ -6,6 +6,11 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement; import be.jeffcheasey88.peeratcode.parser.java.Operation; public class ContinueOperation extends Operation{ + + @Override + public void build(Builder builder) throws Exception{ + builder.append("continue;"); + } @Override public E find(Function finder){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/MethodCallOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/MethodCallOperation.java index 158c98c..26e4060 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/MethodCallOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/MethodCallOperation.java @@ -37,11 +37,17 @@ public class MethodCallOperation extends Operation{ public List getParameters(){ return this.parameters; } + + @Override + public void build(Builder builder) throws Exception{ + if(previous != null) previous.build(builder); + //todo + } @Override public E find(Function finder) { if(finder.apply(start)) return (E)start; - if(finder.apply(previous)) return (E) previous; + if(previous != null) if(finder.apply(previous)) return (E) previous; for(Value param : this.parameters){ if(finder.apply(param)) return (E)param; } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/OperationBag.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/OperationBag.java index ddd197e..25817e3 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/OperationBag.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/OperationBag.java @@ -38,4 +38,9 @@ public class OperationBag extends Operation implements VariableContainer, Operat return null; } + @Override + public void build(Builder builder) throws Exception { + //todo and super on child + } + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/ReturnOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/ReturnOperation.java index 7dff44f..6197a13 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/ReturnOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/ReturnOperation.java @@ -18,9 +18,15 @@ public class ReturnOperation extends Operation{ return this.value; } + @Override + public void build(Builder builder) throws Exception { + value.build(builder); + } + @Override public E find(Function finder) { return value != null ? finder.apply(value) ? (E)value : null : null; } + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/SwitchOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/SwitchOperation.java index 7a7422e..e1ed984 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/SwitchOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/SwitchOperation.java @@ -6,6 +6,10 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement; import be.jeffcheasey88.peeratcode.parser.java.Operation; public class SwitchOperation extends Operation{ + + @Override + public void build(Builder builder) throws Exception{ + } @Override public E find(Function finder) { diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operation/ThrowOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operation/ThrowOperation.java index 933ae1c..1fe5244 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operation/ThrowOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operation/ThrowOperation.java @@ -17,6 +17,12 @@ public class ThrowOperation extends Operation{ public Value getValue(){ return this.value; } + + @Override + public void build(Builder builder) throws Exception { + builder.append(" throw "); + value.build(builder); + } @Override public E find(Function finder) {