Util -> Find update deep with trace
This commit is contained in:
parent
a3c96aecc0
commit
8c90fc60b9
10 changed files with 57 additions and 19 deletions
|
@ -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.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -109,11 +110,12 @@ public class Class extends JavaElement{
|
|||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
trace.add(this);
|
||||
if(!deep.apply(trace)) return null;
|
||||
for(JavaElement element : this.childs){
|
||||
JavaElement result = element.find(search, deep);
|
||||
JavaElement result = element.find(search, deep, trace);
|
||||
if(result != null) return result;
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import java.util.Set;
|
||||
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);
|
||||
public abstract JavaElement find(Function<JavaElement, Boolean> search, Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace);
|
||||
|
||||
//Only for development
|
||||
public abstract void show(int tab);
|
||||
|
|
|
@ -7,7 +7,9 @@ import java.io.FileReader;
|
|||
import java.io.FileWriter;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
||||
|
@ -110,8 +112,8 @@ 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 JavaElement find(Function<JavaElement, Boolean> search, Function<Set<JavaElement>, Boolean> deep){
|
||||
return this.clazz.find(search, deep, new HashSet<>());
|
||||
}
|
||||
|
||||
public Package getPackage(){
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.lang.reflect.Modifier;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -132,9 +133,10 @@ public class Variable extends JavaElement{
|
|||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
trace.add(this);
|
||||
if(!deep.apply(trace)) return null;
|
||||
//Value of the variable
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -34,9 +35,10 @@ public class AssigmentOperation extends JavaElement{
|
|||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
trace.add(this);
|
||||
if(!deep.apply(trace)) return null;
|
||||
//value
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -30,7 +31,7 @@ public class LoopAffectOperation extends JavaElement{
|
|||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace){
|
||||
return search.apply(this) ? this : null;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -25,6 +26,10 @@ public class MethodCallOperation extends JavaElement{
|
|||
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
||||
public String getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab){
|
||||
|
@ -34,7 +39,7 @@ public class MethodCallOperation extends JavaElement{
|
|||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace){
|
||||
return search.apply(this) ? this : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java.operations;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||
|
@ -67,11 +68,12 @@ public abstract class OperationContainer extends JavaElement{
|
|||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
trace.add(this);
|
||||
if(!deep.apply(trace)) return null;
|
||||
for(JavaElement element : this.childs){
|
||||
JavaElement result = element.find(search, deep);
|
||||
JavaElement result = element.find(search, deep, trace);
|
||||
if(result != null) return result;
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -48,10 +49,11 @@ public class ReturnOperation extends JavaElement{
|
|||
}
|
||||
|
||||
@Override
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<JavaElement, Boolean> deep){
|
||||
public JavaElement find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<Set<JavaElement>, Boolean> deep, Set<JavaElement> trace){
|
||||
if(search.apply(this)) return this;
|
||||
if(!deep.apply(this)) return null;
|
||||
JavaElement result = value.find(search, deep);
|
||||
trace.add(this);
|
||||
if(!deep.apply(trace)) return null;
|
||||
JavaElement result = value.find(search, deep, trace);
|
||||
if(result != null) return result;
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -13,12 +13,14 @@ 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.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.OperationContainer;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.SynchronizedOperation;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
|
@ -47,7 +49,7 @@ class SearchTest{
|
|||
|
||||
JavaElement element = parser.find(
|
||||
(e) -> e instanceof MethodCallOperation,
|
||||
(e) -> true);
|
||||
(trace) -> true);
|
||||
|
||||
assertNotNull(element);
|
||||
} catch (Exception e) {
|
||||
|
@ -56,4 +58,21 @@ class SearchTest{
|
|||
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue