use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class LexicalPreservingPrinter method interpret.
private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) {
LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(csm, node);
List<TokenTextElement> indentation = findIndentation(node);
boolean pendingIndentation = false;
for (CsmElement element : calculatedSyntaxModel.elements) {
if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) {
indentation.forEach(nodeText::addElement);
}
pendingIndentation = false;
if (element instanceof LexicalDifferenceCalculator.CsmChild) {
nodeText.addChild(((LexicalDifferenceCalculator.CsmChild) element).getChild());
} else if (element instanceof CsmToken) {
CsmToken csmToken = (CsmToken) element;
nodeText.addToken(csmToken.getTokenType(), csmToken.getContent(node));
if (csmToken.isNewLine()) {
pendingIndentation = true;
}
} else if (element instanceof CsmMix) {
CsmMix csmMix = (CsmMix) element;
csmMix.getElements().forEach(e -> interpret(node, e, nodeText));
} else {
throw new UnsupportedOperationException(element.getClass().getSimpleName());
}
}
// so they have to be handled in a special way
if (node instanceof VariableDeclarator) {
VariableDeclarator variableDeclarator = (VariableDeclarator) node;
variableDeclarator.getParentNode().ifPresent(parent -> ((NodeWithVariables<?>) parent).getMaximumCommonType().ifPresent(mct -> {
int extraArrayLevels = variableDeclarator.getType().getArrayLevel() - mct.getArrayLevel();
for (int i = 0; i < extraArrayLevels; i++) {
nodeText.addElement(new TokenTextElement(LBRACKET));
nodeText.addElement(new TokenTextElement(RBRACKET));
}
}));
}
return nodeText;
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class LexicalPreservingPrinter method findNodeListName.
private static ObservableProperty findNodeListName(NodeList nodeList) {
Node parent = nodeList.getParentNodeForChildren();
for (Method m : parent.getClass().getMethods()) {
if (m.getParameterCount() == 0 && m.getReturnType().getCanonicalName().equals(NodeList.class.getCanonicalName())) {
try {
Object raw = m.invoke(parent);
if (!(raw instanceof NodeList)) {
throw new IllegalStateException("Expected NodeList, found " + raw.getClass().getCanonicalName());
}
NodeList result = (NodeList) raw;
if (result == nodeList) {
String name = m.getName();
if (name.startsWith("get")) {
name = name.substring("get".length());
}
return ObservableProperty.fromCamelCaseName(decapitalize(name));
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else if (m.getParameterCount() == 0 && isReturningOptionalNodeList(m)) {
try {
Optional<NodeList<?>> raw = (Optional<NodeList<?>>) m.invoke(parent);
if (raw.isPresent() && raw.get() == nodeList) {
String name = m.getName();
if (name.startsWith("get")) {
name = name.substring("get".length());
}
return ObservableProperty.fromCamelCaseName(decapitalize(name));
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
throw new IllegalArgumentException("Cannot find list name of NodeList of size " + nodeList.size());
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class Navigator method findType.
public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, String qualifiedName) {
final String typeName = getOuterTypeName(qualifiedName);
Optional<TypeDeclaration<?>> type = Optional.empty();
for (Node n : td.getMembers()) {
if (n instanceof TypeDeclaration && ((TypeDeclaration<?>) n).getName().getId().equals(typeName)) {
type = Optional.of((TypeDeclaration<?>) n);
break;
}
}
final String innerTypeName = getInnerTypeName(qualifiedName);
if (type.isPresent() && !innerTypeName.isEmpty()) {
return findType(type.get(), innerTypeName);
}
return type;
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class SourceFileInfoExtractor method solve.
public void solve(File file) throws IOException, ParseException {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
solve(f);
}
} else {
if (file.getName().endsWith(".java")) {
if (printFileName) {
out.println("- parsing " + file.getAbsolutePath());
}
CompilationUnit cu = JavaParser.parse(file);
List<Node> nodes = collectAllNodes(cu);
nodes.forEach(n -> solve(n));
}
}
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class LexicalDifferenceCalculator method calculateListReplacementDifference.
Difference calculateListReplacementDifference(ObservableProperty observableProperty, NodeList nodeList, int index, Node newValue) {
Node container = nodeList.getParentNodeForChildren();
CsmElement element = ConcreteSyntaxModel.forClass(container.getClass());
CalculatedSyntaxModel original = calculatedSyntaxModelForNode(element, container);
CalculatedSyntaxModel after = calculatedSyntaxModelAfterListReplacement(element, observableProperty, nodeList, index, newValue);
return Difference.calculate(original, after);
}
Aggregations