public boolean canMoveTo(SourcePosition position) {
   if (!super.canMoveTo(position)) {
     return false;
   }
   final Document document =
       PsiDocumentManager.getInstance(getProject()).getDocument(position.getFile());
   return canAddLineBreakpoint(myProject, document, position.getLine());
 }
 @Override
 @Nullable
 public String getShortClassName() {
   final SourcePosition pos = getSourcePosition();
   if (pos != null) {
     if (pos.getFile() instanceof JspFile) {
       return getClassName();
     }
   }
   return super.getShortClassName();
 }
Exemplo n.º 3
0
 @Nullable
 public static XSourcePosition toXSourcePosition(@NotNull SourcePosition position) {
   VirtualFile file = position.getFile().getVirtualFile();
   if (file == null) {
     file = position.getFile().getOriginalFile().getVirtualFile();
   }
   if (file == null) {
     return null;
   }
   return new JavaXSourcePosition(position, file);
 }
Exemplo n.º 4
0
 public static boolean inTheSameMethod(
     @NotNull SourcePosition pos1, @NotNull SourcePosition pos2) {
   ApplicationManager.getApplication().assertReadAccessAllowed();
   PsiElement elem1 = pos1.getElementAt();
   PsiElement elem2 = pos2.getElementAt();
   if (elem1 == null) return elem2 == null;
   if (elem2 != null) {
     PsiElement expectedMethod = getContainingMethod(elem1);
     PsiElement currentMethod = getContainingMethod(elem2);
     return Comparing.equal(expectedMethod, currentMethod);
   }
   return false;
 }
 @Nullable
 public Document getDocument() {
   final RangeHighlighter highlighter = getHighlighter();
   if (highlighter != null) {
     return highlighter.getDocument();
   }
   final SourcePosition position = getSourcePosition();
   if (position != null) {
     final PsiFile file = position.getFile();
     return PsiDocumentManager.getInstance(getProject()).getDocument(file);
   }
   return null;
 }
 @Override
 public List<ReferenceType> getAllClasses(final SourcePosition source) throws NoDataException {
   PsiFile _file = source.getFile();
   Language _language = _file.getLanguage();
   boolean _notEquals = (!Objects.equal(_language, this.language));
   if (_notEquals) {
     throw NoDataException.INSTANCE;
   }
   final Map<URI, AbstractTraceRegion> traces =
       this._debugProcessExtensions.getTracesForSource(this.process, source);
   final ArrayList<ReferenceType> allClasses = CollectionLiterals.<ReferenceType>newArrayList();
   final int line = source.getLine();
   Set<Map.Entry<URI, AbstractTraceRegion>> _entrySet = traces.entrySet();
   for (final Map.Entry<URI, AbstractTraceRegion> uri2trace : _entrySet) {
     {
       AbstractTraceRegion _value = uri2trace.getValue();
       TreeIterator<AbstractTraceRegion> _treeIterator = _value.treeIterator();
       final Function1<AbstractTraceRegion, Boolean> _function =
           new Function1<AbstractTraceRegion, Boolean>() {
             @Override
             public Boolean apply(final AbstractTraceRegion it) {
               List<ILocationData> _associatedLocations = it.getAssociatedLocations();
               ILocationData _head =
                   IterableExtensions.<ILocationData>head(_associatedLocations);
               int _lineNumber = 0;
               if (_head != null) {
                 _lineNumber = _head.getLineNumber();
               }
               return Boolean.valueOf((_lineNumber == line));
             }
           };
       final AbstractTraceRegion region =
           IteratorExtensions.<AbstractTraceRegion>findFirst(_treeIterator, _function);
       boolean _notEquals_1 = (!Objects.equal(region, null));
       if (_notEquals_1) {
         URI _key = uri2trace.getKey();
         final PsiFile psiFile = this._debugProcessExtensions.getPsiFile(this.process, _key);
         PositionManagerImpl _javaPositionManger =
             this._debugProcessExtensions.getJavaPositionManger(this.process);
         int _myLineNumber = region.getMyLineNumber();
         int _plus = (_myLineNumber + 1);
         SourcePosition _createFromLine = SourcePosition.createFromLine(psiFile, _plus);
         final List<ReferenceType> classes = _javaPositionManger.getAllClasses(_createFromLine);
         allClasses.addAll(classes);
       }
     }
   }
   return allClasses;
 }
