From e93c573898b033bb03496b89575e66b114fbfe34 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 8 Aug 2013 19:13:27 +0400 Subject: [PATCH] Create JavaSignatureFormatter, a wrapper over PsiFormatUtil --- .../resolve/java/DescriptorResolverUtils.java | 16 ++----- .../JavaToKotlinMethodMap.java | 14 +----- .../java/resolver/JavaFunctionResolver.java | 4 +- .../structure/JavaSignatureFormatter.java | 46 +++++++++++++++++++ .../jvm/GenerateJavaToKotlinMethodMap.java | 10 +++- 5 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaSignatureFormatter.java diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/DescriptorResolverUtils.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/DescriptorResolverUtils.java index 92968064fe9..18503b4b0fb 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/DescriptorResolverUtils.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/DescriptorResolverUtils.java @@ -18,24 +18,18 @@ package org.jetbrains.jet.lang.resolve.java; import com.google.common.collect.ImmutableSet; import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiSubstitutor; import com.intellij.psi.impl.compiled.ClsClassImpl; -import com.intellij.psi.util.PsiFormatUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.resolve.OverrideResolver; import org.jetbrains.jet.lang.resolve.java.resolver.FakeOverrideVisibilityResolver; -import org.jetbrains.jet.lang.resolve.java.structure.JavaClass; -import org.jetbrains.jet.lang.resolve.java.structure.JavaField; -import org.jetbrains.jet.lang.resolve.java.structure.JavaMember; -import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod; +import org.jetbrains.jet.lang.resolve.java.structure.*; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; import java.util.*; -import static com.intellij.psi.util.PsiFormatUtilBase.*; import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isEnumClassObject; public final class DescriptorResolverUtils { @@ -124,8 +118,7 @@ public final class DescriptorResolverUtils { if (member instanceof JavaField && ((JavaField) member).isEnumEntry()) return true; if (!(member instanceof JavaMethod)) return false; - String signature = PsiFormatUtil.formatMethod(((JavaMethod) member).getPsi(), PsiSubstitutor.EMPTY, SHOW_NAME | SHOW_PARAMETERS, - SHOW_TYPE | SHOW_FQ_CLASS_NAMES); + String signature = JavaSignatureFormatter.formatMethod((JavaMethod) member); return "values()".equals(signature) || "valueOf(java.lang.String)".equals(signature); @@ -140,9 +133,8 @@ public final class DescriptorResolverUtils { } public static boolean isObjectMethod(@NotNull JavaMethod method) { - String formattedMethod = PsiFormatUtil.formatMethod( - method.getPsi(), PsiSubstitutor.EMPTY, SHOW_NAME | SHOW_PARAMETERS, SHOW_TYPE | SHOW_FQ_CLASS_NAMES); - return OBJECT_METHODS.contains(formattedMethod); + String signature = JavaSignatureFormatter.formatMethod(method); + return OBJECT_METHODS.contains(signature); } @NotNull diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/JavaToKotlinMethodMap.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/JavaToKotlinMethodMap.java index 87e9c31b879..d2c34348633 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/JavaToKotlinMethodMap.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/JavaToKotlinMethodMap.java @@ -21,14 +21,12 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Lists; import com.intellij.openapi.util.Pair; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiSubstitutor; -import com.intellij.psi.util.PsiFormatUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod; +import org.jetbrains.jet.lang.resolve.java.structure.JavaSignatureFormatter; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; @@ -39,8 +37,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static com.intellij.psi.util.PsiFormatUtilBase.*; - public class JavaToKotlinMethodMap { public static final JavaToKotlinMethodMap INSTANCE = new JavaToKotlinMethodMap(); @@ -61,7 +57,7 @@ public class JavaToKotlinMethodMap { Set allSuperClasses = DescriptorUtils.getAllSuperClasses(containingClass); - String serializedMethod = serializePsiMethod(javaMethod.getPsi()); + String serializedMethod = JavaSignatureFormatter.formatMethod(javaMethod); for (ClassData classData : classDatas) { String expectedSerializedFunction = classData.method2Function.get(serializedMethod); if (expectedSerializedFunction == null) continue; @@ -81,12 +77,6 @@ public class JavaToKotlinMethodMap { return result; } - @NotNull - public static String serializePsiMethod(@NotNull PsiMethod psiMethod) { - return PsiFormatUtil.formatMethod( - psiMethod, PsiSubstitutor.EMPTY, SHOW_NAME | SHOW_PARAMETERS, SHOW_TYPE | SHOW_FQ_CLASS_NAMES); - } - @NotNull public static String serializeFunction(@NotNull FunctionDescriptor fun) { return DescriptorRenderer.COMPACT.render(fun); diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaFunctionResolver.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaFunctionResolver.java index cdc64e15fd3..18ca4a059c7 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaFunctionResolver.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaFunctionResolver.java @@ -19,7 +19,6 @@ package org.jetbrains.jet.lang.resolve.java.resolver; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.psi.util.PsiFormatUtil; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -36,6 +35,7 @@ import org.jetbrains.jet.lang.resolve.java.descriptor.SamConstructorDescriptor; import org.jetbrains.jet.lang.resolve.java.kotlinSignature.SignaturesUtil; import org.jetbrains.jet.lang.resolve.java.scope.NamedMembers; import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod; +import org.jetbrains.jet.lang.resolve.java.structure.JavaSignatureFormatter; import org.jetbrains.jet.lang.resolve.java.structure.JavaType; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.scopes.JetScope; @@ -229,7 +229,7 @@ public final class JavaFunctionResolver { + "super class = " + superFunction.getContainingDeclaration() + "\n" + "sub function = " + functionDescriptor + "\n" + "sub class = " + functionDescriptor.getContainingDeclaration() + "\n" - + "sub method = " + PsiFormatUtil.getExternalName(method.getPsi()) + "\n" + + "sub method = " + JavaSignatureFormatter.getExternalName(method) + "\n" + "@KotlinSignature = " + SignaturesUtil.getKotlinSignature(method)); } } diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaSignatureFormatter.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaSignatureFormatter.java new file mode 100644 index 00000000000..cfa7485d68b --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/structure/JavaSignatureFormatter.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2013 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.jet.lang.resolve.java.structure; + +import com.intellij.psi.PsiSubstitutor; +import com.intellij.psi.util.PsiFormatUtil; +import org.jetbrains.annotations.NotNull; + +import static com.intellij.psi.util.PsiFormatUtilBase.*; + +public class JavaSignatureFormatter { + private JavaSignatureFormatter() { + } + + /** + * @return a formatted signature of a method, showing method name and fully qualified names of its parameter types, e.g.: + * {@code "foo(double, java.lang.String)"} + */ + public static String formatMethod(@NotNull JavaMethod method) { + return PsiFormatUtil.formatMethod(method.getPsi(), PsiSubstitutor.EMPTY, SHOW_NAME | SHOW_PARAMETERS, + SHOW_TYPE | SHOW_FQ_CLASS_NAMES); + } + + /** + * @return a formatted signature of a method, showing method's containing class, return type and parameter types, all names are fully + * qualified, e.g.: + * {@code "java.lang.Class boolean isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation>)"} + */ + public static String getExternalName(@NotNull JavaMethod method) { + return PsiFormatUtil.getExternalName(method.getPsi()); + } +} diff --git a/generators/org/jetbrains/jet/generators/jvm/GenerateJavaToKotlinMethodMap.java b/generators/org/jetbrains/jet/generators/jvm/GenerateJavaToKotlinMethodMap.java index 8a0541cb718..ae4f14e0484 100644 --- a/generators/org/jetbrains/jet/generators/jvm/GenerateJavaToKotlinMethodMap.java +++ b/generators/org/jetbrains/jet/generators/jvm/GenerateJavaToKotlinMethodMap.java @@ -23,8 +23,10 @@ import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiSubstitutor; import com.intellij.psi.impl.file.impl.JavaFileManager; import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.util.PsiFormatUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil; @@ -47,9 +49,9 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import static com.intellij.psi.util.PsiFormatUtilBase.*; import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.CLASSPATH_KEY; import static org.jetbrains.jet.lang.resolve.java.kotlinSignature.JavaToKotlinMethodMap.serializeFunction; -import static org.jetbrains.jet.lang.resolve.java.kotlinSignature.JavaToKotlinMethodMap.serializePsiMethod; public class GenerateJavaToKotlinMethodMap { @@ -189,6 +191,12 @@ public class GenerateJavaToKotlinMethodMap { return false; } + @NotNull + private static String serializePsiMethod(@NotNull PsiMethod psiMethod) { + return PsiFormatUtil.formatMethod(psiMethod, PsiSubstitutor.EMPTY, SHOW_NAME | SHOW_PARAMETERS, + SHOW_TYPE | SHOW_FQ_CLASS_NAMES); + } + @Nullable private static PsiMethod findMethod(@NotNull PsiMethod[] methods, @NotNull FunctionDescriptor fun) { PsiMethod found = null;