10: Create ParentNodeFilter
This commit is contained in:
@@ -2,6 +2,7 @@ package com.bartek.esa.core.di;
|
||||
|
||||
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||
import com.bartek.esa.core.executor.PluginExecutor;
|
||||
import com.bartek.esa.core.helper.ParentNodeFinder;
|
||||
import com.bartek.esa.core.helper.StaticScopeHelper;
|
||||
import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
@@ -35,4 +36,9 @@ public class CoreModule {
|
||||
public StaticScopeHelper staticScopeHelper() {
|
||||
return new StaticScopeHelper();
|
||||
}
|
||||
|
||||
@Provides
|
||||
public ParentNodeFinder parentNodeFinder() {
|
||||
return new ParentNodeFinder();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.bartek.esa.core.helper;
|
||||
|
||||
|
||||
import com.github.javaparser.ast.Node;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ParentNodeFinder {
|
||||
|
||||
@Inject
|
||||
public ParentNodeFinder() {
|
||||
|
||||
}
|
||||
|
||||
public <T extends Node> Optional<T> findParentNode(Node child, Class<T> nodeType) {
|
||||
Node parent = child.getParentNode().orElse(null);
|
||||
|
||||
while(parent != null && !parent.getClass().equals(nodeType)) {
|
||||
parent = parent.getParentNode().orElse(null);
|
||||
}
|
||||
|
||||
return Optional.ofNullable(parent).map(nodeType::cast);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user