Exemplo n.º 7
0
 @Nullable
 public static PsiClass getClassAt(@Nullable SourcePosition position) {
   if (position == null) {
     return null;
   }
   final PsiElement element = position.getElementAt();
   return (element != null) ? PsiTreeUtil.getParentOfType(element, PsiClass.class, false) : null;
 }
 public boolean canMoveTo(@Nullable final SourcePosition position) {
   if (position == null || !position.getFile().isValid()) {
     return false;
   }
   final PsiFile psiFile = position.getFile();
   final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(psiFile);
   if (document == null) {
     return false;
   }
   final int spOffset = position.getOffset();
   if (spOffset < 0) {
     return false;
   }
   final BreakpointManager breakpointManager =
       DebuggerManagerEx.getInstanceEx(getProject()).getBreakpointManager();
   return breakpointManager.findBreakpoint(document, spOffset, getCategory()) == null;
 }
Exemplo n.º 9
0
  public static List<PsiLambdaExpression> collectLambdas(
      @NotNull SourcePosition position, final boolean onlyOnTheLine) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiFile file = position.getFile();
    final int line = position.getLine();
    final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    if (document == null || line >= document.getLineCount()) {
      return Collections.emptyList();
    }
    PsiElement element = position.getElementAt();
    final TextRange lineRange = DocumentUtil.getLineTextRange(document, line);
    do {
      PsiElement parent = element.getParent();
      if (parent == null || (parent.getTextOffset() < lineRange.getStartOffset())) {
        break;
      }
      element = parent;
    } while (true);

    final List<PsiLambdaExpression> lambdas = new ArrayList<PsiLambdaExpression>(3);
    final PsiElementVisitor lambdaCollector =
        new JavaRecursiveElementVisitor() {
          @Override
          public void visitLambdaExpression(PsiLambdaExpression expression) {
            super.visitLambdaExpression(expression);
            if (!onlyOnTheLine || getFirstElementOnTheLine(expression, document, line) != null) {
              lambdas.add(expression);
            }
          }
        };
    element.accept(lambdaCollector);
    // add initial lambda if we're inside already
    PsiElement method = getContainingMethod(element);
    if (method instanceof PsiLambdaExpression) {
      lambdas.add((PsiLambdaExpression) method);
    }
    for (PsiElement sibling = getNextElement(element);
        sibling != null;
        sibling = getNextElement(sibling)) {
      if (!intersects(lineRange, sibling)) {
        break;
      }
      sibling.accept(lambdaCollector);
    }
    return lambdas;
  }
Exemplo n.º 10
0
 public static JVMName getJVMQualifiedName(PsiClass psiClass) {
   if (!PsiUtil.isLocalOrAnonymousClass(psiClass)) {
     final String name = getNonAnonymousClassName(psiClass);
     if (name != null) {
       return getJVMRawText(name);
     }
   }
   return new JVMClassAt(SourcePosition.createFromElement(psiClass));
 }
 @Override
 public void reload() {
   ApplicationManager.getApplication().assertReadAccessAllowed();
   final XSourcePosition position = myXBreakpoint.getSourcePosition();
   try {
     final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(position.getFile());
     mySourcePosition = SourcePosition.createFromOffset(psiFile, position.getOffset());
   } catch (Exception e) {
     mySourcePosition = null;
   }
   reload(BreakpointManager.getPsiFile(myXBreakpoint, myProject));
 }
 @Override
 public void reload() {
   ApplicationManager.getApplication().assertReadAccessAllowed();
   XSourcePosition position = myXBreakpoint.getSourcePosition();
   PsiFile psiFile = getPsiFile();
   if (position != null && psiFile != null) {
     mySourcePosition = SourcePosition.createFromLine(psiFile, position.getLine());
     reload(psiFile);
   } else {
     mySourcePosition = null;
   }
 }
  public boolean moveTo(@NotNull SourcePosition position) {
    if (!canMoveTo(position)) {
      return false;
    }
    final PsiFile psiFile = position.getFile();
    final PsiFile oldFile = getSourcePosition().getFile();
    final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(psiFile);
    final Document oldDocument = PsiDocumentManager.getInstance(getProject()).getDocument(oldFile);
    if (document == null || oldDocument == null) {
      return false;
    }
    final RangeHighlighter newHighlighter =
        createHighlighter(myProject, document, position.getLine());
    if (newHighlighter == null) {
      return false;
    }
    final RangeHighlighter oldHighlighter = myHighlighter;
    myHighlighter = newHighlighter;

    reload();

    if (!isValid()) {
      myHighlighter.dispose();
      myHighlighter = oldHighlighter;
      reload();
      return false;
    }

    if (oldHighlighter != null) {
      oldHighlighter.dispose();
    }

    DebuggerManagerEx.getInstanceEx(getProject())
        .getBreakpointManager()
        .fireBreakpointChanged(this);
    updateUI();

    return true;
  }
  private void update(@NotNull List<BreakpointWithHighlighter> breakpoints) {
    final TIntHashSet intHash = new TIntHashSet();
    for (BreakpointWithHighlighter breakpoint : breakpoints) {
      SourcePosition sourcePosition = breakpoint.getSourcePosition();
      breakpoint.reload();

      if (breakpoint.isValid()) {
        if (sourcePosition == null
            || breakpoint.getSourcePosition().getLine() != sourcePosition.getLine()) {
          fireBreakpointChanged(breakpoint);
        }

        if (intHash.contains(breakpoint.getLineIndex())) {
          remove(breakpoint);
        } else {
          intHash.add(breakpoint.getLineIndex());
        }
      } else {
        remove(breakpoint);
      }
    }
  }
 private boolean isInScopeOf(DebugProcessImpl debugProcess, String className) {
   final SourcePosition position = getSourcePosition();
   if (position != null) {
     final VirtualFile breakpointFile = position.getFile().getVirtualFile();
     final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
     if (breakpointFile != null && fileIndex.isInSourceContent(breakpointFile)) {
       // apply filtering to breakpoints from content sources only, not for sources attached to
       // libraries
       final Collection<VirtualFile> candidates =
           findClassCandidatesInSourceContent(className, debugProcess.getSearchScope(), fileIndex);
       if (candidates == null) {
         return true;
       }
       for (VirtualFile classFile : candidates) {
         if (breakpointFile.equals(classFile)) {
           return true;
         }
       }
       return false;
     }
   }
   return true;
 }
