From 4db724e2afa9c6fd301219c9b909f99f8ad46d3d Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Sat, 15 Apr 2023 21:25:55 +0200 Subject: [PATCH] Refractor Parser (begin) & Route Extracter --- src/be/jeffcheasey88/peeratcode/Main.java | 3 + .../bonus/extract/RouteExtracter.java | 33 ++++ .../peeratcode/parser/java/Class.java | 4 +- .../peeratcode/parser/java/Variable.java | 148 ++++++------------ .../peeratcode/parser/java/VariableTest.java | 4 +- 5 files changed, 91 insertions(+), 101 deletions(-) create mode 100644 src/be/jeffcheasey88/peeratcode/bonus/extract/RouteExtracter.java diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index ecaf94e..983e892 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -11,6 +11,7 @@ import javax.net.ssl.SSLServerSocket; import javax.net.ssl.SSLServerSocketFactory; import be.jeffcheasey88.peeratcode.bonus.discord.Bot; +import be.jeffcheasey88.peeratcode.bonus.extract.RouteExtracter; import be.jeffcheasey88.peeratcode.framework.Client; import be.jeffcheasey88.peeratcode.framework.HttpReader; import be.jeffcheasey88.peeratcode.framework.HttpUtil; @@ -68,6 +69,8 @@ public class Main{ }); initRoutes(router, config); +// RouteExtracter extracter = new RouteExtracter(router); +// extracter.extract(); startWebServer(config, router); } diff --git a/src/be/jeffcheasey88/peeratcode/bonus/extract/RouteExtracter.java b/src/be/jeffcheasey88/peeratcode/bonus/extract/RouteExtracter.java new file mode 100644 index 0000000..0809bdd --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/bonus/extract/RouteExtracter.java @@ -0,0 +1,33 @@ +package be.jeffcheasey88.peeratcode.bonus.extract; + +import java.lang.reflect.Field; +import java.util.Map; +import java.util.Map.Entry; + +import be.jeffcheasey88.peeratcode.framework.RequestType; +import be.jeffcheasey88.peeratcode.framework.Response; +import be.jeffcheasey88.peeratcode.framework.Route; +import be.jeffcheasey88.peeratcode.framework.Router; + +//A noter que le but est d'extraire des informations sans modifier le code source, +//comme les tests unitaire, ici je veux générer un élément textuel qui me donnera l'état des routes, +//je ne vais donc pas modifier la classe router pour donner un accès au route. +public class RouteExtracter { + + private Router router; + + public RouteExtracter(Router router){ + this.router = router; + } + + public void extract() throws Exception{ + Field field = Router.class.getDeclaredField("responses"); + field.setAccessible(true); + Map> responses = (Map>) field.get(this.router); + for(Entry> types : responses.entrySet()){ + for(Entry routes : types.getValue().entrySet()){ + System.out.println("["+types.getKey()+"] "+routes.getValue().path()); + } + } + } +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 9611916..65ef1a3 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -13,8 +13,8 @@ public class Class { private int modifier; private String name; - private Listvars; - private Listfunctions; + private List vars; + private List functions; public Class(){} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index 5ed1aa0..791cf9b 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -1,6 +1,7 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.lang.reflect.Modifier; +import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -20,118 +21,71 @@ public class Variable { this.type = type; } - //int i = 4; - //int i,j,k,l=1; - //int lm ; - //public static int l; - //Testt; - //Test j = new Test().schedule(p -> { return true;}); - //int i =j=k=l=4; - public int parse(String content) throws Exception{ Matcher matcher = PATTERN.matcher(content); matcher.matches(); int offset = matcher.group(1).length(); - boolean hasEquals = false; - boolean fromMinus = false; String body = matcher.group(2); - while(body.length() > 0){ - while(indexOf(body, "\\s+") == 0){ - body = body.substring(1); - offset++; - } - int space = indexOf(body, "\\s+"); - int equals = indexOf(body, "="); - int quote = indexOf(body,","); - int quotes = indexOf(body, ";"); - int minus = indexOf(body, "<"); - - - int min = min(space, equals, quote, quotes, minus); - String value = body.substring(0,min); - if(hasEquals){ - this.value = new Value(value); - body = body.substring(value.length()+1); - offset+=value.length()+1; - break; - }else if(fromMinus){ - this.name = value; - body = body.substring(value.length()+1); - offset+=value.length()+1; - break; - } else if(min == space){ - int mod = JavaParser.getModifier(value); - if(mod > 0){ - this.modifier+=mod; - }else{ - if(type == null){ - this.type = value; - }else if(name == null){ - this.name = value; - } - } - body = body.substring(value.length()+1); - offset+=value.length()+1; - }else if(min == equals){ - if(this.name == null) this.name = value; - hasEquals = true; - body = body.substring(value.length()+1); - offset+=value.length()+1; - }else if(min == minus){ - value = value+"<"; - int maxus = 1; - while(maxus > 0){ - char current = body.charAt(value.length()); - value+=current; - if(current == '<'){ - maxus++; - } - if(current == '>'){ - maxus--; - } - } - this.type = value; - body = body.substring(value.length()); - offset+=value.length(); - while(indexOf(body, "\\s+") == 0){ - body = body.substring(1); - offset++; - } - fromMinus = true; - }else if(min == quote || min == quotes){ - if(this.name != null) break; - this.name = value; - body = body.substring(value.length()); - offset+=value.length(); - - if(min == quotes){ - body = body.substring(1); - offset+=1; - } - break; - }else { - offset+=value.length()+1; - break; - } + int equals = indexOf(body, "="); + int quote = indexOf(body,","); + int quotes = indexOf(body, ";"); + int min = Math.min(quote, quotes); + body = body.substring(0, min); + + if(equals < quote && equals < quotes){ + assigment(body); + }else{ + onlyDefine(body); } - return offset; + return offset+min; + } + + private void assigment(String content){ + } + + private void onlyDefine(String content){ + content = generiqueTypes(content); + System.out.println(content); + String[] values = content.split("\\s+"); + for(String value : values){ + int modifier = JavaParser.getModifier(value); + if(modifier > 0){ + this.modifier+=modifier; + continue; + } + if(this.type == null){ + this.type = value; + continue; + } + this.name = value; + } + } + + private String generiqueTypes(String content){ + System.out.println(content); + String result = ""; + int opened = 0; + for(char c : content.toCharArray()){ + if(c == '<') opened++; + else if(c == '>') opened--; + + if(opened > 0){ + if(Character.isWhitespace(c)) continue; + } + result+=c; + } + System.out.println(result); + result = result.replaceAll("(>\\s*)", "> ").replace("> >", ">>"); + return result; } private int indexOf(String value, String target){ return value.split(target)[0].length(); } - private int min(int... mins){ - int result = mins[0]; - for(int min : mins){ - if(min < result) result = min; - } - return result; - } - public int getModifier(){ return this.modifier; } diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java index aa450e8..5e0bcbd 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java @@ -69,10 +69,10 @@ class VariableTest { void case4(){ try { Variable variable = new Variable(); - variable.parse("Testast; "); + variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; "); assertEquals(0, variable.getModifier()); - assertEquals("Testas", variable.getType()); + assertEquals("Testas,Test>>>", variable.getType()); assertEquals("t", variable.getName()); assertNull(variable.getValue()); }catch(Exception e){