Compare commits

...
Sign in to create a new pull request.

50 commits

Author SHA1 Message Date
6975d6dad0 Annotation -> Multiple Annotation on function 2023-06-12 21:07:00 +02:00
af3481fd4d Annotation -> Variable 2023-06-11 18:10:43 +02:00
cc6142562b Annotation -> Class 2023-06-11 17:12:52 +02:00
01107c788f Annotation -> Function 2023-06-10 22:42:16 +02:00
31fbc33fa6 Try Catch Finally 2023-06-10 22:19:25 +02:00
a77404952f Throw operator 2023-06-10 21:27:16 +02:00
b82e91fefa Optimise Regex with Mehdi H. 2023-06-09 22:44:04 +02:00
9b41348588 Build -> little change when method is empty 2023-06-06 18:07:20 +02:00
97aa0ac391 Parseur -> Build -> Adaptation to include the Cleaner Pool 2023-06-05 22:40:49 +02:00
0159f9c5fc Parseur -> build clazz (simple replace of show method) 2023-06-05 11:26:46 +02:00
6909ca513c ORM ANNOTATION PROCESSOR & ITS WWWWWWWWWWOOOOOOOOOOOOOOOOORRRRRRRRRRRRRRRRRKKKKKKKKKKKKKKKKKKKSSSSSSSSSSSSSSSSSSSSSS 2023-06-04 00:33:30 +02:00
a937f119ae Util -> Find -> Fix trace 2023-06-01 21:53:21 +02:00
da87230afc Util -> Find -> Generic type 2023-06-01 20:26:28 +02:00
0afdb0b76e Util -> Find | 2 method, one more optimized, one more easealy to use 2023-06-01 19:28:49 +02:00
8c90fc60b9 Util -> Find update deep with trace 2023-06-01 18:50:06 +02:00
a3c96aecc0 Util -> Find method 2023-06-01 18:11:44 +02:00
3a2e39f223 Operation -> Synchronized 2023-05-31 15:55:00 +02:00
6f8a1b4ceb Operation -> Break 2023-05-31 15:19:20 +02:00
eba6a051fd Operation -> Continue 2023-05-31 15:17:29 +02:00
89fd0553a2 Operation -> Do While 2023-05-31 15:08:45 +02:00
ab03e73f58 Operation -> Condition case with just ; 2023-05-31 14:59:25 +02:00
jeffcheasey88
f4ff094554 Operation -> Return statement -> get value 2023-05-31 12:00:38 +02:00
jeffcheasey88
c7b9454ede Operation -> Fix CleanerPool using 2023-05-31 11:47:38 +02:00
jeffcheasey88
ad430cb659 Operation -> Method Call 2023-05-30 21:46:40 +02:00
jeffcheasey88
be289b8d7f Fix Cleaner unzip one, fix logic 2023-05-30 21:35:55 +02:00
jeffcheasey88
800ad9a4e9 Fix Cleaner when modifier return null case 2023-05-30 19:31:57 +02:00
jeffcheasey88
b800b88fb4 Operation -> For and While Statement (simple case) 2023-05-30 18:46:49 +02:00
jeffcheasey88
0c98fe6e2e Operation -> Fix empty return case 2023-05-30 18:38:44 +02:00
jeffcheasey88
9a19c54e34 Operation -> Else Statement 2023-05-30 18:21:59 +02:00
jeffcheasey88
8f37f9882b Operation -> If Statement 2023-05-30 18:13:55 +02:00
jeffcheasey88
a231434ecd Operation -> OperationContainer (Recursive {} body system) 2023-05-30 18:06:50 +02:00
jeffcheasey88
56ce2b7235 Operation -> Assigment 2023-05-30 11:19:55 +02:00
jeffcheasey88
77f075abdc Start Operation (return & call method valid) 2023-05-29 21:41:17 +02:00
jeffcheasey88
86e326db87 Use CleanerPool in reading & Refractor Type for Global Heritage 2023-05-29 18:21:43 +02:00
jeffcheasey88
bd2cf6c26a Simple Comment Case 2023-05-26 18:36:04 +02:00
jeffcheasey88
784a51e75c Multiple Variable Definition 2023-05-20 23:26:02 +02:00
jeffcheasey88
4b1b83930e Function signature & delimitation 2023-05-15 19:11:27 +02:00
jeffcheasey88
0111df23f5 Fix new instance on parenthesis 2023-05-10 08:39:01 +02:00
jeffcheasey88
5e3208c262 Change unique char for CleanerPool 2023-05-10 08:15:18 +02:00
jeffcheasey88
57132f320a Remove unwanted space & Fix space case on array define 2023-05-09 09:02:36 +02:00
jeffcheasey88
5754720fe5 Add Assigment Cases & Add unzip options 2023-05-08 12:45:14 +02:00
jeffcheasey88
8941eac25c Fix multiple equals & Take Case when value has spaces 2023-05-08 09:39:52 +02:00
jeffcheasey88
e6ad2822eb Define Variable with simple value 2023-05-06 15:56:15 +02:00
jeffcheasey88
38f22e48b0 All Case for declaring vars 2023-05-05 15:10:25 +02:00
jeffcheasey88
b0da444be1 Merci pour le rappel Mr Hendrikx 2023-05-05 14:04:10 +02:00
jeffcheasey88
3c17c823fe Response Code Message, a enrichir 2023-05-03 09:21:15 +02:00
jeffcheasey88
5d27475a9b FINALY 2023-05-02 14:31:21 +02:00
jeffcheasey88
813ebda6cb Fix Cleaner 2023-05-02 10:34:48 +02:00
jeffcheasey88
d2fd5aeff1 Update CleanerPool, no more interface and constants everywhere 2023-04-30 22:09:18 +02:00
jeffcheasey88
4befbd7147 CleanerPool > Cutter > 2 methods 2023-04-30 21:53:09 +02:00
33 changed files with 2376 additions and 408 deletions

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry exported="true" kind="lib" path=".generated"/>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/> <classpathentry kind="src" path="test"/>
<classpathentry exported="true" kind="lib" path="json-simple-1.1.1.jar"/> <classpathentry exported="true" kind="lib" path="json-simple-1.1.1.jar"/>
@ -9,11 +10,6 @@
<classpathentry exported="true" kind="lib" path="slf4j-api-2.0.6.jar"/> <classpathentry exported="true" kind="lib" path="slf4j-api-2.0.6.jar"/>
<classpathentry exported="true" kind="lib" path="jose4j-0.9.3.jar"/> <classpathentry exported="true" kind="lib" path="jose4j-0.9.3.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="src" path=".apt_generated">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="Treasure.jar"/> <classpathentry kind="lib" path="Treasure.jar"/>
<classpathentry exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/> <classpathentry exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>

2
.gitignore vendored
View file

