Util -> Find method
This commit is contained in:
parent
3a2e39f223
commit
a3c96aecc0
11 changed files with 125 additions and 6 deletions
|
@ -107,4 +107,15 @@ public class Class extends JavaElement{
|
|||
}
|
||||
System.out.println(start+"}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
for(JavaElement element : this.childs){
|
||||
JavaElement result = element.find(search, deep);
|
||||
if(result != null) return result;
|
||||
}
|
||||
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,13 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public abstract class JavaElement {
|
||||
|
||||
public abstract int parse(String content, CleanerPool global, CleanerPool local) throws Exception;
|
||||
|
||||
public abstract JavaElement find(Function<JavaElement, Boolean> search, Function<JavaElement, Boolean> deep);
|
||||
|
||||
//Only for development
|
||||
public abstract void show(int tab);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.io.FileWriter;
|
|||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
||||
|
||||
|
@ -109,6 +110,10 @@ public class JavaParser{
|
|||
content = content.substring(index);
|
||||
}
|
||||
|
||||
public JavaElement find(Function<JavaElement, Boolean> search, Function<JavaElement, Boolean> deep){
|
||||
return this.clazz.find(search, deep);
|
||||
}
|
||||
|
||||
public Package getPackage(){
|
||||
return this.pack;
|
||||
}
|
||||
|
@ -147,4 +152,4 @@ public class JavaParser{
|
|||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -131,6 +131,14 @@ public class Variable extends JavaElement{
|
|||
System.out.println(start+Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
//Value of the variable
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Value extends Variable{
|
||||
|
||||
private String value;
|
||||
|
|
|
@ -33,5 +33,11 @@ public class AssigmentOperation extends JavaElement{
|
|||
System.out.println(start+variable+" = "+value+";");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
//value
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,11 @@ public class LoopAffectOperation extends JavaElement{
|
|||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+"loop affect??;");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
return search.apply(this) ? this : null;
|
||||
}
|
||||
|
||||
public static class ContinueOperation extends LoopAffectOperation{
|
||||
|
||||
|
|
|
@ -33,5 +33,8 @@ public class MethodCallOperation extends JavaElement{
|
|||
System.out.println(start+value+";");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
return search.apply(this) ? this : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,15 +21,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 +65,15 @@ public abstract class OperationContainer extends JavaElement{
|
|||
public List<JavaElement> getChilds(){
|
||||
return this.childs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
for(JavaElement element : this.childs){
|
||||
JavaElement result = element.find(search, deep);
|
||||
if(result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,5 +46,14 @@ public class ReturnOperation extends JavaElement{
|
|||
}
|
||||
System.out.println(start+"return "+toChange+";");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
JavaElement result = value.find(search, deep);
|
||||
if(result != null) return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
59
test/be/jeffcheasey88/peeratcode/parser/java/SearchTest.java
Normal file
59
test/be/jeffcheasey88/peeratcode/parser/java/SearchTest.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
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 javax.lang.model.element.Element;
|
||||
|
||||
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 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,
|
||||
(e) -> true);
|
||||
|
||||
assertNotNull(element);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue