Java 类com.intellij.psi.PsiPackageStatement 实例源码

项目:nullability-annotations-inspection    文件:AddPackageInfoWithNullabilityDefaultsFix.java   
@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;
}
项目:nullability-annotations-inspection    文件:AddPackageInfoWithNullabilityDefaultsFix.java   
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);
}
项目:consulo-javaee    文件:JamAnnotationMeta.java   
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);
}
项目:consulo-java    文件:JavaCreateFromTemplateHandler.java   
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();
        }
    }
}
项目:intellij-ce-playground    文件:PackageNamingConventionInspection.java   
@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);
      }
    }
  };
}
项目:consulo-java    文件:AdjustPackageNameFix.java   
@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);
  }
}
项目:Android_Lint_SRP_Practice_Example    文件:PsiClassStructureDetector.java   
@Override
public void visitPackageStatement(PsiPackageStatement statement) {
    mVisitor.report("PsiPackageStatement", statement.getText(), statement);
    super.visitElement(statement);
}