Esempio n. 1
0
  @Override
  public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
    PsiElement mirrorElement = SourceTreeToPsiMap.treeToPsiNotNull(element);
    if (!(mirrorElement instanceof PsiJavaFile)) {
      throw new InvalidMirrorException("Unexpected mirror file: " + mirrorElement);
    }

    PsiJavaFile mirrorFile = (PsiJavaFile) mirrorElement;
    setMirrorIfPresent(getPackageStatement(), mirrorFile.getPackageStatement());
    setMirrors(getClasses(), mirrorFile.getClasses());
  }
 @NotNull
 @Override
 public PsiPackageStatement createPackageStatement(@NotNull final String name)
     throws IncorrectOperationException {
   final PsiJavaFile aFile = createDummyJavaFile("package " + name + ";");
   final PsiPackageStatement stmt = aFile.getPackageStatement();
   if (stmt == null) {
     throw new IncorrectOperationException("Incorrect package name: " + name);
   }
   return stmt;
 }
  @Test
  public void testFirstChildIsPackage() throws Exception {
    final PsiJavaFile psiJavaFile = mock(PsiJavaFile.class);
    final PsiClass toImport = mock(PsiClass.class);

    final PsiImportStatement psiImportStatement = mock(PsiImportStatement.class);
    final PsiImportList psiImportList = mock(PsiImportList.class);
    final RunnableHolder writeActionHolder = getWriteActionHolder();
    final PsiPackageStatement psiPackageStatement = mock(PsiPackageStatement.class);

    when(psiJavaFile.getProject()).thenReturn(project);
    when(psiImportStatement.getParent()).thenReturn(psiImportList);
    when(psiJavaFile.getPackageStatement()).thenReturn(psiPackageStatement);
    when(psiJavaFile.getFirstChild()).thenReturn(psiPackageStatement);
    when(psiElementFactory.createImportStatement(toImport)).thenReturn(psiImportStatement);
    addImportService.addImportStatement(psiJavaFile, toImport);
    assertNotNull(writeActionHolder.getRunnable());
    verify(psiJavaFile, never()).addAfter(any(), any());
    writeActionHolder.getRunnable().run();
    verify(psiJavaFile).addAfter(psiPackageStatement, psiImportList);
  }
  @Nullable
  public ProblemDescriptor[] checkFile(
      @NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // does not work in tests since CodeInsightTestCase copies file into temporary location
    if (ApplicationManager.getApplication().isUnitTestMode()) return null;
    if (file instanceof PsiJavaFile) {
      if (JspPsiUtil.isInJspFile(file)) return null;
      PsiJavaFile javaFile = (PsiJavaFile) file;

      PsiDirectory directory = javaFile.getContainingDirectory();
      if (directory == null) return null;
      PsiPackage dirPackage = JavaDirectoryService.getInstance().getPackage(directory);
      if (dirPackage == null) return null;
      PsiPackageStatement packageStatement = javaFile.getPackageStatement();

      // highlight the first class in the file only
      PsiClass[] classes = javaFile.getClasses();
      if (classes.length == 0 && packageStatement == null) return null;

      String packageName = dirPackage.getQualifiedName();
      if (!Comparing.strEqual(packageName, "", true) && packageStatement == null) {
        String description = JavaErrorMessages.message("missing.package.statement", packageName);

        return new ProblemDescriptor[] {
          manager.createProblemDescriptor(
              classes[0].getNameIdentifier(),
              description,
              new AdjustPackageNameFix(javaFile, null, dirPackage),
              ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
              isOnTheFly)
        };
      }
      if (packageStatement != null) {
        final PsiJavaCodeReferenceElement packageReference = packageStatement.getPackageReference();
        PsiPackage classPackage = (PsiPackage) packageReference.resolve();
        List<LocalQuickFix> availableFixes = new ArrayList<LocalQuickFix>();
        if (classPackage == null) {
          availableFixes.add(new AdjustPackageNameFix(javaFile, packageStatement, dirPackage));
        } else if (!Comparing.equal(
            dirPackage.getQualifiedName(), packageReference.getText(), true)) {
          availableFixes.add(new AdjustPackageNameFix(javaFile, packageStatement, dirPackage));
          MoveToPackageFix moveToPackageFix = new MoveToPackageFix(file, classPackage);
          if (moveToPackageFix.isAvailable()) {
            availableFixes.add(moveToPackageFix);
          }
        }
        if (!availableFixes.isEmpty()) {
          String description =
              JavaErrorMessages.message(
                  "package.name.file.path.mismatch",
                  packageReference.getText(),
                  dirPackage.getQualifiedName());
          return new ProblemDescriptor[] {
            manager.createProblemDescriptor(
                packageStatement.getPackageReference(),
                description,
                isOnTheFly,
                availableFixes.toArray(new LocalQuickFix[availableFixes.size()]),
                ProblemHighlightType.GENERIC_ERROR_OR_WARNING)
          };
        }
      }
    }
    return null;
  }