Package com.google.javascript.jscomp
Class SourceFile
- java.lang.Object
-
- com.google.javascript.jscomp.SourceFile
-
- All Implemented Interfaces:
StaticSourceFile,java.io.Serializable
public class SourceFile extends java.lang.Object implements StaticSourceFile, java.io.Serializable
An abstract representation of a source file that provides access to language-neutral features. The source file can be loaded from various locations, such as from disk or from a preloaded string.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSourceFile.BuilderA builder interface for source files.static interfaceSourceFile.GeneratorA JavaScript source code provider.-
Nested classes/interfaces inherited from interface com.google.javascript.rhino.StaticSourceFile
StaticSourceFile.SourceKind
-
-
Constructor Summary
Constructors Constructor Description SourceFile(java.lang.String fileName, StaticSourceFile.SourceKind kind)Construct a new abstract source file.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static SourceFile.Builderbuilder()Create a new builder for source files.voidclearCachedSource()static SourceFilefromCode(java.lang.String fileName, java.lang.String code)static SourceFilefromCode(java.lang.String fileName, java.lang.String code, StaticSourceFile.SourceKind kind)static SourceFilefromFile(java.lang.String fileName)static SourceFilefromFile(java.lang.String fileName, java.nio.charset.Charset charset)static SourceFilefromFile(java.lang.String fileName, java.nio.charset.Charset charset, StaticSourceFile.SourceKind kind)static SourceFilefromGenerator(java.lang.String fileName, SourceFile.Generator generator)static SourceFilefromInputStream(java.lang.String fileName, java.io.InputStream s)Deprecated.static SourceFilefromInputStream(java.lang.String fileName, java.io.InputStream s, java.nio.charset.Charset charset)static SourceFilefromPath(java.nio.file.Path path, java.nio.charset.Charset charset)static SourceFilefromPath(java.nio.file.Path path, java.nio.charset.Charset charset, StaticSourceFile.SourceKind kind)static SourceFilefromReader(java.lang.String fileName, java.io.Reader r)static SourceFilefromZipEntry(java.lang.String originalZipPath, java.lang.String absoluteZipPath, java.lang.String entryPath, java.nio.charset.Charset inputCharset)static SourceFilefromZipEntry(java.lang.String originalZipPath, java.lang.String absoluteZipPath, java.lang.String entryPath, java.nio.charset.Charset inputCharset, StaticSourceFile.SourceKind kind)static java.util.List<SourceFile>fromZipFile(java.lang.String zipName, java.nio.charset.Charset inputCharset)static java.util.List<SourceFile>fromZipInput(java.lang.String zipName, java.io.InputStream input, java.nio.charset.Charset inputCharset)java.lang.StringgetCode()Gets all the code in this source file.java.io.ReadergetCodeReader()Gets a reader for the code in this source file.intgetColumnOfOffset(int offset)Gets the 0-based column number of the given source offset.StaticSourceFile.SourceKindgetKind()Returns the source kind.java.lang.StringgetLine(int lineNumber)Gets the source line for the indicated line number.intgetLineOffset(int lineno)Returns the offset of the given line number relative to the file start.intgetLineOfOffset(int offset)Gets the 1-based line number of the given source offset.RegiongetLines(int lineNumber, int length)Gets the source lines starting at `lineNumber` and continuing until `length`.java.lang.StringgetName()Returns a unique name for the source file.java.lang.StringgetOriginalPath()RegiongetRegion(int lineNumber)Get a region around the indicated line number.voidrestoreFrom(SourceFile sourceFile)voidsetKind(StaticSourceFile.SourceKind kind)Sets the source kind.voidsetOriginalPath(java.lang.String originalPath)java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.google.javascript.rhino.StaticSourceFile
isExtern, isStrong, isWeak
-
-
-
-
Constructor Detail
-
SourceFile
public SourceFile(java.lang.String fileName, StaticSourceFile.SourceKind kind)Construct a new abstract source file.- Parameters:
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.kind- The source kind.
-
-
Method Detail
-
getLineOffset
public int getLineOffset(int lineno)
Description copied from interface:StaticSourceFileReturns the offset of the given line number relative to the file start. Line number should be 1-based. If the source file doesn't have line information, it should return Integer.MIN_VALUE. The negative offsets will make it more obvious what happened.- Specified by:
getLineOffsetin interfaceStaticSourceFile- Parameters:
lineno- the line of the input to get the absolute offset of.- Returns:
- the absolute offset of the start of the provided line.
-
getCode
public java.lang.String getCode() throws java.io.IOExceptionGets all the code in this source file.- Throws:
java.io.IOException
-
getCodeReader
@GwtIncompatible("java.io.Reader") public java.io.Reader getCodeReader() throws java.io.IOExceptionGets a reader for the code in this source file.- Throws:
java.io.IOException
-
getOriginalPath
public java.lang.String getOriginalPath()
-
setOriginalPath
public void setOriginalPath(java.lang.String originalPath)
-
clearCachedSource
public void clearCachedSource()
-
getName
public java.lang.String getName()
Returns a unique name for the source file.- Specified by:
getNamein interfaceStaticSourceFile
-
getKind
public StaticSourceFile.SourceKind getKind()
Returns the source kind.- Specified by:
getKindin interfaceStaticSourceFile
-
setKind
public void setKind(StaticSourceFile.SourceKind kind)
Sets the source kind.
-
getLineOfOffset
public int getLineOfOffset(int offset)
Description copied from interface:StaticSourceFileGets the 1-based line number of the given source offset.- Specified by:
getLineOfOffsetin interfaceStaticSourceFile- Parameters:
offset- An absolute file offset.- Returns:
- The 1-based line number of that offset. The behavior is undefined if this offset does not exist in the source file.
-
getColumnOfOffset
public int getColumnOfOffset(int offset)
Description copied from interface:StaticSourceFileGets the 0-based column number of the given source offset.- Specified by:
getColumnOfOffsetin interfaceStaticSourceFile- Parameters:
offset- An absolute file offset.- Returns:
- The 0-based column number of that offset. The behavior is undefined if this offset does not exist in the source file.
-
getLine
public java.lang.String getLine(int lineNumber)
Gets the source line for the indicated line number.- Parameters:
lineNumber- the line number, 1 being the first line of the file.- Returns:
- The line indicated. Does not include the newline at the end
of the file. Returns
nullif it does not exist, or if there was an IO exception.
-
getLines
public Region getLines(int lineNumber, int length)
Gets the source lines starting at `lineNumber` and continuing until `length`. Omits any trailing newlines.- Parameters:
lineNumber- the line number, 1 being the first line of the file.length- the number of characters desired, starting at the 0th character of the specified line. If negative or 0, returns a single line.- Returns:
- The line(s) indicated. Returns
nullif it does not exist or if there was an IO exception.
-
getRegion
public Region getRegion(int lineNumber)
Get a region around the indicated line number. The exact definition of a region is implementation specific, but it must contain the line indicated by the line number. A region must not start or end by a carriage return.- Parameters:
lineNumber- the line number, 1 being the first line of the file.- Returns:
- The line indicated. Returns
nullif it does not exist, or if there was an IO exception.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
fromZipFile
@GwtIncompatible("fromZipInput") public static java.util.List<SourceFile> fromZipFile(java.lang.String zipName, java.nio.charset.Charset inputCharset) throws java.io.IOException- Throws:
java.io.IOException
-
fromZipInput
@GwtIncompatible("java.util.zip.ZipInputStream") public static java.util.List<SourceFile> fromZipInput(java.lang.String zipName, java.io.InputStream input, java.nio.charset.Charset inputCharset) throws java.io.IOException- Throws:
java.io.IOException
-
fromZipEntry
@GwtIncompatible("java.io.File") public static SourceFile fromZipEntry(java.lang.String originalZipPath, java.lang.String absoluteZipPath, java.lang.String entryPath, java.nio.charset.Charset inputCharset) throws java.net.MalformedURLException- Throws:
java.net.MalformedURLException
-
fromZipEntry
@GwtIncompatible("java.io.File") public static SourceFile fromZipEntry(java.lang.String originalZipPath, java.lang.String absoluteZipPath, java.lang.String entryPath, java.nio.charset.Charset inputCharset, StaticSourceFile.SourceKind kind) throws java.net.MalformedURLException- Throws:
java.net.MalformedURLException
-
fromFile
@GwtIncompatible("java.io.File") public static SourceFile fromFile(java.lang.String fileName, java.nio.charset.Charset charset, StaticSourceFile.SourceKind kind)
-
fromFile
@GwtIncompatible("java.io.File") public static SourceFile fromFile(java.lang.String fileName, java.nio.charset.Charset charset)
-
fromFile
@GwtIncompatible("java.io.File") public static SourceFile fromFile(java.lang.String fileName)
-
fromPath
@GwtIncompatible("java.io.File") public static SourceFile fromPath(java.nio.file.Path path, java.nio.charset.Charset charset, StaticSourceFile.SourceKind kind)
-
fromPath
@GwtIncompatible("java.io.File") public static SourceFile fromPath(java.nio.file.Path path, java.nio.charset.Charset charset)
-
fromCode
public static SourceFile fromCode(java.lang.String fileName, java.lang.String code, StaticSourceFile.SourceKind kind)
-
fromCode
public static SourceFile fromCode(java.lang.String fileName, java.lang.String code)
-
fromInputStream
@Deprecated @GwtIncompatible("java.io.InputStream") public static SourceFile fromInputStream(java.lang.String fileName, java.io.InputStream s) throws java.io.IOExceptionDeprecated.- Throws:
java.io.IOException
-
fromInputStream
@GwtIncompatible("java.io.InputStream") public static SourceFile fromInputStream(java.lang.String fileName, java.io.InputStream s, java.nio.charset.Charset charset) throws java.io.IOException- Throws:
java.io.IOException
-
fromReader
@GwtIncompatible("java.io.Reader") public static SourceFile fromReader(java.lang.String fileName, java.io.Reader r) throws java.io.IOException- Throws:
java.io.IOException
-
fromGenerator
public static SourceFile fromGenerator(java.lang.String fileName, SourceFile.Generator generator)
-
builder
public static SourceFile.Builder builder()
Create a new builder for source files.
-
restoreFrom
public void restoreFrom(SourceFile sourceFile)
-
-