Class Symbol
- All Implemented Interfaces:
SymbolContainer
,SymbolDependencyContainer
,ToSmithyBuilder<Symbol>
SymbolProvider
, and represents the
qualified name of a type in a target programming language.
A symbol contains an optional namespace, optional namespace delimiter, name, a map of additional properties, a declaration file the determines where the symbol is declared, and a definition file that determines where a symbol is defined.
A namespace can be used when the target language supports namespaces. The provided namespace can be in whatever format is useful to the target language. A namespace delimiter is injected between the namespace and the name when creating the fully-qualified symbol name. The "name" is the unqualified name of the symbol (e.g., "str", or "MyShape").
Additional properties can be included when it's useful to provide more information about a symbol. For example, it might be useful to identify the name of a dependency that needs to be pulled in through a package manager when a symbol is used.
The following example shows how a Java type could be made into a symbol:
Class<Symbol> klass = Symbol.class;
Symbol symbol = Symbol.builder()
.namespace(klass.getPackage().toString(), ".")
.name(klass.getSimpleName())
.build();
System.out.println(symbol);
// ^ outputs "software.amazon.smithy.codegen.Symbol"
System.out.println(symbol.relativize("software.amazon.smithy.codegen");
// ^ outputs "Symbol"
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Symbol.Builder
builder()
Creates a new Symbol builder.boolean
expectProperty
(String name) Gets a specific additional property or throws if missing.<T> T
expectProperty
(String name, Class<T> type) Gets a specific additional property or throws if missing or if the property is not an instance of the given type.Gets the location/filename in which the symbol is declared.Gets the location/filename in which the symbol is defined.Gets the list of dependencies that this object introduces.Gets the full name of the symbol.getName()
Gets the unqualified name of the symbol, that is, a name without namespace.Provides the namespace of the symbol or "" if empty.Provides the namespace delimiter of the symbol or "" if empty.Gets the additional properties of the object.getProperty
(String name) Gets a specific property if present.<T> Optional<T>
getProperty
(String name, Class<T> type) Gets an additional property of a specific type.Gets the list of symbols that are referenced by this symbol.Returns anySymbol
objects contained in the object.int
hashCode()
relativize
(String namespace) Creates a relativized Symbol for the given namespace.Take this object and create a builder that contains all of the current property values of this object.toReference
(String alias, SymbolReference.Option... options) Converts the symbol to aSymbolReference
using the givenalias
.toReferencedSymbol
(String alias) Converts the symbol to aSymbol
that refers to this Symbol using aSymbolReference
.toReferencedSymbolBuilder
(String alias) Converts the symbol to aSymbol.Builder
that refers to this Symbol using aSymbolReference
via an alias.toString()
-
Method Details
-
builder
Creates a new Symbol builder.- Returns:
- Returns the created symbol builder.
-
getNamespace
Provides the namespace of the symbol or "" if empty.- Returns:
- Returns the optional namespace.
-
getNamespaceDelimiter
Provides the namespace delimiter of the symbol or "" if empty.This delimiter is injected between the namespace and name when creating the full name.
- Returns:
- Returns the optional namespace.
-
getName
Gets the unqualified name of the symbol, that is, a name without namespace.- Returns:
- Returns the name of the symbol.
-
getDeclarationFile
Gets the location/filename in which the symbol is declared.Code generators should write the generated code for this symbol's declaration in a file with the same name that is returned from this method. Not all languages separate a symbol's definition from its declaration. This method is useful for things like C and C++ header files.
This method returns an empty string if no value was provided in the builder.
- Returns:
- The name of the file the symbol is declared.
-
getDefinitionFile
Gets the location/filename in which the symbol is defined.Code generators should write the generated code for a symbol in a file with the same name that is returned from this method.
This method returns an empty string if no value was provided for either the declaration file or the definition file.
- Returns:
- The name of the file the symbol is defined.
-
getFullName
Gets the full name of the symbol.The full name is the concatenation of the namespace, the namespace delimiter, and the name.
- Returns:
- Returns the fully qualified name of the symbol.
-
relativize
Creates a relativized Symbol for the given namespace.If this symbol is in the same namespace as the provided namespace, then only the symbol name is returned. Otherwise, the fully-qualified symbol is returned.
- Parameters:
namespace
- Namespace to relativize against.- Returns:
- Returns the relativized symbol.
-
toReference
Converts the symbol to aSymbolReference
using the givenalias
.- Parameters:
alias
- Alias to use to refer to the symbol.options
- Variadic array ofSymbolReference.Option
s.- Returns:
- Returns the created SymbolReference.
-
toReferencedSymbol
Converts the symbol to aSymbol
that refers to this Symbol using aSymbolReference
. This makes it easier to refer to a type using an alias but still through a Symbol to be compatible withSymbolProvider
.The following example creates a Symbol that is referred to as "__Document" in code, but is an alias of "foo.Document".
Symbol aliasedSymbol = Symbol.builder() .name("Document") .namespace("foo", ".") .build() .toReferenceSymbol("__Document");
When used with a
SymbolWriter
, the writer should add an import on "foo.Document" and alias it to "__Document".The created symbol uses an empty namespace (""). If this is not compatible with specific
ImportContainer
s to understand that the aliased symbol itself needs no imports, then you can augment the symbol with other metadata by instead usingtoReferencedSymbolBuilder(String)
.Note that this does not work with every programming language. For example, Java does not support aliasing whereas TypeScript does.
- Parameters:
alias
- Alias to use to refer to the symbol.- Returns:
- Returns the created Symbol.
- See Also:
-
toReferencedSymbolBuilder
Converts the symbol to aSymbol.Builder
that refers to this Symbol using aSymbolReference
via an alias. This makes it easier to refer to type using an alias but still use a Symbol to be compatible with SymbolProviders.- Parameters:
alias
- Alias to use to refer to the symbol.- Returns:
- Returns a SymbolBuilder that is prepared with the symbol and alias.
- See Also:
-
getReferences
Gets the list of symbols that are referenced by this symbol.- Returns:
- Returns the Symbol references.
-
getDependencies
Description copied from interface:SymbolDependencyContainer
Gets the list of dependencies that this object introduces.A dependency is a dependency on another package that a Symbol or type requires. It is quite different from a reference since a reference only refers to a symbol; a reference provides no context as to whether or not a dependency is required or the dependency's coordinates.
- Specified by:
getDependencies
in interfaceSymbolDependencyContainer
- Returns:
- Returns the dependencies.
-
getSymbols
Description copied from interface:SymbolContainer
Returns anySymbol
objects contained in the object.- Specified by:
getSymbols
in interfaceSymbolContainer
- Returns:
- Returns a collection of
Symbol
s.
-
toBuilder
Description copied from interface:ToSmithyBuilder
Take this object and create a builder that contains all of the current property values of this object.- Specified by:
toBuilder
in interfaceToSmithyBuilder<Symbol>
- Returns:
- a builder for type T
-
toString
-
equals
-
hashCode
public int hashCode() -
getProperties
Gets the additional properties of the object.- Returns:
- Returns a map of additional property strings.
-
getProperty
Gets a specific property if present.- Parameters:
name
- Property to retrieve.- Returns:
- Returns the optionally found property.
-
getProperty
Gets an additional property of a specific type.- Type Parameters:
T
- Type of value to expect.- Parameters:
name
- Name of the property to get.type
- Type of value to expect.- Returns:
- Returns a map of additional property strings.
- Throws:
IllegalArgumentException
- if the value is not of the given type.
-
expectProperty
Gets a specific additional property or throws if missing.- Parameters:
name
- Property to retrieve.- Returns:
- Returns the found property.
- Throws:
IllegalArgumentException
- if the property is not present.
-
expectProperty
Gets a specific additional property or throws if missing or if the property is not an instance of the given type.- Type Parameters:
T
- Type of value to expect.- Parameters:
name
- Property to retrieve.type
- Type of value to expect.- Returns:
- Returns the found property.
- Throws:
IllegalArgumentException
- if the property is not present.IllegalArgumentException
- if the value is not of the given type.
-