From 90071c69b850a0f3c79d7a67b3b51f51af61bb80 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 28 Sep 2014 12:11:35 -0500 Subject: [PATCH 1/2] Fix getSourceName returning null (fixes #667) --- runtime/Java/src/org/antlr/v4/runtime/ANTLRInputStream.java | 4 ++++ .../Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java | 4 ++++ tool/test/org/antlr/v4/test/BaseTest.java | 2 +- tool/test/org/antlr/v4/test/TestCommonTokenStream.java | 5 +++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRInputStream.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRInputStream.java index d120b1c96..62c562e3f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRInputStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRInputStream.java @@ -238,6 +238,10 @@ public class ANTLRInputStream implements CharStream { @Override public String getSourceName() { + if (name == null || name.isEmpty()) { + return UNKNOWN_SOURCE_NAME; + } + return name; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java b/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java index d07e0ec21..eb23ae714 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java @@ -297,6 +297,10 @@ public class UnbufferedCharStream implements CharStream { @Override public String getSourceName() { + if (name == null || name.isEmpty()) { + return UNKNOWN_SOURCE_NAME; + } + return name; } diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index ee48dcf3b..0fafb0c72 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -1269,7 +1269,7 @@ public abstract class BaseTest { @Override public String getSourceName() { - return null; + return UNKNOWN_SOURCE_NAME; } @Override diff --git a/tool/test/org/antlr/v4/test/TestCommonTokenStream.java b/tool/test/org/antlr/v4/test/TestCommonTokenStream.java index a58451568..2578d093b 100644 --- a/tool/test/org/antlr/v4/test/TestCommonTokenStream.java +++ b/tool/test/org/antlr/v4/test/TestCommonTokenStream.java @@ -33,6 +33,7 @@ package org.antlr.v4.test; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.IntStream; import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.TokenFactory; @@ -235,7 +236,7 @@ public class TestCommonTokenStream extends TestBufferedTokenStream { @Override public String getSourceName() { - return null; + return IntStream.UNKNOWN_SOURCE_NAME; } @Override @@ -283,7 +284,7 @@ public class TestCommonTokenStream extends TestBufferedTokenStream { @Override public String getSourceName() { - return null; + return IntStream.UNKNOWN_SOURCE_NAME; } @Override From 065c3e68b870b22c4361f005da01f2b191f0fbd6 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 28 Sep 2014 12:11:50 -0500 Subject: [PATCH 2/2] Add additional NotNull and Nullable annotations --- .../src/org/antlr/v4/runtime/ANTLRFileStream.java | 8 +++++--- .../Java/src/org/antlr/v4/runtime/TokenSource.java | 1 + .../Java/src/org/antlr/v4/runtime/misc/Utils.java | 13 ++++++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRFileStream.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRFileStream.java index ba4a53136..b28b86125 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRFileStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRFileStream.java @@ -29,6 +29,8 @@ */ package org.antlr.v4.runtime; +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.runtime.misc.Utils; import java.io.IOException; @@ -40,16 +42,16 @@ import java.io.IOException; public class ANTLRFileStream extends ANTLRInputStream { protected String fileName; - public ANTLRFileStream(String fileName) throws IOException { + public ANTLRFileStream(@NotNull String fileName) throws IOException { this(fileName, null); } - public ANTLRFileStream(String fileName, String encoding) throws IOException { + public ANTLRFileStream(@NotNull String fileName, String encoding) throws IOException { this.fileName = fileName; load(fileName, encoding); } - public void load(String fileName, String encoding) + public void load(@NotNull String fileName, @Nullable String encoding) throws IOException { data = Utils.readFile(fileName, encoding); diff --git a/runtime/Java/src/org/antlr/v4/runtime/TokenSource.java b/runtime/Java/src/org/antlr/v4/runtime/TokenSource.java index 67e67b644..9567bee02 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/TokenSource.java +++ b/runtime/Java/src/org/antlr/v4/runtime/TokenSource.java @@ -91,6 +91,7 @@ public interface TokenSource { * non-null, non-empty string. If such a name is not known, this method * returns {@link IntStream#UNKNOWN_SOURCE_NAME}. */ + @NotNull public String getSourceName(); /** diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/Utils.java b/runtime/Java/src/org/antlr/v4/runtime/misc/Utils.java index 619afdcd6..3718ff00b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/Utils.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/Utils.java @@ -96,11 +96,11 @@ public class Utils { return buf.toString(); } - public static void writeFile(String fileName, String content) throws IOException { + public static void writeFile(@NotNull String fileName, @NotNull String content) throws IOException { writeFile(fileName, content, null); } - public static void writeFile(String fileName, String content, String encoding) throws IOException { + public static void writeFile(@NotNull String fileName, @NotNull String content, @Nullable String encoding) throws IOException { File f = new File(fileName); FileOutputStream fos = new FileOutputStream(f); OutputStreamWriter osw; @@ -119,14 +119,13 @@ public class Utils { } } - public static char[] readFile(String fileName) throws IOException { + @NotNull + public static char[] readFile(@NotNull String fileName) throws IOException { return readFile(fileName, null); } - public static char[] readFile(String fileName, String encoding) throws IOException { - if ( fileName==null ) { - return null; - } + @NotNull + public static char[] readFile(@NotNull String fileName, @Nullable String encoding) throws IOException { File f = new File(fileName); int size = (int)f.length(); InputStreamReader isr;