Package com.google.javascript.jscomp
Class SourceFile
java.lang.Object
com.google.javascript.jscomp.SourceFile
- All Implemented Interfaces:
StaticSourceFile,Serializable
- Direct Known Subclasses:
JSSourceFile
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder interface for source files.static interfaceA JavaScript source code provider. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic SourceFile.Builderbuilder()Create a new builder for source files.voidstatic SourceFilestatic SourceFilestatic SourceFilestatic SourceFilestatic SourceFilestatic SourceFilestatic SourceFilefromGenerator(String fileName, SourceFile.Generator generator) static SourceFilefromInputStream(String fileName, InputStream s) static SourceFilefromInputStream(String fileName, String originalPath, InputStream s) static SourceFilefromReader(String fileName, Reader r) getCode()Gets all the code in this source file.Gets a reader for the code in this source file.intgetColumnOfOffset(int offset) Gets the 0-based column number of the given source offset.getLine(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.getName()Returns a unique name for the source file.getRegion(int lineNumber) Get a region around the indicated line number.booleanisExtern()Returns whether this is an extern.voidsetOriginalPath(String originalPath) toString()
-
Constructor Details
-
SourceFile
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.
-
-
Method Details
-
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
Gets all the code in this source file.- Throws:
IOException
-
getCodeReader
Gets a reader for the code in this source file.- Throws:
IOException
-
getOriginalPath
-
setOriginalPath
-
clearCachedSource
public void clearCachedSource() -
getName
Returns a unique name for the source file.- Specified by:
getNamein interfaceStaticSourceFile
-
isExtern
public boolean isExtern()Returns whether this is an extern.- Specified by:
isExternin interfaceStaticSourceFile
-
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
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.
-
getRegion
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
-
fromFile
-
fromFile
-
fromFile
-
fromFile
-
fromCode
-
fromCode
-
fromInputStream
- Throws:
IOException
-
fromInputStream
public static SourceFile fromInputStream(String fileName, String originalPath, InputStream s) throws IOException - Throws:
IOException
-
fromReader
- Throws:
IOException
-
fromGenerator
-
builder
Create a new builder for source files.
-