Kapt: Fixes on review
(cherry picked from commit be31262)
This commit is contained in:
committed by
Yan Zhulanow
parent
5b8e6abdeb
commit
f61367df28
+49
-41
@@ -121,51 +121,59 @@ class AnnotationProcessingExtension(
|
||||
}
|
||||
}
|
||||
|
||||
internal class AnalysisContext(val annotationsMap: MutableMap<String, MutableList<PsiModifierListOwner>>)
|
||||
|
||||
private fun AnalysisContext.analyzeFiles(files: Collection<KtFile>) {
|
||||
for (file in files) {
|
||||
analyzeFile(file)
|
||||
}
|
||||
}
|
||||
|
||||
private fun AnalysisContext.analyzeFile(file: KtFile) {
|
||||
val lightClass = file.findFacadeClass()
|
||||
internal class AnalysisContext(annotationsMap: MutableMap<String, MutableList<PsiModifierListOwner>>) {
|
||||
private val mutableAnnotationsMap = annotationsMap
|
||||
|
||||
if (lightClass != null) {
|
||||
analyzeDeclaration(lightClass)
|
||||
}
|
||||
val annotationsMap: Map<String, List<PsiModifierListOwner>>
|
||||
get() = mutableAnnotationsMap
|
||||
|
||||
for (declaration in file.declarations) {
|
||||
if (declaration !is KtClassOrObject) continue
|
||||
val clazz = declaration.toLightClass() ?: continue
|
||||
analyzeDeclaration(clazz)
|
||||
}
|
||||
}
|
||||
|
||||
private fun AnalysisContext.analyzeFile(file: PsiJavaFile) {
|
||||
file.classes.forEach { analyzeDeclaration(it) }
|
||||
}
|
||||
|
||||
private fun AnalysisContext.analyzeDeclaration(declaration: PsiElement) {
|
||||
if (declaration !is PsiModifierListOwner) return
|
||||
|
||||
val annotations = declaration.modifierList?.annotations
|
||||
if (annotations != null) {
|
||||
for (annotation in annotations) {
|
||||
val fqName = annotation.qualifiedName ?: continue
|
||||
annotationsMap.getOrPut(fqName, { mutableListOf() }).add(declaration)
|
||||
fun analyzeFiles(files: Collection<KtFile>) {
|
||||
for (file in files) {
|
||||
analyzeFile(file)
|
||||
}
|
||||
}
|
||||
|
||||
if (declaration is PsiClass) {
|
||||
declaration.methods.forEach { analyzeDeclaration(it) }
|
||||
declaration.fields.forEach { analyzeDeclaration(it) }
|
||||
declaration.innerClasses.forEach { analyzeDeclaration(it) }
|
||||
|
||||
fun analyzeFile(file: KtFile) {
|
||||
val lightClass = file.findFacadeClass()
|
||||
|
||||
if (lightClass != null) {
|
||||
analyzeDeclaration(lightClass)
|
||||
}
|
||||
|
||||
for (declaration in file.declarations) {
|
||||
if (declaration !is KtClassOrObject) continue
|
||||
val clazz = declaration.toLightClass() ?: continue
|
||||
analyzeDeclaration(clazz)
|
||||
}
|
||||
}
|
||||
|
||||
when (declaration) {
|
||||
is KtClassOrObject -> declaration.declarations.forEach { analyzeDeclaration(it) }
|
||||
|
||||
|
||||
fun analyzeFile(file: PsiJavaFile) {
|
||||
file.classes.forEach { analyzeDeclaration(it) }
|
||||
}
|
||||
|
||||
fun analyzeDeclaration(declaration: PsiElement) {
|
||||
if (declaration !is PsiModifierListOwner) return
|
||||
|
||||
//TODO support inherited annotations
|
||||
|
||||
val annotations = declaration.modifierList?.annotations
|
||||
if (annotations != null) {
|
||||
for (annotation in annotations) {
|
||||
val fqName = annotation.qualifiedName ?: continue
|
||||
mutableAnnotationsMap.getOrPut(fqName, { mutableListOf() }).add(declaration)
|
||||
}
|
||||
}
|
||||
|
||||
if (declaration is PsiClass) {
|
||||
declaration.methods.forEach { analyzeDeclaration(it) }
|
||||
declaration.fields.forEach { analyzeDeclaration(it) }
|
||||
declaration.innerClasses.forEach { analyzeDeclaration(it) }
|
||||
}
|
||||
|
||||
if (declaration is PsiMethod) {
|
||||
for (parameter in declaration.parameterList.parameters) {
|
||||
analyzeDeclaration(parameter)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -58,7 +58,7 @@ class AnnotationProcessingCommandLineProcessor : CommandLineProcessor {
|
||||
override val pluginId: String = ANNOTATION_PROCESSING_COMPILER_PLUGIN_ID
|
||||
|
||||
override val pluginOptions: Collection<CliOption> =
|
||||
listOf(GENERATED_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION)
|
||||
listOf(GENERATED_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, CLASS_FILES_OUTPUT_DIR_OPTION)
|
||||
|
||||
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
|
||||
when (option) {
|
||||
|
||||
+3
-2
@@ -69,7 +69,8 @@ class KotlinElements(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchSc
|
||||
}
|
||||
|
||||
override fun getBinaryName(type: TypeElement) = JeName((type as JeTypeElement).psi.qualifiedName)
|
||||
|
||||
|
||||
//TODO
|
||||
override fun getDocComment(e: Element?) = ""
|
||||
|
||||
override fun isDeprecated(e: Element?): Boolean {
|
||||
@@ -104,7 +105,7 @@ class KotlinElements(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchSc
|
||||
}
|
||||
|
||||
override fun getAllAnnotationMirrors(e: Element): List<AnnotationMirror> {
|
||||
val annotations = (e as? JeElement)?.annotationMirrors?.toMutableList() ?: return emptyList()
|
||||
val annotations = (e as? JeElement)?.annotationMirrors?.toMutableList() ?: mutableListOf()
|
||||
|
||||
if (e is JeTypeElement) {
|
||||
var parent = e.psi.superClass
|
||||
|
||||
+10
-17
@@ -29,18 +29,7 @@ class KotlinFiler(val generatedSourceDir: File, val classesOutputDir: File) : Fi
|
||||
val PACKAGE_INFO_SUFFIX = ".packageInfo"
|
||||
}
|
||||
|
||||
private var dotGeneratedGenerated = false
|
||||
|
||||
private fun createDotGeneratedIfNeeded() {
|
||||
if (!dotGeneratedGenerated) {
|
||||
dotGeneratedGenerated = true
|
||||
File(generatedSourceDir, ".generated").writeText("Generated by kapt2")
|
||||
}
|
||||
}
|
||||
|
||||
private fun getGeneratedFile(nameCharSequence: CharSequence, extension: String): File {
|
||||
createDotGeneratedIfNeeded()
|
||||
|
||||
val name = nameCharSequence.toString()
|
||||
val isPackageInfo = name.endsWith(PACKAGE_INFO_SUFFIX)
|
||||
val fqName = if (isPackageInfo) name.substring(0, PACKAGE_INFO_SUFFIX.length) else name
|
||||
@@ -59,7 +48,7 @@ class KotlinFiler(val generatedSourceDir: File, val classesOutputDir: File) : Fi
|
||||
}
|
||||
|
||||
override fun getResource(location: JavaFileManager.Location, pkg: CharSequence, relativeName: CharSequence): FileObject? {
|
||||
throw UnsupportedOperationException()
|
||||
return KotlinFileObject(getResourceFile(location, pkg, relativeName))
|
||||
}
|
||||
|
||||
override fun createResource(
|
||||
@@ -68,18 +57,22 @@ class KotlinFiler(val generatedSourceDir: File, val classesOutputDir: File) : Fi
|
||||
relativeName: CharSequence,
|
||||
vararg originatingElements: Element?
|
||||
): FileObject? {
|
||||
val resourceFile = getResourceFile(location, pkg, relativeName)
|
||||
resourceFile.parentFile.mkdirs()
|
||||
return KotlinFileObject(resourceFile)
|
||||
}
|
||||
|
||||
private fun getResourceFile(location: JavaFileManager.Location, pkg: CharSequence, relativeName: CharSequence): File {
|
||||
val baseDir = when (location) {
|
||||
StandardLocation.CLASS_OUTPUT -> classesOutputDir
|
||||
StandardLocation.SOURCE_OUTPUT -> generatedSourceDir
|
||||
else -> throw IllegalArgumentException("Location is not supported: $location (${location.name})")
|
||||
}
|
||||
|
||||
|
||||
val targetDir = File(baseDir, pkg.toString().replace('.', '/'))
|
||||
targetDir.mkdirs()
|
||||
|
||||
val targetFile = File(targetDir, relativeName.toString())
|
||||
|
||||
return KotlinFileObject(targetFile)
|
||||
|
||||
return File(targetDir, relativeName.toString())
|
||||
}
|
||||
|
||||
override fun createClassFile(name: CharSequence, vararg originatingElements: Element?): JavaFileObject {
|
||||
|
||||
+1
-3
@@ -49,9 +49,7 @@ abstract class KotlinAbstractFileObject(val file: File) : FileObject {
|
||||
|
||||
override fun openInputStream() = file.inputStream()
|
||||
|
||||
override fun getCharContent(ignoreEncodingErrors: Boolean): CharSequence? {
|
||||
TODO()
|
||||
}
|
||||
override fun getCharContent(ignoreEncodingErrors: Boolean) = file.readText()
|
||||
|
||||
override fun getLastModified() = file.lastModified()
|
||||
|
||||
|
||||
+6
-1
@@ -21,6 +21,7 @@ import javax.lang.model.element.AnnotationMirror
|
||||
import javax.lang.model.element.AnnotationValue
|
||||
import javax.lang.model.element.Element
|
||||
import javax.tools.Diagnostic
|
||||
import javax.tools.Diagnostic.Kind
|
||||
|
||||
class KotlinMessager : Messager {
|
||||
override fun printMessage(kind: Diagnostic.Kind, msg: CharSequence) = printMessage(kind, msg, null)
|
||||
@@ -32,6 +33,10 @@ class KotlinMessager : Messager {
|
||||
}
|
||||
|
||||
override fun printMessage(kind: Diagnostic.Kind, msg: CharSequence, e: Element?, a: AnnotationMirror?, v: AnnotationValue?) {
|
||||
println(msg)
|
||||
val output = when (kind) {
|
||||
Kind.ERROR, Kind.WARNING, Kind.MANDATORY_WARNING -> System.err
|
||||
else -> System.out
|
||||
}
|
||||
output.println(msg)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -33,7 +33,7 @@ class KotlinProcessingEnvironment(
|
||||
override fun getTypeUtils() = types
|
||||
override fun getMessager() = messager
|
||||
override fun getLocale() = Locale.getDefault()
|
||||
override fun getSourceVersion() = SourceVersion.RELEASE_6
|
||||
override fun getSourceVersion() = SourceVersion.RELEASE_8
|
||||
override fun getOptions() = options
|
||||
override fun getFiler() = filer
|
||||
}
|
||||
+2
-2
@@ -17,7 +17,7 @@
|
||||
package org.jetbrains.kotlin.annotation.processing.impl
|
||||
|
||||
import org.jetbrains.kotlin.annotation.AnalysisContext
|
||||
import org.jetbrains.kotlin.java.model.JeConverter
|
||||
import org.jetbrains.kotlin.java.model.toJeElement
|
||||
import javax.annotation.processing.RoundEnvironment
|
||||
import javax.lang.model.element.Element
|
||||
import javax.lang.model.element.TypeElement
|
||||
@@ -35,7 +35,7 @@ internal class KotlinRoundEnvironment(
|
||||
val declarations = context.annotationsMap[fqName] ?: return emptySet()
|
||||
return hashSetOf<Element>().apply {
|
||||
for (declaration in declarations) {
|
||||
JeConverter.convert(declaration)?.let { add(it) }
|
||||
declaration.toJeElement()?.let { add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-6
@@ -139,8 +139,12 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val psiManager: PsiManager,
|
||||
}
|
||||
|
||||
override fun isSameType(t1: TypeMirror, t2: TypeMirror) = t1 == t2
|
||||
|
||||
override fun getNoType(kind: TypeKind) = CustomJeNoneType(kind)
|
||||
|
||||
override fun getNoType(kind: TypeKind) = when (kind) {
|
||||
TypeKind.VOID -> JeVoidType
|
||||
TypeKind.NONE -> JeNoneType
|
||||
else -> illegalArg("Must be kind of VOID or NONE, got ${kind.name}")
|
||||
}
|
||||
|
||||
override fun getDeclaredType(typeElem: TypeElement, vararg typeArgMirrors: TypeMirror): DeclaredType {
|
||||
val psiClass = (typeElem as JeTypeElement).psi
|
||||
@@ -165,7 +169,7 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val psiManager: PsiManager,
|
||||
|
||||
val psiType = createDeclaredType(psiClass, typeArgs) ?:
|
||||
throw IllegalStateException("Can't create declared type ($psiClass, $typeArgs)")
|
||||
return JeDeclaredType(psiType, psiClass, typeArgumentMirrors = typeArgMirrors.toList())
|
||||
return JeDeclaredType(psiType, psiClass)
|
||||
}
|
||||
|
||||
override fun getDeclaredType(
|
||||
@@ -197,7 +201,7 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val psiManager: PsiManager,
|
||||
|
||||
val psiType = createDeclaredType(psiClass, typeArgs) ?:
|
||||
throw IllegalStateException("Can't create declared type ($psiClass, $typeArgs)")
|
||||
return JeDeclaredType(psiType, psiClass, containing, typeArgMirrors.toList())
|
||||
return JeDeclaredType(psiType, psiClass, containing)
|
||||
}
|
||||
|
||||
override fun asMemberOf(containing: DeclaredType, element: Element): TypeMirror {
|
||||
@@ -258,9 +262,9 @@ private fun illegalArg(text: String? = null): Nothing {
|
||||
}
|
||||
|
||||
private fun assertKindNot(typeMirror: TypeMirror, vararg kinds: TypeKind): Unit {
|
||||
if (typeMirror.kind in kinds) illegalArg("type must not be kind of " + typeMirror.kind.name + ", got ${typeMirror.kind}")
|
||||
if (typeMirror.kind in kinds) illegalArg("type must not be kind of " + kinds.joinToString { it.name } + ", got ${typeMirror.kind.name}")
|
||||
}
|
||||
|
||||
private fun assertJeType(type: TypeMirror) {
|
||||
illegalArg("Must be a subclass of JePsiType, got ${type.javaClass.canonicalName}")
|
||||
illegalArg("Must be a subclass of JePsiType, got ${type.javaClass.name}")
|
||||
}
|
||||
Reference in New Issue
Block a user