Exemplo n.º 16
0
  @Nullable
  public static String getSourcePositionClassDisplayName(
      DebugProcessImpl debugProcess, @Nullable SourcePosition position) {
    if (position == null) {
      return null;
    }
    final PsiFile positionFile = position.getFile();
    if (positionFile instanceof JspFile) {
      return positionFile.getName();
    }

    final PsiClass psiClass = getClassAt(position);

    if (psiClass != null) {
      final String qName = psiClass.getQualifiedName();
      if (qName != null) {
        return qName;
      }
    }

    if (debugProcess != null && debugProcess.isAttached()) {
      List<ReferenceType> allClasses = debugProcess.getPositionManager().getAllClasses(position);
      if (!allClasses.isEmpty()) {
        return allClasses.get(0).name();
      }
    }
    if (psiClass == null) {
      if (positionFile instanceof PsiClassOwner) {
        return positionFile.getName();
      }

      return DebuggerBundle.message(
          "string.file.line.position", positionFile.getName(), position.getLine());
    }
    return calcClassDisplayName(psiClass);
  }
Exemplo n.º 17
0
  @Nullable
  public static String getSourcePositionPackageDisplayName(
      DebugProcessImpl debugProcess, @Nullable SourcePosition position) {
    if (position == null) {
      return null;
    }
    final PsiFile positionFile = position.getFile();
    if (positionFile instanceof JspFile) {
      final PsiDirectory dir = positionFile.getContainingDirectory();
      return dir != null ? dir.getVirtualFile().getPresentableUrl() : null;
    }

    final PsiClass psiClass = getClassAt(position);

    if (psiClass != null) {
      PsiClass toplevel = PsiUtil.getTopLevelClass(psiClass);
      if (toplevel != null) {
        String qName = toplevel.getQualifiedName();
        if (qName != null) {
          int i = qName.lastIndexOf('.');
          return i > 0 ? qName.substring(0, i) : "";
        }
      }
    }

    if (positionFile instanceof PsiClassOwner) {
      String name = ((PsiClassOwner) positionFile).getPackageName();
      if (!StringUtil.isEmpty(name)) {
        return name;
      }
    }

    if (debugProcess != null && debugProcess.isAttached()) {
      List<ReferenceType> allClasses = debugProcess.getPositionManager().getAllClasses(position);
      if (!allClasses.isEmpty()) {
        final String className = allClasses.get(0).name();
        int dotIndex = className.lastIndexOf('.');
        if (dotIndex >= 0) {
          return className.substring(0, dotIndex);
        }
      }
    }
    return "";
  }
 @Override
 public SourcePosition getSourcePosition(final Location location) throws NoDataException {
   int _lineNumber = location.lineNumber();
   final int line = (_lineNumber - 1);
   final PsiFile psiFile = this._debugProcessExtensions.getPsiFile(this.process, location);
   boolean _equals = Objects.equal(psiFile, null);
   if (_equals) {
     throw NoDataException.INSTANCE;
   }
   SourcePosition _createFromLine = SourcePosition.createFromLine(psiFile, line);
   final AbstractTraceRegion trace =
       this._debugProcessExtensions.getTraceForJava(_createFromLine);
   boolean _equals_1 = Objects.equal(trace, null);
   if (_equals_1) {
     throw NoDataException.INSTANCE;
   }
   final URI sourceURI =
       this._debugProcessExtensions.findOriginalDeclaration(this.process, location);
   SourcePosition fallBack = null;
   SourcePosition secondaryFallBack = null;
   TreeIterator<AbstractTraceRegion> iter = trace.treeIterator();
   while (iter.hasNext()) {
     {
       final AbstractTraceRegion n = iter.next();
       int _myLineNumber = n.getMyLineNumber();
       boolean _tripleEquals = (_myLineNumber == line);
       if (_tripleEquals) {
         final ILocationData mergedAssociatedLocation = n.getMergedAssociatedLocation();
         if (((n.isUseForDebugging() && (n.getMyEndLineNumber() == line))
             && (mergedAssociatedLocation.getLineNumber()
                 == mergedAssociatedLocation.getEndLineNumber()))) {
           final PsiFile psi = this._debugProcessExtensions.getPsiFile(this.process, sourceURI);
           int _offset = mergedAssociatedLocation.getOffset();
           return SourcePosition.createFromOffset(psi, _offset);
         } else {
           final PsiFile psi_1 =
               this._debugProcessExtensions.getPsiFile(this.process, sourceURI);
           int _offset_1 = mergedAssociatedLocation.getOffset();
           SourcePosition _createFromOffset = SourcePosition.createFromOffset(psi_1, _offset_1);
           fallBack = _createFromOffset;
         }
       }
       int _myEndLineNumber = n.getMyEndLineNumber();
       boolean _tripleEquals_1 = (_myEndLineNumber == line);
       if (_tripleEquals_1) {
         final ILocationData mergedAssociatedLocation_1 = n.getMergedAssociatedLocation();
         final PsiFile psi_2 = this._debugProcessExtensions.getPsiFile(this.process, sourceURI);
         int _endLineNumber = mergedAssociatedLocation_1.getEndLineNumber();
         SourcePosition _createFromLine_1 = SourcePosition.createFromLine(psi_2, _endLineNumber);
         secondaryFallBack = _createFromLine_1;
       }
     }
   }
   SourcePosition _elvis = null;
   if (fallBack != null) {
     _elvis = fallBack;
   } else {
     _elvis = secondaryFallBack;
   }
   return _elvis;
 }
