Java Code Examples for com.github.javaparser.ast.ImportDeclaration#isStatic()

The following examples show how to use com.github.javaparser.ast.ImportDeclaration#isStatic() . 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: PrettyPrintVisitor.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void visit(final ImportDeclaration n, final Void arg) {
    boolean isEmpty = "empty".equals(n.getNameAsString());
    if (!isEmpty){
        printJavaComment(n.getComment(), arg);
        printer.print("import ");
        if (n.isStatic()) {
            printer.print("static ");
        }
        n.getName().accept(this, arg);
        if (n.isAsterisk()) {
            printer.print(".*");
        }
        printer.println(";");
    } else {
        printer.println("");
    }

    printOrphanCommentsEnding(n);
}
 
Example 2
Source File: ReferenceToConstantsAnalyzer.java    From deadcode4j with Apache License 2.0 6 votes vote down vote up
private static Function<? super ImportDeclaration, ? extends String> toImportedType() {
    return new Function<ImportDeclaration, String>() {
        @Nullable
        @Override
        public String apply(@Nullable ImportDeclaration input) {
            if (input == null) {
                return null;
            }
            NameExpr name = input.getName();
            if (input.isStatic() && !input.isAsterisk()) {
                name = QualifiedNameExpr.class.cast(name).getQualifier();
            }
            return name.toString();
        }
    };
}
 
Example 3
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the imports.
 *
 * @param cu the cu
 * @return the imports
 */
static Set<String> getImports(CompilationUnit cu) {
    Set<String> importSet = null;
    List<ImportDeclaration> imports = cu.getImports();
    if (CollectionUtil.isNotEmpty(imports)) {
        importSet = new HashSet<String>(imports.size());
        for (ImportDeclaration im : imports) {
            String imStr = (im.isStatic() ? "static " : "") + im.getName() + (im.isAsterisk() ? ".*" : "");
            importSet.add(imStr);
        }
    } else {
        importSet = new HashSet<String>(0);
    }
    return importSet;
}
 
Example 4
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public int compare(ImportDeclaration o1, ImportDeclaration o2) {
    String stringWithoutComments1 = o1.getName().toString(prettyPrinterConfiguration);
    String stringWithoutComments2 = o2.getName().toString(prettyPrinterConfiguration);

    if (o1.isStatic() && !o2.isStatic()) {
        return -1;
    } else if (!o1.isStatic() && o2.isStatic()) {
        return 1;
    } else if (o1.isStatic() && o2.isStatic()) {
        return stringWithoutComments1.compareTo(stringWithoutComments2);
    }

    int compareEclipseImport = compareEclipseImport(stringWithoutComments1, stringWithoutComments2, "java.");
    if (compareEclipseImport != -2) {
        return compareEclipseImport;
    }

    compareEclipseImport = compareEclipseImport(stringWithoutComments1, stringWithoutComments2, "javax.");
    if (compareEclipseImport != -2) {
        return compareEclipseImport;
    }

    compareEclipseImport = compareEclipseImport(stringWithoutComments1, stringWithoutComments2, "org.");
    if (compareEclipseImport != -2) {
        return compareEclipseImport;
    }

    compareEclipseImport = compareEclipseImport(stringWithoutComments1, stringWithoutComments2, "cn.");
    if (compareEclipseImport != -2) {
        return compareEclipseImport;
    }

    compareEclipseImport = compareEclipseImport(stringWithoutComments1, stringWithoutComments2, "com.");
    if (compareEclipseImport != -2) {
        return compareEclipseImport;
    }

    return stringWithoutComments1.compareTo(stringWithoutComments2);
}
 
Example 5
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);
}
 
Example 6
Source File: ImportDeclarationMerger.java    From dolphin with Apache License 2.0 4 votes vote down vote up
@Override public boolean doIsEquals(ImportDeclaration first, ImportDeclaration second) {
  if(!isEqualsUseMerger(first.getName(),second.getName())) return false;
  if(first.isStatic() != second.isStatic()) return false;
  if(first.isAsterisk() != second.isAsterisk()) return false;
  return true;
}