Java Code Examples for com.intellij.reference.SoftReference#dereference()

The following examples show how to use com.intellij.reference.SoftReference#dereference() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EditorHighlighterCache.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
  if (document == null) {
    return null;
  }
  final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
  final EditorHighlighter someEditorHighlighter = SoftReference.dereference(editorHighlighterWeakReference);

  if (someEditorHighlighter instanceof LexerEditorHighlighter &&
      ((LexerEditorHighlighter)someEditorHighlighter).isValid()
          ) {
    return someEditorHighlighter;
  }
  document.putUserData(ourSomeEditorSyntaxHighlighter, null);
  return null;
}
 
Example 2
Source File: ApiDebuggerBundle.java    From ApiDebugger with Apache License 2.0 5 votes vote down vote up
private static ResourceBundle getBundle() {
    ResourceBundle bundle = SoftReference.dereference(mBundleReference);
    if (bundle == null) {
        bundle = ResourceBundle.getBundle(BUNDLE);
        mBundleReference = new java.lang.ref.SoftReference<>(bundle);
    }
    return bundle;
}
 
Example 3
Source File: FrozenDocument.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private LineSet getLineSet() {
  LineSet lineSet = SoftReference.dereference(myLineSet);
  if (lineSet == null) {
    myLineSet = new SoftReference<>(lineSet = LineSet.createLineSet(myText));
  }
  return lineSet;
}
 
Example 4
Source File: IntelliJDeodorantBundle.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
private static ResourceBundle getBundle() {
    ResourceBundle bundle = SoftReference.dereference(INSTANCE);
    if (bundle == null) {
        bundle = ResourceBundle.getBundle(BUNDLE);
        INSTANCE = new SoftReference<>(bundle);
    }
    return bundle;
}
 
Example 5
Source File: ImagePreviewComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static JComponent getPreviewComponent(@Nullable final PsiElement parent) {
  if (parent == null) {
    return null;
  }
  final PsiReference[] references = parent.getReferences();
  for (final PsiReference reference : references) {
    final PsiElement fileItem = reference.resolve();
    if (fileItem instanceof PsiFileSystemItem) {
      final PsiFileSystemItem item = (PsiFileSystemItem)fileItem;
      if (!item.isDirectory()) {
        final VirtualFile file = item.getVirtualFile();
        if (file != null && supportedExtensions.contains(file.getExtension())) {
          try {
            refresh(file);
            SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
            final BufferedImage image = SoftReference.dereference(imageRef);
            if (image != null) {
              return new ImagePreviewComponent(image, file.getLength());
            }
          }
          catch (IOException ignored) {
            // nothing
          }
        }
      }
    }
  }

  return null;
}
 
Example 6
Source File: DocumentImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void getSaveRMTree(@Nonnull VirtualFile f, @Nonnull Key<Reference<RangeMarkerTree<RangeMarkerEx>>> key, @Nonnull RangeMarkerTree<RangeMarkerEx> tree) {
  RMTreeReference freshRef = new RMTreeReference(tree, f);
  Reference<RangeMarkerTree<RangeMarkerEx>> oldRef;
  do {
    oldRef = f.getUserData(key);
  }
  while (!f.replace(key, oldRef, freshRef));
  RangeMarkerTree<RangeMarkerEx> oldTree = SoftReference.dereference(oldRef);

  if (oldTree == null) {
    // no tree was saved in virtual file before. happens when created new document.
    // or the old tree got gc-ed, because no reachable markers retaining it are left alive. good riddance.
    return;
  }

  // old tree was saved in the virtual file. Have to transfer markers from there.
  TextRange myDocumentRange = new TextRange(0, getTextLength());
  oldTree.processAll(r -> {
    if (r.isValid() && myDocumentRange.contains(r)) {
      registerRangeMarker(r, r.getStartOffset(), r.getEndOffset(), r.isGreedyToLeft(), r.isGreedyToRight(), 0);
    }
    else {
      ((RangeMarkerImpl)r).invalidate("document was gc-ed and re-created");
    }
    return true;
  });
}
 
Example 7
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
private DataContext getDataContext() {
  Component referenceComponent;
  if (myReferenceComponent == null) {
    referenceComponent = IdeFocusManager.getInstance(myManager.myProject).getFocusOwner();
    myReferenceComponent = new WeakReference<>(referenceComponent);
  }
  else {
    referenceComponent = SoftReference.dereference(myReferenceComponent);
    if (referenceComponent == null || !referenceComponent.isShowing()) referenceComponent = myHint.getComponent();
  }
  return DataManager.getInstance().getDataContext(referenceComponent);
}
 
