diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java index 7345ba4..e7f59b4 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java @@ -1,14 +1,18 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class CleanerPool{ private List cleaners; - public CleanerPool(List cleaners){ - this.cleaners = new ArrayList<>(cleaners); + public CleanerPool(Cleaner... cleaners){ + this.cleaners = Arrays.asList(cleaners); } public String clean(String value){ @@ -16,22 +20,29 @@ public class CleanerPool{ return value; } - public boolean isConstant(String value){ - for(Cleaner cleaner : this.cleaners) if(value.startsWith(cleaner.getPattern())) return true; - return false; + public String unzip(String value){ + return unzip(value, (s) -> s); } - - public String getConstant(String value){ + + public String unzip(String value, Function modifier){ + boolean edited = false; for(Cleaner cleaner : this.cleaners){ - if(value.startsWith(cleaner.getPattern())){ - return cleaner.getConstant(value); + Matcher matcher = cleaner.getMatcher(value); + if(matcher.matches()){ + String key = matcher.group(2); + String zip = cleaner.getConstant(key); + value = matcher.group(1)+cleaner.open+modifier.apply(zip)+cleaner.close+matcher.group(3); + edited = true; } } - return null; + if(edited) return unzip(value, modifier); + return value; } public static class Cleaner{ + private Pattern rPattern; + private String pattern; private char open; private char close; @@ -39,12 +50,17 @@ public class CleanerPool{ public List constants; public Cleaner(String pattern, char open, char close){ - this.pattern = pattern; + this.pattern = "$"+pattern; this.open = open; this.close = close; + this.rPattern = Pattern.compile("^(.*)(\\$"+pattern+"\\d+)(.*)$"); this.constants = new ArrayList<>(); } + public Matcher getMatcher(String value){ + return this.rPattern.matcher(value); + } + public String getPattern(){ return this.pattern; } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 2e4025e..f70901e 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -54,7 +54,7 @@ public class JavaParser { } this.clazz = new Class(); - index = this.clazz.parse(content, new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>')))); + index = this.clazz.parse(content, new CleanerPool()); content = content.substring(index); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index da77208..31b3403 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -1,10 +1,11 @@ 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; +import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; + public class Variable { private static Pattern PATTERN = Pattern.compile("^(\\s*)(.*)$"); @@ -22,6 +23,11 @@ public class Variable { } public int parse(String content, CleanerPool cleaner) throws Exception{ + System.out.println("parse "+content); + CleanerPool generic = new CleanerPool(new Cleaner("GENERIC_TYPE_",'<','>')); + content = generic.clean(content); + System.out.println("clean "+content); + Matcher matcher = PATTERN.matcher(content); matcher.matches(); @@ -35,19 +41,20 @@ public class Variable { body = body.substring(0, min); if(equals < quote && equals < quotes){ - assigment(body); + assigment(body, generic); }else{ - onlyDefine(body); + onlyDefine(body, generic); } return offset+min; } - private void assigment(String content){ + private void assigment(String content, CleanerPool cleaner){ } - private void onlyDefine(String content){ - content = generiqueTypes(content); + private void onlyDefine(String content, CleanerPool cleaner){ + System.out.println("onlyDefine "+content); + content = generiqueTypes(content, cleaner); System.out.println(content); String[] values = content.split("\\s+"); for(String value : values){ @@ -64,8 +71,8 @@ public class Variable { } } - private String generiqueTypes(String content){ - return content; + private String generiqueTypes(String content, CleanerPool cleaner){ + return cleaner.unzip(content, (s) -> s.replaceAll("\\s+", "")).replaceAll(">(?[^>\\d,;])", "> ${varname}"); } private int indexOf(String value, String target){ diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/CleanerTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/CleanerTest.java index 81a771b..5680054 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/CleanerTest.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/CleanerTest.java @@ -12,14 +12,8 @@ public class CleanerTest { @Test void cutter(){ - CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>'))); + CleanerPool cleaner = new CleanerPool(); String result = cleaner.clean("Test0< List< Map< Test1, List< Test2 >, Test3>> > "); assertEquals("Test0$TEST3 ", result); } - @Test - void cutterLittle(){ - CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>'))); - String result = cleaner.clean("Test0> "); - assertEquals("Test0$TEST1 ", result); - } } diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java index 3c5e389..bc5f28c 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java @@ -30,7 +30,7 @@ class VariableTest{ @BeforeAll void init(){ - this.cleaner = new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>'))); + this.cleaner = new CleanerPool(); } @Test @@ -137,7 +137,7 @@ class VariableTest{ void case7(){ try { Class clazz = new Class(); - clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE",'<','>')))); + clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool()); List vars = clazz.getVariables(); assertEquals(vars.size(), 4);