@Override
 public Map<String, Set<RefEntity>> getOldContent() {
   if (myOldProblemElements == null) return null;
   final Map<String, Set<RefEntity>> oldContents =
       new com.intellij.util.containers.HashMap<String, Set<RefEntity>>();
   final Set<RefEntity> elements = myOldProblemElements.keySet();
   for (RefEntity element : elements) {
     String groupName =
         element instanceof RefElement
             ? element.getRefManager().getGroupName((RefElement) element)
             : element.getName();
     final Set<RefEntity> collection = myContents.get(groupName);
     if (collection != null) {
       final Set<RefEntity> currentElements = new HashSet<RefEntity>(collection);
       if (RefUtil.contains(element, currentElements)) continue;
     }
     Set<RefEntity> oldContent = oldContents.get(groupName);
     if (oldContent == null) {
       oldContent = new HashSet<RefEntity>();
       oldContents.put(groupName, oldContent);
     }
     oldContent.add(element);
   }
   return oldContents;
 }
  @Override
  public Object getData(String dataId) {
    if (PlatformDataKeys.HELP_ID.is(dataId)) return HELP_ID;
    if (DATA_KEY.is(dataId)) return this;
    if (myTree == null) return null;
    TreePath[] paths = myTree.getSelectionPaths();

    if (paths == null || paths.length == 0) return null;

    if (paths.length > 1) {
      if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
        return collectPsiElements();
      }
      return null;
    }

    TreePath path = paths[0];

    InspectionTreeNode selectedNode = (InspectionTreeNode) path.getLastPathComponent();

    if (selectedNode instanceof RefElementNode) {
      final RefElementNode refElementNode = (RefElementNode) selectedNode;
      RefEntity refElement = refElementNode.getElement();
      if (refElement == null) return null;
      final RefEntity item = refElement.getRefManager().getRefinedElement(refElement);

      if (!item.isValid()) return null;

      PsiElement psiElement = item instanceof RefElement ? ((RefElement) item).getElement() : null;
      if (psiElement == null) return null;

      final CommonProblemDescriptor problem = refElementNode.getProblem();
      if (problem != null) {
        if (problem instanceof ProblemDescriptor) {
          psiElement = ((ProblemDescriptor) problem).getPsiElement();
          if (psiElement == null) return null;
        } else {
          return null;
        }
      }

      if (CommonDataKeys.NAVIGATABLE.is(dataId)) {
        return getSelectedNavigatable(problem, psiElement);
      } else if (CommonDataKeys.PSI_ELEMENT.is(dataId)) {
        return psiElement.isValid() ? psiElement : null;
      }
    } else if (selectedNode instanceof ProblemDescriptionNode
        && CommonDataKeys.NAVIGATABLE.is(dataId)) {
      return getSelectedNavigatable(((ProblemDescriptionNode) selectedNode).getDescriptor());
    }

    return null;
  }
 @Nullable
 public String getQualifiedName(final RefEntity refEntity) {
   if (refEntity instanceof RefJavaElement && ((RefJavaElement) refEntity).isSyntheticJSP()) {
     return XmlStringUtil.escapeString(refEntity.getName());
   } else if (refEntity instanceof RefMethod) {
     PsiMethod psiMethod = (PsiMethod) ((RefMethod) refEntity).getElement();
     if (psiMethod != null) {
       return psiMethod.getName();
     } else {
       return refEntity.getName();
     }
   }
   return null;
 }
 @Nullable
 public RefElementContainer getOwner() {
   final RefEntity entity = myElement.getOwner();
   if (entity instanceof RefElement) {
     return new RefElementContainer(entity, myDescriptors);
   }
   return null;
 }
Esempio n. 5
0
 public void showPageFor(RefEntity newEntity) {
   if (newEntity == null) {
     showEmpty();
     return;
   }
   // multiple problems for one entity -> refresh browser
   showPageFromHistory(newEntity.getRefManager().getRefinedElement(newEntity));
 }
