001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.java.kotlinSignature;
018
019 import com.google.common.collect.ImmutableCollection;
020 import com.google.common.collect.ImmutableMap;
021 import com.google.common.collect.ImmutableMultimap;
022 import com.google.common.collect.Lists;
023 import com.intellij.openapi.util.Pair;
024 import org.jetbrains.annotations.NotNull;
025 import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
026 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
027 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
028 import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
029 import org.jetbrains.jet.lang.resolve.java.structure.JavaSignatureFormatter;
030 import org.jetbrains.jet.lang.resolve.name.FqName;
031 import org.jetbrains.jet.lang.resolve.name.Name;
032 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
033 import org.jetbrains.jet.renderer.DescriptorRenderer;
034
035 import java.util.Collection;
036 import java.util.List;
037 import java.util.Map;
038 import java.util.Set;
039
040 public class JavaToKotlinMethodMap {
041 public static final JavaToKotlinMethodMap INSTANCE = new JavaToKotlinMethodMap();
042
043 private final JavaToKotlinMethodMapGenerated mapContainer = new JavaToKotlinMethodMapGenerated();
044
045 private JavaToKotlinMethodMap() {
046 }
047
048 @NotNull
049 public List<FunctionDescriptor> getFunctions(
050 @NotNull JavaMethod javaMethod,
051 @NotNull FqName classFqName,
052 @NotNull ClassDescriptor containingClass
053 ) {
054 ImmutableCollection<ClassData> classDatas = mapContainer.map.get(classFqName.asString());
055
056 List<FunctionDescriptor> result = Lists.newArrayList();
057
058 Set<ClassDescriptor> allSuperClasses = DescriptorUtils.getAllSuperClasses(containingClass);
059
060 String serializedMethod = JavaSignatureFormatter.getInstance().formatMethod(javaMethod);
061 for (ClassData classData : classDatas) {
062 String expectedSerializedFunction = classData.method2Function.get(serializedMethod);
063 if (expectedSerializedFunction == null) continue;
064
065 ClassDescriptor kotlinClass = classData.kotlinClass;
066 if (!allSuperClasses.contains(kotlinClass)) continue;
067
068 Collection<FunctionDescriptor> functions = kotlinClass.getDefaultType().getMemberScope().getFunctions(javaMethod.getName());
069
070 for (FunctionDescriptor function : functions) {
071 if (expectedSerializedFunction.equals(serializeFunction(function))) {
072 result.add(function);
073 }
074 }
075 }
076
077 return result;
078 }
079
080 @NotNull
081 public static String serializeFunction(@NotNull FunctionDescriptor fun) {
082 return DescriptorRenderer.COMPACT.render(fun);
083 }
084
085 // used in generated code
086 static Pair<String, String> pair(String a, String b) {
087 return Pair.create(a, b);
088 }
089
090 // used in generated code
091 static void put(
092 ImmutableMultimap.Builder<String, ClassData> builder,
093 String javaFqName,
094 String kotlinQualifiedName,
095 Pair<String, String>... methods2Functions
096 ) {
097 ImmutableMap<String, String> methods2FunctionsMap = pairs2Map(methods2Functions);
098
099 ClassDescriptor kotlinClass;
100 if (kotlinQualifiedName.contains(".")) { // Map.Entry and MutableMap.MutableEntry
101 String[] kotlinNames = kotlinQualifiedName.split("\\.");
102 assert kotlinNames.length == 2 : "unexpected qualified name " + kotlinQualifiedName;
103
104 ClassDescriptor outerClass = KotlinBuiltIns.getInstance().getBuiltInClassByName(Name.identifier(kotlinNames[0]));
105 kotlinClass = DescriptorUtils.getInnerClassByName(outerClass, kotlinNames[1]);
106 assert kotlinClass != null : "Class not found: " + kotlinQualifiedName;
107 }
108 else {
109 kotlinClass = KotlinBuiltIns.getInstance().getBuiltInClassByName(Name.identifier(kotlinQualifiedName));
110 }
111
112 builder.put(javaFqName, new ClassData(kotlinClass, methods2FunctionsMap));
113 }
114
115 private static ImmutableMap<String, String> pairs2Map(Pair<String, String>[] pairs) {
116 ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
117 for (Pair<String, String> pair : pairs) {
118 builder.put(pair.first, pair.second);
119 }
120 return builder.build();
121 }
122
123 static class ClassData {
124 @NotNull
125 public final ClassDescriptor kotlinClass;
126 @NotNull
127 public Map<String, String> method2Function;
128
129 public ClassData(@NotNull ClassDescriptor kotlinClass, @NotNull Map<String, String> method2Function) {
130 this.kotlinClass = kotlinClass;
131 this.method2Function = method2Function;
132 }
133 }
134 }