@ -4,4 +4,4 @@ bin/
config.txt config.txt
dist/ dist/
testApi/ testApi/
.apt_generated/* .generated/*

View file

@ -1,6 +1,6 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=true org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genSrcDir=.apt_generated org.eclipse.jdt.apt.genSrcDir=.generated
org.eclipse.jdt.apt.genTestSrcDir=.apt_generated_tests org.eclipse.jdt.apt.genTestSrcDir=.generated_tests
org.eclipse.jdt.apt.processorOptions/treasure.source=%sourcepath% org.eclipse.jdt.apt.processorOptions/treasure.source=%sourcepath%
org.eclipse.jdt.apt.reconcileEnabled=true org.eclipse.jdt.apt.reconcileEnabled=true

Binary file not shown.

View file

@ -83,6 +83,8 @@ public class HttpWriter{
NINEBITS[113] = " Request Entity Too Large"; NINEBITS[113] = " Request Entity Too Large";
NINEBITS[114] = " Request-URI Too Large"; NINEBITS[114] = " Request-URI Too Large";
NINEBITS[115] = " Unsupported Media Type"; NINEBITS[115] = " Unsupported Media Type";
NINEBITS[123] = " Locked";
NINEBITS[125] = " Too Early";
NINEBITS[200] = " Internal Server Error"; NINEBITS[200] = " Internal Server Error";
NINEBITS[201] = " Not Implemented"; NINEBITS[201] = " Not Implemented";

View file

@ -0,0 +1,53 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Annotation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*@\\s*([^\\s\\^]*)\\s*(\\^GENERIC_PARENTHESIS\\d+)?)");
private String type;
private String param;
public Annotation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception {
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
System.out.println("parseAnnotation("+content.length()+") "+content);
this.type = matcher.group(2);
this.param = matcher.group(3);
return matcher.group(1).length();
}
public String getType(){
return this.type;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="@"+type+(param == null ? "":param));
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> search, Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace) {
return null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace) {
return null;
}
}

View file

@ -0,0 +1,28 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Array;
import java.util.Iterator;
public class ArrayBuffer<E>{
private E[] elements;
public ArrayBuffer(java.lang.Class<?> type){
this.elements = (E[]) Array.newInstance(type, 0);
}
public void add(E e){
E[] copy = (E[]) Array.newInstance(e.getClass(), this.elements.length+1);
System.arraycopy(elements, 0, copy, 0, elements.length);
copy[elements.length] = e;
this.elements = copy;
}
public void append(java.util.function.Function<E, E> modifier){
this.elements[elements.length-1] = modifier.apply(this.elements[elements.length-1]);
}
public Iterator<E> iterator(){
return new ArrayIterator<E>(elements);
}
}

View file

@ -0,0 +1,29 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Array;
import java.util.Iterator;
public class ArrayIterator<E> implements Iterator<E>{
private E[] elements;
private int index;
public ArrayIterator(E[] elements){
E[] copy = (E[]) Array.newInstance(elements.getClass().getComponentType(), elements.length);
System.arraycopy(elements, 0, copy, 0, elements.length);
this.elements = copy;
}
@Override
public boolean hasNext(){
return index < elements.length;
}
@Override
public E next(){
return elements[index++];
}
}

View file

@ -2,85 +2,103 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
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;
public class Class { import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;
public class Class extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
private List<Annotation> annotations;
private int modifier; private int modifier;
private String name; private String name;
private List<Variable> vars; private List<JavaElement> childs;
private List<Function> functions;
public Class(){} public Class(){
this.annotations = new ArrayList<>();
}
public int parse(String content) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.matches();
String[] split = matcher.group(2).split("\\s+"); Iterator<String> values = new ArrayIterator<>(matcher.group(2).split("\\s+"));
for(int i = 0; i < split.length-1; i++){ String value = null;
this.modifier+=JavaParser.getModifier(split[i]); int modifier;
while(values.hasNext() && ((value = values.next()).charAt(0) == '@')){
Annotation annotation = new Annotation();
annotation.parse(value, global, local);
this.annotations.add(annotation);
}
if((modifier = JavaParser.getModifier(value)) > 0){
do{
this.modifier+=modifier;
}while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
} }
this.name = split[split.length-1];
this.vars = new ArrayList<>(); String type = value; //class interface enum
this.functions = new ArrayList<>(); this.name = values.next();
if(values.hasNext()){
//extends implements
}
this.childs = new ArrayList<>();
CleanerPool tmp = new CleanerPool(new Cleaner("GENERIC_PARENTHESIS", '(', ')'));
content = matcher.group(3); content = matcher.group(3);
Pattern empty = Pattern.compile("^\\s*$"); Pattern empty = Pattern.compile("^\\s*$");
while(!(empty.matcher(content).matches())){ while(!(empty.matcher(content).matches())){
int quotes = indexOf(content,";"); String zip = tmp.clean(content);
int braces = indexOf(content,"\\{"); int quotes = indexOf(zip,";");
int equals = indexOf(content,"="); int braces = indexOf(zip,"\\{");
if(quotes < braces && quotes < equals){ int equals = indexOf(zip,"=");
boolean quote = false; if((quotes < braces && quotes < equals) || (equals < braces)){
Variable last = null; Variable variable = new Variable();
do { int index = variable.parse(content, global, local);
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
int index = variable.parse(content);
this.vars.add(variable);
content = content.substring(index);
quote = content.startsWith(",");
if(quote) {
content = content.substring(1);
last = variable;
}
}while(quote);
}else if(equals < braces){
//variable with value
boolean quote = false;
Variable last = null;
do {
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
int index = variable.parse(content);
this.vars.add(variable);
content = content.substring(index);
quote = content.startsWith(",");
if(quote) {
content = content.substring(1);
last = variable;
}else if(indexOf(content, "=") < indexOf(content, ";")){
Operation operation = new Operation();
index = operation.parse(content);
content = content.substring(index);
break;
}
}while(quote);
}else{
System.out.println("Function "+content);
Function func = new Function();
int index = func.parse(content);
this.functions.add(func);
content = content.substring(index); content = content.substring(index);
System.out.println("End "+content); boolean quote = content.startsWith(",");
if(quote){
content = content.substring(1);
MultipleDeclaratedVariable multiple = new MultipleDeclaratedVariable(variable.getModifier(), variable.getType());
multiple.addVariable(variable.getName(), variable.getValue());
while(quote){
variable = new Variable(variable.getModifier(), variable.getType());
index = variable.parse(content, global, local);
content = content.substring(index);
quote = content.startsWith(",");
if(quote) content = content.substring(1);
multiple.addVariable(variable.getName(), variable.getValue());
}
this.childs.add(multiple);
}else{
this.childs.add(variable);
}
content = content.substring(1);
}else{
// System.out.println("Function "+content);
Function func = new Function();
int index = func.parse(content, global, local);
this.childs.add(func);
content = content.substring(index);
// System.out.println("End "+content);
} }
} }
// return cleaner.unzip(matcher.group(1), ((s,p) -> s)).length();
return matcher.group(1).length(); return matcher.group(1).length();
} }
@ -96,15 +114,59 @@ public class Class {
return this.name; return this.name;
} }
public List<Variable> getVariables(){ public List<JavaElement> getChilds(){
return this.vars; return this.childs;
} }
public void show(){ // public List<Variable> getVariables(){
System.out.println(Modifier.toString(modifier)+" "+this.name+"{"); // return this.vars;
for(Variable v : this.vars) v.show(1); // }
System.out.println();
for(Function f : this.functions) f.show(1); @Override
System.out.println("}"); public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
for(Annotation annotation : this.annotations){
annotation.build(buffer, tab);
buffer.add("");
}
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+this.name+"{");
buffer.add("");
for(JavaElement child : this.childs){
child.build(buffer, tab+1);
buffer.add("");
}
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
if(search.apply(this)) return (E)this;
trace.add(this);
int index = trace.size()-1;
if(!deep.apply(trace)) return null;
for(JavaElement element : this.childs){
E result = element.find(search, deep, trace);
if(result != null) return result;
}
trace.remove(index);
return null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
int index = trace.size()-1;
if(search.apply(this, trace)) return (E)this;
for(JavaElement element : this.childs){
E result = element.find(search, trace);
if(result != null) return result;
}
trace.remove(index);
return null;
} }
} }

View file

@ -1,93 +1,174 @@
package be.jeffcheasey88.peeratcode.parser.java; package be.jeffcheasey88.peeratcode.parser.java;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class CleanerPool { public class CleanerPool{
private static String CONSTANT_REPLACER_STRING = "$STRING_STATEMENT_CONSTANT_"; private List<Cleaner> cleaners;
private static String CONSTANT_REPLACER_CHAR = "$CHAR_STATEMENT_CONSTANT_";
private static String CONSTANT_REPLACER_GENERIC = "$GENERIC_STATEMENT_CONSTANT_";
private List<String> constants; public CleanerPool(Cleaner... cleaners){
this.cleaners = new ArrayList<>(Arrays.asList(cleaners));
}
private CleanerPool(){ public CleanerPool group(CleanerPool... pools){
this.constants = new ArrayList<>(); CleanerPool pool = new CleanerPool();
pool.cleaners.addAll(cleaners);
for(CleanerPool other : pools){
for(Cleaner cleaner : other.cleaners){
boolean contains = false;
for(Cleaner include : pool.cleaners){
if(include.getPattern().equals(cleaner.getPattern())){
contains = true;
break;
}
}
if(contains) continue;
pool.cleaners.add(cleaner);
}
}
return pool;
}
@Override
public String toString(){
String s = "CleanerPool[\n";
for(Cleaner cleaner : cleaners) s+=cleaner.pattern+"['"+cleaner.open+"' -> '"+cleaner.close+"']\n";
s+="\n]";
return s;
} }
public String clean(String statement){ public String clean(String value){
char[] chars = statement.toCharArray(); for(Cleaner cleaner : this.cleaners) value = cleaner.clean(value);
StringBuilder builder = new StringBuilder(); return value;
for(int i = 0; i < chars.length; i++){ }
char current = chars[i];
if(current== '"'){ public String unzipOne(String value, BiFunction<String, String, String> modifier){
int constantPos = this.constants.size(); boolean edited;
String constant = cutConstant(chars, i); String tmp = value;
i+=constant.length()+1; Map<String, String> map = new HashMap<>();
builder.append(CONSTANT_REPLACER_STRING+constantPos); do{
this.constants.add(constant); edited = false;
}else{ for(Cleaner cleaner : this.cleaners){
builder.append(current); Matcher matcher = cleaner.getMatcher(tmp);
if(matcher.matches()){
String key = matcher.group(2);
String zip = cleaner.getConstant(key);
String modified = modifier.apply(zip, cleaner.getPattern());
if(modified == null) continue;
map.put(key, cleaner.open+modified+cleaner.close);
tmp = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
edited = true;
}
} }
}while(edited);
String base = value;
for(Entry<String, String> unzip : map.entrySet()){
Pattern pattern = Pattern.compile("\\"+unzip.getKey()+"(?<e>([^\\d]|$))");
if(pattern.matcher(base).find()) value = pattern.matcher(value).replaceAll(unzip.getValue()+"${e}");
} }
for(String s : constants){ return value;
System.out.println("CONSTANT="+s);
}
return builder.toString();
} }
public boolean isConstant(String region){ public String unzip(String value, BiFunction<String, String, String> modifier){
return region.startsWith(CONSTANT_REPLACER_STRING); boolean edited;
} do{
edited = false;
public String getConstant(String replacer){ for(Cleaner cleaner : this.cleaners){
if(!replacer.startsWith(CONSTANT_REPLACER_STRING)) return null; Matcher matcher = cleaner.getMatcher(value);
return this.constants.get(Integer.parseInt(replacer.replace(CONSTANT_REPLACER_STRING,""))); if(matcher.matches()){
} String key = matcher.group(2);
String zip = cleaner.getConstant(key);
public List<String> getConstants(){ String modified = modifier.apply(zip, cleaner.getPattern());
return this.constants; if(modified == null) continue;
} value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
edited = true;
private static Pattern parenthesisPattern = Pattern.compile("^\\$SQL_STATEMENT_PARENTHESIS_([0-9]*$)"); }
public boolean isParenthesis(String region){
return parenthesisPattern.matcher(region).matches();
}
private String cutConstant(char[] chars, int pos){
StringBuilder builder = new StringBuilder();
for(int i = pos+1; i < chars.length; i++){
char current = chars[i];
if(current == '"'){
if(current == '\\'){ //toChange
builder.append(current);
}else break;
}else{
builder.append(current);
} }
} }while(edited);
return builder.toString(); return value;
} }
public static interface Cutter{ public static class Cleaner{
String execute(String value, List<String> constants, String pattern); private Pattern rPattern;
default String cutOpenable(String value, List<String> constants, String pattern, char open, char close){ private String pattern;
private char open;
private char close;
public List<String> constants;
public Cleaner(String pattern, char open, char close){
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;
}
public String getConstant(String value){
return this.constants.get(Integer.parseInt(value.replace(this.pattern, "")));
}
public String clean(String value){
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for(int i = 0; i < value.length(); i++){
for(char c : value.toCharArray()){ char c = value.charAt(i);
if(c == open){ if(c == open){
int cut = cutOpenable(value.substring(i+1), constants, pattern, open, close);
}else if(c == close){ if(cut < 0){
builder.append(c);
continue;
}
i+=cut;
builder.append(pattern+(constants.size()-1));
}else{
builder.append(c);
}
}
return builder.toString();
}
private int cutOpenable(String value, List<String> constants, String pattern, char open, char close){
StringBuilder builder = new StringBuilder();
int i = 0;
for(;i < value.length(); i++){
char c = value.charAt(i);
if(c == close){
constants.add(builder.toString());
return i+1;
}else if(c == open){
int cut = cutOpenable(value.substring(i+1), constants, pattern, open, close);
if(cut < 0){
builder.append(c);
continue;
}
i+=cut;
builder.append(pattern+(constants.size()-1));
}else{
builder.append(c);
} }
} }
return builder.toString(); return -1;
} }
} }

View file

@ -2,84 +2,159 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class Function { import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
import be.jeffcheasey88.peeratcode.parser.java.operations.OperationContainer;
public class Function extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$"); private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$");
private static Pattern ANNOTATION_PATTERN = Pattern.compile("^(\\s*(@\\s*[^\\s]+\\s*\\^GENERIC_PARENTHESIS\\d+\\s+))");
private List<Annotation> annotations;
private int modifier; private int modifier;
private String returnType;
private String name; private String name;
private List<Variable> parameters;
private String exceptions; private String exceptions;
private String parameters;
private List<Function> functions; private boolean constructor;
private List<Operation> operations;
public Function(){ public Function(){
this.functions = new ArrayList<>(); this.annotations = new ArrayList<>();
this.operations = new ArrayList<>(); this.parameters = new ArrayList<>();
} }
public int parse(String content) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); local = new CleanerPool(new Cleaner("GENERIC_PARENTHESIS", '(', ')'));
content = local.clean(content);
int annotationOffset = 0;
Matcher matcher;
while((matcher = ANNOTATION_PATTERN.matcher(content)).lookingAt()){
String annot = matcher.group(1);
annotationOffset+=local.unzip(annot, (s,p) -> s).length();
content = content.substring(annot.length());
Annotation annotation = new Annotation();
annotation.parse(matcher.group(2), global, local);
this.annotations.add(annotation);
}
content = local.unzipOne(local.clean(content), (s,p) -> s);
matcher = PATTERN.matcher(content);
matcher.matches(); matcher.matches();
String[] split = matcher.group(2).split("\\s+"); attribute(matcher.group(2));
for(int i = 0; i < split.length-2; i++){ parameters(matcher.group(3)+";", global, local);
this.modifier+=JavaParser.getModifier(split[i]); this.exceptions = matcher.group(4).trim();
}
this.name = split[split.length-1];
this.parameters = matcher.group(3);
this.exceptions = matcher.group(4);
String body = matcher.group(5); local = local.group(new CleanerPool(new Cleaner("GENERIC_FUNCTION", '{', '}')));
int offset = 0;
int index = 0;
do { String zip = local.clean("{"+matcher.group(5));
int end = body.indexOf('}'); String body = local.unzipOne(zip, (s,p) -> s);
int braces = body.indexOf('{'); String unzip = body.substring(1, body.indexOf('}'));
int quotes = body.indexOf(';');
super.parse(local.clean(unzip), global, local);
if((end < 0) || (end < braces && end < quotes)){
if(end > 0) offset+=end; return annotationOffset+local.unzip(matcher.group(1), (s,p) -> s).length()+local.unzip(unzip, ((s,p) -> s)).length()+1;
break;
}
if(braces < 0 && quotes < 0){
if(end > 0) offset+=end;
break;
}
if(braces >= 0 && braces < quotes){
Function func = new Function();
index = func.parse(body.substring(0, end+1));
this.functions.add(func);
}else{
Operation op = new Operation();
index = op.parse(body.substring(0, end+1));
this.operations.add(op);
}
offset+=index+1;
body = body.substring(index);
}while(offset > -1);
return matcher.group(1).length()+offset;
} }
public void show(int tab){ private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
String start = ""; private static Pattern UNZIP_MAJ = Pattern.compile(">(?<e>[^>\\d,;(])");
for(int i = 0; i < tab; i++) start+="\t"; private static Pattern UNZIP_ARRAY = Pattern.compile("](?<e>[^\\[\\d,;])");
System.out.println(start+Modifier.toString(modifier)+" "+name+"("+parameters+") "+exceptions+" {");
for(Operation o : this.operations) o.show(tab+1); private void attribute(String content) throws Exception{
System.out.println(); CleanerPool generic = new CleanerPool(
for(Function f : this.functions) f.show(tab+1); new Cleaner("GENERIC_TYPE_",'<','>'),
System.out.println(start+"}"); new Cleaner("GENERIC_ARRAY",'[',']'));
String zip = generic.clean(content);
String unzip = generic.unzip(zip, (value, pattern) -> {
return value.replaceAll("\\s+", "");
});
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
System.out.println("split space on "+unzip);
Iterator<String> values = new ArrayIterator<>(unzip.split("\\s+"));
String value = null;
int modifier;
while(values.hasNext() && ((value = values.next()).charAt(0) == '@')){
Annotation annotation = new Annotation();
annotation.parse(value, null, generic);
this.annotations.add(annotation);
System.out.println("parse annotation "+value);
}
System.out.println("out in("+values.hasNext()+" -> "+value.charAt(0)+") "+value);
if((modifier = JavaParser.getModifier(value)) > 0){
do{
this.modifier+=modifier;
}while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
}
if(this.returnType == null){
this.returnType = value;
if(values.hasNext()) value = values.next();
else constructor = true;
}
if(this.name == null){
this.name = value;
}
} }
private void parameters(String content, CleanerPool global, CleanerPool local) throws Exception{
if(content.length() == 1) return;
boolean quote = false;
do {
Variable variable = new Variable();
int index = variable.parse(content, global, local);
this.parameters.add(variable);
content = content.substring(index);
quote = content.startsWith(",");
if(quote) content = content.substring(1);
}while(quote);
}
@Override @Override
public String toString(){ public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]"; for(Annotation annotation : this.annotations){
annotation.build(buffer, tab);
buffer.add("");
}
boolean empty = getChilds().size() == 0;
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"(");
boolean first = true;
for(Variable variable : this.parameters){
for(Annotation annotation : variable.getAnnotations()){
annotation.build(buffer, 0);
buffer.append((s) -> s+=" ");
}
if(first) first = false;
else{
buffer.append((s) -> s+=",");
}
buffer.append((s) -> s+=variable.getType()+" "+variable.getName());
}
buffer.append((s) -> s+=") "+exceptions+"{"+((empty ? "}":"")));
buffer.add("");
if(empty) return;
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
} }
} }

View file

@ -5,10 +5,10 @@ import java.util.regex.Pattern;
public class Import { public class Import {
private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*import\\s+([^;]*);)");
public static boolean isImport(String content){ public static boolean isImport(String content){
return PATTERN.matcher(content).matches(); return PATTERN.matcher(content).lookingAt();
} }
private String name; private String name;
@ -17,10 +17,12 @@ public class Import {
public int parse(String content) throws Exception{ public int parse(String content) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
this.name = matcher.group(2); this.name = matcher.group(2);
return matcher.group(1).length(); return matcher.group(1).length();
} }
// public int doYouSeeMe(){ return false; }
public String getName(){ public String getName(){
return this.name; return this.name;

View file

@ -0,0 +1,20 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
public abstract class JavaElement {
public abstract int parse(String content, CleanerPool global, CleanerPool local) throws Exception;
public abstract <E extends JavaElement> E find(Function<JavaElement, Boolean> search, Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace);
public abstract <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace);
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modifier = spacement;
buffer.append((s) -> s+=modifier);
}
}

View file

@ -1,42 +1,103 @@
package be.jeffcheasey88.peeratcode.parser.java; package be.jeffcheasey88.peeratcode.parser.java;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
public class JavaParser { import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class JavaParser{
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Import.java"); File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\routes\\PuzzleResponse.java");
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));
JavaParser parser = new JavaParser(reader); JavaParser parser = new JavaParser();
parser.parse(); parser.parse(reader);
System.out.println("build-----------------");
parser.build(new BufferedWriter(new FileWriter(new File("/home/buildClazzFromParser.txt"))));
reader = new BufferedReader(new FileReader(new File("/home/buildClazzFromParser.txt")));
String line;
while((line = reader.readLine()) != null) System.out.println(line);
reader.close();
}
public static void mainee(String[] args) throws Exception {
File dir = new File("");
show(dir);
}
public static void mainf(String[] args) throws Exception{
String variable = "var myName = \"Test\";";
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();
parser.parse(reader);
System.out.println("SHOW-----------------"); System.out.println("SHOW-----------------");
parser.show(); }
private static void show(File dir) throws Exception{
if(dir.isFile()){
if(!dir.getName().endsWith(".java")) return;
System.out.println("| "+dir.getAbsolutePath());
BufferedReader reader = new BufferedReader(new FileReader(dir));
try {
JavaParser parser = new JavaParser();
parser.parse(reader);
Class clazz = parser.getClazz();
System.out.println(clazz.getName());
// for(Variable variable : clazz.getVariables()){
// variable.show(1);
// }
}catch(Exception e) {}
}else{
for(File file : dir.listFiles()) show(file);
}
} }
private Package pack; private Package pack;
private List<Import> imports; private List<Import> imports;
private Class clazz; private Class clazz;
private BufferedReader reader; //later, maybe put string in the element
private CleanerPool cleaner;
public JavaParser(BufferedReader reader){ public JavaParser(){}
this.reader = reader;
}
public void parse() throws Exception{ public void parse(BufferedReader reader) throws Exception{
String content = ""; String content = "";
int index; int index;
String line; cleaner = new CleanerPool(
while((line = reader.readLine()) != null) content+=line; new Cleaner("CONSTANT_STRING",'"','"'),
new Cleaner("CONSTANT_CHAR",'\'','\''));
// content = CleanerPool.getterToDelete.clean(content); String line;
while((line = reader.readLine()) != null){
line = cleaner.clean(line);
index = line.indexOf("//");
if(index >= 0) line = line.substring(0, index);
content+=line+" ";
}
System.out.println(content);
this.pack = new Package(); this.pack = new Package();
index = this.pack.parse(content); index = this.pack.parse(content);
@ -51,10 +112,18 @@ public class JavaParser {
} }
this.clazz = new Class(); this.clazz = new Class();
index = this.clazz.parse(content); index = this.clazz.parse(content, cleaner, null);
content = content.substring(index); content = content.substring(index);
} }
public <E extends JavaElement> E find(Function<JavaElement, Boolean> search, Function<List<JavaElement>, Boolean> deep){
return (E)this.clazz.find(search, deep, new ArrayList<>());
}
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search){
return (E) this.clazz.find(search, new ArrayList<>());
}
public Package getPackage(){ public Package getPackage(){
return this.pack; return this.pack;
} }
@ -67,14 +136,28 @@ public class JavaParser {
return this.clazz; return this.clazz;
} }
public void show(){ public void build(BufferedWriter writer) throws Exception{
System.out.println("package "+this.pack.getName()+";"); writer.write("package "+this.pack.getName()+";\n");
System.out.println(); writer.write("\n");
for(Import i : this.imports) System.out.println("import "+i.getName()+";"); for(Import element : this.imports) writer.write("import "+element.getName()+";\n");
System.out.println(); writer.write("\n");
this.clazz.show();
ArrayBuffer<String> buffer = new ArrayBuffer<String>(String.class);
buffer.add("");
this.clazz.build(buffer, 0);
Iterator<String> lines = buffer.iterator();
while(lines.hasNext()){
String line = lines.next();
line = this.cleaner.unzip(line, (s,p) -> s);
writer.write(line+"\n");
}
writer.flush();
writer.close();
} }
public static int getModifier(String modifier){ public static int getModifier(String modifier){
switch(modifier){ switch(modifier){
case "public": return Modifier.PUBLIC; case "public": return Modifier.PUBLIC;
@ -92,5 +175,5 @@ public class JavaParser {
} }
return 0; return 0;
} }
} }

View file

@ -1,29 +0,0 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Operation {
private static Pattern VARIABLE_PATTERN = Pattern.compile("^(\\s*([^;]*)).*$");
private String tmp;
public Operation(){}
public int parse(String content) throws Exception{
Matcher matcher = VARIABLE_PATTERN.matcher(content);
if(matcher.matches()){
this.tmp = matcher.group(2);
return matcher.group(1).length()+1;
}
return 0;
}
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+tmp+";");
}
}

View file

@ -5,7 +5,7 @@ import java.util.regex.Pattern;
public class Package { public class Package {
private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);)");
private String name; private String name;
@ -13,7 +13,7 @@ public class Package {
public int parse(String content) throws Exception{ public int parse(String content) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
this.name = matcher.group(2); this.name = matcher.group(2);
return matcher.group(1).length(); return matcher.group(1).length();
} }

View file

@ -1,27 +1,46 @@
package be.jeffcheasey88.peeratcode.parser.java; package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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;
public class Variable { import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class Variable extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*)(.*)$"); private static Pattern PATTERN = Pattern.compile("^(\\s*)(.*)$");
private List<Annotation> annotations;
private int modifier; private int modifier;
private String name; private String name;
private String type; private String type;
private Variable value; private Variable value; //Change into operation or JavaElement
public Variable(){} public Variable(){
this.annotations = new ArrayList<>();
}
public Variable(int modifier, String type){ public Variable(int modifier, String type){
this();
this.modifier = modifier; this.modifier = modifier;
this.type = type; this.type = type;
} }
public int parse(String content) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_TYPE",'<','>'),
new Cleaner("GENERIC_ARRAY",'[',']'),
new Cleaner("GENERIC_FUNCTION", '{','}'),
new Cleaner("GENERIC_PARENTHESIS",'(',')'));
if(local == null) local = generic;
else local = local.group(generic);
content = local.clean(content);
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.matches();
@ -35,50 +54,74 @@ public class Variable {
body = body.substring(0, min); body = body.substring(0, min);
if(equals < quote && equals < quotes){ if(equals < quote && equals < quotes){
assigment(body); assigment(body, local);
}else{ }else{
onlyDefine(body); onlyDefine(body, local);
} }
return offset+min; return offset+local.unzipOne(body, (s,p) -> (p.equals("^GENERIC_TYPE") || p.equals("^GENERIC_ARRAY")) ? s : null).length();
} }
private void assigment(String content){ private void assigment(String content, CleanerPool cleaner) throws Exception{
Iterator<String> values = onlyDefine(content, cleaner);
if(!values.hasNext()) return;
values.next();
if(!values.hasNext()) return;
String spacesValue = values.next();
while(values.hasNext()) spacesValue+=" "+values.next();
this.value = new Value(spacesValue);
} }
private void onlyDefine(String content){ private Iterator<String> onlyDefine(String content, CleanerPool cleaner) throws Exception{
content = generiqueTypes(content); content = generiqueTypes(content, cleaner);
System.out.println(content);
String[] values = content.split("\\s+"); CleanerPool tmp = new CleanerPool(new Cleaner("GENERIC_PARENTHESIS",'(',')'));
for(String value : values){ content = tmp.clean(content);
int modifier = JavaParser.getModifier(value);
if(modifier > 0){ String[] array = content.split("\\s+");
// for(int i = 0; i < array.length; i++) array[i] = tmp.unzip(array[i], (s,p) -> s);
Iterator<String> values = new ArrayIterator<>(array);
String value = null;
int modifier;
while(values.hasNext() && ((value = values.next()).charAt(0) == '@')){
Annotation annotation = new Annotation();
annotation.parse(value, cleaner, tmp);
this.annotations.add(annotation);
}
if((modifier = JavaParser.getModifier(value)) > 0){
do{
this.modifier+=modifier; this.modifier+=modifier;
continue; }while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
} }
if(this.type == null){ if(this.type == null){
this.type = value; this.type = value;
continue; if(values.hasNext()) value = values.next();
} }
if(this.name == null){
this.name = value; this.name = value;
} }
return values;
} }
private String generiqueTypes(String content){ private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
System.out.println(content); private static Pattern UNZIP_MAJ = Pattern.compile(">(?<e>[^>\\d,;(])");
String result = ""; private static Pattern UNZIP_ARRAY = Pattern.compile("](?<e>[^\\[\\d,;])");
int opened = 0; private static Pattern UNZIP_EQUALS_LEFT = Pattern.compile("(?<e>[^=\\s])=");
for(char c : content.toCharArray()){ private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?<e>[^=\\s])");
if(c == '<') opened++;
else if(c == '>') opened--; private String generiqueTypes(String content, CleanerPool cleaner){
String unzip = cleaner.unzip(content, (value, pattern) -> {
if(opened > 0){ if(pattern.equals("^GENERIC_FUNCTION")) return null;
if(Character.isWhitespace(c)) continue; if(pattern.equals("^GENERIC_PARENTHESIS")) return value.replace("\\s+", " ");
} return value.replaceAll("\\s+", "");
result+=c; });
} unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
result = result.replaceAll("(>\\s*)", "> ").replace("> >", ">>"); unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
return result; unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} =");
unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}");
return unzip;
} }
private int indexOf(String value, String target){ private int indexOf(String value, String target){
@ -101,10 +144,40 @@ public class Variable {
return this.value; return this.value;
} }
public void show(int tab){ public List<Annotation> getAnnotations(){
String start = ""; return this.annotations;
for(int i = 0; i < tab; i++) start+="\t"; }
System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":"="+value+";"));
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
for(Annotation annotation : this.annotations){
annotation.build(buffer, 0);
buffer.append((s) -> s+=" ");
}
buffer.append((s) -> s+=Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
buffer.add("");
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
if(search.apply(this)) return (E)this;
trace.add(this);
int index = trace.size()-1;
if(!deep.apply(trace)) return null;
//Value of the variable
trace.remove(index);
return null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
int index = trace.size()-1;
if(search.apply(this, trace)) return (E)this;
//value
trace.remove(index);
return null;
} }
public static class Value extends Variable{ public static class Value extends Variable{
@ -124,4 +197,39 @@ public class Variable {
return this.value; return this.value;
} }
} }
public static class MultipleDeclaratedVariable extends Variable{
private List<String> names;
private List<Variable> values;
public MultipleDeclaratedVariable(int modifier, String type){
super(modifier, type);
this.names = new ArrayList<>();
this.values = new ArrayList<>();
}
public void addVariable(String name, Variable value){
this.names.add(name);
this.values.add(value);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String sMod = spacement;
String vars = "";
for(int i = 0; i < this.names.size(); i++){
Variable value = this.values.get(i);
vars+=","+this.names.get(i)+((value == null) ? "" : " = "+value);
}
vars = vars.substring(1);
String varMod = vars;
buffer.append((s) -> s+=sMod+Modifier.toString(getModifier())+" "+getType()+" "+varMod+";");
buffer.add("");
}
}
} }

View file

@ -0,0 +1,59 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class AssigmentOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\s]+)\\s*=\\s*([^;]+);)");
private String variable;
private String value;
public AssigmentOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
this.variable = local.unzip(matcher.group(2), (s,p) -> s);
this.value = local.unzip(matcher.group(3), (s,p) -> s);
return matcher.group(1).length();
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=variable+" = "+value+";");
buffer.add("");
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
if(search.apply(this)) return (E)this;
trace.add(this);
int index = trace.size()-1;
if(!deep.apply(trace)) return null;
//value
trace.remove(index);
return null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
int index = trace.size()-1;
if(search.apply(this, trace)) return (E)this;
//value
trace.remove(index);
return null;
}
}

View file

@ -0,0 +1,142 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ConditionalOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+)");
private Pattern pattern;
private String condition;
public ConditionalOperation(Pattern pattern){
this.pattern = pattern;
}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = this.pattern.matcher(content);
matcher.lookingAt();
this.condition = local.unzipOne(matcher.group(2), (s,p) -> s);
// System.out.println("CONDITION "+condition);
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content);
if(matcher.lookingAt()){
index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length());
}
int bodysize;
if(content.startsWith("^")){
matcher = PATTERN.matcher(content);
matcher.lookingAt();
content = matcher.group(1);
bodysize = content.length();
content = local.unzipOne(content, (s,p) -> s);
content = content.substring(1, content.length()-1);
content = local.clean(content);
super.parse(content, global, local);
}else if(content.startsWith(";")){
bodysize=1;
}else{
bodysize = parseOne(content, global, local);
}
return index+bodysize;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
boolean empty = getChilds().size() == 0;
if(empty){
buffer.append((s) -> s+=";");
buffer.add("");
return;
}
boolean oneChild = getChilds().size() == 1;
if(oneChild) buffer.append((s) -> s+=" ");
else{
buffer.append((s) -> s+="{");
buffer.add("");
}
for(JavaElement child : getChilds()) child.build(buffer, oneChild ? 0 : tab+1);
if(!oneChild){
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
public static class IfOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*if\\s*(\\^GENERIC_PARENTHESIS\\d+))");
public IfOperation(){
super(PATTERN);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modSpace = spacement;
buffer.append((s) -> s+=modSpace+"if"+super.condition);
super.build(buffer, tab);
}
}
public static class ForOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*for\\s*(\\^GENERIC_PARENTHESIS\\d+))");
public ForOperation(){
super(PATTERN);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modSpace = spacement;
buffer.append((s) -> s+=modSpace+"for"+super.condition);
super.build(buffer, tab);
}
}
public static class WhileOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*while\\s*(\\^GENERIC_PARENTHESIS\\d+))");
public WhileOperation(){
super(PATTERN);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
String spacement = "";
for(int i = 0; i < tab; i++) spacement+="\t";
final String modSpace = spacement;
buffer.append((s) -> s+=modSpace+"while"+super.condition);
super.build(buffer, tab);
}
}
}

View file

@ -0,0 +1,53 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class DoOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*do\\s*)");
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
public DoOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_CLEANER.matcher(content);
matcher.lookingAt();
content = matcher.group(1);
index += content.length();
content = local.unzipOne(content, (s,p) -> s);
content = content.substring(1, content.length()-1);
content = local.clean(content);
super.parse(content, global, local);
return index;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="do{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}

View file

@ -0,0 +1,65 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ElseOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*)");
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
public ElseOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
int index = matcher.group(1).length();
content = content.substring(index);
int bodysize;
if(content.startsWith("^")){
matcher = PATTERN_CLEANER.matcher(content);
matcher.lookingAt();
content = matcher.group(1);
bodysize = content.length();
content = local.unzipOne(content, (s,p) -> s);
content = content.substring(1, content.length()-1);
content = local.clean(content);
super.parse(content, global, local);
}else{
bodysize = parseOne(content, global, local);
}
return index+bodysize;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="else ");
boolean oneOperation = getChilds().size() == 1;
if(oneOperation){
getChilds().get(0).build(buffer, 0);
return;
}
buffer.append((s) -> s+="{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}

View file

@ -0,0 +1,72 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class LoopAffectOperation extends JavaElement{
private Pattern pattern;
public LoopAffectOperation(Pattern pattern){
this.pattern = pattern;
}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = this.pattern.matcher(content);
matcher.lookingAt();
return matcher.group(1).length();
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
return search.apply(this) ? (E)this : null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
return search.apply(this, trace) || trace.remove(trace.size()-1) == null ? (E)this : null;
}
public static class ContinueOperation extends LoopAffectOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*continue\\s*;)");
public ContinueOperation(){
super(PATTERN);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="continue;");
buffer.add("");
}
}
public static class BreakOperation extends LoopAffectOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*break\\s*;)");
public BreakOperation(){
super(PATTERN);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="break;");
buffer.add("");
}
}
}

View file

@ -0,0 +1,53 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class MethodCallOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+);)");
private String value;
public MethodCallOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
content = local.clean(content);
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
this.value = local.unzip(matcher.group(2), (s,p) -> s);
return matcher.group(1).length();
}
public String getValue(){
return this.value;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+=value+";");
buffer.add("");
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
return search.apply(this) ? (E)this : null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
return search.apply(this, trace) || trace.remove(trace.size()-1) == null ? (E)this : null;
}
}

View file

@ -0,0 +1,96 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Variable;
import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;
public abstract class OperationContainer extends JavaElement{
private static OperationFactory FACTORY = OperationFactory.getFactory();
private List<JavaElement> childs;
public OperationContainer(){
this.childs = new ArrayList<>();
}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
// System.out.println("OperationContainer.parse -> "+content);
while(!(content.replaceAll("\\s+", "").isEmpty())) content = internalParse(content, global, local);
return 0;
}
public int parseOne(String content, CleanerPool global, CleanerPool local) throws Exception{
// System.out.println("OperationContainer.parseOne -> "+content);
String modify = internalParse(content, global, local);
return content.length()-modify.length();
}
private String internalParse(String content, CleanerPool global, CleanerPool local) throws Exception{
// System.out.println("OperationContainer.internalParse -> "+content);
JavaElement operation = FACTORY.buildOperation(content);
// System.out.println(operation.getClass().getSimpleName()+" operation = FACTORY.buildOperation();");
int index = operation.parse(content, global, local);
content = content.substring(index);
if(operation instanceof Variable){
if(content.startsWith(",")){
Variable variable = (Variable)operation;
MultipleDeclaratedVariable multiple = new MultipleDeclaratedVariable(variable.getModifier(), variable.getType());
multiple.addVariable(variable.getName(), variable.getValue());
operation = multiple;
boolean quote;
do{
variable = new Variable(variable.getModifier(), variable.getType());
index = variable.parse(content, global, local);
multiple.addVariable(variable.getName(), variable.getValue());
content = content.substring(index);
quote = content.startsWith(",");
content = content.substring(1);
}while(quote);
}else content = content.substring(1);
}
this.childs.add(operation);
return content;
}
public List<JavaElement> getChilds(){
return this.childs;
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
if(search.apply(this)) return (E)this;
trace.add(this);
int index = trace.size()-1;
if(!deep.apply(trace)) return null;
for(JavaElement element : this.childs){
E result = element.find(search, deep, trace);
if(result != null) return result;
}
trace.remove(index);
return null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
int index = trace.size()-1;
if(search.apply(this, trace)) return (E)this;
for(JavaElement element : this.childs){
E result = element.find(search, trace);
if(result != null) return result;
}
trace.remove(index);
return null;
}
}

View file

@ -0,0 +1,117 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Variable;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.ForOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.IfOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.BreakOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.ContinueOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.CatchOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.FinallyOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.TryOperation;
public class OperationFactory{
private static final OperationFactory SINGLETON = new OperationFactory();
public static OperationFactory getFactory(){
return SINGLETON;
}
private Map<Pattern, Class<? extends JavaElement>> patterns;
private OperationFactory(){
this.patterns = new LinkedHashMap<>();
this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;"), ReturnOperation.class);
this.patterns.put(Pattern.compile("^\\s*if\\s*\\^GENERIC_PARENTHESIS\\d+"), IfOperation.class);
this.patterns.put(Pattern.compile("^\\s*for\\s*\\^GENERIC_PARENTHESIS\\d+"), ForOperation.class);
this.patterns.put(Pattern.compile("^\\s*while\\s*\\^GENERIC_PARENTHESIS\\d+"), WhileOperation.class);
this.patterns.put(Pattern.compile("^\\s*else\\s*"), ElseOperation.class);
this.patterns.put(Pattern.compile("^\\s*do\\s*\\^GENERIC_FUNCTION\\d+"), DoOperation.class);
this.patterns.put(Pattern.compile("^\\s*synchronized\\s*\\^GENERIC_PARENTHESIS\\d+"), SynchronizedOperation.class);
this.patterns.put(Pattern.compile("^\\s*continue\\s*;"), ContinueOperation.class);
this.patterns.put(Pattern.compile("^\\s*break\\s*;"), BreakOperation.class);
this.patterns.put(Pattern.compile("^\\s*throw\\s+[^;]+"), ThrowOperation.class);
this.patterns.put(Pattern.compile("^\\s*try\\s*\\^"), TryOperation.class);
this.patterns.put(Pattern.compile("^\\s*catch\\s*\\^GENERIC_PARENTHESIS\\d+"), CatchOperation.class);
this.patterns.put(Pattern.compile("^\\s*finally\\s*\\^GENERIC_FUNCTION\\d+"), FinallyOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;"), MethodCallOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;"), Variable.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s*=\\s*[^;]+;"), AssigmentOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^;]+;"), Variable.class);
}
/*
* variable OK
* assignation OK
* exec method OK
* for OK
* do while OK
* while OK
* if OK
* switch case
* return OK
* continue OK
* break OK
* try catch finally OK
* throw OK
* else OK
* synchronized OK
* instance of
*
* native operation style i++
*
* assert ??
* Goto ??
* Const ??
*/
public JavaElement buildOperation(String content) throws Exception{
// System.out.println("Factory.buildOperation -> "+content);
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_ARRAY",'[',']'),
new Cleaner("GENERIC_TYPE_",'<','>'));
content = generic.clean(content);
content = generiqueTypes(content, generic);
for(Entry<Pattern, Class<? extends JavaElement>> entries : this.patterns.entrySet()){
if(entries.getKey().matcher(content).lookingAt()) return entries.getValue().newInstance();
}
return null;
}
private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
private static Pattern UNZIP_MAJ = Pattern.compile(">(?<e>[^>\\d,;(])");
private static Pattern UNZIP_ARRAY = Pattern.compile("](?<e>[^\\[\\d,;])");
private static Pattern UNZIP_EQUALS_LEFT = Pattern.compile("(?<e>[^=\\s])=");
private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?<e>[^=\\s])");
private String generiqueTypes(String content, CleanerPool cleaner){
String unzip = cleaner.unzip(content, (value, pattern) -> {
if(pattern.equals("^GENERIC_FUNCTION") || pattern.equals("^GENERIC_PARENTHESIS")) return null;
return value.replaceAll("\\s+", "");
});
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} =");
unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}");
return unzip;
}
}