Exemplo n.º 19
0
 @Nullable
 public static PsiElement getContainingMethod(@NotNull SourcePosition position) {
   return getContainingMethod(position.getElementAt());
 }
Exemplo n.º 20
0
 public static boolean inTheMethod(@NotNull SourcePosition pos, @NotNull PsiElement method) {
   PsiElement elem = pos.getElementAt();
   if (elem == null) return false;
   return Comparing.equal(getContainingMethod(elem), method);
 }
  private boolean isInScopeOf(DebugProcessImpl debugProcess, String className) {
    final SourcePosition position = getSourcePosition();
    if (position != null) {
      final VirtualFile breakpointFile = position.getFile().getVirtualFile();
      final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
      if (breakpointFile != null
          && fileIndex.isUnderSourceRootOfType(breakpointFile, JavaModuleSourceRootTypes.SOURCES)) {
        if (debugProcess.getSearchScope().contains(breakpointFile)) {
          return true;
        }
        // apply filtering to breakpoints from content sources only, not for sources attached to
        // libraries
        final Collection<VirtualFile> candidates =
            findClassCandidatesInSourceContent(className, debugProcess.getSearchScope(), fileIndex);
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "Found "
                  + (candidates == null ? "null" : candidates.size())
                  + " candidate containing files for class "
                  + className);
        }
        if (candidates == null) {
          // If no candidates are found in scope then assume that class is loaded dynamically and
          // allow breakpoint
          return true;
        }

        // breakpointFile is not in scope here and there are some candidates in scope
        // for (VirtualFile classFile : candidates) {
        //  if (LOG.isDebugEnabled()) {
        //    LOG.debug("Breakpoint file: " + breakpointFile.getPath()+ "; candidate file: " +
        // classFile.getPath());
        //  }
        //  if (breakpointFile.equals(classFile)) {
        //    return true;
        //  }
        // }
        if (LOG.isDebugEnabled()) {
          final GlobalSearchScope scope = debugProcess.getSearchScope();
          final boolean contains = scope.contains(breakpointFile);
          final Project project = getProject();
          final List<VirtualFile> files =
              ContainerUtil.map(
                  JavaFullClassNameIndex.getInstance().get(className.hashCode(), project, scope),
                  new Function<PsiClass, VirtualFile>() {
                    @Override
                    public VirtualFile fun(PsiClass aClass) {
                      return aClass.getContainingFile().getVirtualFile();
                    }
                  });
          final List<VirtualFile> allFiles =
              ContainerUtil.map(
                  JavaFullClassNameIndex.getInstance()
                      .get(className.hashCode(), project, new EverythingGlobalScope(project)),
                  new Function<PsiClass, VirtualFile>() {
                    @Override
                    public VirtualFile fun(PsiClass aClass) {
                      return aClass.getContainingFile().getVirtualFile();
                    }
                  });
          final VirtualFile contentRoot = fileIndex.getContentRootForFile(breakpointFile);
          final Module module = fileIndex.getModuleForFile(breakpointFile);

          LOG.debug(
              "Did not find '"
                  + className
                  + "' in "
                  + scope
                  + "; contains="
                  + contains
                  + "; contentRoot="
                  + contentRoot
                  + "; module = "
                  + module
                  + "; all files in index are: "
                  + files
                  + "; all possible files are: "
                  + allFiles);
        }

        return false;
      }
    }
    return true;
  }
