com.github.javaparser.ast.Node类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(819)

本文整理了Java中com.github.javaparser.ast.Node类的一些代码示例,展示了Node类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node类的具体详情如下:
包路径:com.github.javaparser.ast.Node
类名称:Node

Node介绍

暂无

代码示例

代码示例来源:origin: JCTools/JCTools

  1. private static boolean isCommentPresent(Node node, String wanted) {
  2. Optional<Comment> maybeComment = node.getComment();
  3. if (maybeComment.isPresent()) {
  4. Comment comment = maybeComment.get();
  5. String content = comment.getContent().trim();
  6. if (wanted.equals(content)) {
  7. return true;
  8. }
  9. }
  10. return false;
  11. }

代码示例来源:origin: javaparser/javasymbolsolver

  1. SymbolReference<ResolvedMethodDeclaration> refMethod = facade.solve(callExpr);
  2. if (!refMethod.isSolved()) {
  3. throw new UnsolvedSymbolException(getParentNode(node).toString(), callExpr.getName().getId());
  4. if (callExpr.getScope().isPresent()) {
  5. Expression scope = callExpr.getScope().get();
  6. if (functionalMethod.isPresent()) {
  7. LambdaExpr lambdaExpr = node;
  8. .map(returnStmt -> {
  9. Optional<Expression> expression = returnStmt.getExpression();
  10. if (expression.isPresent()){
  11. .filter(x -> x != null && !x.isVoid() && !x.isNull())
  12. .findFirst()
  13. .orElse(ResolvedVoidType.INSTANCE);

代码示例来源:origin: javaparser/javasymbolsolver

  1. public static <N extends Node> Optional<N> findAncestor(Node node, Class<N> clazz) {
  2. if (!node.getParentNode().isPresent()) {
  3. return Optional.empty();
  4. } else if (clazz.isInstance(node.getParentNode().get())) {
  5. return Optional.of(clazz.cast(node.getParentNode().get()));
  6. } else {
  7. return findAncestor(node.getParentNode().get(), clazz);
  8. }
  9. }

代码示例来源:origin: org.jooby/jooby-spec

  1. private Map<String, Object> doc(final Node node, final Context ctx) {
  2. Map<String, Object> hash = new HashMap<>();
  3. Comment comment = node.getComment();
  4. if (comment != null) {
  5. String doc = comment.getContent().trim();
  6. .splitToList(doc)
  7. .stream()
  8. .map(l -> l.charAt(0) == '*' ? l.substring(1).trim() : l)
  9. .collect(Collectors.joining("\n"));
  10. int at = clean.indexOf('@');
  11. String text = at == 0 ? null : (at > 0 ? clean.substring(0, at) : clean).trim();
  12. while (cmatcher.find()) {
  13. Status status = Status.valueOf(Integer.parseInt(cmatcher.group(1).trim()));
  14. String message = Optional.ofNullable(cmatcher.group(3)).orElse(status.reason()).trim();
  15. codes.put(status.value(), message);
  16. TypeFromDoc.parse(node, ctx, returnText).ifPresent(type -> hash.put("@type", type));

代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core

  1. ResolvedEnumDeclaration enumDeclaration = node.findAncestor(EnumDeclaration.class).get().resolve().asEnum();
  2. ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration) c).getWrappedNode() == node).findFirst().get();
  3. if (resultClass.isInstance(resolved)) {
  4. return resultClass.cast(resolved);
  5. TypeDeclaration<?> typeDeclaration = (TypeDeclaration<?>) node.getParentNode().get();
  6. ResolvedReferenceTypeDeclaration resolvedTypeDeclaration = resolveDeclaration(typeDeclaration, ResolvedReferenceTypeDeclaration.class);
  7. ResolvedConstructorDeclaration resolved = resolvedTypeDeclaration.getConstructors().stream()
  8. .filter(c -> c instanceof JavaParserConstructorDeclaration)
  9. .filter(c -> ((JavaParserConstructorDeclaration) c).getWrappedNode() == constructorDeclaration)
  10. .findFirst()
  11. ResolvedAnnotationDeclaration annotationDeclaration = node.findAncestor(AnnotationDeclaration.class).get().resolve();
  12. ResolvedAnnotationMemberDeclaration resolved = annotationDeclaration.getAnnotationMembers().stream().filter(c -> ((JavaParserAnnotationMemberDeclaration) c).getWrappedNode() == node).findFirst().get();
  13. if (resultClass.isInstance(resolved)) {
  14. if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof FieldDeclaration) {
  15. resolved = new JavaParserFieldDeclaration((VariableDeclarator) node, typeSolver);
  16. } else if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof VariableDeclarationExpr) {
  17. resolved = new JavaParserVariableDeclaration((VariableDeclarator) node, typeSolver);
  18. } else {
  19. throw new UnsupportedOperationException("Parent of VariableDeclarator is: " + node.getParentNode());
  20. if (ResolvedParameterDeclaration.class.equals(resultClass)) {
  21. Parameter parameter = (Parameter) node;
  22. CallableDeclaration callableDeclaration = node.findAncestor(CallableDeclaration.class).get();
  23. ResolvedMethodLikeDeclaration resolvedMethodLikeDeclaration;
  24. if (callableDeclaration.isConstructorDeclaration()) {

代码示例来源:origin: com.github.javaparser/java-symbol-solver-core

  1. ResolvedEnumDeclaration enumDeclaration = Navigator.findAncestor(node, EnumDeclaration.class).get().resolve().asEnum();
  2. ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration)c).getWrappedNode() == node).findFirst().get();
  3. if (resultClass.isInstance(resolved)) {
  4. return resultClass.cast(resolved);
  5. ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration)node.getParentNode().get();
  6. ResolvedClassDeclaration resolvedClass = resolveDeclaration(classOrInterfaceDeclaration, ResolvedClassDeclaration.class).asClass();
  7. ResolvedConstructorDeclaration resolved = resolvedClass.getConstructors().stream().filter(c -> ((JavaParserConstructorDeclaration)c).getWrappedNode() == constructorDeclaration).findFirst().get();
  8. if (resultClass.isInstance(resolved)) {
  9. return resultClass.cast(resolved);

代码示例来源:origin: javaparser/javasymbolsolver

  1. private <P extends Node> boolean parentIs(Node node, Class<P> parentClass) {
  2. if (node.getParentNode().isPresent()) {
  3. return parentClass.isInstance(node.getParentNode().get());
  4. } else {
  5. return false;
  6. }
  7. }

代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core

  1. static String getPackageName(Node container) {
  2. if (container instanceof CompilationUnit) {
  3. Optional<PackageDeclaration> p = ((CompilationUnit) container).getPackageDeclaration();
  4. if (p.isPresent()) {
  5. return p.get().getName().toString();
  6. }
  7. } else if (container != null) {
  8. return getPackageName(container.getParentNode().orElse(null));
  9. }
  10. return "";
  11. }

代码示例来源:origin: javaparser/javasymbolsolver

  1. SymbolReference<ResolvedMethodDeclaration> refMethod = facade.solve(callExpr, false);
  2. if (!refMethod.isSolved()) {
  3. throw new UnsolvedSymbolException(getParentNode(node).toString(), callExpr.getName().getId());
  4. if (functionalMethod.isPresent()) {
  5. if (node instanceof MethodReferenceExpr) {
  6. MethodReferenceExpr methodReferenceExpr = (MethodReferenceExpr) node;
  7. ResolvedType formalType = functionalMethod.get().returnType();

代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core

  1. private static Optional<SwitchStmt> findSwitchHelper(Node node) {
  2. // TODO can be replaced by findFirst with the correct algorithm.
  3. if (node instanceof SwitchStmt) {
  4. return Optional.of((SwitchStmt) node);
  5. }
  6. for (Node child : node.getChildNodes()) {
  7. Optional<SwitchStmt> resChild = findSwitchHelper(child);
  8. if (resChild.isPresent()) {
  9. return resChild;
  10. }
  11. }
  12. return Optional.empty();
  13. }
  14. }

代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core

  1. protected Collection<ResolvedReferenceTypeDeclaration> findTypeDeclarations(Optional<Expression> optScope) {
  2. if (optScope.isPresent()) {
  3. Expression scope = optScope.get();
  4. throw new UnsolvedSymbolException(scope.toString(), wrappedNode.toString(), e);
  5. } else if (typeOfScope.isUnionType()) {
  6. return typeOfScope.asUnionType().getCommonAncestor()
  7. .map(ResolvedReferenceType::getTypeDeclaration)
  8. .map(Collections::singletonList)
  9. .orElseThrow(() -> new UnsolvedSymbolException("No common ancestor available for UnionType"

代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core

  1. if (name.getParentNode().isPresent()) {
  2. Node parent = name.getParentNode().get();
  3. if (isAName(parent) && nameAsString(name).equals(nameAsString(parent))) {
  4. return syntacticClassificationAccordingToContext(parent);
  5. return NameCategory.TYPE_NAME;
  6. if (name.getParentNode().isPresent() && name.getParentNode().get() instanceof ClassOrInterfaceType) {
  7. return NameCategory.TYPE_NAME;
  8. if (name.getParentNode().isPresent() && name.getParentNode().get() instanceof FieldAccessExpr) {
  9. return NameCategory.EXPRESSION_NAME;
  10. + name.getParentNode().get().getClass().getSimpleName() + ". See " + name + " at " + name.getRange());

代码示例来源:origin: org.apache.uima/uimaj-v3migration-jcas

  1. Optional<Node> maybeParent = n.getParentNode();
  2. if (maybeParent.isPresent()) {
  3. Node parent = n.getParentNode().get();
  4. if (parent instanceof EnclosedExpr) {
  5. ((EnclosedExpr)parent).setInner(v);
  6. System.out.println(" node: " + n.toString());
  7. System.out.println(" expression replacing: " + v.toString());
  8. throw new RuntimeException();

代码示例来源:origin: org.apache.uima/uimaj-v3migration-jcas

  1. ((p1 = n.getParentNode()).isPresent() && p1.get() instanceof ExpressionStmt) &&
  2. ((p2 = p1.get().getParentNode()).isPresent() && p2.get() instanceof BlockStmt) &&
  3. ((p3 = p2.get().getParentNode()).isPresent() && p3.get() == get_set_method)) {
  4. NodeList<Statement> stmts = ((BlockStmt)p2.get()).getStatements();
  5. stmts.set(stmts.indexOf(p1.get()), new EmptyStmt());
  6. updatedNode.accept(this, null);
  7. } else {
  8. super.visit(n, null);

代码示例来源:origin: org.jooby/jooby-spec

  1. private String summary(final MethodCallExpr it, final Context ctx) {
  2. return usePath(it)
  3. .map(use -> {
  4. Node node = use;
  5. while (!(node instanceof ExpressionStmt)) {
  6. node = node.getParentNode();
  7. }
  8. return node == null ? null : (String) doc(node, ctx).get("@text");
  9. }).orElse(null);
  10. }

代码示例来源:origin: GumTreeDiff/gumtree

  1. protected void pushNode(Node n, String label) {
  2. int type = n.getClass().getName().hashCode();
  3. String typeName = n.getClass().getSimpleName();
  4. try {
  5. Position begin = n.getRange().get().begin;
  6. Position end = n.getRange().get().end;
  7. push(type, typeName, label, reader.positionFor(begin.line, begin.column),
  8. reader.positionFor(end.line,end.column));
  9. }
  10. catch (NoSuchElementException ignore) { }
  11. }

代码示例来源:origin: com.github.javaparser/java-symbol-solver-core

  1. public static Node getParentNode(Node node) {
  2. Node parent = node.getParentNode().orElse(null);
  3. return parent;
  4. }

代码示例来源:origin: com.github.javaparser/java-symbol-solver-core

  1. private void solveMethodCalls(Node node) {
  2. if (node instanceof MethodCallExpr) {
  3. out.println(" Line " + node.getBegin().get().line + ") " + node + " ==> " + toString((MethodCallExpr) node));
  4. }
  5. for (Node child : node.getChildNodes()) {
  6. solveMethodCalls(child);
  7. }
  8. }

代码示例来源:origin: javaparser/javasymbolsolver

  1. private List<Node> collectAllNodes(Node node) {
  2. List<Node> nodes = new LinkedList<>();
  3. collectAllNodes(node, nodes);
  4. nodes.sort((n1, n2) -> n1.getBegin().get().compareTo(n2.getBegin().get()));
  5. return nodes;
  6. }

代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core

  1. private List<Node> collectAllNodes(Node node) {
  2. List<Node> nodes = new ArrayList<>();
  3. node.walk(nodes::add);
  4. nodes.sort(comparing(n -> n.getBegin().get()));
  5. return nodes;
  6. }

相关文章