diff truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 22093:0f0e34039769

Truffle/instrumentation: remove SourceTag machinery, not used so far. It will eventually be restored in the new API framework.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Mon, 17 Aug 2015 15:24:00 -0700
parents e7c2d36daf72
children 0556c0056d71
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java	Mon Aug 17 17:40:00 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java	Mon Aug 17 15:24:00 2015 -0700
@@ -27,15 +27,11 @@
 import java.io.*;
 import java.lang.ref.*;
 import java.net.*;
+import java.nio.*;
+import java.nio.charset.*;
 import java.util.*;
 
 import com.oracle.truffle.api.*;
-import com.oracle.truffle.api.instrument.*;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
-import java.nio.charset.CharacterCodingException;
-import java.nio.charset.Charset;
-import java.nio.charset.CharsetDecoder;
 
 /**
  * Representation of a guest language source code unit and its contents. Sources originate in
@@ -89,65 +85,12 @@
  * reload.</li>
  * </ol>
  * <p>
- *
- * @see SourceTag
- * @see SourceListener
  */
 public abstract class Source {
 
     // TODO (mlvdv) consider canonicalizing and reusing SourceSection instances
     // TOOD (mlvdv) connect SourceSections into a spatial tree for fast geometric lookup
 
-    public enum Tags implements SourceTag {
-
-        /**
-         * From bytes.
-         */
-        FROM_BYTES("bytes", "read from bytes"),
-
-        /**
-         * Read from a file.
-         */
-        FROM_FILE("file", "read from a file"),
-
-        /**
-         * From literal text.
-         */
-        FROM_LITERAL("literal", "from literal text"),
-
-        /**
-         * From a {@linkplain java.io.Reader Reader}.
-         */
-        FROM_READER("reader", "read from a Java Reader"),
-
-        /**
-         * Read from a URL.
-         */
-        FROM_URL("URL", "read from a URL");
-
-        private final String name;
-        private final String description;
-
-        private Tags(String name, String description) {
-            this.name = name;
-            this.description = description;
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public String getDescription() {
-            return description;
-        }
-
-    }
-
-    /**
-     * All Sources that have been created.
-     */
-    private static final List<WeakReference<Source>> allSources = Collections.synchronizedList(new ArrayList<WeakReference<Source>>());
-
     /**
      * Index of all named sources.
      */
@@ -155,8 +98,6 @@
 
     private static boolean fileCacheEnabled = true;
 
-    private static final List<SourceListener> sourceListeners = new ArrayList<>();
-
     /**
      * Locates an existing instance by the name under which it was indexed.
      */
@@ -194,7 +135,6 @@
         if (reset) {
             source.reset();
         }
-        notifyNewSource(source).tagAs(Tags.FROM_FILE);
         return source;
     }
 
@@ -237,7 +177,6 @@
                 nameToSource.put(path, new WeakReference<>(source));
             }
         }
-        notifyNewSource(source).tagAs(Tags.FROM_FILE);
         return source;
     }
 
@@ -250,11 +189,8 @@
      */
     public static Source fromText(CharSequence chars, String description) {
         CompilerAsserts.neverPartOfCompilation();
-
         assert chars != null;
-        final LiteralSource source = new LiteralSource(description, chars.toString());
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
-        return source;
+        return new LiteralSource(description, chars.toString());
     }
 
     /**
@@ -266,10 +202,7 @@
      */
     public static Source fromAppendableText(String description) {
         CompilerAsserts.neverPartOfCompilation();
-
-        final Source source = new AppendableLiteralSource(description);
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
-        return source;
+        return new AppendableLiteralSource(description);
     }
 
     /**
@@ -283,10 +216,8 @@
      */
     public static Source fromNamedText(CharSequence chars, String name) {
         CompilerAsserts.neverPartOfCompilation();
-
         final Source source = new LiteralSource(name, chars.toString());
         nameToSource.put(name, new WeakReference<>(source));
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
         return source;
     }
 
@@ -301,10 +232,8 @@
      */
     public static Source fromNamedAppendableText(String name) {
         CompilerAsserts.neverPartOfCompilation();
-
         final Source source = new AppendableLiteralSource(name);
         nameToSource.put(name, new WeakReference<>(source));
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
         return source;
     }
 
