diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/ResourceCompilationUnit.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/ResourceCompilationUnit.java index d1ab9506672..fe8741fe793 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/ResourceCompilationUnit.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/ResourceCompilationUnit.java @@ -13,6 +13,8 @@ *******************************************************************************/ package org.eclipse.jdt.internal.core.util; +import java.lang.ref.SoftReference; + import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.compiler.CharOperation; @@ -24,7 +26,7 @@ public class ResourceCompilationUnit implements ICompilationUnit { private final IFile file; - private char[] contents; + private volatile SoftReference contentRef; private final char[] fileName; private final char[] mainTypeName; private final char[] module; @@ -46,15 +48,22 @@ public ResourceCompilationUnit(IFile file, char[] mod) { @Override public char[] getContents() { - if (this.contents != null) - return this.contents; // answer the cached source - - // otherwise retrieve it + SoftReference cr = this.contentRef; + if (cr != null) { + char[] cachedContents = cr.get(); + if (cachedContents != null) { + return cachedContents; + } + } + char[] contents; try { - return (this.contents = Util.getResourceContentsAsCharArray(this.file)); + contents = Util.getResourceContentsAsCharArray(this.file); } catch (CoreException e) { - return CharOperation.NO_CHAR; + contents = CharOperation.NO_CHAR; } + // softly cache the result: + this.contentRef = new SoftReference<>(contents); + return contents; } @Override