View file

@ -0,0 +1,77 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ReturnOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s*([^;]*);)");
private static OperationFactory FACTORY = OperationFactory.getFactory();
private String toChange;
private JavaElement value;
public ReturnOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
//To update for native obj
this.toChange = matcher.group(2);
JavaElement operation = FACTORY.buildOperation(toChange+";");
if(operation != null){
System.out.println(operation.getClass().getSimpleName());
value = operation;
value.parse(toChange+";", global, local);
}
return matcher.group(1).length();
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
if(value != null){
buffer.append((s) -> s+="return ");
value.build(buffer, 0);
buffer.add("");
return;
}
buffer.append((s) -> s+="return "+toChange+";");
buffer.add("");
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
if(search.apply(this)) return (E)this;
trace.add(this);
int index = trace.size()-1;
if(!deep.apply(trace)) return null;
E result = value.find(search, deep, trace);
if(result != null) return result;
trace.remove(index);
return null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
int index = trace.size()-1;
if(search.apply(this, trace)) return (E)this;
E result = value.find(search, trace);
if(result != null) return result;
trace.remove(index);
return null;
}
}

View file

@ -0,0 +1,54 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class SynchronizedOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*synchronized\\s*)(\\^GENERIC_PARENTHESIS\\d+)(\\s*)");
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
private String include;
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
this.include = local.unzip(matcher.group(2), (s,p) -> s);
int index = matcher.group(1).length()+matcher.group(2).length()+matcher.group(3).length();
content = content.substring(index);
matcher = PATTERN_CLEANER.matcher(content);
matcher.lookingAt();
content = matcher.group(1);
index += content.length();
content = local.unzipOne(content, (s,p) -> s);
content = content.substring(1, content.length()-1);
content = local.clean(content);
super.parse(content, global, local);
return index;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="synchronized"+this.include+"{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}

