From 5e3208c262e5fb7099d17563abb963c8b7f824eb Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Wed, 10 May 2023 08:15:18 +0200 Subject: [PATCH] Change unique char for CleanerPool --- .../peeratcode/parser/java/Class.java | 2 +- .../peeratcode/parser/java/CleanerPool.java | 4 +-- .../peeratcode/parser/java/JavaParser.java | 34 ++++++++++++++++--- .../peeratcode/parser/java/Variable.java | 4 +-- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 7f7c34b..851f9b1 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -61,7 +61,7 @@ public class Class{ } } - return matcher.group(1).length(); + return cleaner.unzip(matcher.group(1), ((s,p) -> s)).length(); } private int indexOf(String value, String target){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java index 84ef1b8..894e8c4 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java @@ -49,10 +49,10 @@ 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.rPattern = Pattern.compile("^(.*)(\\^"+pattern+"\\d+)(.*)$"); this.constants = new ArrayList<>(); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 975733f..163e3f4 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -1,11 +1,13 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.lang.reflect.Modifier; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collection; import java.util.List; import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; @@ -22,11 +24,33 @@ public class JavaParser{ parser.show(); } - public static void main(String[] args) throws Exception { - File dir = new File("C:\\Users\\jeffc\\eclipse-workspace\\"); + public static void mainee(String[] args) throws Exception { + File dir = new File(""); show(dir); } + public static void main(String[] args) throws Exception{ + String variable = "Collection collection = new ArrayList() {\r\n" + + " { add(\"1\");\r\n" + + " add(\"2\");\r\n" + + " add(\"3\");\r\n" + + " }\r\n" + + " };"; + + String clazz = "package test; public class Test{ "+variable+" }"; + BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/tmp.txt"))); + writer.write(clazz); + writer.flush(); + writer.close(); + + BufferedReader reader = new BufferedReader(new FileReader(new File("/home/tmp.txt"))); + + JavaParser parser = new JavaParser(reader); + parser.parse(); + System.out.println("SHOW-----------------"); + parser.show(); + } + private static void show(File dir) throws Exception{ if(dir.isFile()){ if(!dir.getName().endsWith(".java")) return; @@ -80,7 +104,9 @@ public class JavaParser{ } this.clazz = new Class(); - index = this.clazz.parse(content, new CleanerPool(new Cleaner("CONSTANT_STRING",'"','"'))); + index = this.clazz.parse(content, new CleanerPool( + new Cleaner("CONSTANT_STRING",'"','"'), + new Cleaner("CONSTANT_CHAR",'\'','\''))); 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 6110daf..4cadca6 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -93,7 +93,7 @@ public class Variable { private String generiqueTypes(String content, CleanerPool cleaner){ System.out.println("generic "+content); String unzip = cleaner.unzip(content, (value, pattern) -> { - return (pattern.equals("$GENERIC_FUNCTION")) ? null : value.replaceAll("\\s+", ""); + return (pattern.equals("^GENERIC_FUNCTION")) ? null : value.replaceAll("\\s+", ""); }); System.out.println("unzip "+unzip); unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}"); @@ -128,7 +128,7 @@ public class Variable { public void show(int tab){ String start = ""; for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":"="+value+";")); + System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":" = "+value+";")); } public static class Value extends Variable{