Class SourceFile
- All Implemented Interfaces:
StaticSourceFile
The source file can be loaded from various locations, such as from disk or from a preloaded string. Loading is done as lazily as possible to minimize IO delays and memory cost of source text.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA builder interface for source files.Nested classes/interfaces inherited from interface com.google.javascript.rhino.StaticSourceFile
StaticSourceFile.SourceKind -
Method Summary
Modifier and TypeMethodDescriptionstatic SourceFile.Builderbuilder()Create a new builder for source files.voidFor SourceFile types which cache source code that can be regenerated easily, flush the cache.static SourceFilestatic SourceFilefromCode(String fileName, String code, StaticSourceFile.SourceKind kind) static SourceFilestatic SourceFilestatic SourceFilestatic SourceFilefromProto(com.google.javascript.jscomp.serialization.SourceFileProto protoSourceFile) static List<SourceFile> fromZipFile(String zipName, Charset inputCharset) static List<SourceFile> fromZipInput(String zipName, InputStream input, Charset inputCharset) final StringgetCode()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.getKind()Returns the source kind.@Nullable 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.@Nullable RegiongetLines(int lineNumber, int length) Gets the source lines starting at `lineNumber` and continuing until `length`.getName()Returns a unique name for the source file.Deprecated.com.google.javascript.jscomp.serialization.SourceFileProtogetProto()@Nullable RegiongetRegion(int lineNumber) Get a region around the indicated line number.booleanWhether this source file contains code that should be optimized using a minimal set of known-safe optimizations.booleanvoidvoidrestoreCachedStateFrom(com.google.javascript.jscomp.serialization.SourceFileProto protoSourceFile) Reconciles serialized state in aSourceFileProtowith the existing state in this file.voidSets the source kind.static SourceFilestubSourceFile(String fileName, StaticSourceFile.SourceKind kind) Returns a stub SourceFile with the given name and kind.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.google.javascript.rhino.StaticSourceFile
isExtern, isNonCode, isStrong, isTypeScriptSource, isWeak
-
Method Details
-
isStubSourceFileForAlreadyProvidedInput
public boolean isStubSourceFileForAlreadyProvidedInput() -
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
Deprecated.alias ofgetName(). Use that instead -
clearCachedSource
public void clearCachedSource()For SourceFile types which cache source code that can be regenerated easily, flush the cache.We maintain the cache mostly to speed up generating source when displaying error messages, so dumping the file contents after the compile is a fine thing to do.
-
getName
Returns a unique name for the source file.This name is not required to be an actual file path on disk.
- Specified by:
getNamein interfaceStaticSourceFile
-
getKind
Returns the source kind.- Specified by:
getKindin interfaceStaticSourceFile
-
setKind
Sets the source kind. -
isClosureUnawareCode
public boolean isClosureUnawareCode()Description copied from interface:StaticSourceFileWhether this source file contains code that should be optimized using a minimal set of known-safe optimizations. In particular, code in this StaticSourceFile is unlikely to be well-typed (or have types annotated at all), and is likely written by someone unaware of the limitations of this compiler.- Specified by:
isClosureUnawareCodein interfaceStaticSourceFile
-
markAsClosureUnawareCode
public void markAsClosureUnawareCode() -
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.
-
getLines
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
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
-
fromZipFile
- Throws:
IOException
-
fromZipInput
public static List<SourceFile> fromZipInput(String zipName, InputStream input, Charset inputCharset) throws IOException - Throws:
IOException
-
fromFile
-
fromFile
-
fromPath
-
fromCode
-
fromCode
-
stubSourceFile
Returns a stub SourceFile with the given name and kind. It is used when the actual sources are provided via TypedASTs to stage2 and stage3. -
restoreCachedStateFrom
public void restoreCachedStateFrom(com.google.javascript.jscomp.serialization.SourceFileProto protoSourceFile) Reconciles serialized state in aSourceFileProtowith the existing state in this file.This should be called whenever initializing a compilation based on TypedAST protos. For these compilations, the compiler initalization methods require creating SourceFiles before deserializing TypedAST protos, so we sometimes get two copies of the same SourceFile.)
-
fromProto
public static SourceFile fromProto(com.google.javascript.jscomp.serialization.SourceFileProto protoSourceFile) -
builder
Create a new builder for source files. -
getProto
public com.google.javascript.jscomp.serialization.SourceFileProto getProto()
-
getName().