private static boolean isCommentPresent(Node node, String wanted) { Optional<Comment> maybeComment = node.getComment(); if (maybeComment.isPresent()) { Comment comment = maybeComment.get(); String content = comment.getContent().trim(); if (wanted.equals(content)) { return true; } } return false; }
SymbolReference<ResolvedMethodDeclaration> refMethod = facade.solve(callExpr); if (!refMethod.isSolved()) { throw new UnsolvedSymbolException(getParentNode(node).toString(), callExpr.getName().getId()); if (callExpr.getScope().isPresent()) { Expression scope = callExpr.getScope().get(); if (functionalMethod.isPresent()) { LambdaExpr lambdaExpr = node; .map(returnStmt -> { Optional<Expression> expression = returnStmt.getExpression(); if (expression.isPresent()){ .filter(x -> x != null && !x.isVoid() && !x.isNull()) .findFirst() .orElse(ResolvedVoidType.INSTANCE);
public static <N extends Node> Optional<N> findAncestor(Node node, Class<N> clazz) { if (!node.getParentNode().isPresent()) { return Optional.empty(); } else if (clazz.isInstance(node.getParentNode().get())) { return Optional.of(clazz.cast(node.getParentNode().get())); } else { return findAncestor(node.getParentNode().get(), clazz); } }
private Map<String, Object> doc(final Node node, final Context ctx) { Map<String, Object> hash = new HashMap<>(); Comment comment = node.getComment(); if (comment != null) { String doc = comment.getContent().trim(); .splitToList(doc) .stream() .map(l -> l.charAt(0) == '*' ? l.substring(1).trim() : l) .collect(Collectors.joining("\n")); int at = clean.indexOf('@'); String text = at == 0 ? null : (at > 0 ? clean.substring(0, at) : clean).trim(); while (cmatcher.find()) { Status status = Status.valueOf(Integer.parseInt(cmatcher.group(1).trim())); String message = Optional.ofNullable(cmatcher.group(3)).orElse(status.reason()).trim(); codes.put(status.value(), message); TypeFromDoc.parse(node, ctx, returnText).ifPresent(type -> hash.put("@type", type));
ResolvedEnumDeclaration enumDeclaration = node.findAncestor(EnumDeclaration.class).get().resolve().asEnum(); ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration) c).getWrappedNode() == node).findFirst().get(); if (resultClass.isInstance(resolved)) { return resultClass.cast(resolved); TypeDeclaration<?> typeDeclaration = (TypeDeclaration<?>) node.getParentNode().get(); ResolvedReferenceTypeDeclaration resolvedTypeDeclaration = resolveDeclaration(typeDeclaration, ResolvedReferenceTypeDeclaration.class); ResolvedConstructorDeclaration resolved = resolvedTypeDeclaration.getConstructors().stream() .filter(c -> c instanceof JavaParserConstructorDeclaration) .filter(c -> ((JavaParserConstructorDeclaration) c).getWrappedNode() == constructorDeclaration) .findFirst() ResolvedAnnotationDeclaration annotationDeclaration = node.findAncestor(AnnotationDeclaration.class).get().resolve(); ResolvedAnnotationMemberDeclaration resolved = annotationDeclaration.getAnnotationMembers().stream().filter(c -> ((JavaParserAnnotationMemberDeclaration) c).getWrappedNode() == node).findFirst().get(); if (resultClass.isInstance(resolved)) { if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof FieldDeclaration) { resolved = new JavaParserFieldDeclaration((VariableDeclarator) node, typeSolver); } else if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof VariableDeclarationExpr) { resolved = new JavaParserVariableDeclaration((VariableDeclarator) node, typeSolver); } else { throw new UnsupportedOperationException("Parent of VariableDeclarator is: " + node.getParentNode()); if (ResolvedParameterDeclaration.class.equals(resultClass)) { Parameter parameter = (Parameter) node; CallableDeclaration callableDeclaration = node.findAncestor(CallableDeclaration.class).get(); ResolvedMethodLikeDeclaration resolvedMethodLikeDeclaration; if (callableDeclaration.isConstructorDeclaration()) {
ResolvedEnumDeclaration enumDeclaration = Navigator.findAncestor(node, EnumDeclaration.class).get().resolve().asEnum(); ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration)c).getWrappedNode() == node).findFirst().get(); if (resultClass.isInstance(resolved)) { return resultClass.cast(resolved); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration)node.getParentNode().get(); ResolvedClassDeclaration resolvedClass = resolveDeclaration(classOrInterfaceDeclaration, ResolvedClassDeclaration.class).asClass(); ResolvedConstructorDeclaration resolved = resolvedClass.getConstructors().stream().filter(c -> ((JavaParserConstructorDeclaration)c).getWrappedNode() == constructorDeclaration).findFirst().get(); if (resultClass.isInstance(resolved)) { return resultClass.cast(resolved);
private <P extends Node> boolean parentIs(Node node, Class<P> parentClass) { if (node.getParentNode().isPresent()) { return parentClass.isInstance(node.getParentNode().get()); } else { return false; } }
static String getPackageName(Node container) { if (container instanceof CompilationUnit) { Optional<PackageDeclaration> p = ((CompilationUnit) container).getPackageDeclaration(); if (p.isPresent()) { return p.get().getName().toString(); } } else if (container != null) { return getPackageName(container.getParentNode().orElse(null)); } return ""; }
SymbolReference<ResolvedMethodDeclaration> refMethod = facade.solve(callExpr, false); if (!refMethod.isSolved()) { throw new UnsolvedSymbolException(getParentNode(node).toString(), callExpr.getName().getId()); if (functionalMethod.isPresent()) { if (node instanceof MethodReferenceExpr) { MethodReferenceExpr methodReferenceExpr = (MethodReferenceExpr) node; ResolvedType formalType = functionalMethod.get().returnType();
private static Optional<SwitchStmt> findSwitchHelper(Node node) { // TODO can be replaced by findFirst with the correct algorithm. if (node instanceof SwitchStmt) { return Optional.of((SwitchStmt) node); } for (Node child : node.getChildNodes()) { Optional<SwitchStmt> resChild = findSwitchHelper(child); if (resChild.isPresent()) { return resChild; } } return Optional.empty(); } }
protected Collection<ResolvedReferenceTypeDeclaration> findTypeDeclarations(Optional<Expression> optScope) { if (optScope.isPresent()) { Expression scope = optScope.get(); throw new UnsolvedSymbolException(scope.toString(), wrappedNode.toString(), e); } else if (typeOfScope.isUnionType()) { return typeOfScope.asUnionType().getCommonAncestor() .map(ResolvedReferenceType::getTypeDeclaration) .map(Collections::singletonList) .orElseThrow(() -> new UnsolvedSymbolException("No common ancestor available for UnionType"
if (name.getParentNode().isPresent()) { Node parent = name.getParentNode().get(); if (isAName(parent) && nameAsString(name).equals(nameAsString(parent))) { return syntacticClassificationAccordingToContext(parent); return NameCategory.TYPE_NAME; if (name.getParentNode().isPresent() && name.getParentNode().get() instanceof ClassOrInterfaceType) { return NameCategory.TYPE_NAME; if (name.getParentNode().isPresent() && name.getParentNode().get() instanceof FieldAccessExpr) { return NameCategory.EXPRESSION_NAME; + name.getParentNode().get().getClass().getSimpleName() + ". See " + name + " at " + name.getRange());
Optional<Node> maybeParent = n.getParentNode(); if (maybeParent.isPresent()) { Node parent = n.getParentNode().get(); if (parent instanceof EnclosedExpr) { ((EnclosedExpr)parent).setInner(v); System.out.println(" node: " + n.toString()); System.out.println(" expression replacing: " + v.toString()); throw new RuntimeException();
((p1 = n.getParentNode()).isPresent() && p1.get() instanceof ExpressionStmt) && ((p2 = p1.get().getParentNode()).isPresent() && p2.get() instanceof BlockStmt) && ((p3 = p2.get().getParentNode()).isPresent() && p3.get() == get_set_method)) { NodeList<Statement> stmts = ((BlockStmt)p2.get()).getStatements(); stmts.set(stmts.indexOf(p1.get()), new EmptyStmt()); updatedNode.accept(this, null); } else { super.visit(n, null);
private String summary(final MethodCallExpr it, final Context ctx) { return usePath(it) .map(use -> { Node node = use; while (!(node instanceof ExpressionStmt)) { node = node.getParentNode(); } return node == null ? null : (String) doc(node, ctx).get("@text"); }).orElse(null); }
protected void pushNode(Node n, String label) { int type = n.getClass().getName().hashCode(); String typeName = n.getClass().getSimpleName(); try { Position begin = n.getRange().get().begin; Position end = n.getRange().get().end; push(type, typeName, label, reader.positionFor(begin.line, begin.column), reader.positionFor(end.line,end.column)); } catch (NoSuchElementException ignore) { } }
public static Node getParentNode(Node node) { Node parent = node.getParentNode().orElse(null); return parent; }
private void solveMethodCalls(Node node) { if (node instanceof MethodCallExpr) { out.println(" Line " + node.getBegin().get().line + ") " + node + " ==> " + toString((MethodCallExpr) node)); } for (Node child : node.getChildNodes()) { solveMethodCalls(child); } }