Compare commits
50 commits
Author | SHA1 | Date | |
---|---|---|---|
6975d6dad0 | |||
af3481fd4d | |||
cc6142562b | |||
01107c788f | |||
31fbc33fa6 | |||
a77404952f | |||
b82e91fefa | |||
9b41348588 | |||
97aa0ac391 | |||
0159f9c5fc | |||
6909ca513c | |||
a937f119ae | |||
da87230afc | |||
0afdb0b76e | |||
8c90fc60b9 | |||
a3c96aecc0 | |||
3a2e39f223 | |||
6f8a1b4ceb | |||
eba6a051fd | |||
89fd0553a2 | |||
ab03e73f58 | |||
|
f4ff094554 | ||
|
c7b9454ede | ||
|
ad430cb659 | ||
|
be289b8d7f | ||
|
800ad9a4e9 | ||
|
b800b88fb4 | ||
|
0c98fe6e2e | ||
|
9a19c54e34 | ||
|
8f37f9882b | ||
|
a231434ecd | ||
|
56ce2b7235 | ||
|
77f075abdc | ||
|
86e326db87 | ||
|
bd2cf6c26a | ||
|
784a51e75c | ||
|
4b1b83930e | ||
|
0111df23f5 | ||
|
5e3208c262 | ||
|
57132f320a | ||
|
5754720fe5 | ||
|
8941eac25c | ||
|
e6ad2822eb | ||
|
38f22e48b0 | ||
|
b0da444be1 | ||
|
3c17c823fe | ||
|
5d27475a9b | ||
|
813ebda6cb | ||
|
d2fd5aeff1 | ||
|
4befbd7147 |
33 changed files with 2376 additions and 408 deletions
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<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="test"/>
|
||||
<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="jose4j-0.9.3.jar"/>
|
||||
<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 exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,4 +4,4 @@ bin/
|
|||
config.txt
|
||||
dist/
|
||||
testApi/
|
||||
.apt_generated/*
|
||||
.generated/*
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.apt.aptEnabled=true
|
||||
org.eclipse.jdt.apt.genSrcDir=.apt_generated
|
||||
org.eclipse.jdt.apt.genTestSrcDir=.apt_generated_tests
|
||||
org.eclipse.jdt.apt.genSrcDir=.generated
|
||||
org.eclipse.jdt.apt.genTestSrcDir=.generated_tests
|
||||
org.eclipse.jdt.apt.processorOptions/treasure.source=%sourcepath%
|
||||
org.eclipse.jdt.apt.reconcileEnabled=true
|
||||
|
|
BIN
Treasure.jar
BIN
Treasure.jar
Binary file not shown.
|
@ -83,6 +83,8 @@ public class HttpWriter{
|
|||
NINEBITS[113] = " Request Entity Too Large";
|
||||
NINEBITS[114] = " Request-URI Too Large";
|
||||
NINEBITS[115] = " Unsupported Media Type";
|
||||
NINEBITS[123] = " Locked";
|
||||
NINEBITS[125] = " Too Early";
|
||||
|
||||
NINEBITS[200] = " Internal Server Error";
|
||||
NINEBITS[201] = " Not Implemented";
|
||||
|
|
53
src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java
Normal file
53
src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
28
src/be/jeffcheasey88/peeratcode/parser/java/ArrayBuffer.java
Normal file
28
src/be/jeffcheasey88/peeratcode/parser/java/ArrayBuffer.java
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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++];
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -2,85 +2,103 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
|||
|
||||
import java.lang.reflect.Modifier;
|
||||
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.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 List<Annotation> annotations;
|
||||
|
||||
private int modifier;
|
||||
private String name;
|
||||
|
||||
private List<Variable> vars;
|
||||
private List<Function> functions;
|
||||
private List<JavaElement> childs;
|
||||
|
||||
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.matches();
|
||||
|
||||
String[] split = matcher.group(2).split("\\s+");
|
||||
for(int i = 0; i < split.length-1; i++){
|
||||
this.modifier+=JavaParser.getModifier(split[i]);
|
||||
Iterator<String> values = new ArrayIterator<>(matcher.group(2).split("\\s+"));
|
||||
String value = null;
|
||||
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<>();
|
||||
this.functions = new ArrayList<>();
|
||||
String type = value; //class interface enum
|
||||
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);
|
||||
Pattern empty = Pattern.compile("^\\s*$");
|
||||
while(!(empty.matcher(content).matches())){
|
||||
int quotes = indexOf(content,";");
|
||||
int braces = indexOf(content,"\\{");
|
||||
int equals = indexOf(content,"=");
|
||||
if(quotes < braces && quotes < equals){
|
||||
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;
|
||||
}
|
||||
}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);
|
||||
String zip = tmp.clean(content);
|
||||
int quotes = indexOf(zip,";");
|
||||
int braces = indexOf(zip,"\\{");
|
||||
int equals = indexOf(zip,"=");
|
||||
if((quotes < braces && quotes < equals) || (equals < braces)){
|
||||
Variable variable = new Variable();
|
||||
int index = variable.parse(content, global, local);
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -96,15 +114,59 @@ public class Class {
|
|||
return this.name;
|
||||
}
|
||||
|
||||
public List<Variable> getVariables(){
|
||||
return this.vars;
|
||||
public List<JavaElement> getChilds(){
|
||||
return this.childs;
|
||||
}
|
||||
|
||||
public void show(){
|
||||
System.out.println(Modifier.toString(modifier)+" "+this.name+"{");
|
||||
for(Variable v : this.vars) v.show(1);
|
||||
System.out.println();
|
||||
for(Function f : this.functions) f.show(1);
|
||||
System.out.println("}");
|
||||
// public List<Variable> getVariables(){
|
||||
// return this.vars;
|
||||
// }
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,93 +1,174 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
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;
|
||||
|
||||
public class CleanerPool {
|
||||
public class CleanerPool{
|
||||
|
||||
private static String CONSTANT_REPLACER_STRING = "$STRING_STATEMENT_CONSTANT_";
|
||||
private static String CONSTANT_REPLACER_CHAR = "$CHAR_STATEMENT_CONSTANT_";
|
||||
private static String CONSTANT_REPLACER_GENERIC = "$GENERIC_STATEMENT_CONSTANT_";
|
||||
private List<Cleaner> cleaners;
|
||||
|
||||
private List<String> constants;
|
||||
|
||||
private CleanerPool(){
|
||||
this.constants = new ArrayList<>();
|
||||
public CleanerPool(Cleaner... cleaners){
|
||||
this.cleaners = new ArrayList<>(Arrays.asList(cleaners));
|
||||
}
|
||||
|
||||
public String clean(String statement){
|
||||
char[] chars = statement.toCharArray();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for(int i = 0; i < chars.length; i++){
|
||||
char current = chars[i];
|
||||
if(current== '"'){
|
||||
int constantPos = this.constants.size();
|
||||
String constant = cutConstant(chars, i);
|
||||
i+=constant.length()+1;
|
||||
builder.append(CONSTANT_REPLACER_STRING+constantPos);
|
||||
this.constants.add(constant);
|
||||
}else{
|
||||
builder.append(current);
|
||||
public CleanerPool group(CleanerPool... pools){
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
for(String s : constants){
|
||||
System.out.println("CONSTANT="+s);
|
||||
}
|
||||
return builder.toString();
|
||||
return pool;
|
||||
}
|
||||
|
||||
public boolean isConstant(String region){
|
||||
return region.startsWith(CONSTANT_REPLACER_STRING);
|
||||
@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 getConstant(String replacer){
|
||||
if(!replacer.startsWith(CONSTANT_REPLACER_STRING)) return null;
|
||||
return this.constants.get(Integer.parseInt(replacer.replace(CONSTANT_REPLACER_STRING,"")));
|
||||
public String clean(String value){
|
||||
for(Cleaner cleaner : this.cleaners) value = cleaner.clean(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public List<String> getConstants(){
|
||||
return this.constants;
|
||||
}
|
||||
|
||||
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);
|
||||
public String unzipOne(String value, BiFunction<String, String, String> modifier){
|
||||
boolean edited;
|
||||
String tmp = value;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
do{
|
||||
edited = false;
|
||||
for(Cleaner cleaner : this.cleaners){
|
||||
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}");
|
||||
}
|
||||
return builder.toString();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static interface Cutter{
|
||||
public String unzip(String value, BiFunction<String, String, String> modifier){
|
||||
boolean edited;
|
||||
do{
|
||||
edited = false;
|
||||
for(Cleaner cleaner : this.cleaners){
|
||||
Matcher matcher = cleaner.getMatcher(value);
|
||||
if(matcher.matches()){
|
||||
String key = matcher.group(2);
|
||||
String zip = cleaner.getConstant(key);
|
||||
String modified = modifier.apply(zip, cleaner.getPattern());
|
||||
if(modified == null) continue;
|
||||
value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
|
||||
edited = true;
|
||||
}
|
||||
}
|
||||
}while(edited);
|
||||
return value;
|
||||
}
|
||||
|
||||
String execute(String value, List<String> constants, String pattern);
|
||||
public static class Cleaner{
|
||||
|
||||
default String cutOpenable(String value, List<String> constants, String pattern, char open, char close){
|
||||
private Pattern rPattern;
|
||||
|
||||
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();
|
||||
|
||||
for(char c : value.toCharArray()){
|
||||
for(int i = 0; i < value.length(); i++){
|
||||
char c = value.charAt(i);
|
||||
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();
|
||||
}
|
||||
|
||||
}else if(c == close){
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,84 +2,159 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
|||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
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 ANNOTATION_PATTERN = Pattern.compile("^(\\s*(@\\s*[^\\s]+\\s*\\^GENERIC_PARENTHESIS\\d+\\s+))");
|
||||
|
||||
private List<Annotation> annotations;
|
||||
private int modifier;
|
||||
private String returnType;
|
||||
private String name;
|
||||
private List<Variable> parameters;
|
||||
private String exceptions;
|
||||
private String parameters;
|
||||
|
||||
private List<Function> functions;
|
||||
private List<Operation> operations;
|
||||
private boolean constructor;
|
||||
|
||||
public Function(){
|
||||
this.functions = new ArrayList<>();
|
||||
this.operations = new ArrayList<>();
|
||||
this.annotations = new ArrayList<>();
|
||||
this.parameters = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int parse(String content) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
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();
|
||||
|
||||
String[] split = matcher.group(2).split("\\s+");
|
||||
for(int i = 0; i < split.length-2; i++){
|
||||
this.modifier+=JavaParser.getModifier(split[i]);
|
||||
}
|
||||
this.name = split[split.length-1];
|
||||
this.parameters = matcher.group(3);
|
||||
this.exceptions = matcher.group(4);
|
||||
attribute(matcher.group(2));
|
||||
parameters(matcher.group(3)+";", global, local);
|
||||
this.exceptions = matcher.group(4).trim();
|
||||
|
||||
String body = matcher.group(5);
|
||||
int offset = 0;
|
||||
int index = 0;
|
||||
do {
|
||||
int end = body.indexOf('}');
|
||||
int braces = body.indexOf('{');
|
||||
int quotes = body.indexOf(';');
|
||||
local = local.group(new CleanerPool(new Cleaner("GENERIC_FUNCTION", '{', '}')));
|
||||
|
||||
if((end < 0) || (end < braces && end < quotes)){
|
||||
if(end > 0) offset+=end;
|
||||
break;
|
||||
}
|
||||
|
||||
if(braces < 0 && quotes < 0){
|
||||
if(end > 0) offset+=end;
|
||||
break;
|
||||
}
|
||||
String zip = local.clean("{"+matcher.group(5));
|
||||
String body = local.unzipOne(zip, (s,p) -> s);
|
||||
String unzip = body.substring(1, body.indexOf('}'));
|
||||
|
||||
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;
|
||||
super.parse(local.clean(unzip), global, local);
|
||||
|
||||
return annotationOffset+local.unzip(matcher.group(1), (s,p) -> s).length()+local.unzip(unzip, ((s,p) -> s)).length()+1;
|
||||
}
|
||||
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+Modifier.toString(modifier)+" "+name+"("+parameters+") "+exceptions+" {");
|
||||
for(Operation o : this.operations) o.show(tab+1);
|
||||
System.out.println();
|
||||
for(Function f : this.functions) f.show(tab+1);
|
||||
System.out.println(start+"}");
|
||||
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 void attribute(String content) throws Exception{
|
||||
CleanerPool generic = new CleanerPool(
|
||||
new Cleaner("GENERIC_TYPE_",'<','>'),
|
||||
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
|
||||
public String toString(){
|
||||
return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]";
|
||||
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
|
||||
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("");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import java.util.regex.Pattern;
|
|||
|
||||
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){
|
||||
return PATTERN.matcher(content).matches();
|
||||
return PATTERN.matcher(content).lookingAt();
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
@ -17,11 +17,13 @@ public class Import {
|
|||
|
||||
public int parse(String content) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
this.name = matcher.group(2);
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
||||
// public int doYouSeeMe(){ return false; }
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
|
20
src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java
Normal file
20
src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java
Normal 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);
|
||||
}
|
||||
}
|
|
@ -1,42 +1,103 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
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 {
|
||||
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));
|
||||
|
||||
JavaParser parser = new JavaParser(reader);
|
||||
parser.parse();
|
||||
JavaParser parser = new JavaParser();
|
||||
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-----------------");
|
||||
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 List<Import> imports;
|
||||
private Class clazz;
|
||||
|
||||
private BufferedReader reader;
|
||||
//later, maybe put string in the element
|
||||
private CleanerPool cleaner;
|
||||
|
||||
public JavaParser(BufferedReader reader){
|
||||
this.reader = reader;
|
||||
}
|
||||
public JavaParser(){}
|
||||
|
||||
public void parse() throws Exception{
|
||||
public void parse(BufferedReader reader) throws Exception{
|
||||
String content = "";
|
||||
int index;
|
||||
|
||||
String line;
|
||||
while((line = reader.readLine()) != null) content+=line;
|
||||
cleaner = new CleanerPool(
|
||||
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();
|
||||
index = this.pack.parse(content);
|
||||
|
@ -51,10 +112,18 @@ public class JavaParser {
|
|||
}
|
||||
|
||||
this.clazz = new Class();
|
||||
index = this.clazz.parse(content);
|
||||
index = this.clazz.parse(content, cleaner, null);
|
||||
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(){
|
||||
return this.pack;
|
||||
}
|
||||
|
@ -67,14 +136,28 @@ public class JavaParser {
|
|||
return this.clazz;
|
||||
}
|
||||
|
||||
public void show(){
|
||||
System.out.println("package "+this.pack.getName()+";");
|
||||
System.out.println();
|
||||
for(Import i : this.imports) System.out.println("import "+i.getName()+";");
|
||||
System.out.println();
|
||||
this.clazz.show();
|
||||
public void build(BufferedWriter writer) throws Exception{
|
||||
writer.write("package "+this.pack.getName()+";\n");
|
||||
writer.write("\n");
|
||||
for(Import element : this.imports) writer.write("import "+element.getName()+";\n");
|
||||
writer.write("\n");
|
||||
|
||||
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){
|
||||
switch(modifier){
|
||||
case "public": return Modifier.PUBLIC;
|
||||
|
|
|
@ -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+";");
|
||||
}
|
||||
|
||||
}
|
|
@ -5,7 +5,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
public class Package {
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);).*$");
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);)");
|
||||
|
||||
private String name;
|
||||
|
||||
|
@ -13,7 +13,7 @@ public class Package {
|
|||
|
||||
public int parse(String content) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
this.name = matcher.group(2);
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
|
|
@ -1,27 +1,46 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
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.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 List<Annotation> annotations;
|
||||
|
||||
private int modifier;
|
||||
private String name;
|
||||
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){
|
||||
this();
|
||||
this.modifier = modifier;
|
||||
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.matches();
|
||||
|
||||
|
@ -35,50 +54,74 @@ public class Variable {
|
|||
body = body.substring(0, min);
|
||||
|
||||
if(equals < quote && equals < quotes){
|
||||
assigment(body);
|
||||
assigment(body, local);
|
||||
}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){
|
||||
content = generiqueTypes(content);
|
||||
System.out.println(content);
|
||||
String[] values = content.split("\\s+");
|
||||
for(String value : values){
|
||||
int modifier = JavaParser.getModifier(value);
|
||||
if(modifier > 0){
|
||||
private Iterator<String> onlyDefine(String content, CleanerPool cleaner) throws Exception{
|
||||
content = generiqueTypes(content, cleaner);
|
||||
|
||||
CleanerPool tmp = new CleanerPool(new Cleaner("GENERIC_PARENTHESIS",'(',')'));
|
||||
content = tmp.clean(content);
|
||||
|
||||
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;
|
||||
continue;
|
||||
}
|
||||
if(this.type == null){
|
||||
this.type = value;
|
||||
continue;
|
||||
}
|
||||
}while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
|
||||
}
|
||||
if(this.type == null){
|
||||
this.type = value;
|
||||
if(values.hasNext()) value = values.next();
|
||||
}
|
||||
if(this.name == null){
|
||||
this.name = value;
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
private String generiqueTypes(String content){
|
||||
System.out.println(content);
|
||||
String result = "";
|
||||
int opened = 0;
|
||||
for(char c : content.toCharArray()){
|
||||
if(c == '<') opened++;
|
||||
else if(c == '>') opened--;
|
||||
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])");
|
||||
|
||||
if(opened > 0){
|
||||
if(Character.isWhitespace(c)) continue;
|
||||
}
|
||||
result+=c;
|
||||
}
|
||||
result = result.replaceAll("(>\\s*)", "> ").replace("> >", ">>");
|
||||
return result;
|
||||
private String generiqueTypes(String content, CleanerPool cleaner){
|
||||
String unzip = cleaner.unzip(content, (value, pattern) -> {
|
||||
if(pattern.equals("^GENERIC_FUNCTION")) return null;
|
||||
if(pattern.equals("^GENERIC_PARENTHESIS")) return value.replace("\\s+", " ");
|
||||
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;
|
||||
}
|
||||
|
||||
private int indexOf(String value, String target){
|
||||
|
@ -101,10 +144,40 @@ public class Variable {
|
|||
return this.value;
|
||||
}
|
||||
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":"="+value+";"));
|
||||
public List<Annotation> getAnnotations(){
|
||||
return this.annotations;
|
||||
}
|
||||
|
||||
@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{
|
||||
|
@ -124,4 +197,39 @@ public class Variable {
|
|||
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("");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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("");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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("");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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("");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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("");
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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("");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
151
test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java
Normal file
151
test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
113
test/be/jeffcheasey88/peeratcode/parser/java/SearchTest.java
Normal file
113
test/be/jeffcheasey88/peeratcode/parser/java/SearchTest.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,16 +1,22 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
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.fail;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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;
|
||||
|
||||
class VariableTest {
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
class VariableTest{
|
||||
|
||||
//int i = 4;
|
||||
//int i,j,k,l=1;
|
||||
|
@ -20,123 +26,295 @@ class VariableTest {
|
|||
//Test<Test,K,L> j = new Test().schedule(p -> { return true;});
|
||||
//int i =j=k=l=4;
|
||||
|
||||
@Test
|
||||
void case1(){
|
||||
try {
|
||||
Variable variable = new Variable();
|
||||
variable.parse(" int i = 4 ; ");
|
||||
|
||||
assertEquals(0, variable.getModifier());
|
||||
assertEquals("int", variable.getType());
|
||||
assertEquals("i", variable.getName());
|
||||
assertEquals("4", ((Value)variable.getValue()).value());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case2(){
|
||||
try {
|
||||
Variable variable = new Variable();
|
||||
variable.parse("public static int l ; ");
|
||||
|
||||
assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier());
|
||||
assertEquals("int", variable.getType());
|
||||
assertEquals("l", variable.getName());
|
||||
assertNull(variable.getValue());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case3(){
|
||||
try {
|
||||
Variable variable = new Variable();
|
||||
variable.parse(" int lm ; ");
|
||||
|
||||
assertEquals(0, variable.getModifier());
|
||||
assertEquals("int", variable.getType());
|
||||
assertEquals("lm", variable.getName());
|
||||
assertNull(variable.getValue());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case4(){
|
||||
try {
|
||||
Variable variable = new Variable();
|
||||
variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; ");
|
||||
|
||||
assertEquals(0, variable.getModifier());
|
||||
assertEquals("Testas<List<Map<Test,List<Test>,Test>>>", variable.getType());
|
||||
assertEquals("t", variable.getName());
|
||||
assertNull(variable.getValue());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case5(){
|
||||
try {
|
||||
Variable variable = new Variable();
|
||||
variable.parse(" int i,j,k,l=1; ");
|
||||
|
||||
assertEquals(0, variable.getModifier());
|
||||
assertEquals("int", variable.getType());
|
||||
assertEquals("i", variable.getName());
|
||||
assertNull(variable.getValue());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case6(){
|
||||
try {
|
||||
Class clazz = new Class();
|
||||
clazz.parse("public class Test{ int i ,j,k,l=1; } ");
|
||||
|
||||
List<Variable> vars = clazz.getVariables();
|
||||
assertEquals(vars.size(), 4);
|
||||
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());
|
||||
}
|
||||
Variable v = vars.get(3);
|
||||
assertEquals(0, v.getModifier());
|
||||
assertEquals("int", v.getType());
|
||||
assertEquals('l', v.getName().charAt(0));
|
||||
assertEquals("1", ((Value)v.getValue()).value());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case7(){
|
||||
try {
|
||||
Class clazz = new Class();
|
||||
clazz.parse("public class Test{ int i ,j,k; int l=i=k=l=4; } ");
|
||||
|
||||
List<Variable> vars = clazz.getVariables();
|
||||
assertEquals(vars.size(), 4);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// private CleanerPool cleaner;
|
||||
//
|
||||
// @BeforeAll
|
||||
// void init(){
|
||||
// this.cleaner = new CleanerPool();
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void case1(){
|
||||
// try {
|
||||
// Variable variable = new Variable();
|
||||
// variable.parse(cleaner.clean(" int i = 4 ; "), cleaner);
|
||||
//
|
||||
// assertEquals(0, variable.getModifier());
|
||||
// assertEquals("int", variable.getType());
|
||||
// assertEquals("i", variable.getName());
|
||||
// assertEquals("4", ((Value)variable.getValue()).value());
|
||||
// }catch(Exception e){
|
||||
// fail(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void case2(){
|
||||
// try {
|
||||
// Variable variable = new Variable();
|
||||
// variable.parse(cleaner.clean("public static int l ; "), cleaner);
|
||||
//
|
||||
// assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier());
|
||||
// assertEquals("int", variable.getType());
|
||||
// assertEquals("l", variable.getName());
|
||||
// assertNull(variable.getValue());
|
||||
// }catch(Exception e){
|
||||
// fail(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void case3(){
|
||||
// try {
|
||||
// Variable variable = new Variable();
|
||||
// variable.parse(cleaner.clean(" int lm ; "), cleaner);
|
||||
//
|
||||
// assertEquals(0, variable.getModifier());
|
||||
// assertEquals("int", variable.getType());
|
||||
// assertEquals("lm", variable.getName());
|
||||
// assertNull(variable.getValue());
|
||||
// }catch(Exception e){
|
||||
// fail(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void case4(){
|
||||
// try {
|
||||
// Variable variable = new Variable();
|
||||
// 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());
|
||||
// assertEquals("t", variable.getName());
|
||||
// assertNull(variable.getValue());
|
||||
// }catch(Exception e){
|
||||
// fail(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void case5(){
|
||||
// try {
|
||||
// Variable variable = new Variable();
|
||||
// variable.parse(cleaner.clean(" int i,j,k,l=1; "), cleaner);
|
||||
//
|
||||
// assertEquals(0, variable.getModifier());
|
||||
// assertEquals("int", variable.getType());
|
||||
// assertEquals("i", variable.getName());
|
||||
// assertNull(variable.getValue());
|
||||
// }catch(Exception e){
|
||||
// fail(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void case6(){
|
||||
// try {
|
||||
// Class clazz = new Class();
|
||||
// clazz.parse(cleaner.clean("public class Test{ int i ,j,k,l=1; } "), 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());
|
||||
//// }
|
||||
//// Variable v = vars.get(3);
|
||||
//// assertEquals(0, v.getModifier());
|
||||
//// assertEquals("int", v.getType());
|
||||
//// assertEquals('l', v.getName().charAt(0));
|
||||
//// assertEquals("1", ((Value)v.getValue()).value());
|
||||
// }catch(Exception e){
|
||||
// fail(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void case7(){
|
||||
// try {
|
||||
// Class clazz = new Class();
|
||||
// clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool());
|
||||
//
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue