com.intellij.psi.PsiImportList Java Examples

The following examples show how to use com.intellij.psi.PsiImportList. 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: JavaImportsUtil.java    From KodeBeagle with Apache License 2.0 6 votes vote down vote up
private Map<String, Set<String>> getFullyQualifiedImportsWithMethods(
        final PsiJavaFile javaFile, final Map<String, Set<String>> importVsMethods) {
    Map<String, Set<String>> fullyQualifiedImportsWithMethods = new HashMap<>();
    PsiImportList importList = javaFile.getImportList();
    Collection<PsiImportStatement> importStatements =
            PsiTreeUtil.findChildrenOfType(importList, PsiImportStatement.class);
    for (PsiImportStatement importStatement : importStatements) {
        if (!importStatement.isOnDemand()) {
            String qualifiedName = importStatement.getQualifiedName();
            if (importVsMethods.containsKey(qualifiedName)) {
                fullyQualifiedImportsWithMethods.put(qualifiedName,
                        importVsMethods.get(qualifiedName));
            }
        }
    }
    return fullyQualifiedImportsWithMethods;
}
 
Example #2
Source File: CodeGenerator.java    From ParcelablePlease with Apache License 2.0 6 votes vote down vote up
private void addImport(PsiElementFactory elementFactory, String fullyQualifiedName){
  final PsiFile file = psiClass.getContainingFile();
  if (!(file instanceof PsiJavaFile)) {
    return;
  }
  final PsiJavaFile javaFile = (PsiJavaFile)file;

  final PsiImportList importList = javaFile.getImportList();
  if (importList == null) {
    return;
  }

  // Check if already imported
  for (PsiImportStatementBase is : importList.getAllImportStatements()) {
    String impQualifiedName = is.getImportReference().getQualifiedName();
    if (fullyQualifiedName.equals(impQualifiedName)){
      return; // Already imported so nothing neede
    }

  }

  // Not imported yet so add it
  importList.add(elementFactory.createImportStatementOnDemand(fullyQualifiedName));
}
 
Example #3
Source File: ProducerUtils.java    From intellij with Apache License 2.0 4 votes vote down vote up
private static boolean hasImport(PsiElement element, String qualifiedName) {
  PsiImportList imports = getImports(element);
  return imports != null && imports.findSingleClassImportStatement(qualifiedName) != null;
}
 
Example #4
Source File: ProducerUtils.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Nullable
private static PsiImportList getImports(PsiElement element) {
  PsiFile file = element.getContainingFile();
  return file instanceof PsiJavaFile ? ((PsiJavaFile) file).getImportList() : null;
}