From 31f4ff749c34c8ff573a1d6e1688c39ea1b4463c Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 5 May 2015 21:35:37 +0300 Subject: [PATCH] Type annotations supported in Java elements Reflection-related implementations are pending --- .../PsiBasedExternalAnnotationResolver.java | 23 ++++++++----- .../impl/JavaAnnotationOwnerImpl.java | 8 ++--- .../java/structure/impl/JavaClassImpl.java | 12 ------- .../structure/impl/JavaClassifierImpl.java | 26 ++++++++++++++- .../java/structure/impl/JavaElementUtil.java | 17 ++++------ .../java/structure/impl/JavaMemberImpl.java | 7 ++++ .../java/structure/impl/JavaTypeImpl.java | 25 +++++++++++++- .../impl/JavaValueParameterImpl.java | 33 ++++++++++++++++++- .../java/structure/JavaClassifierType.java | 2 +- .../reflect/ReflectJavaClassifierType.kt | 14 +++++--- 10 files changed, 125 insertions(+), 42 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/PsiBasedExternalAnnotationResolver.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/PsiBasedExternalAnnotationResolver.java index 4bd2daea519..0b8ff30a6a0 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/PsiBasedExternalAnnotationResolver.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/PsiBasedExternalAnnotationResolver.java @@ -24,8 +24,8 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; import org.jetbrains.kotlin.load.java.structure.JavaAnnotationOwner; import org.jetbrains.kotlin.load.java.structure.impl.JavaAnnotationImpl; -import org.jetbrains.kotlin.load.java.structure.impl.JavaAnnotationOwnerImpl; import org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectionFromPsiArrayUtil; +import org.jetbrains.kotlin.load.java.structure.impl.JavaModifierListOwnerImpl; import org.jetbrains.kotlin.name.FqName; import java.util.Collection; @@ -35,18 +35,25 @@ public class PsiBasedExternalAnnotationResolver implements ExternalAnnotationRes @Nullable @Override public JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName) { - PsiAnnotation psiAnnotation = findExternalAnnotation(((JavaAnnotationOwnerImpl) owner).getPsi(), fqName); - return psiAnnotation == null ? null : new JavaAnnotationImpl(psiAnnotation); + if (owner instanceof JavaModifierListOwnerImpl) { + JavaModifierListOwnerImpl modifierListOwner = (JavaModifierListOwnerImpl) owner; + PsiAnnotation psiAnnotation = findExternalAnnotation(modifierListOwner.getPsi(), fqName); + return psiAnnotation == null ? null : new JavaAnnotationImpl(psiAnnotation); + } + return null; } @NotNull @Override public Collection findExternalAnnotations(@NotNull JavaAnnotationOwner owner) { - PsiModifierListOwner psiOwner = ((JavaAnnotationOwnerImpl) owner).getPsi(); - PsiAnnotation[] annotations = ExternalAnnotationsManager.getInstance(psiOwner.getProject()).findExternalAnnotations(psiOwner); - return annotations == null - ? Collections.emptyList() - : JavaElementCollectionFromPsiArrayUtil.annotations(annotations); + if (owner instanceof JavaModifierListOwnerImpl) { + PsiModifierListOwner psiOwner = ((JavaModifierListOwnerImpl) owner).getPsi(); + PsiAnnotation[] annotations = ExternalAnnotationsManager.getInstance(psiOwner.getProject()).findExternalAnnotations(psiOwner); + return annotations == null + ? Collections.emptyList() + : JavaElementCollectionFromPsiArrayUtil.annotations(annotations); + } + return Collections.emptyList(); } @Nullable diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationOwnerImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationOwnerImpl.java index dab67466412..c13860e33d6 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationOwnerImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationOwnerImpl.java @@ -16,11 +16,11 @@ package org.jetbrains.kotlin.load.java.structure.impl; -import com.intellij.psi.PsiModifierListOwner; -import org.jetbrains.annotations.NotNull; +import com.intellij.psi.PsiAnnotationOwner; +import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.load.java.structure.JavaAnnotationOwner; public interface JavaAnnotationOwnerImpl extends JavaAnnotationOwner { - @NotNull - PsiModifierListOwner getPsi(); + @Nullable + PsiAnnotationOwner getAnnotationOwnerPsi(); } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java index 57db2aef037..d7f9e5d13dd 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java @@ -146,18 +146,6 @@ public class JavaClassImpl extends JavaClassifierImpl implements JavaC return JavaElementUtil.getVisibility(this); } - @NotNull - @Override - public Collection getAnnotations() { - return JavaElementUtil.getAnnotations(this); - } - - @Nullable - @Override - public JavaAnnotation findAnnotation(@NotNull FqName fqName) { - return JavaElementUtil.findAnnotation(this, fqName); - } - @Override @NotNull public JavaClassifierType getDefaultType() { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierImpl.java index cbca1c0ecaf..f94bd71652e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierImpl.java @@ -16,16 +16,28 @@ package org.jetbrains.kotlin.load.java.structure.impl; +import com.intellij.psi.PsiAnnotationOwner; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiTypeParameter; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; import org.jetbrains.kotlin.load.java.structure.JavaClassifier; +import org.jetbrains.kotlin.name.FqName; -public abstract class JavaClassifierImpl extends JavaElementImpl implements JavaClassifier { +import java.util.Collection; + +public abstract class JavaClassifierImpl extends JavaElementImpl implements JavaClassifier, JavaAnnotationOwnerImpl { protected JavaClassifierImpl(@NotNull Psi psiClass) { super(psiClass); } + @NotNull + @Override + public PsiAnnotationOwner getAnnotationOwnerPsi() { + return getPsi().getModifierList(); + } + @NotNull /* package */ static JavaClassifier create(@NotNull PsiClass psiClass) { if (psiClass instanceof PsiTypeParameter) { @@ -35,4 +47,16 @@ public abstract class JavaClassifierImpl extends JavaEleme return new JavaClassImpl(psiClass); } } + + @NotNull + @Override + public Collection getAnnotations() { + return JavaElementUtil.getAnnotations(this); + } + + @Nullable + @Override + public JavaAnnotation findAnnotation(@NotNull FqName fqName) { + return JavaElementUtil.findAnnotation(this, fqName); + } } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementUtil.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementUtil.java index 03c6cdc46d6..c0a225744c3 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementUtil.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementUtil.java @@ -16,10 +16,7 @@ package org.jetbrains.kotlin.load.java.structure.impl; -import com.intellij.psi.PsiAnnotation; -import com.intellij.psi.PsiModifier; -import com.intellij.psi.PsiModifierList; -import com.intellij.psi.PsiModifierListOwner; +import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.Visibilities; @@ -66,18 +63,18 @@ import static org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectio @NotNull public static Collection getAnnotations(@NotNull JavaAnnotationOwnerImpl owner) { - PsiModifierList modifierList = owner.getPsi().getModifierList(); - if (modifierList != null) { - return annotations(modifierList.getAnnotations()); + PsiAnnotationOwner annotationOwnerPsi = owner.getAnnotationOwnerPsi(); + if (annotationOwnerPsi != null) { + return annotations(annotationOwnerPsi.getAnnotations()); } return Collections.emptyList(); } @Nullable public static JavaAnnotation findAnnotation(@NotNull JavaAnnotationOwnerImpl owner, @NotNull FqName fqName) { - PsiModifierList modifierList = owner.getPsi().getModifierList(); - if (modifierList != null) { - PsiAnnotation psiAnnotation = modifierList.findAnnotation(fqName.asString()); + PsiAnnotationOwner annotationOwnerPsi = owner.getAnnotationOwnerPsi(); + if (annotationOwnerPsi != null) { + PsiAnnotation psiAnnotation = annotationOwnerPsi.findAnnotation(fqName.asString()); return psiAnnotation == null ? null : new JavaAnnotationImpl(psiAnnotation); } return null; diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMemberImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMemberImpl.java index 03989fb0a0b..f730d42fc57 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMemberImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMemberImpl.java @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.load.java.structure.impl; +import com.intellij.psi.PsiAnnotationOwner; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiMember; import org.jetbrains.annotations.NotNull; @@ -35,6 +36,12 @@ public abstract class JavaMemberImpl extends JavaElementI super(psiMember); } + @Nullable + @Override + public PsiAnnotationOwner getAnnotationOwnerPsi() { + return getPsi().getModifierList(); + } + @NotNull @Override public Name getName() { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeImpl.java index 1b55598c529..e2874bafc5a 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeImpl.java @@ -19,10 +19,14 @@ package org.jetbrains.kotlin.load.java.structure.impl; import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; import org.jetbrains.kotlin.load.java.structure.JavaArrayType; import org.jetbrains.kotlin.load.java.structure.JavaType; +import org.jetbrains.kotlin.name.FqName; -public abstract class JavaTypeImpl implements JavaType { +import java.util.Collection; + +public abstract class JavaTypeImpl implements JavaType, JavaAnnotationOwnerImpl { private final Psi psiType; public JavaTypeImpl(@NotNull Psi psiType) { @@ -34,6 +38,12 @@ public abstract class JavaTypeImpl implements JavaType { return psiType; } + @Nullable + @Override + public PsiAnnotationOwner getAnnotationOwnerPsi() { + return getPsi(); + } + @NotNull public static JavaTypeImpl create(@NotNull PsiType psiType) { return psiType.accept(new PsiTypeVisitor>() { @@ -75,6 +85,19 @@ public abstract class JavaTypeImpl implements JavaType { return new JavaArrayTypeImpl(getPsi().createArrayType()); } + @NotNull + @Override + public Collection getAnnotations() { + return JavaElementUtil.getAnnotations(this); + } + + @Nullable + @Override + public JavaAnnotation findAnnotation(@NotNull FqName fqName) { + return JavaElementUtil.findAnnotation(this, fqName); + } + + @Override public int hashCode() { return getPsi().hashCode(); diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaValueParameterImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaValueParameterImpl.java index d54774cbed4..d8a03f5ad37 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaValueParameterImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaValueParameterImpl.java @@ -16,10 +16,13 @@ package org.jetbrains.kotlin.load.java.structure.impl; +import com.intellij.psi.PsiAnnotationOwner; import com.intellij.psi.PsiParameter; import com.intellij.psi.impl.compiled.ClsParameterImpl; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.descriptors.Visibilities; +import org.jetbrains.kotlin.descriptors.Visibility; import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; import org.jetbrains.kotlin.load.java.structure.JavaType; import org.jetbrains.kotlin.load.java.structure.JavaValueParameter; @@ -28,11 +31,39 @@ import org.jetbrains.kotlin.name.Name; import java.util.Collection; -public class JavaValueParameterImpl extends JavaElementImpl implements JavaValueParameter, JavaAnnotationOwnerImpl { +public class JavaValueParameterImpl extends JavaElementImpl + implements JavaValueParameter, JavaAnnotationOwnerImpl, JavaModifierListOwnerImpl { public JavaValueParameterImpl(@NotNull PsiParameter psiParameter) { super(psiParameter); } + @Nullable + @Override + public PsiAnnotationOwner getAnnotationOwnerPsi() { + return getPsi().getModifierList(); + } + + @Override + public boolean isAbstract() { + return false; + } + + @Override + public boolean isStatic() { + return false; + } + + @Override + public boolean isFinal() { + return false; + } + + @NotNull + @Override + public Visibility getVisibility() { + return Visibilities.LOCAL; + } + @NotNull @Override public Collection getAnnotations() { diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java index d776e0de3c8..02711b38a9b 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java @@ -23,7 +23,7 @@ import org.jetbrains.annotations.ReadOnly; import java.util.Collection; import java.util.List; -public interface JavaClassifierType extends JavaType { +public interface JavaClassifierType extends JavaType, JavaAnnotationOwner { @Nullable JavaClassifier getClassifier(); diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt index f18d036a06f..f7e863fe164 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt @@ -16,10 +16,8 @@ package org.jetbrains.kotlin.load.java.structure.reflect -import org.jetbrains.kotlin.load.java.structure.JavaClassifier -import org.jetbrains.kotlin.load.java.structure.JavaClassifierType -import org.jetbrains.kotlin.load.java.structure.JavaType -import org.jetbrains.kotlin.load.java.structure.JavaTypeSubstitutor +import org.jetbrains.kotlin.load.java.structure.* +import org.jetbrains.kotlin.name.FqName import java.lang.reflect.ParameterizedType import java.lang.reflect.Type import java.lang.reflect.TypeVariable @@ -48,4 +46,12 @@ public class ReflectJavaClassifierType(public override val type: Type) : Reflect override fun getTypeArguments(): List { return (type as? ParameterizedType)?.getActualTypeArguments()?.map { ReflectJavaType.create(it) } ?: listOf() } + + override fun getAnnotations(): Collection { + return emptyList() // TODO + } + + override fun findAnnotation(fqName: FqName): JavaAnnotation? { + return null // TODO + } }