From e3b01f073f08fefb2e4f44aa9fb53b194e5ac31c Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 29 Aug 2014 12:06:33 +0400 Subject: [PATCH] Replace FqName by ClassId where possible in Java resolver --- .../structure/impl/JavaAnnotationImpl.java | 29 +++++++++++++---- .../lang/resolve/java/JvmAnnotationNames.java | 5 +-- .../jet/lang/resolve/java/JvmClassName.java | 13 ++++++-- .../lang/resolve/java/lazy/annotationUtils.kt | 10 ++---- .../LazyJavaAnnotationDescriptor.kt | 31 +++++++++++-------- .../java/structure/JavaAnnotation.java | 4 +-- 6 files changed, 59 insertions(+), 33 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/impl/JavaAnnotationImpl.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/impl/JavaAnnotationImpl.java index 22f5ab1dd12..6fe30c4ae66 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/impl/JavaAnnotationImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/impl/JavaAnnotationImpl.java @@ -19,6 +19,7 @@ package org.jetbrains.jet.lang.resolve.java.structure.impl; import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.descriptors.serialization.ClassId; import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotation; import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotationArgument; import org.jetbrains.jet.lang.resolve.java.structure.JavaClass; @@ -49,20 +50,36 @@ public class JavaAnnotationImpl extends JavaElementImpl implement @Override @Nullable - public FqName getFqName() { - String qualifiedName = getPsi().getQualifiedName(); - return qualifiedName == null ? null : new FqName(qualifiedName); + public ClassId getClassId() { + PsiClass resolved = resolvePsi(); + return resolved == null ? null : computeClassId(resolved); } @Nullable @Override public JavaClass resolve() { + PsiClass resolved = resolvePsi(); + return resolved == null ? null : new JavaClassImpl(resolved); + } + + @Nullable + private static ClassId computeClassId(@NotNull PsiClass psiClass) { + PsiClass container = psiClass.getContainingClass(); + if (container != null) { + ClassId parentClassId = computeClassId(container); + return parentClassId == null ? null : parentClassId.createNestedClassId(Name.identifier(psiClass.getName())); + } + + String fqName = psiClass.getQualifiedName(); + return fqName == null ? null : ClassId.topLevel(new FqName(fqName)); + } + + @Nullable + private PsiClass resolvePsi() { PsiJavaCodeReferenceElement referenceElement = getPsi().getNameReferenceElement(); if (referenceElement == null) return null; PsiElement resolved = referenceElement.resolve(); - if (!(resolved instanceof PsiClass)) return null; - - return new JavaClassImpl((PsiClass) resolved); + return resolved instanceof PsiClass ? (PsiClass) resolved : null; } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmAnnotationNames.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmAnnotationNames.java index 466a7103e35..0422b68a08b 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmAnnotationNames.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmAnnotationNames.java @@ -18,6 +18,7 @@ package org.jetbrains.jet.lang.resolve.java; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.descriptors.serialization.ClassId; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; @@ -102,8 +103,8 @@ public final class JvmAnnotationNames { SPECIAL_ANNOTATIONS.add(KotlinSyntheticClass.CLASS_NAME); } - public static boolean isSpecialAnnotation(@NotNull FqName fqName) { - return isSpecialAnnotation(JvmClassName.byFqNameWithoutInnerClasses(fqName)); + public static boolean isSpecialAnnotation(@NotNull ClassId classId) { + return isSpecialAnnotation(JvmClassName.byClassId(classId)); } public static boolean isSpecialAnnotation(@NotNull JvmClassName name) { diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmClassName.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmClassName.java index d6f11a54e98..2cadd6a0512 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmClassName.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/JvmClassName.java @@ -17,12 +17,10 @@ package org.jetbrains.jet.lang.resolve.java; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.descriptors.serialization.ClassId; import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.lang.resolve.name.Name; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; public class JvmClassName { @NotNull @@ -30,6 +28,15 @@ public class JvmClassName { return new JvmClassName(internalName); } + @NotNull + public static JvmClassName byClassId(@NotNull ClassId classId) { + FqName packageFqName = classId.getPackageFqName(); + String relativeClassName = classId.getRelativeClassName().asString().replace('.', '$'); + return packageFqName.isRoot() + ? new JvmClassName(relativeClassName) + : new JvmClassName(packageFqName.asString().replace('.', '/') + "/" + relativeClassName); + } + /** * WARNING: fq name cannot be uniquely mapped to JVM class name. */ diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/annotationUtils.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/annotationUtils.kt index 7fb1cea8628..0da8303a0a5 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/annotationUtils.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/annotationUtils.kt @@ -21,17 +21,13 @@ import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotationOwner import org.jetbrains.jet.lang.resolve.name.FqName import org.jetbrains.jet.lang.descriptors.annotations.Annotations import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.jet.lang.resolve.java.lazy.descriptors.LazyJavaAnnotationDescriptor import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames +import org.jetbrains.jet.lang.resolve.java.lazy.descriptors.resolveAnnotation class LazyJavaAnnotations(c: LazyJavaResolverContextWithTypes, val annotationOwner: JavaAnnotationOwner) : Annotations { private val annotationDescriptors = c.storageManager.createMemoizedFunctionWithNullableValues { - (jAnnotation: JavaAnnotation) -> - val fqName = jAnnotation.getFqName() - if (fqName == null || JvmAnnotationNames.isSpecialAnnotation(fqName)) { - null - } - else LazyJavaAnnotationDescriptor(c, jAnnotation) + (annotation: JavaAnnotation) -> + c.resolveAnnotation(annotation) } override fun findAnnotation(fqName: FqName): AnnotationDescriptor? { diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt index b04d7e2a38c..723c7a11880 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt @@ -17,11 +17,10 @@ package org.jetbrains.jet.lang.resolve.java.lazy.descriptors import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.jet.lang.types.JetType +import org.jetbrains.jet.lang.types.* import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor import org.jetbrains.jet.lang.resolve.java.lazy.LazyJavaResolverContextWithTypes import org.jetbrains.jet.lang.resolve.java.structure.* -import org.jetbrains.jet.lang.types.ErrorUtils import org.jetbrains.jet.lang.resolve.constants.* import org.jetbrains.jet.lang.resolve.name.Name import org.jetbrains.jet.lang.resolve.name.FqName @@ -30,27 +29,37 @@ import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.* import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames import org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils import org.jetbrains.jet.lang.resolve.java.resolver.TypeUsage -import org.jetbrains.jet.lang.types.TypeProjectionImpl import org.jetbrains.jet.lang.resolve.java.lazy.types.LazyJavaType import org.jetbrains.jet.utils.valuesToMap import org.jetbrains.jet.utils.keysToMapExceptNulls import org.jetbrains.jet.lang.resolve.java.lazy.types.toAttributes import org.jetbrains.jet.renderer.DescriptorRenderer import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap -import org.jetbrains.jet.lang.types.TypeUtils import org.jetbrains.jet.lang.resolve.resolveTopLevelClass +import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinFqNameToJavaFqName private object DEPRECATED_IN_JAVA : JavaLiteralAnnotationArgument { override val name: Name? = null override val value: Any? = "Deprecated in Java" } +fun LazyJavaResolverContextWithTypes.resolveAnnotation(annotation: JavaAnnotation): LazyJavaAnnotationDescriptor? { + val classId = annotation.getClassId() + if (classId == null || JvmAnnotationNames.isSpecialAnnotation(classId)) return null + return LazyJavaAnnotationDescriptor(this, annotation) +} + class LazyJavaAnnotationDescriptor( private val c: LazyJavaResolverContextWithTypes, val javaAnnotation : JavaAnnotation ) : AnnotationDescriptor { - private val fqName = c.storageManager.createNullableLazyValue { javaAnnotation.getFqName() } + private val fqName = c.storageManager.createNullableLazyValue { + val classId = javaAnnotation.getClassId() + if (classId == null) null + else kotlinFqNameToJavaFqName(classId.asSingleFqName()) + } + private val _type = c.storageManager.createLazyValue {() : JetType -> val fqName = fqName() if (fqName == null) return@createLazyValue ErrorUtils.createErrorType("No fqName: $javaAnnotation") @@ -69,7 +78,7 @@ class LazyJavaAnnotationDescriptor( arguments.valuesToMap { it.name } } - private val valueArguments = c.storageManager.createMemoizedFunctionWithNullableValues> { + private val valueArguments = c.storageManager.createMemoizedFunctionWithNullableValues> { valueParameter -> val nameToArg = nameToArgument() @@ -109,14 +118,10 @@ class LazyJavaAnnotationDescriptor( } private fun resolveFromAnnotation(javaAnnotation: JavaAnnotation): CompileTimeConstant<*>? { - val fqName = javaAnnotation.getFqName() - if (fqName == null) return null + val descriptor = c.resolveAnnotation(javaAnnotation) + if (descriptor == null) return null - if (JvmAnnotationNames.isSpecialAnnotation(fqName)) { - return null - } - - return AnnotationValue(LazyJavaAnnotationDescriptor(c, javaAnnotation)) + return AnnotationValue(descriptor) } private fun resolveFromArray(argumentName: Name, elements: List): CompileTimeConstant<*>? { diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaAnnotation.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaAnnotation.java index 30403a892e2..7ac083373ef 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaAnnotation.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaAnnotation.java @@ -18,7 +18,7 @@ package org.jetbrains.jet.lang.resolve.java.structure; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.descriptors.serialization.ClassId; import org.jetbrains.jet.lang.resolve.name.Name; import java.util.Collection; @@ -31,7 +31,7 @@ public interface JavaAnnotation extends JavaElement { Collection getArguments(); @Nullable - FqName getFqName(); + ClassId getClassId(); @Nullable JavaClass resolve();