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)); }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) { if(before == null) { if(first == last && (first.getElementType() == JavaElementType.IMPORT_STATEMENT || first.getElementType() == JavaElementType.IMPORT_STATIC_STATEMENT)) { final PsiImportList list = (PsiImportList) SourceTreeToPsiMap.treeElementToPsi(this); final PsiImportStatementBase statement = (PsiImportStatementBase) SourceTreeToPsiMap.treeElementToPsi(first); final JavaPsiImplementationHelper instance = JavaPsiImplementationHelper.getInstance(list.getProject()); if(instance != null) { anchor = instance.getDefaultImportAnchor(list, statement); } before = Boolean.TRUE; } } return super.addInternal(first, last, anchor, before); }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before){ if (before == null){ if (first == last && (first.getElementType() == JavaElementType.IMPORT_STATEMENT || first.getElementType() == JavaElementType.IMPORT_STATIC_STATEMENT)){ final PsiImportList list = (PsiImportList)SourceTreeToPsiMap.treeElementToPsi(this); final PsiImportStatementBase statement = (PsiImportStatementBase)SourceTreeToPsiMap.treeElementToPsi(first); final JavaPsiImplementationHelper instance = JavaPsiImplementationHelper.getInstance(list.getProject()); if (instance != null) { anchor = instance.getDefaultImportAnchor(list, statement); } before = Boolean.TRUE; } } return super.addInternal(first, last, anchor, before); }
@Override public boolean isVisible(@NotNull Usage usage) { final PsiElement psiElement = usage instanceof PsiElementUsage? ((PsiElementUsage)usage).getElement() : null; if (psiElement != null) { final PsiFile containingFile = psiElement.getContainingFile(); if (containingFile instanceof PsiJavaFile) { // check whether the element is in the import list final PsiImportList importList = PsiTreeUtil.getParentOfType(psiElement, PsiImportList.class, true); return importList == null; } } return true; }
@Override @Nullable public SmartPointerElementInfo createElementInfo(@NotNull final PsiElement element, @NotNull PsiFile containingFile) { if (element instanceof PsiImportList) { return new ImportListInfo((PsiJavaFile)containingFile); } return null; }
public boolean isVisible(@NotNull Usage usage) { if (usage instanceof PsiElementUsage) { final PsiElement psiElement = ((PsiElementUsage)usage).getElement(); final PsiFile containingFile = psiElement.getContainingFile(); if (containingFile instanceof PsiJavaFile) { // check whether the element is in the import list final PsiImportList importList = PsiTreeUtil.getParentOfType(psiElement, PsiImportList.class, true); return importList == null; } } return true; }
@Override @Nullable public SmartPointerElementInfo createElementInfo(@NotNull final PsiElement element) { if (element instanceof PsiImportList) { return new ImportListInfo((PsiJavaFile)element.getContainingFile()); } return null; }
@Override public void visitClass(@NotNull PsiClass aClass) { // no call to super, so it doesn't drill down if (!(aClass.getParent() instanceof PsiJavaFile)) { return; } if (FileTypeUtils.isInJsp(aClass.getContainingFile())) { return; } final PsiJavaFile file = (PsiJavaFile)aClass.getParent(); if (file == null) { return; } if (!file.getClasses()[0].equals(aClass)) { return; } final PsiImportList importList = file.getImportList(); if (importList == null) { return; } final PsiImportStatement[] importStatements = importList.getImportStatements(); for (final PsiImportStatement importStatement : importStatements) { if (!importStatement.isOnDemand()) { registerError(importStatement); } } }
@Nullable protected PsiElement[] getImportsList() { final PsiJavaFile javaFile = (PsiJavaFile) getFile(); final PsiImportList importList = javaFile.getImportList(); return importList == null ? null : importList.getChildren(); }
@Override public boolean ignoreChanges(PsiElement element) { if(element instanceof PsiMethod) { return true; } return PsiTreeUtil.getParentOfType(element, PsiImportList.class) != null; }
public MainSketchSelectListItem(Map.Entry<Pair<PsiFile, PsiClass>, PsiImportList> entry) { this.entry = entry; }
public Map.Entry<Pair<PsiFile, PsiClass>, PsiImportList> getEntry() { return entry; }
@Override public void visitImportList(PsiImportList list) { mVisitor.report("PsiImportList", list.getText(), list); super.visitElement(list); }
@Override public PsiImportList createPsi(@NotNull final PsiImportListStub stub) { return getPsiFactory(stub).createImportList(stub); }
@Override public PsiImportList createPsi(@NotNull final ASTNode node) { return new PsiImportListImpl(node); }
private static void ensureBoundModelIsValid(ProblemsHolder holder, PsiAnnotation annotation) { final BoundMetaData boundMetaData = DataBindUtil.getBoundMetaData(annotation); if (!boundMetaData.getBindingMetaData().isValidBindableModel()) { final PsiClass boundClass = boundMetaData.getBindingMetaData().getBoundClass(); if (boundClass != null) { final PsiVariable var = (PsiVariable) Util.getImmediateOwnerElement(annotation); if (var == null) { return; } final PsiTypeElement typeElement = var.getTypeElement(); if (typeElement == null) { return; } holder.registerProblem(typeElement, "The model type (" + boundClass.getQualifiedName() + ") is not bindable."); } else { final Collection<AnnotationSearchResult> autoBoundAnnotations = boundMetaData.getBindingMetaData().getModelAnnotations(); if (autoBoundAnnotations.size() > 1) { holder.registerProblem(annotation, "Multiple model binding annotations found"); } } } else { final PsiElement element = Util.getMethodOrField(annotation); if (!Util.elementIsAnnotated(element, Types.JAVAX_INJECT)) { holder.registerProblem(annotation, "Model specifier is not injected", new LocalQuickFix() { @NotNull @Override public String getName() { return "Add @Inject to class member"; } @NotNull @Override public String getFamilyName() { return GroupNames.BUGS_GROUP_NAME; } @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final JavaPsiFacade instance = JavaPsiFacade.getInstance(project); final PsiElementFactory elementFactory = instance.getElementFactory(); final PsiImportStatement importJavaxInject = instance.getElementFactory() .createImportStatement( instance.findClass(Types.JAVAX_INJECT, ProjectScope.getAllScope(project)) ); final PsiImportList importList = ((PsiJavaFile) PsiUtil.getTopLevelClass(element).getParent()).getImportList(); importList.add(importJavaxInject); ((PsiModifierListOwner) element).getModifierList().addAnnotation("Inject"); } }); } } }
@Override public ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement) { throw new UnsupportedOperationException("TODO"); }
@Override public UsageGroup groupUsage(@NotNull Usage usage) { if (!(usage instanceof PsiElementUsage)) { return null; } final PsiElement psiElement = ((PsiElementUsage)usage).getElement(); final PsiFile containingFile = psiElement.getContainingFile(); PsiFile topLevelFile = InjectedLanguageManager.getInstance(containingFile.getProject()).getTopLevelFile(containingFile); if (!(topLevelFile instanceof PsiJavaFile) /*|| topLevelFile instanceof JspFile*/) { return null; } PsiElement containingClass = topLevelFile == containingFile ? psiElement : InjectedLanguageManager .getInstance(containingFile.getProject()).getInjectionHost(containingFile); do { containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class, true); if (containingClass == null || ((PsiClass)containingClass).getQualifiedName() != null) break; } while (true); if (containingClass == null) { // check whether the element is in the import list PsiImportList importList = PsiTreeUtil.getParentOfType(psiElement, PsiImportList.class, true); if (importList != null) { final String fileName = getFileNameWithoutExtension(topLevelFile); final PsiClass[] classes = ((PsiJavaFile)topLevelFile).getClasses(); for (final PsiClass aClass : classes) { if (fileName.equals(aClass.getName())) { containingClass = aClass; break; } } } } else { // skip JspClass synthetic classes. /*if (containingClass.getParent() instanceof PsiFile && JspPsiUtil.isInJspFile(containingClass)) { containingClass = null; } */ } if (containingClass != null) { return new ClassUsageGroup((PsiClass)containingClass); } final VirtualFile virtualFile = topLevelFile.getVirtualFile(); if (virtualFile != null) { return new FileGroupingRule.FileUsageGroup(topLevelFile.getProject(), virtualFile); } return null; }
@Override @NotNull public Runnable processFile(final PsiFile file) { if(!(file instanceof PsiJavaFile)) { return EmptyRunnable.getInstance(); } Project project = file.getProject(); final PsiImportList newImportList = JavaCodeStyleManager.getInstance(project).prepareOptimizeImportsResult((PsiJavaFile) file); if(newImportList == null) { return EmptyRunnable.getInstance(); } return new CollectingInfoRunnable() { private int myImportListLengthDiff; @Override public void run() { try { final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject()); final Document document = manager.getDocument(file); if(document != null) { manager.commitDocument(document); } final PsiImportList oldImportList = ((PsiJavaFile) file).getImportList(); if(oldImportList == null) { return; } int importsBefore = oldImportList.getAllImportStatements().length; oldImportList.replace(newImportList); myImportListLengthDiff = importsBefore - newImportList.getAllImportStatements().length; } catch(IncorrectOperationException e) { LOG.error(e); } } @Nullable @Override public String getUserNotificationInfo() { if(myImportListLengthDiff > 0) { return "removed " + myImportListLengthDiff + " import" + (myImportListLengthDiff > 1 ? "s" : ""); } return null; } }; }
public abstract ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement);