Java Code Examples for com.github.javaparser.ast.Node#toString()

The following examples show how to use com.github.javaparser.ast.Node#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemberExplorer.java    From jeddict with Apache License 2.0 6 votes vote down vote up
public String getDefaultValue() {
    String defaultValue = null;
    if (field != null && field.getVariables().get(0).getChildNodes().size() == 3) {
        Node node = field.getVariables().get(0).getChildNodes().get(2);
        if (node instanceof Expression) { //FieldAccessExpr, MethodCallExpr, ObjectCreationExpr
            defaultValue = node.toString();
            Map<String, ImportDeclaration> imports = clazz.getImports();
             String importList = imports.keySet()
                     .stream()
                    .filter(defaultValue::contains)
                    .map(imports::get)
                    .map(ImportDeclaration::getNameAsString)
                    .collect(joining(" ,\n"));
            defaultValue = importList.isEmpty() ? defaultValue : "[\n" + importList + "\n]\n" + defaultValue;
        } else if (node instanceof NodeWithSimpleName) {
            defaultValue = ((NodeWithSimpleName) node).getNameAsString();
        } else if (node instanceof LiteralStringValueExpr) {
            defaultValue = "'" + ((LiteralStringValueExpr) node).getValue() + "'";
        } else {
            throw new UnsupportedOperationException();
        }
    }
    return defaultValue;
}
 
Example 2
Source File: PackageDocScanParser.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static String describe(Node node) {
    if (node instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) node;
        return "Method " + methodDeclaration.getDeclarationAsString(false, false, true);
    }
    if (node instanceof ConstructorDeclaration) {
        ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) node;
        return "Constructor " + constructorDeclaration.getDeclarationAsString();
    }
    if (node instanceof ClassOrInterfaceDeclaration) {
        ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) node;
        if (classOrInterfaceDeclaration.isInterface()) {
            return "Interface " + classOrInterfaceDeclaration.getName();
        } else {
            return "Class " + classOrInterfaceDeclaration.getName();
        }
    }
    if (node instanceof EnumDeclaration) {
        EnumDeclaration enumDeclaration = (EnumDeclaration) node;
        return "Enum " + enumDeclaration.getName();
    }
    if (node instanceof FieldDeclaration) {
        FieldDeclaration fieldDeclaration = (FieldDeclaration) node;
        List<String> varNames = fieldDeclaration.getVariables().stream().map(v -> v.getName().getId()).collect(Collectors.toList());
        return "Field " + String.join(", ", varNames);
    }
    return node.toString();
}
 
Example 3
Source File: ImpSort.java    From impsort-maven-plugin with Apache License 2.0 5 votes vote down vote up
private static void convertAndAddImport(LinkedHashSet<Import> allImports, List<Node> thisImport,
    String eol) {
  boolean isStatic = false;
  String importItem = null;
  String prefix = "";
  String suffix = "";
  for (Node n : thisImport) {
    if (n instanceof Comment) {
      if (importItem == null) {
        prefix += n.toString();
      } else {
        suffix += n.toString();
      }
    }
    if (n instanceof ImportDeclaration) {
      ImportDeclaration i = (ImportDeclaration) n;
      isStatic = i.isStatic();
      importItem = i.getName().asString() + (i.isAsterisk() ? ".*" : "");
    }
  }
  suffix = suffix.trim();
  if (!suffix.isEmpty()) {
    suffix = " " + suffix;
  }
  Import imp = new Import(isStatic, importItem, prefix.trim(), suffix, eol);
  Iterator<Import> iter = allImports.iterator();
  // this de-duplication can probably be made more efficient by doing it all at the end
  while (iter.hasNext()) {
    Import candidate = iter.next(); // potential duplicate
    if (candidate.isDuplicatedBy(imp)) {
      iter.remove();
      imp = candidate.combineWith(imp);
    }
  }
  allImports.add(imp);
}