1313 *******************************************************************************/
1414package org .eclipse .jdt .internal .core .util ;
1515
16+ import java .lang .ref .SoftReference ;
17+
1618import org .eclipse .core .resources .IFile ;
1719import org .eclipse .core .runtime .CoreException ;
1820import org .eclipse .jdt .core .compiler .CharOperation ;
2426public class ResourceCompilationUnit implements ICompilationUnit {
2527
2628 private final IFile file ;
27- private char [] contents ;
29+ private volatile SoftReference < char []> contentRef ;
2830 private final char [] fileName ;
2931 private final char [] mainTypeName ;
3032 private final char [] module ;
@@ -46,15 +48,22 @@ public ResourceCompilationUnit(IFile file, char[] mod) {
4648
4749 @ Override
4850 public char [] getContents () {
49- if (this .contents != null )
50- return this .contents ; // answer the cached source
51-
52- // otherwise retrieve it
51+ SoftReference <char []> cr = this .contentRef ;
52+ if (cr != null ) {
53+ char [] cachedContents = cr .get ();
54+ if (cachedContents != null ) {
55+ return cachedContents ;
56+ }
57+ }
58+ char [] contents ;
5359 try {
54- return ( this . contents = Util .getResourceContentsAsCharArray (this .file ) );
60+ contents = Util .getResourceContentsAsCharArray (this .file );
5561 } catch (CoreException e ) {
56- return CharOperation .NO_CHAR ;
62+ contents = CharOperation .NO_CHAR ;
5763 }
64+ // softly cache the result:
65+ this .contentRef = new SoftReference <>(contents );
66+ return contents ;
5867 }
5968
6069 @ Override
0 commit comments