From ee8ca0b850fadea49c103f11030e786bea1e6736 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 9 Jun 2016 17:26:57 +0300 Subject: [PATCH] J2K JavaElementCollectionFromPsiArrayUtil --- ...JavaElementCollectionFromPsiArrayUtil.java | 202 ------------------ .../JavaElementCollectionFromPsiArrayUtil.kt | 75 +++++++ .../java/structure/impl/JavaPackageImpl.kt | 2 - 3 files changed, 75 insertions(+), 204 deletions(-) delete mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.java create mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.kt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.java deleted file mode 100644 index 6ae40fefe3a..00000000000 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.load.java.structure.impl; - -import com.intellij.psi.*; -import com.intellij.psi.search.GlobalSearchScope; -import kotlin.collections.CollectionsKt; -import kotlin.jvm.functions.Function1; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.load.java.structure.*; -import org.jetbrains.kotlin.name.Name; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -public class JavaElementCollectionFromPsiArrayUtil { - private JavaElementCollectionFromPsiArrayUtil() { - } - - private interface Factory { - @NotNull - Java create(@NotNull Psi psi); - } - - private static class Factories { - private static final Factory CLASSES = new Factory() { - @NotNull - @Override - public JavaClass create(@NotNull PsiClass psiClass) { - return new JavaClassImpl(psiClass); - } - }; - - private static final Factory METHODS = new Factory() { - @NotNull - @Override - public JavaMethod create(@NotNull PsiMethod psiMethod) { - return new JavaMethodImpl(psiMethod); - } - }; - - private static final Factory CONSTRUCTORS = new Factory() { - @NotNull - @Override - public JavaConstructor create(@NotNull PsiMethod psiMethod) { - return new JavaConstructorImpl(psiMethod); - } - }; - - private static final Factory FIELDS = new Factory() { - @NotNull - @Override - public JavaField create(@NotNull PsiField psiField) { - return new JavaFieldImpl(psiField); - } - }; - - private static final Factory VALUE_PARAMETERS = new Factory() { - @NotNull - @Override - public JavaValueParameter create(@NotNull PsiParameter psiParameter) { - return new JavaValueParameterImpl(psiParameter); - } - }; - - private static final Factory TYPE_PARAMETERS = - new Factory() { - @NotNull - @Override - public JavaTypeParameter create(@NotNull PsiTypeParameter psiTypeParameter) { - return new JavaTypeParameterImpl(psiTypeParameter); - } - }; - - private static final Factory CLASSIFIER_TYPES = new Factory() { - @NotNull - @Override - public JavaClassifierType create(@NotNull PsiClassType psiClassType) { - return new JavaClassifierTypeImpl(psiClassType); - } - }; - - private static final Factory ANNOTATIONS = new Factory() { - @NotNull - @Override - public JavaAnnotation create(@NotNull PsiAnnotation psiAnnotation) { - return new JavaAnnotationImpl(psiAnnotation); - } - }; - - private static final Factory NAMED_ANNOTATION_ARGUMENTS = - new Factory() { - @NotNull - @Override - public JavaAnnotationArgument create(@NotNull PsiNameValuePair psiNameValuePair) { - String name = psiNameValuePair.getName(); - PsiAnnotationMemberValue value = psiNameValuePair.getValue(); - assert value != null : "Annotation argument value cannot be null: " + name; - return JavaAnnotationArgumentImpl.Factory.create(value, name == null ? null : Name.identifier(name)); - } - }; - } - - @NotNull - private static List convert(@NotNull Psi[] elements, @NotNull Factory factory) { - if (elements.length == 0) return Collections.emptyList(); - List result = new ArrayList(elements.length); - for (Psi element : elements) { - result.add(factory.create(element)); - } - return result; - } - - @NotNull - private static List convert(@NotNull Iterable elements, @NotNull final Factory factory) { - if (!elements.iterator().hasNext()) return Collections.emptyList(); - - return CollectionsKt.map(elements, new Function1() { - @Override - public Java invoke(Psi psi) { - return factory.create(psi); - } - }); - } - - @NotNull - public static Collection classes(@NotNull PsiClass[] classes) { - return convert(classes, Factories.CLASSES); - } - - @NotNull - public static Collection classes(@NotNull Iterable classes) { - return convert(classes, Factories.CLASSES); - } - - @NotNull - public static Collection packages(@NotNull PsiPackage[] packages, @NotNull final GlobalSearchScope scope) { - return convert(packages, new Factory() { - @NotNull - @Override - public JavaPackage create(@NotNull PsiPackage aPackage) { - return new JavaPackageImpl(aPackage, scope); - } - }); - } - - @NotNull - public static Collection methods(@NotNull Iterable methods) { - return convert(methods, Factories.METHODS); - } - - @NotNull - public static Collection constructors(@NotNull Iterable methods) { - return convert(methods, Factories.CONSTRUCTORS); - } - - @NotNull - public static Collection fields(@NotNull Iterable fields) { - return convert(fields, Factories.FIELDS); - } - - @NotNull - public static List valueParameters(@NotNull PsiParameter[] parameters) { - return convert(parameters, Factories.VALUE_PARAMETERS); - } - - @NotNull - public static List typeParameters(@NotNull PsiTypeParameter[] typeParameters) { - return convert(typeParameters, Factories.TYPE_PARAMETERS); - } - - @NotNull - public static Collection classifierTypes(@NotNull PsiClassType[] classTypes) { - return convert(classTypes, Factories.CLASSIFIER_TYPES); - } - - @NotNull - public static Collection annotations(@NotNull PsiAnnotation[] annotations) { - return convert(annotations, Factories.ANNOTATIONS); - } - - @NotNull - public static Collection namedAnnotationArguments(@NotNull PsiNameValuePair[] nameValuePairs) { - return convert(nameValuePairs, Factories.NAMED_ANNOTATION_ARGUMENTS); - } -} diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.kt new file mode 100644 index 00000000000..3c77d1144ca --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaElementCollectionFromPsiArrayUtil.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:JvmName("JavaElementCollectionFromPsiArrayUtil") + +package org.jetbrains.kotlin.load.java.structure.impl + +import com.intellij.psi.* +import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.load.java.structure.* +import org.jetbrains.kotlin.name.Name + +private inline fun Array.convert(factory: (Psi) -> Java): List = + when (size) { + 0 -> emptyList() + 1 -> listOf(factory(first())) + else -> map(factory) + } + +private fun Collection.convert(factory: (Psi) -> Java): List = + when (size) { + 0 -> emptyList() + 1 -> listOf(factory(first())) + else -> map(factory) + } + +internal fun classes(classes: Array): Collection = + classes.convert(::JavaClassImpl) + +internal fun classes(classes: Collection): Collection = + classes.convert(::JavaClassImpl) + +internal fun packages(packages: Array, scope: GlobalSearchScope): Collection = + packages.convert { psi -> JavaPackageImpl(psi, scope) } + +internal fun methods(methods: Collection): Collection = + methods.convert(::JavaMethodImpl) + +internal fun constructors(methods: Collection): Collection = + methods.convert(::JavaConstructorImpl) + +internal fun fields(fields: Collection): Collection = + fields.convert(::JavaFieldImpl) + +internal fun valueParameters(parameters: Array): List = + parameters.convert(::JavaValueParameterImpl) + +internal fun typeParameters(typeParameters: Array): List = + typeParameters.convert(::JavaTypeParameterImpl) + +internal fun classifierTypes(classTypes: Array): Collection = + classTypes.convert(::JavaClassifierTypeImpl) + +internal fun annotations(annotations: Array): Collection = + annotations.convert(::JavaAnnotationImpl) + +internal fun namedAnnotationArguments(nameValuePairs: Array): Collection = + nameValuePairs.convert { psi -> + val name = psi.name?.let(Name::identifier) + val value = psi.value ?: error("Annotation argument value cannot be null: $name") + JavaAnnotationArgumentImpl.create(value, name) + } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPackageImpl.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPackageImpl.kt index 83edbeb7d7a..ffe636a3fa8 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPackageImpl.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPackageImpl.kt @@ -20,8 +20,6 @@ import com.intellij.psi.PsiPackage import com.intellij.psi.search.GlobalSearchScope import org.jetbrains.kotlin.load.java.structure.JavaClass import org.jetbrains.kotlin.load.java.structure.JavaPackage -import org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.classes -import org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.packages import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name