Compare commits
6 commits
3a2e39f223
...
6909ca513c
Author | SHA1 | Date | |
---|---|---|---|
6909ca513c | |||
a937f119ae | |||
da87230afc | |||
0afdb0b76e | |||
8c90fc60b9 | |||
a3c96aecc0 |
16 changed files with 289 additions and 14 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.
|
@ -3,6 +3,7 @@ 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;
|
||||
|
||||
|
@ -107,4 +108,31 @@ 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,4 +103,5 @@ public class Function extends OperationContainer{
|
|||
for(JavaElement child : getChilds()) child.show(tab+1);
|
||||
System.out.println(start+"}");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
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,6 +8,8 @@ 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;
|
||||
|
||||
|
@ -109,6 +111,14 @@ 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;
|
||||
}
|
||||
|
@ -147,4 +157,4 @@ public class JavaParser{
|
|||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ 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;
|
||||
|
||||
|
@ -131,6 +132,27 @@ 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,5 +1,7 @@
|
|||
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;
|
||||
|
||||
|
@ -33,5 +35,24 @@ 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,7 +5,6 @@ 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,5 +1,7 @@
|
|||
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,6 +30,17 @@ public class LoopAffectOperation extends JavaElement{
|
|||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
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{
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
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;
|
||||
|
||||
|
@ -25,6 +27,10 @@ public class MethodCallOperation extends JavaElement{
|
|||
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
||||
public String getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab){
|
||||
|
@ -33,5 +39,14 @@ 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,6 +2,7 @@ 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;
|
||||
|
@ -21,15 +22,12 @@ 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();
|
||||
}
|
||||
|
@ -68,4 +66,31 @@ 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,5 +1,7 @@
|
|||
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;
|
||||
|
||||
|
@ -46,5 +48,28 @@ 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
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(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