Example 8
Source File: LeafElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public String getText() {
  CharSequence text = myText;
  if (text.length() > 1000 && !(text instanceof String)) { // e.g. a large text file
    String cachedText = SoftReference.dereference(getUserData(CACHED_TEXT));
    if (cachedText == null) {
      cachedText = text.toString();
      putUserData(CACHED_TEXT, new SoftReference<>(cachedText));
    }
    return cachedText;
  }

  return text.toString();
}
 
Example 9
Source File: RecursionManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
MemoizedValue getMemoizedValue(MyKey realKey) {
  List<SoftReference<MemoizedValue>> refs = intermediateCache.get(realKey);
  if (refs != null) {
    for (SoftReference<MemoizedValue> ref : refs) {
      MemoizedValue value = SoftReference.dereference(ref);
      if (value != null && value.isActual(this)) {
        return value;
      }
    }
  }
  return null;
}
 
Example 10
Source File: StatisticsManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private StatisticsUnit getUnit(int unitNumber) {
  StatisticsUnit unit = SoftReference.dereference(myUnits.get(unitNumber));
  if (unit != null) return unit;
  unit = loadUnit(unitNumber);
  if (unit == null){
    unit = new StatisticsUnit(unitNumber);
  }
  myUnits.set(unitNumber, new SoftReference<>(unit));
  return unit;
}
 
Example 11
Source File: FrozenDocument.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public String getText() {
  String s = SoftReference.dereference(myTextString);
  if (s == null) {
    myTextString = new SoftReference<>(s = myText.toString());
  }
  return s;
}
 
Example 12
Source File: DnDManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public Component getLastDropHandler() {
  return SoftReference.dereference(myLastDropHandler);
}
 
Example 13
Source File: IdeRepaintManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void checkThreadViolations(JComponent c) {
  if (!SwingUtilities.isEventDispatchThread() && c.isShowing()) {
    boolean repaint = false;
    boolean fromSwing = false;
    boolean swingKnownNonAwtOperations = false;
    final Exception exception = new Exception();
    StackTraceElement[] stackTrace = exception.getStackTrace();
    for (StackTraceElement st : stackTrace) {
      String className = st.getClassName();
      String methodName = st.getMethodName();

      if (repaint && className.startsWith("javax.swing.")) {
        fromSwing = true;
      }
      if (repaint && "imageUpdate".equals(methodName)) {
        swingKnownNonAwtOperations = true;
      }

      if ("read".equals(methodName) && className.startsWith("javax.swing.JEditorPane") ||
          "setCharacterAttributes".equals(methodName) && className.startsWith("javax.swing.text.DefaultStyledDocument")) {
        swingKnownNonAwtOperations = true;
        break;
      }

      if ("repaint".equals(methodName)) {
        repaint = true;
        fromSwing = false;
      }
    }
    if (swingKnownNonAwtOperations) {
      return;
    }
    if (repaint && !fromSwing) {
      //no problems here, since repaint() is thread safe
      return;
    }
    //ignore the last processed component
    if (SoftReference.dereference(myLastComponent) == c) {
      return;
    }
    myLastComponent = new WeakReference<>(c);

    LOG.warn("Access to realized (ever shown) UI components should be done only from the AWT event dispatch thread," +
             " revalidate(), invalidate() & repaint() is ok from any thread", exception);
  }
}
 
Example 14
Source File: EditorNotificationsImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static ProgressIndicator getCurrentProgress(VirtualFile file) {
  return SoftReference.dereference(file.getUserData(CURRENT_UPDATES));
}
 
Example 15
Source File: DialogWrapperPeerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private Project getProject() {
  return SoftReference.dereference(myProject);
}
 
Example 16
Source File: KeyProcessorContext.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public Component getFocusOwner() {
  return SoftReference.dereference(myFocusOwner);
}
 
Example 17
Source File: EditorMouseHoverPopupManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
private HighlightInfo getHighlightInfo() {
  return SoftReference.dereference(highlightInfo);
}
 
Example 18
Source File: BaseDataManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
protected C getComponent() {
  return SoftReference.dereference(myRef);
}
 
Example 19
Source File: QuickDocOnMouseOverManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private DocumentationManager getDocManager() {
  return SoftReference.dereference(myDocumentationManager);
}
 
Example 20
Source File: FileTrees.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
StubTree derefStub() {
  return SoftReference.dereference(myStub);
}