Add Assigment Cases & Add unzip options

This commit is contained in:
jeffcheasey88 2023-05-08 12:45:14 +02:00
parent 8941eac25c
commit 5754720fe5
4 changed files with 87 additions and 11 deletions

View file

@ -60,6 +60,7 @@ public class Class{
int index = variable.parse(content, cleaner); int index = variable.parse(content, cleaner);
this.vars.add(variable); this.vars.add(variable);
content = content.substring(index); content = content.substring(index);
System.out.println("CONTENT IS NOW "+content);
quote = content.startsWith(","); quote = content.startsWith(",");
if(quote) content = content.substring(1); if(quote) content = content.substring(1);
}while(quote); }while(quote);

View file

@ -3,7 +3,7 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.BiFunction;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -20,18 +20,17 @@ public class CleanerPool{
return value; return value;
} }
public String unzip(String value){
return unzip(value, (s) -> s);
}
public String unzip(String value, Function<String, String> modifier){ public String unzip(String value, BiFunction<String,String, String> modifier){
boolean edited = false; boolean edited = false;
for(Cleaner cleaner : this.cleaners){ for(Cleaner cleaner : this.cleaners){
Matcher matcher = cleaner.getMatcher(value); Matcher matcher = cleaner.getMatcher(value);
if(matcher.matches()){ if(matcher.matches()){
String key = matcher.group(2); String key = matcher.group(2);
String zip = cleaner.getConstant(key); String zip = cleaner.getConstant(key);
value = matcher.group(1)+cleaner.open+modifier.apply(zip)+cleaner.close+matcher.group(3); String modified = modifier.apply(zip, cleaner.getPattern());
if(modified == null) modified = zip;
value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
edited = true; edited = true;
} }
} }

View file

@ -28,7 +28,8 @@ public class Variable {
CleanerPool generic = new CleanerPool( CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_TYPE_",'<','>'), new Cleaner("GENERIC_TYPE_",'<','>'),
new Cleaner("GENERIC_ARRAY",'[',']'), new Cleaner("GENERIC_ARRAY",'[',']'),
new Cleaner("GENERIC_FUNCTION", '{','}')); new Cleaner("GENERIC_FUNCTION", '{','}'),
new Cleaner("GENERIC_PARENTHESIS",'(',')'));
content = generic.clean(content); content = generic.clean(content);
System.out.println("clean "+content); System.out.println("clean "+content);
@ -49,10 +50,8 @@ public class Variable {
}else{ }else{
onlyDefine(body, generic); onlyDefine(body, generic);
} }
System.out.print("INSTANCE ");
show(1); show(1);
return offset+min; return offset+generic.unzip(body, ((s,p) -> s)).length();
} }
private void assigment(String content, CleanerPool cleaner){ private void assigment(String content, CleanerPool cleaner){
@ -89,7 +88,9 @@ public class Variable {
private String generiqueTypes(String content, CleanerPool cleaner){ private String generiqueTypes(String content, CleanerPool cleaner){
return cleaner. return cleaner.
unzip(content, (s) -> s.replaceAll("\\s+", "")). unzip(content, (value, pattern) -> {
return (pattern.equals("$GENERIC_FUNCTION")) ? null : value.replaceAll("\\s+", "");
}).
replaceAll(">(?<varname>[^>\\d,;])", "> ${varname}"). replaceAll(">(?<varname>[^>\\d,;])", "> ${varname}").
replaceAll("](?<varname>[^\\[\\d,;])", "] ${varname}"). replaceAll("](?<varname>[^\\[\\d,;])", "] ${varname}").
replaceAll("(?<varname>[^=\\s])=","${varname} ="). replaceAll("(?<varname>[^=\\s])=","${varname} =").

View file

@ -202,4 +202,79 @@ class VariableTest{
} }
} }
@Test
void case11(){
try {
Variable variable = new Variable();
variable.parse(cleaner.clean("java.util.function.Function<Integer, Integer> j = ((i) -> {return 4;});"), cleaner);
assertEquals(0, variable.getModifier());
assertEquals("java.util.function.Function<Integer,Integer>", variable.getType());
assertEquals("j", variable.getName());
assertEquals("((i)-> {return 4;})", ((Value)variable.getValue()).value());
}catch(Exception e){
fail(e);
}
}
@Test
void case12(){
try {
Variable variable = new Variable();
variable.parse(cleaner.clean(" public static final Locale FRENCH = createConstant(\"fr\", \"\");"), cleaner);
assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static")+JavaParser.getModifier("final"), variable.getModifier());
assertEquals("Locale", variable.getType());
assertEquals("FRENCH", variable.getName());
assertEquals("createConstant(\"fr\",\"\")", ((Value)variable.getValue()).value());
}catch(Exception e){
fail(e);
}
}
@Test
void case13(){
try {
Variable variable = new Variable();
variable.parse(cleaner.clean("private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[] { new ObjectStreamField(\"language\", String.class), new ObjectStreamField(\"country\", String.class), new ObjectStreamField(\"variant\", String.class), new ObjectStreamField(\"hashcode\", int.class), new ObjectStreamField(\"script\", String.class), new ObjectStreamField(\"extensions\", String.class) };"), cleaner);
assertEquals(JavaParser.getModifier("private")+JavaParser.getModifier("static")+JavaParser.getModifier("final"), variable.getModifier());
assertEquals("ObjectStreamField[]", variable.getType());
assertEquals("serialPersistentFields", variable.getName());
assertEquals("new ObjectStreamField[] { new ObjectStreamField(\"language\", String.class), new ObjectStreamField(\"country\", String.class), new ObjectStreamField(\"variant\", String.class), new ObjectStreamField(\"hashcode\", int.class), new ObjectStreamField(\"script\", String.class), new ObjectStreamField(\"extensions\", String.class) }", ((Value)variable.getValue()).value());
}catch(Exception e){
fail(e);
}
}
@Test
void case14(){
try {
Class clazz = new Class();
clazz.parse(cleaner.clean("public class Test{ private java.util.function.Function<Integer, Integer> j = ((i) -> {return 4;}), k = ((i) -> 4); } "), cleaner);
List<Variable> vars = clazz.getVariables();
assertEquals(2, vars.size());
Variable variable;
variable = vars.get(0);
assertEquals(JavaParser.getModifier("private"), variable.getModifier());
assertEquals("java.util.function.Function<Integer,Integer>", variable.getType());
assertEquals("j", variable.getName());
assertEquals("((i)-> {return 4;})", ((Value)variable.getValue()).value());
variable = vars.get(1);
assertEquals(JavaParser.getModifier("private"), variable.getModifier());
assertEquals("java.util.function.Function<Integer,Integer>", variable.getType());
assertEquals("k", variable.getName());
assertEquals("((i)->4)", ((Value)variable.getValue()).value());
}catch(Exception e){
fail(e);
}
}
} }