Exemplo n.º 22
0
 @Override
 public int getOffset() {
   return mySourcePosition.getOffset();
 }
 @Override
 protected void doAction(final SourcePosition sourcePosition) {
   if (sourcePosition != null) {
     sourcePosition.navigate(true);
   }
 }
 public int getLineIndex() {
   final SourcePosition sourcePosition = getSourcePosition();
   return sourcePosition != null ? sourcePosition.getLine() : -1;
 }
Exemplo n.º 25
0
 @Override
 public int getLine() {
   return mySourcePosition.getLine();
 }
Exemplo n.º 26
0
  private static Pair<Set<String>, Set<TextWithImports>> findReferencedVars(
      Set<String> visibleVars, SourcePosition position) {
    final int line = position.getLine();
    if (line < 0) {
      return Pair.create(Collections.<String>emptySet(), Collections.<TextWithImports>emptySet());
    }
    final PsiFile positionFile = position.getFile();
    if (!positionFile.getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
      return Pair.create(visibleVars, Collections.<TextWithImports>emptySet());
    }

    final VirtualFile vFile = positionFile.getVirtualFile();
    final Document doc =
        vFile != null ? FileDocumentManager.getInstance().getDocument(vFile) : null;
    if (doc == null || doc.getLineCount() == 0 || line > (doc.getLineCount() - 1)) {
      return Pair.create(Collections.<String>emptySet(), Collections.<TextWithImports>emptySet());
    }

    final TextRange limit = calculateLimitRange(positionFile, doc, line);

    int startLine = Math.max(limit.getStartOffset(), line - 1);
    startLine = Math.min(startLine, limit.getEndOffset());
    while (startLine > limit.getStartOffset() && shouldSkipLine(positionFile, doc, startLine)) {
      startLine--;
    }
    final int startOffset = doc.getLineStartOffset(startLine);

    int endLine = Math.min(line + 2, limit.getEndOffset());
    while (endLine < limit.getEndOffset() && shouldSkipLine(positionFile, doc, endLine)) {
      endLine++;
    }
    final int endOffset = doc.getLineEndOffset(endLine);

    final TextRange lineRange = new TextRange(startOffset, endOffset);
    if (!lineRange.isEmpty()) {
      final int offset =
          CharArrayUtil.shiftForward(doc.getCharsSequence(), doc.getLineStartOffset(line), " \t");
      PsiElement element = positionFile.findElementAt(offset);
      if (element != null) {
        PsiMethod method = PsiTreeUtil.getNonStrictParentOfType(element, PsiMethod.class);
        if (method != null) {
          element = method;
        } else {
          PsiField field = PsiTreeUtil.getNonStrictParentOfType(element, PsiField.class);
          if (field != null) {
            element = field;
          } else {
            final PsiClassInitializer initializer =
                PsiTreeUtil.getNonStrictParentOfType(element, PsiClassInitializer.class);
            if (initializer != null) {
              element = initializer;
            }
          }
        }

        //noinspection unchecked
        if (element instanceof PsiCompiledElement) {
          return Pair.create(visibleVars, Collections.<TextWithImports>emptySet());
        } else {
          VariablesCollector collector =
              new VariablesCollector(visibleVars, adjustRange(element, lineRange));
          element.accept(collector);
          return Pair.create(collector.getVars(), collector.getExpressions());
        }
      }
    }
    return Pair.create(Collections.<String>emptySet(), Collections.<TextWithImports>emptySet());
  }