spoon.reflect.path.impl.CtPathImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spoon-core Show documentation
Show all versions of spoon-core Show documentation
Spoon is a tool for meta-programming, analysis and transformation of Java programs.
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2023 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.reflect.path.impl;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.path.CtPath;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* Default implementation for a CtPath
*/
public class CtPathImpl implements CtPath {
private LinkedList elements = new LinkedList<>();
public List getElements() {
return elements;
}
@Override
public List evaluateOn(CtElement... startNode) {
Collection filtered = Arrays.asList(startNode);
for (CtPathElement element : elements) {
filtered = element.getElements(filtered);
}
return (List) filtered;
}
@Override
public CtPath relativePath(CtElement parent) {
List roots = new ArrayList<>();
roots.add(parent);
int index = 0;
for (CtPathElement pathEl : getElements()) {
if (pathEl.getElements(roots).size() > 0) {
break;
}
index++;
}
CtPathImpl result = new CtPathImpl();
result.elements = new LinkedList<>(elements.subList(index, elements.size()));
return result;
}
public CtPathImpl addFirst(CtPathElement element) {
elements.addFirst(element);
return this;
}
public CtPathImpl addLast(CtPathElement element) {
elements.addLast(element);
return this;
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
for (CtPathElement element : elements) {
str.append(element.toString());
}
return str.toString();
}
}