Compare commits
No commits in common. "6909ca513c5ee2aaf202e2094dddec7e7718d268" and "3a2e39f223368420ef717069b1dca4c4d9e2e37d" have entirely different histories.
6909ca513c
...
3a2e39f223
16 changed files with 14 additions and 289 deletions
|
@ -1,7 +1,6 @@
|
|||
<?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"/>
|
||||
|
@ -10,6 +9,11 @@
|
|||
<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/
|
||||
.generated/*
|
||||
.apt_generated/*
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.apt.aptEnabled=true
|
||||
org.eclipse.jdt.apt.genSrcDir=.generated
|
||||
org.eclipse.jdt.apt.genTestSrcDir=.generated_tests
|
||||
org.eclipse.jdt.apt.genSrcDir=.apt_generated
|
||||
org.eclipse.jdt.apt.genTestSrcDir=.apt_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.
|
@ -3,7 +3,6 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
|||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -108,31 +107,4 @@ public class Class extends JavaElement{
|
|||
}
|
||||
System.out.println(start+"}");
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,5 +103,4 @@ public class Function extends OperationContainer{
|
|||
for(JavaElement child : getChilds()) child.show(tab+1);
|
||||
System.out.println(start+"}");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
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);
|
||||
|
||||
//Only for development
|
||||
public abstract void show(int tab);
|
||||
}
|
||||
|
|
|
@ -8,8 +8,6 @@ import java.io.FileWriter;
|
|||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
||||
|
||||
|
@ -111,14 +109,6 @@ public class JavaParser{
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ 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;
|
||||
|
||||
|
@ -132,27 +131,6 @@ public class Variable extends JavaElement{
|
|||
System.out.println(start+Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
|
||||
}
|
||||
|
||||
@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{
|
||||
|
||||
private String value;
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
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;
|
||||
|
||||
|
@ -35,24 +33,5 @@ public class AssigmentOperation extends JavaElement{
|
|||
System.out.println(start+variable+" = "+value+";");
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
||||
|
||||
public class ElseOperation extends OperationContainer{
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
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;
|
||||
|
||||
|
@ -31,17 +29,6 @@ public class LoopAffectOperation extends JavaElement{
|
|||
System.out.println(start+"loop affect??;");
|
||||
}
|
||||
|
||||
@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*;).*$");
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
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;
|
||||
|
||||
|
@ -28,10 +26,6 @@ public class MethodCallOperation extends JavaElement{
|
|||
return matcher.group(1).length();
|
||||
}
|
||||
|
||||
public String getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
|
@ -39,14 +33,5 @@ public class MethodCallOperation extends JavaElement{
|
|||
System.out.println(start+value+";");
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ 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;
|
||||
|
@ -22,12 +21,15 @@ public abstract class OperationContainer extends JavaElement{
|
|||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("OperationContainer.parse -> "+content);
|
||||
// content = local.unzipOne(content, (s,p) -> s);
|
||||
|
||||
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);
|
||||
// content = local.unzip(content, (s,p) -> s);
|
||||
String modify = internalParse(content, global, local);
|
||||
return content.length()-modify.length();
|
||||
}
|
||||
|
@ -66,31 +68,4 @@ public abstract class OperationContainer extends JavaElement{
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
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;
|
||||
|
||||
|
@ -49,27 +47,4 @@ public class ReturnOperation extends JavaElement{
|
|||
System.out.println(start+"return "+toChange+";");
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,113 +0,0 @@
|
|||
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(reader);
|
||||
parser.parse();
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue