Interface Resolver
-
- All Implemented Interfaces:
public interface ResolverResolver provides SymbolProcessor with access to compiler details such as Symbols.
-
-
Method Summary
Modifier and Type Method Description abstract Sequence<KSFile>getNewFiles()Get all new files in the module / compilation unit. abstract Sequence<KSFile>getAllFiles()Get all files in the module / compilation unit. abstract Sequence<KSAnnotated>getSymbolsWithAnnotation(String annotationName, Boolean inDepth)Get all symbols with specified annotation in the current compilation unit. abstract KSClassDeclarationgetClassDeclarationByName(KSName name)Find a class in the compilation classpath for the given name. abstract Sequence<KSFunctionDeclaration>getFunctionDeclarationsByName(KSName name, Boolean includeTopLevel)Find functions in the compilation classpath for the given name. abstract KSPropertyDeclarationgetPropertyDeclarationByName(KSName name, Boolean includeTopLevel)Find a property in the compilation classpath for the given name. abstract KSTypeArgumentgetTypeArgument(KSTypeReference typeRef, Variance variance)Compose a type argument out of a type reference and a variance abstract KSNamegetKSNameFromString(String name)Get a KSName from a String. abstract KSTypeReferencecreateKSTypeReferenceFromKSType(KSType type)Create a KSTypeReference from a KSType abstract StringmapToJvmSignature(KSDeclaration declaration)map a declaration to jvm signature. abstract Booleanoverrides(KSDeclaration overrider, KSDeclaration overridee)abstract Booleanoverrides(KSDeclaration overrider, KSDeclaration overridee, KSClassDeclaration containingClass)abstract StringgetJvmName(KSFunctionDeclaration declaration)Returns the jvm name of the given function. abstract StringgetJvmName(KSPropertyAccessor accessor)Returns the jvm name of the given property accessor. abstract StringgetOwnerJvmClassName(KSPropertyDeclaration declaration)Returns the binary class name of the owner class in JVM for the given KSPropertyDeclaration. abstract StringgetOwnerJvmClassName(KSFunctionDeclaration declaration)Returns the binary class name of the owner class in JVM for the given KSFunctionDeclaration. abstract Sequence<KSType>getJvmCheckedException(KSFunctionDeclaration function)Returns checked exceptions declared in a function's header. abstract Sequence<KSType>getJvmCheckedException(KSPropertyAccessor accessor)Returns checked exceptions declared in a property accessor's header. abstract Sequence<KSDeclaration>getDeclarationsFromPackage(String packageName)Returns declarations with the given package name. abstract KSNamemapJavaNameToKotlin(KSName javaName)Returns the corresponding Kotlin class with the given Java class. abstract KSNamemapKotlinNameToJava(KSName kotlinName)Returns the corresponding Java class with the given Kotlin class. abstract Sequence<KSDeclaration>getDeclarationsInSourceOrder(KSDeclarationContainer container)Same as KSDeclarationContainer.declarations, but sorted by declaration order in the source. abstract Set<Modifier>effectiveJavaModifiers(KSDeclaration declaration)Returns a set of effective Java modifiers, if declaration is being / was generated to Java bytecode. abstract KSTypeReferencegetJavaWildcard(KSTypeReference reference)Compute the corresponding Java wildcard, from the given reference. abstract BooleanisJavaRawType(KSType type)Tests a type if it was declared as legacy "raw" type in Java - a type with its type arguments fully omitted. abstract Sequence<KSAnnotation>getPackageAnnotations(String packageName)Returns annotations applied in package-info.java (if applicable) for given package name. abstract Sequence<String>getPackagesWithAnnotation(String annotationName)Returns name of packages with given annotation. abstract KSNamegetModuleName()abstract KSBuiltInsgetBuiltIns()Provides built in types for convenience. -
-
Method Detail
-
getNewFiles
abstract Sequence<KSFile> getNewFiles()
Get all new files in the module / compilation unit.
- Returns:
new files generated from last last round of processing in the module.
-
getAllFiles
abstract Sequence<KSFile> getAllFiles()
Get all files in the module / compilation unit.
- Returns:
all input files including generated files from previous rounds, note when incremental is enabled, only dirty files up for processing will be returned.
-
getSymbolsWithAnnotation
abstract Sequence<KSAnnotated> getSymbolsWithAnnotation(String annotationName, Boolean inDepth)
Get all symbols with specified annotation in the current compilation unit. Note that in multiple round processing, only symbols from deferred symbols of last round and symbols from newly generated files will be returned in this function.
- Parameters:
annotationName- is the fully qualified name of the annotation; using '.' as separator.inDepth- whether to check symbols in depth, i.e.- Returns:
Elements annotated with the specified annotation.
-
getClassDeclarationByName
abstract KSClassDeclaration getClassDeclarationByName(KSName name)
Find a class in the compilation classpath for the given name.
This returns the exact platform class when given a platform name. Note that java.lang.String isn't compatible with kotlin.String in the type system. Therefore, processors need to use mapJavaNameToKotlin() and mapKotlinNameToJava() explicitly to find the corresponding class names before calling getClassDeclarationByName if type checking is needed for the classes loaded by this.
This behavior is limited to getClassDeclarationByName; When processors get a class or type from a Java source file, the conversion is done automatically. E.g., a java.lang.String in a Java source file is loaded as kotlin.String in KSP.
- Parameters:
name- fully qualified name of the class to be loaded; using '.' as separator.- Returns:
a KSClassDeclaration, or null if not found.
-
getFunctionDeclarationsByName
abstract Sequence<KSFunctionDeclaration> getFunctionDeclarationsByName(KSName name, Boolean includeTopLevel)
Find functions in the compilation classpath for the given name.
- Parameters:
name- fully qualified name of the function to be loaded; using '.' as separator.includeTopLevel- a boolean value indicate if top level functions should be searched.- Returns:
a Sequence of KSFunctionDeclaration
-
getPropertyDeclarationByName
abstract KSPropertyDeclaration getPropertyDeclarationByName(KSName name, Boolean includeTopLevel)
Find a property in the compilation classpath for the given name.
- Parameters:
name- fully qualified name of the property to be loaded; using '.' as separator.includeTopLevel- a boolean value indicate if top level properties should be searched.- Returns:
a KSPropertyDeclaration, or null if not found.
-
getTypeArgument
abstract KSTypeArgument getTypeArgument(KSTypeReference typeRef, Variance variance)
Compose a type argument out of a type reference and a variance
- Parameters:
typeRef- a type reference to be used in type argumentvariance- specifies a use-site variance- Returns:
a type argument with use-site variance
-
getKSNameFromString
abstract KSName getKSNameFromString(String name)
Get a KSName from a String.
-
createKSTypeReferenceFromKSType
abstract KSTypeReference createKSTypeReferenceFromKSType(KSType type)
Create a KSTypeReference from a KSType
-
mapToJvmSignature
abstract String mapToJvmSignature(KSDeclaration declaration)
map a declaration to jvm signature. This function might fail due to resolution error, in case of error, null is returned. Resolution error could be caused by bad code that could not be resolved by compiler, or KSP bugs. If you believe your code is correct, please file a bug at https://github.com/google/ksp/issues/new
-
overrides
abstract Boolean overrides(KSDeclaration overrider, KSDeclaration overridee)
-
overrides
abstract Boolean overrides(KSDeclaration overrider, KSDeclaration overridee, KSClassDeclaration containingClass)
- Parameters:
overrider- the candidate overriding declaration being checked.overridee- the candidate overridden declaration being checked.containingClass- the containing class of candidate overriding and overridden declaration being checked.- Returns:
boolean value indicating whether overrider overrides overridee Calling overrides is expensive and should be avoided if possible.
-
getJvmName
abstract String getJvmName(KSFunctionDeclaration declaration)
Returns the jvm name of the given function. This function might fail due to resolution error, in case of error, null is returned. Resolution error could be caused by bad code that could not be resolved by compiler, or KSP bugs. If you believe your code is correct, please file a bug at https://github.com/google/ksp/issues/new
The jvm name of a function might depend on the Kotlin Compiler version hence it is not guaranteed to be compatible between different compiler versions except for the rules outlined in the Java interoperability documentation: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html.
If the declaration is annotated with JvmName, that name will be returned from this function.
Note that this might be different from the name declared in the Kotlin source code in two cases: a) If the function receives or returns an inline class, its name will be mangled according to https://kotlinlang.org/docs/reference/inline-classes.html#mangling. b) If the function is declared as internal, it will include a suffix with the module name.
NOTE: As inline classes are an experimental feature, the result of this function might change based on the kotlin version used in the project.
-
getJvmName
abstract String getJvmName(KSPropertyAccessor accessor)
Returns the jvm name of the given property accessor. This function might fail due to resolution error, in case of error, null is returned. Resolution error could be caused by bad code that could not be resolved by compiler, or KSP bugs. If you believe your code is correct, please file a bug at https://github.com/google/ksp/issues/new
The jvm name of an accessor might depend on the Kotlin Compiler version hence it is not guaranteed to be compatible between different compiler versions except for the rules outlined in the Java interoperability documentation: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html.
If the accessor is annotated with JvmName, that name will be returned from this function.
By default, this name will match the name calculated according to https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#properties. Note that the result of this function might be different from that name in two cases: a) If the property's type is an internal class, accessor's name will be mangled according to https://kotlinlang.org/docs/reference/inline-classes.html#mangling. b) If the function is declared as internal, it will include a suffix with the module name.
NOTE: As inline classes are an experimental feature, the result of this function might change based on the kotlin version used in the project. see: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#properties
-
getOwnerJvmClassName
abstract String getOwnerJvmClassName(KSPropertyDeclaration declaration)
Returns the binary class name of the owner class in JVM for the given KSPropertyDeclaration.
For properties declared in classes / interfaces; this value is the binary class name of the declaring class.
For top level properties, this is the binary class name of the synthetic class that is generated for the Kotlin file. see: https://kotlinlang.org/docs/java-to-kotlin-interop.html#package-level-functions
Note that, for properties declared in companion objects, the returned owner class will be the Companion class. see: https://kotlinlang.org/docs/java-to-kotlin-interop.html#static-methods
-
getOwnerJvmClassName
abstract String getOwnerJvmClassName(KSFunctionDeclaration declaration)
Returns the binary class name of the owner class in JVM for the given KSFunctionDeclaration.
For functions declared in classes / interfaces; this value is the binary class name of the declaring class.
For top level functions, this is the binary class name of the synthetic class that is generated for the Kotlin file. see: https://kotlinlang.org/docs/java-to-kotlin-interop.html#package-level-functions
Note that, for functions declared in companion objects, the returned owner class will be the Companion class. see: https://kotlinlang.org/docs/java-to-kotlin-interop.html#static-methods
-
getJvmCheckedException
abstract Sequence<KSType> getJvmCheckedException(KSFunctionDeclaration function)
Returns checked exceptions declared in a function's header.
- Returns:
A sequence of KSType declared in
throwsstatement for a Java method or in @Throws annotation for a Kotlin function. Checked exceptions from class files are not supported yet, an empty sequence will be returned instead.
-
getJvmCheckedException
abstract Sequence<KSType> getJvmCheckedException(KSPropertyAccessor accessor)
Returns checked exceptions declared in a property accessor's header.
- Returns:
A sequence of KSType declared @Throws annotation for a Kotlin property accessor. Checked exceptions from class files are not supported yet, an empty sequence will be returned instead.
-
getDeclarationsFromPackage
abstract Sequence<KSDeclaration> getDeclarationsFromPackage(String packageName)
Returns declarations with the given package name.
getDeclarationsFromPackage looks for declaration in the whole classpath, including dependencies.
- Parameters:
packageName- the package name to look up.- Returns:
A sequence of KSDeclaration with matching package name. This will return declarations from both dependencies and source.
-
mapJavaNameToKotlin
abstract KSName mapJavaNameToKotlin(KSName javaName)
Returns the corresponding Kotlin class with the given Java class.
E.g. java.lang.String -> kotlin.String java.lang.Integer -> kotlin.Int java.util.List -> kotlin.List java.util.Map.Entry -> kotlin.Map.Entry java.lang.Void -> null
- Parameters:
javaName- a Java class name- Returns:
corresponding Kotlin class name or null
-
mapKotlinNameToJava
abstract KSName mapKotlinNameToJava(KSName kotlinName)
Returns the corresponding Java class with the given Kotlin class.
E.g. kotlin.Throwable -> java.lang.Throwable kotlin.Int -> java.lang.Integer kotlin.Nothing -> java.lang.Void kotlin.IntArray -> null
- Parameters:
kotlinName- a Java class name- Returns:
corresponding Java class name or null
-
getDeclarationsInSourceOrder
abstract Sequence<KSDeclaration> getDeclarationsInSourceOrder(KSDeclarationContainer container)
Same as KSDeclarationContainer.declarations, but sorted by declaration order in the source.
Note that this is SLOW. AVOID IF POSSIBLE.
-
effectiveJavaModifiers
abstract Set<Modifier> effectiveJavaModifiers(KSDeclaration declaration)
Returns a set of effective Java modifiers, if declaration is being / was generated to Java bytecode.
-
getJavaWildcard
abstract KSTypeReference getJavaWildcard(KSTypeReference reference)
Compute the corresponding Java wildcard, from the given reference.
- Parameters:
reference- the reference to the type usage- Returns:
an equivalent type reference from the Java wildcard's point of view
-
isJavaRawType
abstract Boolean isJavaRawType(KSType type)
Tests a type if it was declared as legacy "raw" type in Java - a type with its type arguments fully omitted.
- Parameters:
type- a type to check.- Returns:
True if the type is a "raw" type.
-
getPackageAnnotations
abstract Sequence<KSAnnotation> getPackageAnnotations(String packageName)
Returns annotations applied in package-info.java (if applicable) for given package name.
- Parameters:
packageName- package name to check.- Returns:
a sequence of KSAnnotations applied in corresponding package-info.java file.
-
getPackagesWithAnnotation
abstract Sequence<String> getPackagesWithAnnotation(String annotationName)
Returns name of packages with given annotation.
- Parameters:
annotationName- name of the annotation to be queried.- Returns:
a sequence of package names with corresponding annotation name.
-
getModuleName
abstract KSName getModuleName()
- Returns:
the name of the kotlin module this resolver is running on.
-
getBuiltIns
abstract KSBuiltIns getBuiltIns()
Provides built in types for convenience. For example, KSBuiltins.anyType is the KSType instance for class 'kotlin.Any'.
-
-
-
-