Implement formatMethod in JavaSignatureFormatter without PSI

This commit is contained in:
zarechenskiy
2014-04-03 15:06:50 +04:00
committed by Alexander Udalov
parent fe48051041
commit 43b41f6db9
4 changed files with 81 additions and 49 deletions
@@ -1 +0,0 @@
org.jetbrains.jet.lang.resolve.java.structure.impl.JavaSignatureFormatterImpl
@@ -1,34 +0,0 @@
/*
* 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.impl;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.util.PsiFormatUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
import org.jetbrains.jet.lang.resolve.java.structure.JavaSignatureFormatter;
import static com.intellij.psi.util.PsiFormatUtilBase.*;
public class JavaSignatureFormatterImpl extends JavaSignatureFormatter {
@NotNull
@Override
public String formatMethod(@NotNull JavaMethod method) {
return PsiFormatUtil.formatMethod(((JavaMethodImpl) method).getPsi(), PsiSubstitutor.EMPTY, SHOW_NAME | SHOW_PARAMETERS,
SHOW_TYPE | SHOW_FQ_CLASS_NAMES);
}
}
@@ -18,19 +18,18 @@ package org.jetbrains.jet.lang.resolve.java.structure;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.List;
public abstract class JavaSignatureFormatter {
public class JavaSignatureFormatter {
private static JavaSignatureFormatter instance;
private JavaSignatureFormatter() {
}
@NotNull
public static JavaSignatureFormatter getInstance() {
if (instance == null) {
Iterator<JavaSignatureFormatter> iterator =
ServiceLoader.load(JavaSignatureFormatter.class, JavaSignatureFormatter.class.getClassLoader()).iterator();
assert iterator.hasNext() : "No service found: " + JavaSignatureFormatter.class.getName();
instance = iterator.next();
instance = new JavaSignatureFormatter();
}
return instance;
}
@@ -40,5 +39,75 @@ public abstract class JavaSignatureFormatter {
* {@code "foo(double, java.lang.String)"}
*/
@NotNull
public abstract String formatMethod(@NotNull JavaMethod method);
public String formatMethod(@NotNull JavaMethod method) {
StringBuilder buffer = new StringBuilder();
buffer.append(method.getName());
buffer.append('(');
boolean firstParameter = true;
for (JavaValueParameter parameter : method.getValueParameters()) {
if (!firstParameter) buffer.append(", ");
firstParameter = false;
buffer.append(formatType(parameter.getType()));
}
buffer.append(')');
return buffer.toString();
}
@NotNull
private String formatType(@NotNull JavaType type) {
if (type instanceof JavaPrimitiveType) {
return ((JavaPrimitiveType) type).getCanonicalText();
}
else if (type instanceof JavaArrayType) {
return formatType(((JavaArrayType) type).getComponentType()) + "[]";
}
else if (type instanceof JavaClassifierType) {
return formatClassifierType((JavaClassifierType) type);
}
else if (type instanceof JavaWildcardType) {
JavaWildcardType wildcardType = (JavaWildcardType) type;
if (wildcardType.isExtends()) {
//noinspection ConstantConditions
return "? extends " + formatType(wildcardType.getBound());
} else {
return "?";
}
} else {
throw new IllegalArgumentException("Wrong type: " + type);
}
}
@NotNull
private String formatClassifierType(@NotNull JavaClassifierType type) {
JavaClassifier classifier = type.getClassifier();
if (classifier == null) return "[UNRESOLVED: " + type + "]";
if (classifier instanceof JavaTypeParameter) {
return classifier.getName().asString();
}
//noinspection ConstantConditions
StringBuilder buffer = new StringBuilder(((JavaClass) classifier).getFqName().asString());
List<JavaType> typeArguments = type.getTypeArguments();
if (!typeArguments.isEmpty()) {
buffer.append("<");
boolean firstArgument = true;
for (JavaType typeArgument : typeArguments) {
if (!firstArgument) buffer.append(",");
firstArgument = false;
buffer.append(formatType(typeArgument));
}
buffer.append(">");
}
return buffer.toString();
}
}
@@ -25,10 +25,8 @@ 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.JetCoreEnvironment;
@@ -39,6 +37,8 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMapBuilder;
import org.jetbrains.jet.lang.resolve.java.structure.JavaSignatureFormatter;
import org.jetbrains.jet.lang.resolve.java.structure.impl.JavaMethodImpl;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import org.jetbrains.jet.utils.PathUtil;
@@ -50,7 +50,6 @@ 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;
@@ -166,7 +165,7 @@ public class GenerateJavaToKotlinMethodMap {
Collections.sort(result, new Comparator<Pair<PsiMethod, FunctionDescriptor>>() {
@Override
public int compare(Pair<PsiMethod, FunctionDescriptor> pair1, Pair<PsiMethod, FunctionDescriptor> pair2) {
public int compare(@NotNull Pair<PsiMethod, FunctionDescriptor> pair1, @NotNull Pair<PsiMethod, FunctionDescriptor> pair2) {
PsiMethod method1 = pair1.first;
PsiMethod method2 = pair2.first;
@@ -206,8 +205,7 @@ public class GenerateJavaToKotlinMethodMap {
@NotNull
private static String serializePsiMethod(@NotNull PsiMethod psiMethod) {
return PsiFormatUtil.formatMethod(psiMethod, PsiSubstitutor.EMPTY, SHOW_NAME | SHOW_PARAMETERS,
SHOW_TYPE | SHOW_FQ_CLASS_NAMES);
return JavaSignatureFormatter.getInstance().formatMethod(new JavaMethodImpl(psiMethod));
}
@Nullable