From 724b0322c06317776e51c11fa91ccff1cc5df199 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Mon, 19 Sep 2016 15:11:45 +0300 Subject: [PATCH] J2K JavaClassImpl: convert code --- .../java/structure/impl/JavaClassImpl.java | 223 ++++++------------ 1 file changed, 75 insertions(+), 148 deletions(-) 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 29c5569cd81..11e416e5a48 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 @@ -14,159 +14,86 @@ * limitations under the License. */ -package org.jetbrains.kotlin.load.java.structure.impl; +package org.jetbrains.kotlin.load.java.structure.impl -import com.intellij.psi.*; -import kotlin.collections.ArraysKt; -import kotlin.collections.CollectionsKt; -import kotlin.jvm.functions.Function1; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.asJava.KtLightClassMarker; -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 com.intellij.psi.PsiClass +import com.intellij.psi.PsiTypeParameter +import org.jetbrains.kotlin.asJava.KtLightClassMarker +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; - -import static org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.*; - -public class JavaClassImpl extends JavaClassifierImpl implements JavaClass, JavaAnnotationOwnerImpl, JavaModifierListOwnerImpl { - public JavaClassImpl(@NotNull PsiClass psiClass) { - super(psiClass); - assert !(psiClass instanceof PsiTypeParameter) - : "PsiTypeParameter should be wrapped in JavaTypeParameter, not JavaClass: use JavaClassifier.create()"; +class JavaClassImpl(psiClass: PsiClass) : JavaClassifierImpl(psiClass), JavaClass, JavaAnnotationOwnerImpl, JavaModifierListOwnerImpl { + init { + assert(psiClass !is PsiTypeParameter) { "PsiTypeParameter should be wrapped in JavaTypeParameter, not JavaClass: use JavaClassifier.create()" } } - @Override - @NotNull - public Collection getInnerClasses() { - return classes(getPsi().getInnerClasses()); - } + override val innerClasses: Collection + get() = classes(psi.innerClasses) - @Override - @Nullable - public FqName getFqName() { - String qualifiedName = getPsi().getQualifiedName(); - return qualifiedName == null ? null : new FqName(qualifiedName); - } - - @NotNull - @Override - public Name getName() { - return SpecialNames.safeIdentifier(getPsi().getName()); - } - - @Override - public boolean isInterface() { - return getPsi().isInterface(); - } - - @Override - public boolean isAnnotationType() { - return getPsi().isAnnotationType(); - } - - @Override - public boolean isEnum() { - return getPsi().isEnum(); - } - - @Override - @Nullable - public JavaClassImpl getOuterClass() { - PsiClass outer = getPsi().getContainingClass(); - return outer == null ? null : new JavaClassImpl(outer); - } - - @NotNull - @Override - public List getTypeParameters() { - return typeParameters(getPsi().getTypeParameters()); - } - - @Override - @NotNull - public Collection getSupertypes() { - return classifierTypes(getPsi().getSuperTypes()); - } - - @Override - @NotNull - public Collection getMethods() { - // We apply distinct here because PsiClass#getMethods() can return duplicate PSI methods, for example in Lombok (see KT-11778) - return CollectionsKt.distinct(methods(ArraysKt.filter(getPsi().getMethods(), new Function1() { - @Override - public Boolean invoke(PsiMethod method) { - // Return type seems to be null for example for the 'clone' Groovy method generated by @AutoClone (see EA-73795) - return !method.isConstructor() && method.getReturnType() != null; - } - }))); - } - - @Override - @NotNull - public Collection getFields() { - // ex. Android plugin generates LightFields for resources started from '.' (.DS_Store file etc) - return fields(ArraysKt.filter(getPsi().getFields(), new Function1() { - @Override - public Boolean invoke(PsiField field) { - String name = field.getName(); - return name != null && Name.isValidIdentifier(name); - } - })); - } - - @Override - @NotNull - public Collection getConstructors() { - return constructors(ArraysKt.filter(getPsi().getConstructors(), new Function1() { - @Override - public Boolean invoke(PsiMethod method) { - // See for example org.jetbrains.plugins.scala.lang.psi.light.ScFunctionWrapper, - // which is present in getConstructors(), but its isConstructor() returns false - return method.isConstructor(); - } - })); - } - - @Override - public boolean isAbstract() { - return JavaElementUtil.isAbstract(this); - } - - @Override - public boolean isStatic() { - return JavaElementUtil.isStatic(this); - } - - @Override - public boolean isFinal() { - return JavaElementUtil.isFinal(this); - } - - @NotNull - @Override - public Visibility getVisibility() { - return JavaElementUtil.getVisibility(this); - } - - @Nullable - @Override - public LightClassOriginKind getLightClassOriginKind() { - PsiClass psiClass = getPsi(); - if (psiClass instanceof KtLightClassMarker) { - return ((KtLightClassMarker) psiClass).getOriginKind(); + override val fqName: FqName? + get() { + val qualifiedName = psi.qualifiedName + return if (qualifiedName == null) null else FqName(qualifiedName) } - return null; - } - @Nullable - @Override - public PsiAnnotationOwner getAnnotationOwnerPsi() { - return getPsi().getModifierList(); - } + override val name: Name + get() = SpecialNames.safeIdentifier(psi.name) + + override val isInterface: Boolean + get() = psi.isInterface + + override val isAnnotationType: Boolean + get() = psi.isAnnotationType + + override val isEnum: Boolean + get() = psi.isEnum + + override val outerClass: JavaClassImpl? + get() { + val outer = psi.containingClass + return if (outer == null) null else JavaClassImpl(outer) + } + + override val typeParameters: List + get() = typeParameters(psi.typeParameters) + + override val supertypes: Collection + get() = classifierTypes(psi.superTypes) + + override // We apply distinct here because PsiClass#getMethods() can return duplicate PSI methods, for example in Lombok (see KT-11778) + // Return type seems to be null for example for the 'clone' Groovy method generated by @AutoClone (see EA-73795) + val methods: Collection + get() = methods(psi.methods.filter { method -> !method.isConstructor && method.returnType != null }).distinct() + + override // ex. Android plugin generates LightFields for resources started from '.' (.DS_Store file etc) + val fields: Collection + get() = fields(psi.fields.filter { field -> + val name = field.name + name != null && Name.isValidIdentifier(name) + }) + + override // See for example org.jetbrains.plugins.scala.lang.psi.light.ScFunctionWrapper, + // which is present in getConstructors(), but its isConstructor() returns false + val constructors: Collection + get() = constructors(psi.constructors.filter { method -> method.isConstructor }) + + override val isAbstract: Boolean + get() = JavaElementUtil.isAbstract(this) + + override val isStatic: Boolean + get() = JavaElementUtil.isStatic(this) + + override val isFinal: Boolean + get() = JavaElementUtil.isFinal(this) + + override val visibility: Visibility + get() = JavaElementUtil.getVisibility(this) + + override val lightClassOriginKind: LightClassOriginKind? + get() = (psi as? KtLightClassMarker)?.originKind + + override fun getAnnotationOwnerPsi() = psi.modifierList }