Package org.graalvm.polyglot
Class Source.Builder
java.lang.Object
org.graalvm.polyglot.Source.Builder
- Enclosing class:
Source
Represents a builder to build
Source
objects.
To load a source from disk one can use:
File file = new File(dir, name); assert name.endsWith(".java") : "Imagine proper file"; String language = Source.findLanguage(file); Source source = Source.newBuilder(language, file).build(); assert file.getName().equals(source.getName()); assert file.getPath().equals(source.getPath()); assert file.toURI().equals(source.getURI());To load source from a
URL
one can use:
URL resource = relativeClass.getResource("sample.js"); Source source = Source.newBuilder("js", resource).build(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toURI().equals(source.getURI());To create a source representing characters:
Source source = Source.newBuilder("js", "function() {\n" + " return 'Hi';\n" + "}\n", "hi.js").buildLiteral(); assert "hi.js".equals(source.getName());or read a source from a
Reader
:
Reader stream = new InputStreamReader( relativeClass.getResourceAsStream("sample.js")); Source source = Source.newBuilder("js", stream, "sample.js").build(); assert "sample.js".equals(source.getName());To create a source representing bytes:
byte[] bytes = new byte[]{ /* Binary */ }; Source source = Source.newBuilder("llvm", ByteSequence.create(bytes), "<literal>").buildLiteral();Once your builder is configured, call
build()
to perform the loading and
construction of new Source
.- Since:
- 19.0
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Uses configuration of this builder to create newSource
object.Uses configuration of this builder to create a newSource
object.cached
(boolean cached) Enables or disables code caching for this source.content
(CharSequence characters) Specifies character based content ofto-be-built
Source
.Specifies content ofto-be-built
Source
.content
(ByteSequence bytes) Specifies byte based content ofto-be-built
Source
.Assigns an encoding used to read the file content.interactive
(boolean interactive) Marks the source as interactive.internal
(boolean internal) Set whether this source has been marked as internal, meaning that it has been provided by the infrastructure, language implementation, or system library.Specifies a name to theto-be-built
Source
.
-
Method Details
-
name
Specifies a name to theto-be-built
Source
.- Parameters:
newName
- name that replaces the previously given one. If set tonull
then"Unnamed"
will be used.- Returns:
- instance of
this
builder - Since:
- 19.0
-
content
Specifies content ofto-be-built
Source
. Using this method one can ignore the real content of a file or URL and use already read one, or completely different one. Example:URL resource = relativeClass.getResource("sample.js"); Source source = Source.newBuilder("js", resource).content("{}").buildLiteral(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toExternalForm().equals(source.getURI().toString()); assert "{}".equals(source.getCharacters());
- Parameters:
code
- the code to be available viaSource.getCharacters()
- Returns:
- instance of this builder
- Since:
- 19.0
-
content
Specifies character based content ofto-be-built
Source
. Using this method one can ignore the real content of a file or URL and use already read one, or completely different one. The given characters must not mutate after they were accessed for the first time. Example:URL resource = relativeClass.getResource("sample.js"); Source source = Source.newBuilder("js", resource).content("{}").buildLiteral(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toExternalForm().equals(source.getURI().toString()); assert "{}".equals(source.getCharacters());
- Parameters:
characters
- the code to be available viaSource.getCharacters()
- Returns:
- instance of this builder - which's
build()
method no longer throws anIOException
- Since:
- 19.0
-
content
Specifies byte based content ofto-be-built
Source
. Using this method one can ignore the real content of a file or URL and use already read one, or completely different one. The given bytes must not mutate after they were accessed for the first time. Example:URL resource = relativeClass.getResource("sample.js"); Source source = Source.newBuilder("js", resource).content("{}").buildLiteral(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toExternalForm().equals(source.getURI().toString()); assert "{}".equals(source.getCharacters());
- Parameters:
bytes
- the code to be available viaSource.getBytes()
- Returns:
- instance of this builder - which's
build()
method no longer throws anIOException
- Since:
- 19.0
- See Also:
-
mimeType
Explicitly assigns aMIME type
to theto-be-built
Source
. If the MIME type isnull
then thedefault MIME type
of the language will be used to interpret the source. If set explicitly then the language needs tosupport
the MIME type in order toevaluate
the source. If notnull
the MIME type is already verified containing no spaces and a '/' between group and sub-group. An example for a valid MIME type is:text/javascript
.The MIME type can be guessed by the system based on
files
orurls
. If a source isbinary
based then the MIME type must also be a binary based MIME type. All MIME types starting with 'text/' will be interpreted as character based MIME types.- Parameters:
mimeType
- the new mime type to be assigned, ornull
if default MIME type should be used.- Returns:
- instance of
this
builder ready tocreate new source
- Since:
- 19.0
- See Also:
-
interactive
Marks the source as interactive.Evaluation
of interactive sources by aninteractive language
can use theContext
output streams to print the result and read an input. However, non-interactive languages are expected to ignore the interactive property of sources and not use the polyglot engine streams. Any desired printing of the evaluated result provided by a non-interactive language needs to be handled by the caller. Calling of this method influences the result ofSource.isInteractive()
.- Returns:
- the instance of this builder
- Since:
- 19.0
-
internal
Set whether this source has been marked as internal, meaning that it has been provided by the infrastructure, language implementation, or system library. Internal sources are presumed to be irrelevant to guest language programmers, as well as possibly confusing and revealing of language implementation details.On the other hand, tools should be free to make internal sources visible in (possibly privileged) modes that are useful for language implementors.
- Returns:
- the instance of this builder
- Since:
- 19.0
-
cached
Enables or disables code caching for this source. By default code caching is enabled. Iftrue
then the source does not require parsing every time this source isevaluated
. Iffalse
then the source requires parsing every time the source is evaluated but does not remember any state. Disabling caching may be useful if the source is known to only be evaluated once.If a source instance is no longer referenced by the client then all code caches will be freed automatically. Also, if the underlying context or engine is no longer referenced then cached code for evaluated sources will be freed automatically.
- Returns:
- instance of
this
builder ready tocreate new source
- Since:
- 19.0
-
uri
Assigns newURI
to theto-be-created
Source
. Each source providesSource.getURI()
as a persistent identification of its location. A default value for the method is deduced from the location or content, but one can change it by using this method- Parameters:
newUri
- the URL to use instead of default one, cannot benull
- Returns:
- the instance of this builder
- Since:
- 19.0
-
encoding
Assigns an encoding used to read the file content. If the encoding isnull
then the file contained encoding information is used. If the file doesn't provide an encoding information the defaultUTF-8
encoding is used.- Parameters:
encoding
- the new file encoding to be used for reading the content- Returns:
- instance of
this
builder ready tocreate new source
- Since:
- 19.0
-
build
Uses configuration of this builder to create newSource
object. The method throws anIOException
if an error loading the source occurred.- Returns:
- the source object
- Throws:
IOException
- Since:
- 19.0
-
buildLiteral
Uses configuration of this builder to create a newSource
object. This method suppresses anyIOException
asAssertionError
and should only be used if the builder was created usingor Source#newBuilder(String, ByteSequence, String) byte sequence literal
.- Returns:
- the source object
- Since:
- 19.0
-