From 0d74fc2290d16d7c97952c4a4b386f3c1403b683 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 17 Mar 2016 20:39:12 +0300 Subject: [PATCH] Minor cleanup in Java model: fix warnings, remove unused --- .../PsiBasedExternalAnnotationResolver.java | 27 +++---------------- .../structure/impl/JavaAnnotationImpl.java | 15 +++++------ .../java/structure/impl/JavaClassImpl.java | 3 ++- .../java/structure/impl/JavaMethodImpl.java | 1 - .../structure/impl/JavaTypeParameterImpl.java | 3 ++- .../ExternalAnnotationResolver.java | 12 --------- .../load/java/structure/JavaAnnotation.java | 4 --- .../reflect/ReflectJavaAnnotation.kt | 13 +++------ .../reflect/ReflectJavaModifierListOwner.kt | 3 ++- .../jetbrains/kotlin/name/SpecialNames.java | 5 ++++ 10 files changed, 23 insertions(+), 63 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 0b8ff30a6a0..c618b2ab73a 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,40 +24,19 @@ 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.JavaElementCollectionFromPsiArrayUtil; import org.jetbrains.kotlin.load.java.structure.impl.JavaModifierListOwnerImpl; import org.jetbrains.kotlin.name.FqName; -import java.util.Collection; -import java.util.Collections; - public class PsiBasedExternalAnnotationResolver implements ExternalAnnotationResolver { @Nullable @Override public JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName) { if (owner instanceof JavaModifierListOwnerImpl) { - JavaModifierListOwnerImpl modifierListOwner = (JavaModifierListOwnerImpl) owner; - PsiAnnotation psiAnnotation = findExternalAnnotation(modifierListOwner.getPsi(), fqName); + PsiModifierListOwner psiOwner = ((JavaModifierListOwnerImpl) owner).getPsi(); + PsiAnnotation psiAnnotation = + ExternalAnnotationsManager.getInstance(psiOwner.getProject()).findExternalAnnotation(psiOwner, fqName.asString()); return psiAnnotation == null ? null : new JavaAnnotationImpl(psiAnnotation); } return null; } - - @NotNull - @Override - public Collection findExternalAnnotations(@NotNull JavaAnnotationOwner owner) { - 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 - public static PsiAnnotation findExternalAnnotation(@NotNull PsiModifierListOwner owner, @NotNull FqName fqName) { - return ExternalAnnotationsManager.getInstance(owner.getProject()).findExternalAnnotation(owner, fqName.asString()); - } } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationImpl.java index 0fed93054f2..6b6bc8b919e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaAnnotationImpl.java @@ -16,7 +16,10 @@ package org.jetbrains.kotlin.load.java.structure.impl; -import com.intellij.psi.*; +import com.intellij.psi.PsiAnnotation; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiJavaCodeReferenceElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; @@ -35,13 +38,6 @@ public class JavaAnnotationImpl extends JavaElementImpl implement super(psiAnnotation); } - @Override - @Nullable - public JavaAnnotationArgument findArgument(@NotNull Name name) { - PsiAnnotationMemberValue attribute = getPsi().findAttributeValue(name.asString()); - return attribute == null ? null : JavaAnnotationArgumentImpl.Factory.create(attribute, name); - } - @Override @NotNull public Collection getArguments() { @@ -74,7 +70,8 @@ public class JavaAnnotationImpl extends JavaElementImpl implement PsiClass container = psiClass.getContainingClass(); if (container != null) { ClassId parentClassId = computeClassId(container); - return parentClassId == null ? null : parentClassId.createNestedClassId(Name.identifier(psiClass.getName())); + String name = psiClass.getName(); + return parentClassId == null || name == null ? null : parentClassId.createNestedClassId(Name.identifier(name)); } String fqName = psiClass.getQualifiedName(); 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 13ec66dccd7..618a8ea940b 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 @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.descriptors.Visibility; import org.jetbrains.kotlin.load.java.structure.*; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.Name; +import org.jetbrains.kotlin.name.SpecialNames; import java.util.Collection; import java.util.List; @@ -55,7 +56,7 @@ public class JavaClassImpl extends JavaClassifierImpl implements JavaC @NotNull @Override public Name getName() { - return Name.identifier(getPsi().getName()); + return SpecialNames.safeIdentifier(getPsi().getName()); } @Override diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMethodImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMethodImpl.java index 91927c71d3a..ce0398471fb 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMethodImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaMethodImpl.java @@ -20,7 +20,6 @@ import com.intellij.psi.PsiAnnotationMethod; import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiType; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.load.java.structure.JavaMethod; import org.jetbrains.kotlin.load.java.structure.JavaType; import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter; diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeParameterImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeParameterImpl.java index 69ca2cc6898..a89cb6516d4 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeParameterImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeParameterImpl.java @@ -23,6 +23,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.load.java.structure.JavaClassifierType; import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter; import org.jetbrains.kotlin.name.Name; +import org.jetbrains.kotlin.name.SpecialNames; import java.util.Collection; @@ -36,7 +37,7 @@ public class JavaTypeParameterImpl extends JavaClassifierImpl @NotNull @Override public Name getName() { - return Name.identifier(getPsi().getName()); + return SpecialNames.safeIdentifier(getPsi().getName()); } @Override diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/ExternalAnnotationResolver.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/ExternalAnnotationResolver.java index da7abdd5393..a1c2b55e910 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/ExternalAnnotationResolver.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/ExternalAnnotationResolver.java @@ -22,9 +22,6 @@ import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; import org.jetbrains.kotlin.load.java.structure.JavaAnnotationOwner; import org.jetbrains.kotlin.name.FqName; -import java.util.Collection; -import java.util.Collections; - public interface ExternalAnnotationResolver { ExternalAnnotationResolver EMPTY = new ExternalAnnotationResolver() { @Nullable @@ -32,17 +29,8 @@ public interface ExternalAnnotationResolver { public JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName) { return null; } - - @NotNull - @Override - public Collection findExternalAnnotations(@NotNull JavaAnnotationOwner owner) { - return Collections.emptyList(); - } }; @Nullable JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName); - - @NotNull - Collection findExternalAnnotations(@NotNull JavaAnnotationOwner owner); } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaAnnotation.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaAnnotation.java index a34475ef991..72379e66ef2 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaAnnotation.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaAnnotation.java @@ -19,14 +19,10 @@ package org.jetbrains.kotlin.load.java.structure; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.name.ClassId; -import org.jetbrains.kotlin.name.Name; import java.util.Collection; public interface JavaAnnotation extends JavaElement { - @Nullable - JavaAnnotationArgument findArgument(@NotNull Name name); - @NotNull Collection getArguments(); diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaAnnotation.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaAnnotation.kt index 0fe1e61380d..c77b92a2e41 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaAnnotation.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaAnnotation.kt @@ -19,19 +19,12 @@ package org.jetbrains.kotlin.load.java.structure.reflect import org.jetbrains.kotlin.load.java.structure.JavaAnnotation import org.jetbrains.kotlin.load.java.structure.JavaAnnotationArgument import org.jetbrains.kotlin.name.Name -import java.lang.reflect.Method class ReflectJavaAnnotation(val annotation: Annotation) : ReflectJavaElement(), JavaAnnotation { - override fun findArgument(name: Name): JavaAnnotationArgument? { - return getArgumentValue(annotation.annotationClass.java.getDeclaredMethod(name.asString())) - } - override fun getArguments(): Collection { - return annotation.annotationClass.java.declaredMethods.map { getArgumentValue(it) } - } - - private fun getArgumentValue(argument: Method): ReflectJavaAnnotationArgument { - return argument.invoke(annotation).let { ReflectJavaAnnotationArgument.create(it, Name.identifier(argument.name)) } + return annotation.annotationClass.java.declaredMethods.map { method -> + ReflectJavaAnnotationArgument.create(method.invoke(annotation), Name.identifier(method.name)) + } } override fun resolve() = ReflectJavaClass(annotation.annotationClass.java) diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaModifierListOwner.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaModifierListOwner.kt index 53bca09b14c..172b90f191b 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaModifierListOwner.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaModifierListOwner.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.load.java.structure.reflect import org.jetbrains.kotlin.descriptors.Visibilities +import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.load.java.JavaVisibilities import org.jetbrains.kotlin.load.java.structure.JavaModifierListOwner import java.lang.reflect.Modifier @@ -28,7 +29,7 @@ interface ReflectJavaModifierListOwner : JavaModifierListOwner { override fun isStatic() = Modifier.isStatic(modifiers) override fun isFinal() = Modifier.isFinal(modifiers) - override fun getVisibility() = modifiers.let { modifiers -> + override fun getVisibility(): Visibility = modifiers.let { modifiers -> when { Modifier.isPublic(modifiers) -> Visibilities.PUBLIC Modifier.isPrivate(modifiers) -> Visibilities.PRIVATE diff --git a/core/descriptors/src/org/jetbrains/kotlin/name/SpecialNames.java b/core/descriptors/src/org/jetbrains/kotlin/name/SpecialNames.java index 2511301a5df..c3e19c5649a 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/name/SpecialNames.java +++ b/core/descriptors/src/org/jetbrains/kotlin/name/SpecialNames.java @@ -36,6 +36,11 @@ public class SpecialNames { return name != null && !name.isSpecial() ? name : SAFE_IDENTIFIER_FOR_NO_NAME; } + @NotNull + public static Name safeIdentifier(@Nullable String name) { + return safeIdentifier(name == null ? null : Name.identifier(name)); + } + public static boolean isSafeIdentifier(@NotNull Name name) { return !name.asString().isEmpty() && !name.isSpecial(); }