typed field instead of Object, cleanup

This commit is contained in:
Alexey Kudravtsev
2014-04-16 13:33:19 +04:00
parent 74bfdbf15c
commit ceb0932039
6 changed files with 49 additions and 42 deletions
@@ -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;
@@ -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<Document> HARD_REFERENCE_TO_DOCUMENT = new Key<Document>("HARD_REFERENCE_TO_DOCUMENT");
private volatile SoftReference<StubTree> myStub;
private volatile Reference<StubTree> 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<FileElement> myTreeElementPointer; // SoftReference/WeakReference to ASTNode or a strong reference to a tree if the file is a DummyHolder
public static final Key<Boolean> BUILDING_STUB = new Key<Boolean>("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<FileElement> 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<FileElement> createTreeElementPointer(@NotNull FileElement treeElement) {
if (isKeepTreeElementByHardReference()) {
return treeElement;
}
return myManager.isBatchFilesProcessingMode()
? new PatchedWeakReference<ASTNode>(treeElement)
: new SoftReference<ASTNode>(treeElement);
? new PatchedWeakReference<FileElement>(treeElement)
: new SoftReference<FileElement>(treeElement);
}
@Override
@@ -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<FileElement> {
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;
}
}
@@ -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<T> extends WeakReference<T>{
public class PatchedWeakReference<T> extends WeakReference<T> implements Getter<T> {
private static final Logger LOG = Logger.getInstance("#com.intellij.util.PatchedWeakReference");
private static List<PatchedWeakReference<?>> ourRefsList = new ArrayList<PatchedWeakReference<?>>();
@@ -47,19 +49,16 @@ public class PatchedWeakReference<T> extends WeakReference<T>{
}
}
/**
* 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;
@@ -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.
@@ -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<T> extends java.lang.ref.SoftReference<T> {
public class SoftReference<T> extends java.lang.ref.SoftReference<T> implements Getter<T> {
//private final T myReferent;
public SoftReference(final T referent) {
@@ -50,4 +51,8 @@ public class SoftReference<T> extends java.lang.ref.SoftReference<T> {
public static <T> T dereference(@Nullable Reference<T> ref) {
return ref == null ? null : ref.get();
}
@Nullable
public static <T> T deref(@Nullable Getter<T> ref) {
return ref == null ? null : ref.get();
}
}