diff --git a/platform/core-impl/src/com/intellij/psi/impl/source/DummyHolder.java b/platform/core-impl/src/com/intellij/psi/impl/source/DummyHolder.java index d94f00b400ed..3aae3c09dcba 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/source/DummyHolder.java +++ b/platform/core-impl/src/com/intellij/psi/impl/source/DummyHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -150,15 +150,15 @@ public class DummyHolder extends PsiFileImpl { @Override @SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException"}) protected PsiFileImpl clone() { - final PsiFileImpl psiFile = cloneImpl(myFileElement); + final PsiFileImpl psiClone = cloneImpl(myFileElement); final DummyHolderViewProvider dummyHolderViewProvider = new DummyHolderViewProvider(getManager()); myViewProvider = dummyHolderViewProvider; - dummyHolderViewProvider.setDummyHolder((DummyHolder)psiFile); + dummyHolderViewProvider.setDummyHolder((DummyHolder)psiClone); final FileElement treeClone = (FileElement)calcTreeElement().clone(); - psiFile.setTreeElementPointer(treeClone); // should not use setTreeElement here because cloned file still have VirtualFile (SCR17963) - psiFile.myOriginalFile = isPhysical() ? this : myOriginalFile; - treeClone.setPsi(psiFile); - return psiFile; + psiClone.setTreeElementPointer(treeClone); // should not use setTreeElement here because cloned file still have VirtualFile (SCR17963) + psiClone.myOriginalFile = isPhysical() ? this : myOriginalFile; + treeClone.setPsi(psiClone); + return psiClone; } private FileViewProvider myViewProvider = null; diff --git a/platform/core-impl/src/com/intellij/psi/impl/source/PsiFileImpl.java b/platform/core-impl/src/com/intellij/psi/impl/source/PsiFileImpl.java index c6b4d0ffb12d..21da2548da51 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/source/PsiFileImpl.java +++ b/platform/core-impl/src/com/intellij/psi/impl/source/PsiFileImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import com.intellij.openapi.editor.Document; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Queryable; +import com.intellij.openapi.util.Getter; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; @@ -74,9 +75,9 @@ public abstract class PsiFileImpl extends ElementBase implements PsiFileEx, PsiF protected PsiFile myOriginalFile = null; private final FileViewProvider myViewProvider; private static final Key HARD_REFERENCE_TO_DOCUMENT = new Key("HARD_REFERENCE_TO_DOCUMENT"); - private volatile SoftReference myStub; + private volatile Reference myStub; protected final PsiManagerEx myManager; - private volatile Object myTreeElementPointer; // SoftReference/WeakReference to ASTNode or a strong reference to a tree if the file is a DummyHolder + private volatile Getter myTreeElementPointer; // SoftReference/WeakReference to ASTNode or a strong reference to a tree if the file is a DummyHolder public static final Key BUILDING_STUB = new Key("Don't use stubs mark!"); protected PsiFileImpl(@NotNull IElementType elementType, IElementType contentElementType, @NotNull FileViewProvider provider) { @@ -129,18 +130,13 @@ public abstract class PsiFileImpl extends ElementBase implements PsiFileEx, PsiF } private FileElement derefTreeElement() { - final Object pointer = myTreeElementPointer; - if (pointer instanceof FileElement) { - return (FileElement)pointer; - } - if (pointer instanceof Reference) { - FileElement treeElement = (FileElement)((Reference)pointer).get(); - if (treeElement != null) return treeElement; + Getter pointer = myTreeElementPointer; + FileElement treeElement = SoftReference.deref(pointer); + if (treeElement != null) return treeElement; - synchronized (PsiLock.LOCK) { - if (myTreeElementPointer == pointer) { - myTreeElementPointer = null; - } + synchronized (PsiLock.LOCK) { + if (myTreeElementPointer == pointer) { + myTreeElementPointer = null; } } return null; @@ -468,7 +464,7 @@ public abstract class PsiFileImpl extends ElementBase implements PsiFileEx, PsiF if (getTreeElement() != null) { // not set by provider in clone final FileElement treeClone = (FileElement)calcTreeElement().clone(); - clone.myTreeElementPointer = treeClone; // should not use setTreeElement here because cloned file still have VirtualFile (SCR17963) + clone.setTreeElementPointer(treeClone); // should not use setTreeElement here because cloned file still have VirtualFile (SCR17963) treeClone.setPsi(clone); } @@ -703,7 +699,7 @@ public abstract class PsiFileImpl extends ElementBase implements PsiFileEx, PsiF protected PsiFileImpl cloneImpl(FileElement treeElementClone) { PsiFileImpl clone = (PsiFileImpl)super.clone(); - clone.myTreeElementPointer = treeElementClone; // should not use setTreeElement here because cloned file still have VirtualFile (SCR17963) + clone.setTreeElementPointer(treeElementClone); // should not use setTreeElement here because cloned file still have VirtualFile (SCR17963) treeElementClone.setPsi(clone); return clone; } @@ -712,13 +708,14 @@ public abstract class PsiFileImpl extends ElementBase implements PsiFileEx, PsiF return !getViewProvider().isEventSystemEnabled(); } - private Object createTreeElementPointer(ASTNode treeElement) { + @NotNull + private Getter createTreeElementPointer(@NotNull FileElement treeElement) { if (isKeepTreeElementByHardReference()) { return treeElement; } return myManager.isBatchFilesProcessingMode() - ? new PatchedWeakReference(treeElement) - : new SoftReference(treeElement); + ? new PatchedWeakReference(treeElement) + : new SoftReference(treeElement); } @Override diff --git a/platform/core-impl/src/com/intellij/psi/impl/source/tree/FileElement.java b/platform/core-impl/src/com/intellij/psi/impl/source/tree/FileElement.java index ed2ba0d60336..f025de75b820 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/source/tree/FileElement.java +++ b/platform/core-impl/src/com/intellij/psi/impl/source/tree/FileElement.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package com.intellij.psi.impl.source.tree; import com.intellij.lang.ASTNode; import com.intellij.lang.FileASTNode; +import com.intellij.openapi.util.Getter; import com.intellij.psi.impl.PsiManagerEx; import com.intellij.psi.impl.source.CharTableImpl; import com.intellij.psi.impl.source.PsiFileImpl; @@ -25,7 +26,7 @@ import com.intellij.psi.tree.IElementType; import com.intellij.util.CharTable; import org.jetbrains.annotations.NotNull; -public class FileElement extends LazyParseableElement implements FileASTNode { +public class FileElement extends LazyParseableElement implements FileASTNode, Getter { private volatile CharTable myCharTable = new CharTableImpl(); @Override @@ -60,4 +61,9 @@ public class FileElement extends LazyParseableElement implements FileASTNode { public void setCharTable(CharTable table) { myCharTable = table; } + + @Override + public FileElement get() { + return this; + } } diff --git a/platform/core-impl/src/com/intellij/util/PatchedWeakReference.java b/platform/core-impl/src/com/intellij/util/PatchedWeakReference.java index 408216276b93..b3c5367f8215 100644 --- a/platform/core-impl/src/com/intellij/util/PatchedWeakReference.java +++ b/platform/core-impl/src/com/intellij/util/PatchedWeakReference.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,15 +17,17 @@ package com.intellij.util; import com.intellij.concurrency.JobScheduler; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.Getter; import org.jetbrains.annotations.TestOnly; +import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; -public class PatchedWeakReference extends WeakReference{ +public class PatchedWeakReference extends WeakReference implements Getter { private static final Logger LOG = Logger.getInstance("#com.intellij.util.PatchedWeakReference"); private static List> ourRefsList = new ArrayList>(); @@ -47,19 +49,16 @@ public class PatchedWeakReference extends WeakReference{ } } - /** - * public for being accessible from the degenerator as timer stuff does not work. - */ - public static void processQueue() { + private static void processQueue() { boolean haveClearedRefs = false; while(true){ - PatchedWeakReference ref = (PatchedWeakReference)ourQueue.poll(); - if (ref != null){ - haveClearedRefs = true; - } - else{ + Reference ref = ourQueue.poll(); + if (ref == null) { break; } + else { + haveClearedRefs = true; + } } if (!haveClearedRefs) return; diff --git a/platform/util/src/com/intellij/openapi/util/Getter.java b/platform/util-rt/src/com/intellij/openapi/util/Getter.java similarity index 94% rename from platform/util/src/com/intellij/openapi/util/Getter.java rename to platform/util-rt/src/com/intellij/openapi/util/Getter.java index c31bf826ced3..8d8334dde675 100644 --- a/platform/util/src/com/intellij/openapi/util/Getter.java +++ b/platform/util-rt/src/com/intellij/openapi/util/Getter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/platform/util-rt/src/com/intellij/reference/SoftReference.java b/platform/util-rt/src/com/intellij/reference/SoftReference.java index fa1065271409..4ec7495de605 100644 --- a/platform/util-rt/src/com/intellij/reference/SoftReference.java +++ b/platform/util-rt/src/com/intellij/reference/SoftReference.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package com.intellij.reference; +import com.intellij.openapi.util.Getter; import org.jetbrains.annotations.Nullable; import java.lang.ref.Reference; @@ -28,7 +29,7 @@ import java.lang.ref.ReferenceQueue; * @author max */ @SuppressWarnings("ClassNameSameAsAncestorName") -public class SoftReference extends java.lang.ref.SoftReference { +public class SoftReference extends java.lang.ref.SoftReference implements Getter { //private final T myReferent; public SoftReference(final T referent) { @@ -50,4 +51,8 @@ public class SoftReference extends java.lang.ref.SoftReference { public static T dereference(@Nullable Reference ref) { return ref == null ? null : ref.get(); } + @Nullable + public static T deref(@Nullable Getter ref) { + return ref == null ? null : ref.get(); + } }