View file

@ -0,0 +1,50 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ThrowOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*throw\\s+([^;]+);)");
private String toChange;
public ThrowOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception {
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
//To update for native obj
this.toChange = matcher.group(2);
return matcher.group(1).length();
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="throw "+toChange+";");
buffer.add("");
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
return search.apply(this) ? (E)this : null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
return search.apply(this, trace) || trace.remove(trace.size()-1) == null ? (E)this : null;
}
}

View file

@ -0,0 +1,151 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class TryCatchOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+)");
public TryCatchOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
content = matcher.group(1);
int bodysize = content.length();
content = local.unzipOne(content, (s,p) -> s);
content = content.substring(1, content.length()-1);
content = local.clean(content);
super.parse(content, global, local);
return bodysize;
}
public static class TryOperation extends TryCatchOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*try\\s*(\\^GENERIC_PARENTHESIS\\d+)?)");
private String resource;
public TryOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = TryOperation.PATTERN.matcher(content);
matcher.lookingAt();
if(matcher.group(2) != null) this.resource = local.unzipOne(matcher.group(2), (s,p) -> s);
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content);
if(matcher.lookingAt()){
index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length());
}
return index+super.parse(content, global, local);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="try"+(resource == null ? "":resource)+"{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
public static class CatchOperation extends TryCatchOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*catch\\s*(\\^GENERIC_PARENTHESIS\\d+))");
private String exceptions;
public CatchOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = CatchOperation.PATTERN.matcher(content);
matcher.lookingAt();
this.exceptions = local.unzipOne(matcher.group(2), (s,p) -> s);
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content);
if(matcher.lookingAt()){
index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length());
}
return index+super.parse(content, global, local);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="catch"+exceptions+"{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
public static class FinallyOperation extends TryCatchOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*finally\\s*)");
public FinallyOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = FinallyOperation.PATTERN.matcher(content);
matcher.lookingAt();
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content);
if(matcher.lookingAt()){
index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length());
}
return index+super.parse(content, global, local);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="finally{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
}