@@ -320,7 +249,6 @@
      */
     public static Source subSource(Source base, int baseCharIndex, int length) {
         CompilerAsserts.neverPartOfCompilation();
-
         final SubSource subSource = SubSource.create(base, baseCharIndex, length);
         return subSource;
     }
@@ -350,10 +278,7 @@
      */
     public static Source fromURL(URL url, String description) throws IOException {
         CompilerAsserts.neverPartOfCompilation();
-
-        final URLSource source = URLSource.get(url, description);
-        notifyNewSource(source).tagAs(Tags.FROM_URL);
-        return source;
+        return URLSource.get(url, description);
     }
 
     /**
@@ -366,10 +291,7 @@
      */
     public static Source fromReader(Reader reader, String description) throws IOException {
         CompilerAsserts.neverPartOfCompilation();
-
-        final LiteralSource source = new LiteralSource(description, read(reader));
-        notifyNewSource(source).tagAs(Tags.FROM_READER);
-        return source;
+        return new LiteralSource(description, read(reader));
     }
 
     /**
@@ -401,10 +323,7 @@
      */
     public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, Charset charset) {
         CompilerAsserts.neverPartOfCompilation();
-
-        final BytesSource source = new BytesSource(description, bytes, byteIndex, length, charset);
-        notifyNewSource(source).tagAs(Tags.FROM_BYTES);
-        return source;
+        return new BytesSource(description, bytes, byteIndex, length, charset);
     }
 
     // TODO (mlvdv) enable per-file choice whether to cache?
@@ -416,50 +335,6 @@
         fileCacheEnabled = enabled;
     }
 
-    /**
-     * Returns all {@link Source}s holding a particular {@link SyntaxTag}, or the whole collection
-     * of Sources if the specified tag is {@code null}.
-     *
-     * @return A collection of Sources containing the given tag.
-     */
-    public static Collection<Source> findSourcesTaggedAs(SourceTag tag) {
-        final List<Source> taggedSources = new ArrayList<>();
-        synchronized (allSources) {
-            for (WeakReference<Source> ref : allSources) {
-                Source source = ref.get();
-                if (source != null) {
-                    if (tag == null || source.isTaggedAs(tag)) {
-                        taggedSources.add(ref.get());
-                    }
-                }
-            }
-        }
-        return taggedSources;
-    }
-
-    /**
-     * Adds a {@link SourceListener} to receive events.
-     */
-    public static void addSourceListener(SourceListener listener) {
-        assert listener != null;
-        sourceListeners.add(listener);
-    }
-
-    /**
-     * Removes a {@link SourceListener}. Ignored if listener not found.
-     */
-    public static void removeSourceListener(SourceListener listener) {
-        sourceListeners.remove(listener);
-    }
-
-    private static Source notifyNewSource(Source source) {
-        allSources.add(new WeakReference<>(source));
-        for (SourceListener listener : sourceListeners) {
-            listener.sourceCreated(source);
-        }
-        return source;
-    }
-
     private static String read(Reader reader) throws IOException {
         final BufferedReader bufferedReader = new BufferedReader(reader);
         final StringBuilder builder = new StringBuilder();
@@ -479,8 +354,6 @@
         return builder.toString();
     }
 
-    private final ArrayList<SourceTag> tags = new ArrayList<>();
-
     private Source() {
     }
 
@@ -488,32 +361,6 @@
 
     abstract void reset();
 
-    public final boolean isTaggedAs(SourceTag tag) {
-        assert tag != null;
-        return tags.contains(tag);
-    }
-
-    public final Collection<SourceTag> getSourceTags() {
-        return Collections.unmodifiableCollection(tags);
-    }
-
-    /**
-     * Adds a {@linkplain SourceTag tag} to the set of tags associated with this {@link Source};
-     * {@code no-op} if already in the set.
-     *
-     * @return this
-     */
-    public final Source tagAs(SourceTag tag) {
-        assert tag != null;
-        if (!tags.contains(tag)) {
-            tags.add(tag);
-            for (SourceListener listener : sourceListeners) {
-                listener.sourceTaggedAs(this, tag);
-            }
-        }
-        return this;
-    }
-
     /**
      * Returns the name of this resource holding a guest language program. An example would be the
      * name of a guest language source code file.