use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class JavaParser method attributeLineCommentToNodeOrChild.
private static boolean attributeLineCommentToNodeOrChild(Node node, LineComment lineComment) {
// let's give to it the comment
if (node.getBeginLine() == lineComment.getBeginLine() && !node.hasComment()) {
node.setComment(lineComment);
return true;
} else {
// try with all the children, sorted by reverse position (so the
// first one is the nearest to the comment
List<Node> children = new LinkedList<Node>();
children.addAll(node.getChildrenNodes());
PositionUtils.sortByBeginPosition(children);
Collections.reverse(children);
for (Node child : children) {
if (attributeLineCommentToNodeOrChild(child, lineComment)) {
return true;
}
}
return false;
}
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class CommentsInserter method attributeLineCommentToNodeOrChild.
private boolean attributeLineCommentToNodeOrChild(Node node, LineComment lineComment) {
// let's give to it the comment
if (node.getBegin().line == lineComment.getBegin().line && !node.hasComment()) {
if (!(node instanceof Comment)) {
node.setComment(lineComment);
}
return true;
} else {
// try with all the children, sorted by reverse position (so the
// first one is the nearest to the comment
List<Node> children = new LinkedList<Node>();
children.addAll(node.getChildrenNodes());
PositionUtils.sortByBeginPosition(children);
Collections.reverse(children);
for (Node child : children) {
if (attributeLineCommentToNodeOrChild(child, lineComment)) {
return true;
}
}
return false;
}
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class JavaParserFacade method solveMethodAsUsage.
public MethodUsage solveMethodAsUsage(MethodCallExpr call) {
List<ResolvedType> params = new ArrayList<>();
if (call.getArguments() != null) {
for (Expression param : call.getArguments()) {
// getTypeConcrete(Node node, boolean solveLambdas)
try {
params.add(getType(param, false));
} catch (Exception e) {
throw new RuntimeException(String.format("Error calculating the type of parameter %s of method call %s", param, call), e);
}
// params.add(getTypeConcrete(param, false));
}
}
Context context = JavaParserFactory.getContext(call, typeSolver);
Optional<MethodUsage> methodUsage = context.solveMethodAsUsage(call.getName().getId(), params, typeSolver);
if (!methodUsage.isPresent()) {
throw new RuntimeException("Method '" + call.getName() + "' cannot be resolved in context " + call + " (line: " + call.getRange().map(r -> "" + r.begin.line).orElse("??") + ") " + context + ". Parameter types: " + params);
}
return methodUsage.get();
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class TokenKindGenerator method generate.
@Override
public void generate() {
Log.info("Running %s", getClass().getSimpleName());
final CompilationUnit javaTokenCu = sourceRoot.parse("com.github.javaparser", "JavaToken.java");
final ClassOrInterfaceDeclaration javaToken = javaTokenCu.getClassByName("JavaToken").orElseThrow(() -> new AssertionError("Can't find class in java file."));
final EnumDeclaration kindEnum = javaToken.findFirst(EnumDeclaration.class, e -> e.getNameAsString().equals("Kind")).orElseThrow(() -> new AssertionError("Can't find class in java file."));
kindEnum.getEntries().clear();
annotateGenerated(kindEnum);
final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class).orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
valueOfSwitch.findAll(SwitchEntryStmt.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);
final CompilationUnit constantsCu = generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
final ClassOrInterfaceDeclaration constants = constantsCu.getInterfaceByName("GeneratedJavaParserConstants").orElseThrow(() -> new AssertionError("Can't find class in java file."));
for (BodyDeclaration<?> member : constants.getMembers()) {
member.toFieldDeclaration().filter(field -> {
String javadoc = field.getJavadocComment().get().getContent();
return javadoc.contains("RegularExpression Id") || javadoc.contains("End of File");
}).map(field -> field.getVariable(0)).ifPresent(var -> {
final String name = var.getNameAsString();
final IntegerLiteralExpr kind = var.getInitializer().get().asIntegerLiteralExpr();
generateEnumEntry(kindEnum, name, kind);
generateValueOfEntry(valueOfSwitch, name, kind);
});
}
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class ConcreteSyntaxModelTest method printAnEmptyInterfaceWithModifier.
@Test
public void printAnEmptyInterfaceWithModifier() {
Node node = JavaParser.parse("public interface A {}");
assertEquals("public interface A {" + EOL + "}" + EOL, print(node));
}
Aggregations