public class SourceFile extends java.lang.Object implements StaticSourceFile, java.io.Serializable
Modifier and Type | Class and Description |
---|---|
(package private) static class |
SourceFile.AtUrl
A source file at a URL where the code is only read into memory if absolutely
necessary.
|
static class |
SourceFile.Builder
A builder interface for source files.
|
(package private) static class |
SourceFile.Generated
A source file where the code will be dynamically generated
from the injected interface.
|
static interface |
SourceFile.Generator
A JavaScript source code provider.
|
(package private) static class |
SourceFile.OnDisk
A source file where the code is only read into memory if absolutely
necessary.
|
(package private) static class |
SourceFile.Preloaded
A source file where the code has been preloaded.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
code |
(package private) static DiagnosticType |
DUPLICATE_ZIP_CONTENTS |
private java.lang.String |
fileName |
private boolean |
isExternFile |
private int[] |
lineOffsets |
private java.lang.String |
originalPath |
private static long |
serialVersionUID |
private static int |
SOURCE_EXCERPT_REGION_LENGTH
Number of lines in the region returned by
getRegion(int) . |
private static java.lang.String |
UTF8_BOM |
Constructor and Description |
---|
SourceFile(java.lang.String fileName)
Construct a new abstract source file.
|
Modifier and Type | Method and Description |
---|---|
static SourceFile.Builder |
builder()
Create a new builder for source files.
|
void |
clearCachedSource() |
private void |
findLineOffsets() |
static SourceFile |
fromCode(java.lang.String fileName,
java.lang.String code) |
static SourceFile |
fromFile(File file) |
static SourceFile |
fromFile(File file,
java.nio.charset.Charset c) |
static SourceFile |
fromFile(java.lang.String fileName) |
static SourceFile |
fromFile(java.lang.String fileName,
java.nio.charset.Charset c) |
static SourceFile |
fromGenerator(java.lang.String fileName,
SourceFile.Generator generator) |
static SourceFile |
fromInputStream(java.lang.String fileName,
java.io.InputStream s)
Deprecated.
|
static SourceFile |
fromInputStream(java.lang.String fileName,
java.io.InputStream s,
java.nio.charset.Charset charset) |
static SourceFile |
fromReader(java.lang.String fileName,
java.io.Reader r) |
static java.util.List<SourceFile> |
fromZipFile(java.lang.String zipName,
java.nio.charset.Charset inputCharset) |
java.lang.String |
getCode()
Gets all the code in this source file.
|
com.google.common.io.CharSource |
getCodeCharSource()
Gets a char source for the code in this source file.
|
(package private) java.lang.String |
getCodeNoCache() |
java.io.Reader |
getCodeReader()
Gets a reader for the code in this source file.
|
int |
getColumnOfOffset(int offset)
Gets the 0-based column number of the given source offset.
|
java.lang.String |
getLine(int lineNumber)
Gets the source line for the indicated line number.
|
int |
getLineOffset(int lineno)
Returns the offset of the given line number relative to the file start.
|
int |
getLineOfOffset(int offset)
Gets the 1-based line number of the given source offset.
|
java.lang.String |
getName()
Returns a unique name for the source file.
|
(package private) int |
getNumLines() |
java.lang.String |
getOriginalPath() |
Region |
getRegion(int lineNumber)
Get a region around the indicated line number.
|
(package private) boolean |
hasSourceInMemory() |
boolean |
isExtern()
Returns whether this is an extern.
|
private void |
resetLineOffsets() |
(package private) void |
setCode(java.lang.String sourceCode) |
(package private) void |
setCode(java.lang.String sourceCode,
boolean removeUtf8Bom) |
(package private) void |
setIsExtern(boolean newVal)
Sets that this is an extern.
|
void |
setOriginalPath(java.lang.String originalPath) |
java.lang.String |
toString() |
private static final long serialVersionUID
private static final java.lang.String UTF8_BOM
private static final int SOURCE_EXCERPT_REGION_LENGTH
getRegion(int)
.
This length must be odd.private final java.lang.String fileName
private boolean isExternFile
private java.lang.String originalPath
private int[] lineOffsets
private java.lang.String code
static final DiagnosticType DUPLICATE_ZIP_CONTENTS
public SourceFile(java.lang.String fileName)
fileName
- The file name of the source file. It does not necessarily
need to correspond to a real path. But it should be unique. Will
appear in warning messages emitted by the compiler.public int getLineOffset(int lineno)
StaticSourceFile
getLineOffset
in interface StaticSourceFile
lineno
- the line of the input to get the absolute offset of.int getNumLines()
private void findLineOffsets()
private void resetLineOffsets()
public java.lang.String getCode() throws java.io.IOException
java.io.IOException
@GwtIncompatible(value="com.google.common.io.CharSource") public com.google.common.io.CharSource getCodeCharSource()
@GwtIncompatible(value="java.io.Reader") public java.io.Reader getCodeReader() throws java.io.IOException
java.io.IOException
java.lang.String getCodeNoCache()
void setCode(java.lang.String sourceCode)
void setCode(java.lang.String sourceCode, boolean removeUtf8Bom)
public java.lang.String getOriginalPath()
public void setOriginalPath(java.lang.String originalPath)
public void clearCachedSource()
boolean hasSourceInMemory()
public java.lang.String getName()
getName
in interface StaticSourceFile
public boolean isExtern()
isExtern
in interface StaticSourceFile
void setIsExtern(boolean newVal)
public int getLineOfOffset(int offset)
StaticSourceFile
getLineOfOffset
in interface StaticSourceFile
offset
- An absolute file offset.public int getColumnOfOffset(int offset)
StaticSourceFile
getColumnOfOffset
in interface StaticSourceFile
offset
- An absolute file offset.public java.lang.String getLine(int lineNumber)
lineNumber
- the line number, 1 being the first line of the file.null
if it does not exist,
or if there was an IO exception.public Region getRegion(int lineNumber)
lineNumber
- the line number, 1 being the first line of the file.null
if it does not exist,
or if there was an IO exception.public java.lang.String toString()
toString
in class java.lang.Object
@GwtIncompatible(value="java.util.zip.ZipFile") public static java.util.List<SourceFile> fromZipFile(java.lang.String zipName, java.nio.charset.Charset inputCharset) throws java.io.IOException
java.io.IOException
@GwtIncompatible(value="java.io.File") public static SourceFile fromFile(java.lang.String fileName, java.nio.charset.Charset c)
public static SourceFile fromFile(java.lang.String fileName)
@GwtIncompatible(value="java.io.File") public static SourceFile fromFile(File file, java.nio.charset.Charset c)
@GwtIncompatible(value="java.io.File") public static SourceFile fromFile(File file)
public static SourceFile fromCode(java.lang.String fileName, java.lang.String code)
@Deprecated @GwtIncompatible(value="java.io.InputStream") public static SourceFile fromInputStream(java.lang.String fileName, java.io.InputStream s) throws java.io.IOException
fromInputStream(String, InputStream, Charset)
java.io.IOException
@GwtIncompatible(value="java.io.InputStream") public static SourceFile fromInputStream(java.lang.String fileName, java.io.InputStream s, java.nio.charset.Charset charset) throws java.io.IOException
java.io.IOException
@GwtIncompatible(value="java.io.Reader") public static SourceFile fromReader(java.lang.String fileName, java.io.Reader r) throws java.io.IOException
java.io.IOException
public static SourceFile fromGenerator(java.lang.String fileName, SourceFile.Generator generator)
public static SourceFile.Builder builder()