# HG changeset patch # User Michael Van De Vanter # Date 1431046315 25200 # Node ID a43c7adc9d99d0554df5057bfc94158fbc6e7a67 # Parent 85d0088596e86a90f3446d519b0e1f9fa39b3f86 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity. diff -r 85d0088596e8 -r a43c7adc9d99 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java Thu May 07 15:04:02 2015 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java Thu May 07 17:51:55 2015 -0700 @@ -35,32 +35,36 @@ * Representation of a guest language source code unit and its contents. Sources originate in * several ways: * *

* File cache: *

    - *
  1. File content caching is optional, off by default.
  2. + *
  3. File content caching is optional, on by default.
  4. *
  5. The first access to source file contents will result in the contents being read, and (if * enabled) cached.
  6. *
  7. If file contents have been cached, access to contents via {@link Source#getInputStream()} or @@ -128,14 +132,24 @@ */ private static final List> allSources = Collections.synchronizedList(new ArrayList>()); - // Files and pseudo files are indexed. - private static final Map> filePathToSource = new HashMap<>(); + /** + * Index of all named sources. + */ + private static final Map> nameToSource = new HashMap<>(); private static boolean fileCacheEnabled = true; private static final List sourceListeners = new ArrayList<>(); /** + * Locates an existing instance by the name under which it was indexed. + */ + public static Source find(String name) { + final WeakReference nameRef = nameToSource.get(name); + return nameRef == null ? null : nameRef.get(); + } + + /** * Gets the canonical representation of a source file, whose contents will be read lazily and * then cached. * @@ -146,7 +160,7 @@ */ public static Source fromFileName(String fileName, boolean reset) throws IOException { - final WeakReference nameRef = filePathToSource.get(fileName); + final WeakReference nameRef = nameToSource.get(fileName); Source source = nameRef == null ? null : nameRef.get(); if (source == null) { final File file = new File(fileName); @@ -154,11 +168,11 @@ throw new IOException("Can't read file " + fileName); } final String path = file.getCanonicalPath(); - final WeakReference pathRef = filePathToSource.get(path); + final WeakReference pathRef = nameToSource.get(path); source = pathRef == null ? null : pathRef.get(); if (source == null) { source = new FileSource(file, fileName, path); - filePathToSource.put(path, new WeakReference<>(source)); + nameToSource.put(path, new WeakReference<>(source)); } } if (reset) { @@ -193,17 +207,17 @@ */ public static Source fromFileName(CharSequence chars, String fileName) throws IOException { - final WeakReference nameRef = filePathToSource.get(fileName); + final WeakReference nameRef = nameToSource.get(fileName); Source source = nameRef == null ? null : nameRef.get(); if (source == null) { final File file = new File(fileName); // We are going to trust that the fileName is readable. final String path = file.getCanonicalPath(); - final WeakReference pathRef = filePathToSource.get(path); + final WeakReference pathRef = nameToSource.get(path); source = pathRef == null ? null : pathRef.get(); if (source == null) { source = new FileSource(file, fileName, path, chars); - filePathToSource.put(path, new WeakReference<>(source)); + nameToSource.put(path, new WeakReference<>(source)); } } notifyNewSource(source).tagAs(Tags.FROM_FILE); @@ -211,8 +225,7 @@ } /** - * Creates a non-canonical source from literal text. If an already created literal source must - * be retrievable by name, use {@link #asPseudoFile(CharSequence, String)}. + * Creates an anonymous source from literal text: not named and not indexed. * * @param chars textual source code * @param description a note about the origin, for error messages and debugging @@ -226,6 +239,22 @@ } /** + * Creates a source from literal text that can be retrieved by name, with no assumptions about + * the structure or meaning of the name. If the name is already in the index, the new instance + * will replace the previously existing instance in the index. + * + * @param chars textual source code + * @param name string to use for indexing/lookup + * @return a newly created, source representation + */ + public static Source fromNamedText(CharSequence chars, String name) { + final Source source = new LiteralSource(name, chars.toString()); + nameToSource.put(name, new WeakReference<>(source)); + notifyNewSource(source).tagAs(Tags.FROM_LITERAL); + return source; + } + + /** * Creates a source whose contents will be read immediately from a URL and cached. * * @param url @@ -286,21 +315,6 @@ return source; } - /** - * Creates a source from literal text, but which acts as a file and can be retrieved by name - * (unlike other literal sources); intended for testing. - * - * @param chars textual source code - * @param pseudoFileName string to use for indexing/lookup - * @return a newly created, source representation, canonical with respect to its name - */ - public static Source asPseudoFile(CharSequence chars, String pseudoFileName) { - final Source source = new LiteralSource(pseudoFileName, chars.toString()); - filePathToSource.put(pseudoFileName, new WeakReference<>(source)); - notifyNewSource(source).tagAs(Tags.FROM_LITERAL); - return source; - } - // TODO (mlvdv) enable per-file choice whether to cache? /** * Enables/disables caching of file contents, disabled by default. Caching of sources