Esempio n. 6
0
  @RequiredReadAction
  @Override
  public void compose(StringBuffer buf, RefEntity refElement, CommonProblemDescriptor descriptor) {
    CommonProblemDescriptor[] descriptions = myTool.getDescriptions(refElement);

    int problemIdx = 0;
    if (descriptions != null) { // server-side inspections
      problemIdx = -1;
      for (int i = 0; i < descriptions.length; i++) {
        CommonProblemDescriptor description = descriptions[i];
        if (description == descriptor) {
          problemIdx = i;
          break;
        }
      }
      if (problemIdx == -1) return;
    }

    genPageHeader(buf, refElement);
    appendHeading(buf, InspectionsBundle.message("inspection.problem.synopsis"));
    //noinspection HardCodedStringLiteral
    buf.append("<br>");
    appendAfterHeaderIndention(buf);

    composeDescription(descriptor, problemIdx, buf, refElement);

    if (refElement instanceof RefElement && !refElement.isValid()) return;

    final QuickFix[] fixes = descriptor.getFixes();
    if (fixes != null && fixes.length > 0) {
      //noinspection HardCodedStringLiteral
      buf.append("<br><br>");
      appendHeading(buf, InspectionsBundle.message("inspection.problem.resolution"));
      //noinspection HardCodedStringLiteral
      buf.append("<br>");
      appendAfterHeaderIndention(buf);

      int idx = 0;
      for (QuickFix fix : fixes) {
        //noinspection HardCodedStringLiteral
        //noinspection HardCodedStringLiteral
        buf.append("<a HREF=\"file://bred.txt#invokelocal:").append(idx++);
        buf.append("\">");
        buf.append(fix.getName());
        //noinspection HardCodedStringLiteral
        buf.append("</a>");
        //noinspection HardCodedStringLiteral
        buf.append("<br>");
        appendAfterHeaderIndention(buf);
      }
    }
  }
  @Override
  @Nullable
  public CommonProblemDescriptor[] getDescriptions(@NotNull RefEntity refEntity) {
    final CommonProblemDescriptor[] problems = getProblemElements().get(refEntity);
    if (problems == null) return null;

    if (!refEntity.isValid()) {
      ignoreElement(refEntity);
      return null;
    }

    return problems;
  }
 @Override
 public void updateContent() {
   myContents.clear();
   myModulesProblems.clear();
   final Set<RefEntity> elements = getProblemElements().keySet();
   for (RefEntity element : elements) {
     if (getContext().getUIOptions().FILTER_RESOLVED_ITEMS
         && getIgnoredElements().containsKey(element)) continue;
     if (element instanceof RefModule) {
       myModulesProblems.add((RefModule) element);
     } else {
       String groupName =
           element instanceof RefElement
               ? element.getRefManager().getGroupName((RefElement) element)
               : null;
       Set<RefEntity> content = myContents.get(groupName);
       if (content == null) {
         content = new HashSet<RefEntity>();
         myContents.put(groupName, content);
       }
       content.add(element);
     }
   }
 }
  private static void ignoreElement(
      @NotNull ProblemDescriptionsProcessor processor, @NotNull RefEntity refElement) {
    processor.ignoreElement(refElement);

    if (refElement instanceof RefClass) {
      RefClass refClass = (RefClass) refElement;
      RefMethod defaultConstructor = refClass.getDefaultConstructor();
      if (defaultConstructor != null) {
        processor.ignoreElement(defaultConstructor);
        return;
      }
    }

    RefEntity owner = refElement.getOwner();
    if (owner instanceof RefElement) {
      processor.ignoreElement(owner);
    }
  }
 public void appendLocation(final RefEntity entity, final StringBuffer buf) {
   RefEntity owner = entity.getOwner();
   if (owner instanceof RefPackage) {
     buf.append(InspectionsBundle.message("inspection.export.results.package"));
     buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.CODE_OPENING);
     buf.append(RefJavaUtil.getInstance().getPackageName(entity));
     buf.append(HTMLComposerImpl.CODE_CLOSING);
   } else if (owner instanceof RefMethod) {
     buf.append(InspectionsBundle.message("inspection.export.results.method"));
     buf.append(HTMLComposerImpl.NBSP);
     myComposer.appendElementReference(buf, (RefElement) owner);
   } else if (owner instanceof RefField) {
     buf.append(InspectionsBundle.message("inspection.export.results.field"));
     buf.append(HTMLComposerImpl.NBSP);
     myComposer.appendElementReference(buf, (RefElement) owner);
     buf.append(HTMLComposerImpl.NBSP);
     buf.append(InspectionsBundle.message("inspection.export.results.initializer"));
   } else if (owner instanceof RefClass) {
     appendClassOrInterface(buf, (RefClass) owner, false);
     buf.append(HTMLComposerImpl.NBSP);
     myComposer.appendElementReference(buf, (RefElement) owner);
   }
 }
  private void exportResults(
      @NotNull final CommonProblemDescriptor[] descriptors,
      @NotNull RefEntity refEntity,
      @NotNull Element parentNode) {
    for (CommonProblemDescriptor descriptor : descriptors) {
      @NonNls final String template = descriptor.getDescriptionTemplate();
      int line =
          descriptor instanceof ProblemDescriptor
              ? ((ProblemDescriptor) descriptor).getLineNumber()
              : -1;
      final PsiElement psiElement =
          descriptor instanceof ProblemDescriptor
              ? ((ProblemDescriptor) descriptor).getPsiElement()
              : null;
      @NonNls
      String problemText =
          StringUtil.replace(
              StringUtil.replace(
                  template,
                  "#ref",
                  psiElement != null
                      ? ProblemDescriptorUtil.extractHighlightedText(descriptor, psiElement)
                      : ""),
              " #loc ",
              " ");

      Element element = refEntity.getRefManager().export(refEntity, parentNode, line);
      if (element == null) return;
      @NonNls
      Element problemClassElement =
          new Element(InspectionsBundle.message("inspection.export.results.problem.element.tag"));
      problemClassElement.addContent(myToolWrapper.getDisplayName());

      final HighlightSeverity severity;
      if (refEntity instanceof RefElement) {
        final RefElement refElement = (RefElement) refEntity;
        severity = getSeverity(refElement);
      } else {
        final InspectionProfile profile =
            InspectionProjectProfileManager.getInstance(getContext().getProject())
                .getInspectionProfile();
        final HighlightDisplayLevel level =
            profile.getErrorLevel(
                HighlightDisplayKey.find(myToolWrapper.getShortName()), psiElement);
        severity = level.getSeverity();
      }

      if (severity != null) {
        ProblemHighlightType problemHighlightType =
            descriptor instanceof ProblemDescriptor
                ? ((ProblemDescriptor) descriptor).getHighlightType()
                : ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
        final String attributeKey =
            getTextAttributeKey(getRefManager().getProject(), severity, problemHighlightType);
        problemClassElement.setAttribute("severity", severity.myName);
        problemClassElement.setAttribute("attribute_key", attributeKey);
      }

      element.addContent(problemClassElement);
      if (myToolWrapper instanceof GlobalInspectionToolWrapper) {
        final GlobalInspectionTool globalInspectionTool =
            ((GlobalInspectionToolWrapper) myToolWrapper).getTool();
        final QuickFix[] fixes = descriptor.getFixes();
        if (fixes != null) {
          @NonNls Element hintsElement = new Element("hints");
          for (QuickFix fix : fixes) {
            final String hint = globalInspectionTool.getHint(fix);
            if (hint != null) {
              @NonNls Element hintElement = new Element("hint");
              hintElement.setAttribute("value", hint);
              hintsElement.addContent(hintElement);
            }
          }
          element.addContent(hintsElement);
        }
      }
      try {
        Element descriptionElement =
            new Element(InspectionsBundle.message("inspection.export.results.description.tag"));
        descriptionElement.addContent(problemText);
        element.addContent(descriptionElement);
      } catch (IllegalDataException e) {
        //noinspection HardCodedStringLiteral,UseOfSystemOutOrSystemErr
        System.out.println(
            "Cannot save results for "
                + refEntity.getName()
                + ", inspection which caused problem: "
                + myToolWrapper.getShortName());
      }
    }
  }
  public void appendReferencePresentation(
      RefEntity refElement, final StringBuffer buf, final boolean isPackageIncluded) {
    if (refElement instanceof RefImplicitConstructor) {
      buf.append(InspectionsBundle.message("inspection.export.results.implicit.constructor"));
      refElement = ((RefImplicitConstructor) refElement).getOwnerClass();
    }

    buf.append(HTMLComposerImpl.CODE_OPENING);

    if (refElement instanceof RefField) {
      RefField field = (RefField) refElement;
      PsiField psiField = field.getElement();
      buf.append(psiField.getType().getPresentableText());
      buf.append(HTMLComposerImpl.NBSP);
    } else if (refElement instanceof RefMethod) {
      RefMethod method = (RefMethod) refElement;
      PsiMethod psiMethod = (PsiMethod) method.getElement();
      PsiType returnType = psiMethod.getReturnType();

      if (returnType != null) {
        buf.append(returnType.getPresentableText());
        buf.append(HTMLComposerImpl.NBSP);
      }
    }

    buf.append(HTMLComposerImpl.A_HREF_OPENING);

    if (myComposer.myExporter == null) {
      buf.append(((RefElementImpl) refElement).getURL());
    } else {
      buf.append(myComposer.myExporter.getURL(refElement));
    }

    buf.append("\">");

    if (refElement instanceof RefClass && ((RefClass) refElement).isAnonymous()) {
      buf.append(InspectionsBundle.message("inspection.reference.anonymous"));
    } else if (refElement instanceof RefJavaElement
        && ((RefJavaElement) refElement).isSyntheticJSP()) {
      buf.append(XmlStringUtil.escapeString(refElement.getName()));
    } else if (refElement instanceof RefMethod) {
      PsiMethod psiMethod = (PsiMethod) ((RefMethod) refElement).getElement();
      buf.append(psiMethod.getName());
    } else {
      buf.append(refElement.getName());
    }

    buf.append(HTMLComposerImpl.A_CLOSING);

    if (refElement instanceof RefMethod) {
      PsiMethod psiMethod = (PsiMethod) ((RefMethod) refElement).getElement();
      appendMethodParameters(buf, psiMethod, false);
    }

    buf.append(HTMLComposerImpl.CODE_CLOSING);

    if (refElement instanceof RefClass && ((RefClass) refElement).isAnonymous()) {
      buf.append(" ");
      buf.append(InspectionsBundle.message("inspection.export.results.anonymous.ref.in.owner"));
      buf.append(" ");
      myComposer.appendElementReference(
          buf, ((RefElement) refElement.getOwner()), isPackageIncluded);
    } else if (isPackageIncluded) {
      buf.append(" ").append(HTMLComposerImpl.CODE_OPENING).append("(");
      myComposer.appendQualifiedName(buf, refElement.getOwner());
      //      buf.append(RefUtil.getPackageName(refElement));
      buf.append(")").append(HTMLComposerImpl.CODE_CLOSING);
    }
  }
  public void appendShortName(final RefEntity refElement, final StringBuffer buf) {
    if (refElement instanceof RefJavaElement) {
      String modifier = ((RefJavaElement) refElement).getAccessModifier();
      if (modifier != null && modifier != PsiModifier.PACKAGE_LOCAL) {
        buf.append(modifier);
        buf.append(HTMLComposerImpl.NBSP);
      }
    }
    refElement.accept(
        new RefJavaVisitor() {
          @Override
          public void visitClass(@NotNull RefClass refClass) {
            if (refClass.isStatic()) {
              buf.append(InspectionsBundle.message("inspection.export.results.static"));
              buf.append(HTMLComposerImpl.NBSP);
            }

            appendClassOrInterface(buf, refClass, false);
            buf.append(HTMLComposerImpl.NBSP)
                .append(HTMLComposerImpl.B_OPENING)
                .append(HTMLComposerImpl.CODE_OPENING);
            final String name = refClass.getName();
            buf.append(refClass.isSyntheticJSP() ? XmlStringUtil.escapeString(name) : name);
            buf.append(HTMLComposerImpl.CODE_CLOSING).append(HTMLComposerImpl.B_CLOSING);
          }

          @Override
          public void visitField(@NotNull RefField field) {
            PsiField psiField = field.getElement();
            if (psiField != null) {
              if (field.isStatic()) {
                buf.append(InspectionsBundle.message("inspection.export.results.static"));
                buf.append(HTMLComposerImpl.NBSP);
              }

              buf.append(InspectionsBundle.message("inspection.export.results.field"));
              buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.CODE_OPENING);

              buf.append(psiField.getType().getPresentableText());
              buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.B_OPENING);
              buf.append(psiField.getName());
              buf.append(HTMLComposerImpl.B_CLOSING).append(HTMLComposerImpl.CODE_CLOSING);
            }
          }

          @Override
          public void visitMethod(@NotNull RefMethod method) {
            PsiMethod psiMethod = (PsiMethod) method.getElement();
            if (psiMethod != null) {
              PsiType returnType = psiMethod.getReturnType();

              if (method.isStatic()) {
                buf.append(InspectionsBundle.message("inspection.export.results.static"));
                buf.append(HTMLComposerImpl.NBSP);
              } else if (method.isAbstract()) {
                buf.append(InspectionsBundle.message("inspection.export.results.abstract"));
                buf.append(HTMLComposerImpl.NBSP);
              }
              buf.append(
                  method.isConstructor()
                      ? InspectionsBundle.message("inspection.export.results.constructor")
                      : InspectionsBundle.message("inspection.export.results.method"));
              buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.CODE_OPENING);

              if (returnType != null) {
                buf.append(returnType.getPresentableText());
                buf.append(HTMLComposerImpl.NBSP);
              }

              buf.append(HTMLComposerImpl.B_OPENING);
              buf.append(psiMethod.getName());
              buf.append(HTMLComposerImpl.B_CLOSING);
              appendMethodParameters(buf, psiMethod, true);
              buf.append(HTMLComposerImpl.CODE_CLOSING);
            }
          }

          @Override
          public void visitFile(@NotNull RefFile file) {
            final PsiFile psiFile = file.getElement();
            buf.append(HTMLComposerImpl.B_OPENING);
            buf.append(psiFile.getName());
            buf.append(HTMLComposerImpl.B_CLOSING);
          }
        });
  }