@Nullable private PsiJavaFile getOrCreatePackageInfoFile(PsiFile file) { if (!(file instanceof PsiJavaFile)) { return null; } PsiPackageStatement packageStatement = ((PsiJavaFile) file).getPackageStatement(); if (packageStatement == null) { return null; } PsiJavaCodeReferenceElement packageReference = packageStatement.getPackageReference(); PsiElement target = packageReference.resolve(); if (!(target instanceof PsiPackage)) { return null; } PsiJavaFile packageInfoFile = packageInfoFile((PsiPackage) target, file.getContainingDirectory()); if (packageInfoFile == null) { packageInfoFile = createPackageInfoFile(file, (PsiPackage) target); } return packageInfoFile; }
private void addAnnotationToPackageInfo(Project project, PsiJavaFile packageInfoFile) { if (!FileModificationService.getInstance().preparePsiElementForWrite(packageInfoFile)) { return; } PsiPackageStatement packageStatement = packageInfoFile.getPackageStatement(); if (packageStatement == null) { return; } PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); PsiAnnotation annotation = factory.createAnnotationFromText("@" + annotationForTypeQualifierFqn, packageInfoFile.getContext()); PsiElement addedAnnotation = packageInfoFile.addBefore(annotation, packageStatement); JavaCodeStyleManager.getInstance(project).shortenClassReferences(addedAnnotation); removeRedundantAnnotationsInPackage(project, packageInfoFile.getContainingDirectory().getFiles(), annotation); }
public <T extends PsiModifierListOwner> void registerTopLevelSem(SemRegistrar registrar, final ElementPattern<? extends T> parentPattern, final JamMemberMeta<T,?> parentMeta) { final boolean isPackage = parentMeta instanceof JamPackageMeta; final PsiAnnotationPattern annoPattern = myAnnoNamePattern.withSuperParent( 2, isPackage ? PACKAGE_STATEMENT.with(new PatternCondition<PsiPackageStatement>("package") { @Override public boolean accepts(@NotNull PsiPackageStatement psiPackageStatement, ProcessingContext context) { return parentPattern.accepts(psiPackageStatement.getPackageReference().resolve(), context); } }) : parentPattern); registrar.registerSemElementProvider(myMetaKey, annoPattern, annotation -> { final PsiElement parent = annotation.getParent().getParent(); final T element = (isPackage && parent instanceof PsiPackageStatement? (T)((PsiPackageStatement)parent).getPackageReference().resolve() : (T)parent); if (element != null && SemService.getSemService(annotation.getProject()).getSemElement(parentMeta.getMetaKey(), element) == parentMeta) { return this; } return null; }); registerChildren(registrar, annoPattern); }
static void hackAwayEmptyPackage(PsiJavaFile file, FileTemplate template, Map<String, Object> props) throws IncorrectOperationException { if(!template.isTemplateOfType(JavaFileType.INSTANCE)) { return; } String packageName = (String) props.get(FileTemplate.ATTRIBUTE_PACKAGE_NAME); if(packageName == null || packageName.length() == 0 || packageName.equals(FileTemplate.ATTRIBUTE_PACKAGE_NAME)) { PsiPackageStatement packageStatement = file.getPackageStatement(); if(packageStatement != null) { packageStatement.delete(); } } }
@Override public BaseInspectionVisitor buildVisitor() { return new BaseInspectionVisitor() { @Override public void visitPackageStatement(PsiPackageStatement statement) { final PsiJavaCodeReferenceElement reference = statement.getPackageReference(); if (reference == null) { return; } final String text = reference.getText(); int start = 0; int index = text.indexOf('.', start); while (index > 0) { final String name = text.substring(start, index); if (!mySettingsDelegate.isValid(name)) { registerErrorAtOffset(reference, start, index - start, name); } start = index + 1; index = text.indexOf('.', start); } final String lastName = text.substring(start); if (!mySettingsDelegate.isValid(lastName)) { registerErrorAtOffset(reference, start, lastName.length(), lastName); } } }; }
@Override public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element == null) return; PsiFile myFile = element.getContainingFile(); if (!FileModificationService.getInstance().prepareFileForWrite(myFile)) return; PsiDirectory directory = myFile.getContainingDirectory(); if (directory == null) return; PsiJavaPackage myTargetPackage = JavaDirectoryService.getInstance().getPackage(directory); if (myTargetPackage == null) return; try { PsiElementFactory factory = JavaPsiFacade.getInstance(myFile.getProject()).getElementFactory(); PsiPackageStatement myStatement = ((PsiJavaFile)myFile).getPackageStatement(); if (myTargetPackage.getQualifiedName().length() == 0) { if (myStatement != null) { myStatement.delete(); } } else { final PsiPackageStatement packageStatement = factory.createPackageStatement(myTargetPackage.getQualifiedName()); if (myStatement != null) { myStatement.getPackageReference().replace(packageStatement.getPackageReference()); } else { myFile.addAfter(packageStatement, null); } } } catch (IncorrectOperationException e) { LOG.error(e); } }
@Override public void visitPackageStatement(PsiPackageStatement statement) { mVisitor.report("PsiPackageStatement", statement.getText(), statement); super.visitElement(statement); }