View file

@ -0,0 +1,27 @@
package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class CleanerTest {
@Test
void cutter(){
CleanerPool cleaner = new CleanerPool();
String result = cleaner.clean("Test0< List< Map< Test1, List< Test2 >, Test3>> > ");
assertEquals("Test0$TEST3 ", result);
}
@Test
void sameOpenClose(){
CleanerPool cleaner = new CleanerPool(new Cleaner("STRINGS",'"','"'));
String result = cleaner.clean("Pattern.compile(\"^\\s*(import\\s+([^;]*);).*$\");");
assertEquals("Pattern.compile($STRINGS0);", result);
}
}

View file

@ -0,0 +1,151 @@
package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.ForOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.DoOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.BreakOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.ContinueOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.MethodCallOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.SynchronizedOperation;
@TestInstance(Lifecycle.PER_CLASS)
class OperationTest{
JavaParser parse(String code) throws Exception{
BufferedReader reader = new BufferedReader(new Reader(){public int read(char[] cbuf, int off, int len) throws IOException{return 0;}public void close() throws IOException {}}) {
private boolean read = false;
@Override
public String readLine() throws IOException{
if(read) return null;
read = true;
return code;
}
};
JavaParser parser = new JavaParser();
parser.parse(reader);
return parser;
}
@Test
void ifOperation(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0) { return i; } return i; } }");
Class clazz = parser.getClazz();
assertEquals(1, clazz.getChilds().size());
Function function = (Function) clazz.getChilds().get(0);
assertEquals(3, function.getChilds().size());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void ifWithNoneOperation(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0); return i; } }");
Class clazz = parser.getClazz();
assertEquals(1, clazz.getChilds().size());
Function function = (Function) clazz.getChilds().get(0);
assertEquals(3, function.getChilds().size());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void doWhileOperation(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; do{ System.out.println(\"Hello\"); }while(i == 0); return i; } }");
Class clazz = parser.getClazz();
assertEquals(1, clazz.getChilds().size());
Function function = (Function) clazz.getChilds().get(0);
assertEquals(4, function.getChilds().size());
assertEquals(DoOperation.class, function.getChilds().get(1).getClass());
DoOperation doOp = (DoOperation)function.getChilds().get(1);
assertEquals(1, doOp.getChilds().size());
assertEquals(MethodCallOperation.class, doOp.getChilds().get(0).getClass());
assertEquals(WhileOperation.class, function.getChilds().get(2).getClass());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void continueOperation(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) continue; } }");
Class clazz = parser.getClazz();
assertEquals(1, clazz.getChilds().size());
Function function = (Function) clazz.getChilds().get(0);
assertEquals(1, function.getChilds().size());
assertEquals(ForOperation.class, function.getChilds().get(0).getClass());
ForOperation f = (ForOperation)function.getChilds().get(0);
assertEquals(1, f.getChilds().size());
assertEquals(ContinueOperation.class, f.getChilds().get(0).getClass());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void breakOperation(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) break ; } }");
Class clazz = parser.getClazz();
assertEquals(1, clazz.getChilds().size());
Function function = (Function) clazz.getChilds().get(0);
assertEquals(1, function.getChilds().size());
assertEquals(ForOperation.class, function.getChilds().get(0).getClass());
ForOperation f = (ForOperation)function.getChilds().get(0);
assertEquals(1, f.getChilds().size());
assertEquals(BreakOperation.class, f.getChilds().get(0).getClass());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void synchronizedOperation(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ synchronized(this) { this.none(); } } }");
Class clazz = parser.getClazz();
assertEquals(1, clazz.getChilds().size());
Function function = (Function) clazz.getChilds().get(0);
assertEquals(1, function.getChilds().size());
assertEquals(SynchronizedOperation.class, function.getChilds().get(0).getClass());
SynchronizedOperation sync = (SynchronizedOperation)function.getChilds().get(0);
assertEquals(1, sync.getChilds().size());
assertEquals(MethodCallOperation.class, sync.getChilds().get(0).getClass());
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,113 @@
package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.IfOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.MethodCallOperation;
@TestInstance(Lifecycle.PER_CLASS)
class SearchTest{
JavaParser parse(String code) throws Exception{
BufferedReader reader = new BufferedReader(new Reader(){public int read(char[] cbuf, int off, int len) throws IOException{return 0;}public void close() throws IOException {}}) {
private boolean read = false;
@Override
public String readLine() throws IOException{
if(read) return null;
read = true;
return code;
}
};
JavaParser parser = new JavaParser();
parser.parse(reader);
return parser;
}
@Test
void methodCallSearch(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none(); } } }");
JavaElement element = parser.find(
(e) -> e instanceof MethodCallOperation,
(trace) -> true);
assertNotNull(element);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void methodCallOutOfIfSearch(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none(); } valid(); } }");
JavaElement element = parser.find(
(e) -> e instanceof MethodCallOperation,
(trace) -> !trace.stream().anyMatch((e) -> (e instanceof ConditionalOperation)));
assertNotNull(element);
assertEquals("valid()", ((MethodCallOperation)element).getValue());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void siplifiedMethodCallInIfSearch(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none(); } valid(); } }");
MethodCallOperation element = parser.find(
(e, trace) -> {
return (e instanceof MethodCallOperation) && (trace.get(trace.size()-2) instanceof IfOperation);
});
assertNotNull(element);
assertEquals("this.none()", element.getValue());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
void verifyPath(){
try {
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0){ this.none();this.yes();this.none(); } valid(); } }");
java.lang.Class[] exceptedStack = { Class.class, Function.class, IfOperation.class, MethodCallOperation.class };
MethodCallOperation element = parser.find(
(e, trace) -> {
if((e instanceof MethodCallOperation) && ((MethodCallOperation)e).getValue().equals("this.yes()")){
java.lang.Class[] stack = (java.lang.Class[]) trace.stream().map((current) -> current.getClass()).toArray((i) -> new java.lang.Class[i]);
assertArrayEquals(exceptedStack, stack);
return true;
}
return false;
});
assertNotNull(element);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -1,16 +1,22 @@
package be.jeffcheasey88.peeratcode.parser.java; package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;
import be.jeffcheasey88.peeratcode.parser.java.Variable.Value; import be.jeffcheasey88.peeratcode.parser.java.Variable.Value;
class VariableTest { @TestInstance(Lifecycle.PER_CLASS)
class VariableTest{
//int i = 4; //int i = 4;
//int i,j,k,l=1; //int i,j,k,l=1;
@ -20,123 +26,295 @@ class VariableTest {
//Test<Test,K,L> j = new Test().schedule(p -> { return true;}); //Test<Test,K,L> j = new Test().schedule(p -> { return true;});
//int i =j=k=l=4; //int i =j=k=l=4;
@Test // private CleanerPool cleaner;
void case1(){ //
try { // @BeforeAll
Variable variable = new Variable(); // void init(){
variable.parse(" int i = 4 ; "); // this.cleaner = new CleanerPool();
// }
assertEquals(0, variable.getModifier()); //
assertEquals("int", variable.getType()); // @Test
assertEquals("i", variable.getName()); // void case1(){
assertEquals("4", ((Value)variable.getValue()).value()); // try {
}catch(Exception e){ // Variable variable = new Variable();
fail(e); // variable.parse(cleaner.clean(" int i = 4 ; "), cleaner);
} //
} // assertEquals(0, variable.getModifier());
// assertEquals("int", variable.getType());
@Test // assertEquals("i", variable.getName());
void case2(){ // assertEquals("4", ((Value)variable.getValue()).value());
try { // }catch(Exception e){
Variable variable = new Variable(); // fail(e);
variable.parse("public static int l ; "); // }
// }
assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier()); //
assertEquals("int", variable.getType()); // @Test
assertEquals("l", variable.getName()); // void case2(){
assertNull(variable.getValue()); // try {
}catch(Exception e){ // Variable variable = new Variable();
fail(e); // variable.parse(cleaner.clean("public static int l ; "), cleaner);
} //
} // assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier());
// assertEquals("int", variable.getType());
@Test // assertEquals("l", variable.getName());
void case3(){ // assertNull(variable.getValue());
try { // }catch(Exception e){
Variable variable = new Variable(); // fail(e);
variable.parse(" int lm ; "); // }
// }
assertEquals(0, variable.getModifier()); //
assertEquals("int", variable.getType()); // @Test
assertEquals("lm", variable.getName()); // void case3(){
assertNull(variable.getValue()); // try {
}catch(Exception e){ // Variable variable = new Variable();
fail(e); // variable.parse(cleaner.clean(" int lm ; "), cleaner);
} //
} // assertEquals(0, variable.getModifier());
// assertEquals("int", variable.getType());
@Test // assertEquals("lm", variable.getName());
void case4(){ // assertNull(variable.getValue());
try { // }catch(Exception e){
Variable variable = new Variable(); // fail(e);
variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; "); // }
// }
assertEquals(0, variable.getModifier()); //
assertEquals("Testas<List<Map<Test,List<Test>,Test>>>", variable.getType()); // @Test
assertEquals("t", variable.getName()); // void case4(){
assertNull(variable.getValue()); // try {
}catch(Exception e){ // Variable variable = new Variable();
fail(e); // variable.parse(cleaner.clean("Test0< List< Map< Test1, List< Test2 >, Test3>> >t; "), cleaner);
} //
} // assertEquals(0, variable.getModifier());
// assertEquals("Test0<List<Map<Test1,List<Test2>,Test3>>>", variable.getType());
@Test // assertEquals("t", variable.getName());
void case5(){ // assertNull(variable.getValue());
try { // }catch(Exception e){
Variable variable = new Variable(); // fail(e);
variable.parse(" int i,j,k,l=1; "); // }
// }
assertEquals(0, variable.getModifier()); //
assertEquals("int", variable.getType()); // @Test
assertEquals("i", variable.getName()); // void case5(){
assertNull(variable.getValue()); // try {
}catch(Exception e){ // Variable variable = new Variable();
fail(e); // variable.parse(cleaner.clean(" int i,j,k,l=1; "), cleaner);
} //
} // assertEquals(0, variable.getModifier());
// assertEquals("int", variable.getType());
@Test // assertEquals("i", variable.getName());
void case6(){ // assertNull(variable.getValue());
try { // }catch(Exception e){
Class clazz = new Class(); // fail(e);
clazz.parse("public class Test{ int i ,j,k,l=1; } "); // }
// }
List<Variable> vars = clazz.getVariables(); //
assertEquals(vars.size(), 4); // @Test
for(int i = 0; i < 3; i++){ // void case6(){
Variable v = vars.get(i); // try {
assertEquals(0, v.getModifier()); // Class clazz = new Class();
assertEquals("int", v.getType()); // clazz.parse(cleaner.clean("public class Test{ int i ,j,k,l=1; } "), cleaner);
assertEquals((char)('i'+i), v.getName().charAt(0)); //
assertNull(v.getValue()); // List<Variable> vars = clazz.getVariables();
} // assertEquals(1, vars.size());
Variable v = vars.get(3); //
assertEquals(0, v.getModifier()); // Variable v = vars.get(0);
assertEquals("int", v.getType()); // assertEquals(0, v.getModifier());
assertEquals('l', v.getName().charAt(0)); // assertEquals("int", v.getType());
assertEquals("1", ((Value)v.getValue()).value()); //// for(int i = 0; i < 3; i++){
}catch(Exception e){ //// Variable v = vars.get(i);
fail(e); //// assertEquals(0, v.getModifier());
} //// assertEquals("int", v.getType());
} //// assertEquals((char)('i'+i), v.getName().charAt(0));
//// assertNull(v.getValue());
@Test //// }
void case7(){ //// Variable v = vars.get(3);
try { //// assertEquals(0, v.getModifier());
Class clazz = new Class(); //// assertEquals("int", v.getType());
clazz.parse("public class Test{ int i ,j,k; int l=i=k=l=4; } "); //// assertEquals('l', v.getName().charAt(0));
//// assertEquals("1", ((Value)v.getValue()).value());
List<Variable> vars = clazz.getVariables(); // }catch(Exception e){
assertEquals(vars.size(), 4); // fail(e);
for(int i = 0; i < 4; i++){ // }
Variable v = vars.get(i); // }
assertEquals(0, v.getModifier()); //
assertEquals("int", v.getType()); // @Test
assertEquals((char)('i'+i), v.getName().charAt(0)); // void case7(){
} // try {
}catch(Exception e){ // Class clazz = new Class();
fail(e); // clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool());
} //
} // List<Variable> vars = clazz.getVariables();
// assertEquals(2, vars.size());
// Variable v = vars.get(0);
// assertEquals(0, v.getModifier());
// assertEquals("int", v.getType());
//// for(int i = 0; i < 4; i++){
//// Variable v = vars.get(i);
//// assertEquals(0, v.getModifier());
//// assertEquals("int", v.getType());
//// assertEquals((char)('i'+i), v.getName().charAt(0));
//// }
// }catch(Exception e){
// fail(e);
// }
// }
//
// @Test
// void case8(){
// try {
// Variable variable = new Variable();
// variable.parse(cleaner.clean("int[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]t; "), cleaner);
//
// assertEquals(0, variable.getModifier());
// assertEquals("int[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]", variable.getType());
// assertEquals("t", variable.getName());
// assertNull(variable.getValue());
// }catch(Exception e){
// fail(e);
// }
// }
//
// @Test
// void case9(){
// try {
// Class clazz = new Class();
// clazz.parse(cleaner.clean("public class Test{ int i ,j,k,l; } "), cleaner);
//
// List<Variable> vars = clazz.getVariables();
// assertEquals(1, vars.size());
// Variable v = vars.get(0);
// assertEquals(0, v.getModifier());
// assertEquals("int", v.getType());
//// for(int i = 0; i < 3; i++){
//// Variable v = vars.get(i);
//// assertEquals(0, v.getModifier());
//// assertEquals("int", v.getType());
//// assertEquals((char)('i'+i), v.getName().charAt(0));
//// assertNull(v.getValue());
//// }
// }catch(Exception e){
// fail(e);
// }
// }
//
// @Test
// void case10(){
// try {
// Variable variable = new Variable();
// variable.parse(cleaner.clean(" boolean i = j << 3==0 ; "), cleaner);
//
// assertEquals(0, variable.getModifier());
// assertEquals("boolean", variable.getType());
// assertEquals("i", variable.getName());
// assertEquals("j<< 3 == 0", ((Value)variable.getValue()).value());
// }catch(Exception e){
// fail(e);
// }
// }
//
// @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(1, vars.size());
// Variable variable;
// variable = vars.get(0);
// assertTrue(variable instanceof MultipleDeclaratedVariable);
// 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);
// }
// }
//
// @Test
// void case15(){
// try {
// Variable variable = new Variable();
// variable.parse(cleaner.clean(" List <String> list = new ArrayList <> () ; "), cleaner);
//
// assertEquals(0, variable.getModifier());
// assertEquals("List<String>", variable.getType());
// assertEquals("list", variable.getName());
// assertEquals("new ArrayList<>()", ((Value)variable.getValue()).value());
// }catch(Exception e){
// fail(e);
// }
// }
//
// @Test
// void case16(){
// try {
// Variable variable = new Variable();
// variable.parse(cleaner.clean(" List <String> a [] = new ArrayList [ 0] ; "), cleaner);
//
// assertEquals(0, variable.getModifier());
// assertEquals("List<String>", variable.getType());
// assertEquals("a[]", variable.getName());
// assertEquals("new ArrayList[0]", ((Value)variable.getValue()).value());
// }catch(Exception e){
// fail(e);
// }
// }
} }