Unused class removed

This commit is contained in:
Andrey Breslav
2014-09-11 21:50:10 +04:00
parent d25a76e044
commit bd21e487fc
5 changed files with 1 additions and 680 deletions
@@ -21,7 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMapBuilder;
/* package */ class CollectionClassMapping extends JavaToKotlinClassMapBuilder {
public class CollectionClassMapping extends JavaToKotlinClassMapBuilder {
private static CollectionClassMapping instance = null;
@NotNull
@@ -1,131 +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.kotlinSignature;
import com.google.common.collect.ImmutableCollection;
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 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;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import java.util.*;
public class JavaToKotlinMethodMap {
public static final JavaToKotlinMethodMap INSTANCE = new JavaToKotlinMethodMap();
private final JavaToKotlinMethodMapGenerated mapContainer = new JavaToKotlinMethodMapGenerated();
private JavaToKotlinMethodMap() {
}
@NotNull
public List<FunctionDescriptor> getFunctions(
@NotNull JavaMethod javaMethod,
@NotNull FqName classFqName,
@NotNull ClassDescriptor containingClass
) {
ImmutableCollection<ClassData> classDatas = mapContainer.map.get(classFqName.asString());
List<FunctionDescriptor> result = Lists.newArrayList();
Set<ClassDescriptor> allSuperClasses = new HashSet<ClassDescriptor>(DescriptorUtils.getSuperclassDescriptors(containingClass));
String serializedMethod = JavaSignatureFormatter.getInstance().formatMethod(javaMethod);
for (ClassData classData : classDatas) {
String expectedSerializedFunction = classData.method2Function.get(serializedMethod);
if (expectedSerializedFunction == null) continue;
ClassDescriptor kotlinClass = classData.kotlinClass;
if (!allSuperClasses.contains(kotlinClass)) continue;
Collection<FunctionDescriptor> functions = kotlinClass.getDefaultType().getMemberScope().getFunctions(javaMethod.getName());
for (FunctionDescriptor function : functions) {
if (expectedSerializedFunction.equals(serializeFunction(function))) {
result.add(function);
}
}
}
return result;
}
@NotNull
public static String serializeFunction(@NotNull FunctionDescriptor fun) {
return DescriptorRenderer.COMPACT.render(fun);
}
// used in generated code
static Pair<String, String> pair(String a, String b) {
return Pair.create(a, b);
}
// used in generated code
static void put(
ImmutableMultimap.Builder<String, ClassData> builder,
String javaFqName,
String kotlinQualifiedName,
Pair<String, String>... methods2Functions
) {
ImmutableMap<String, String> methods2FunctionsMap = pairs2Map(methods2Functions);
ClassDescriptor kotlinClass;
if (kotlinQualifiedName.contains(".")) { // Map.Entry and MutableMap.MutableEntry
String[] kotlinNames = kotlinQualifiedName.split("\\.");
assert kotlinNames.length == 2 : "unexpected qualified name " + kotlinQualifiedName;
ClassDescriptor outerClass = KotlinBuiltIns.getInstance().getBuiltInClassByName(Name.identifier(kotlinNames[0]));
kotlinClass = DescriptorUtils.getInnerClassByName(outerClass, kotlinNames[1]);
assert kotlinClass != null : "Class not found: " + kotlinQualifiedName;
}
else {
kotlinClass = KotlinBuiltIns.getInstance().getBuiltInClassByName(Name.identifier(kotlinQualifiedName));
}
builder.put(javaFqName, new ClassData(kotlinClass, methods2FunctionsMap));
}
private static ImmutableMap<String, String> pairs2Map(Pair<String, String>[] pairs) {
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (Pair<String, String> pair : pairs) {
builder.put(pair.first, pair.second);
}
return builder.build();
}
static class ClassData {
@NotNull
public final ClassDescriptor kotlinClass;
@NotNull
public Map<String, String> method2Function;
public ClassData(@NotNull ClassDescriptor kotlinClass, @NotNull Map<String, String> method2Function) {
this.kotlinClass = kotlinClass;
this.method2Function = method2Function;
}
}
}
@@ -1,254 +0,0 @@
/*
* Copyright 2010-2014 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.kotlinSignature;
import com.google.common.collect.ImmutableMultimap;
import static org.jetbrains.jet.lang.resolve.java.kotlinSignature.JavaToKotlinMethodMap.*;
/* This file is generated by org.jetbrains.jet.generators.jvm.GenerateJavaToKotlinMethodMap. DO NOT EDIT! */
@SuppressWarnings("unchecked")
class JavaToKotlinMethodMapGenerated {
final ImmutableMultimap<String, JavaToKotlinMethodMap.ClassData> map;
JavaToKotlinMethodMapGenerated() {
ImmutableMultimap.Builder<String, JavaToKotlinMethodMap.ClassData> b = ImmutableMultimap.builder();
put(b, "java.lang.Object", "Any",
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("toString()", "fun toString(): kotlin.String")
);
put(b, "java.lang.String", "String",
pair("compareTo(java.lang.String)", "fun compareTo(other: kotlin.String): kotlin.Int"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("toString()", "fun toString(): kotlin.String")
);
put(b, "java.lang.CharSequence", "CharSequence",
pair("toString()", "fun toString(): kotlin.String")
);
put(b, "java.lang.Throwable", "Throwable",
pair("getCause()", "fun getCause(): kotlin.Throwable?"),
pair("getMessage()", "fun getMessage(): kotlin.String?"),
pair("printStackTrace()", "fun printStackTrace(): kotlin.Unit"),
pair("toString()", "fun toString(): kotlin.String")
);
put(b, "java.lang.Comparable", "Comparable",
pair("compareTo(T)", "fun compareTo(other: T): kotlin.Int")
);
put(b, "java.lang.Enum", "Enum",
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("name()", "fun name(): kotlin.String"),
pair("ordinal()", "fun ordinal(): kotlin.Int"),
pair("toString()", "fun toString(): kotlin.String")
);
put(b, "java.lang.annotation.Annotation", "Annotation",
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("toString()", "fun toString(): kotlin.String")
);
put(b, "java.lang.Iterable", "Iterable",
pair("iterator()", "fun iterator(): kotlin.Iterator<T>")
);
put(b, "java.lang.Iterable", "MutableIterable",
pair("iterator()", "fun iterator(): kotlin.MutableIterator<T>")
);
put(b, "java.util.Iterator", "Iterator",
pair("hasNext()", "fun hasNext(): kotlin.Boolean"),
pair("next()", "fun next(): T")
);
put(b, "java.util.Iterator", "MutableIterator",
pair("hasNext()", "fun hasNext(): kotlin.Boolean"),
pair("next()", "fun next(): T"),
pair("remove()", "fun remove(): kotlin.Unit")
);
put(b, "java.util.Collection", "Collection",
pair("contains(java.lang.Object)", "fun contains(o: kotlin.Any?): kotlin.Boolean"),
pair("containsAll(java.util.Collection<?>)", "fun containsAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("iterator()", "fun iterator(): kotlin.Iterator<E>"),
pair("size()", "fun size(): kotlin.Int")
);
put(b, "java.util.Collection", "MutableCollection",
pair("add(E)", "fun add(e: E): kotlin.Boolean"),
pair("addAll(java.util.Collection<? extends E>)", "fun addAll(c: kotlin.Collection<E>): kotlin.Boolean"),
pair("clear()", "fun clear(): kotlin.Unit"),
pair("contains(java.lang.Object)", "fun contains(o: kotlin.Any?): kotlin.Boolean"),
pair("containsAll(java.util.Collection<?>)", "fun containsAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("iterator()", "fun iterator(): kotlin.MutableIterator<E>"),
pair("remove(java.lang.Object)", "fun remove(o: kotlin.Any?): kotlin.Boolean"),
pair("removeAll(java.util.Collection<?>)", "fun removeAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("retainAll(java.util.Collection<?>)", "fun retainAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("size()", "fun size(): kotlin.Int")
);
put(b, "java.util.List", "List",
pair("contains(java.lang.Object)", "fun contains(o: kotlin.Any?): kotlin.Boolean"),
pair("containsAll(java.util.Collection<?>)", "fun containsAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("get(int)", "fun get(index: kotlin.Int): E"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("indexOf(java.lang.Object)", "fun indexOf(o: kotlin.Any?): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("iterator()", "fun iterator(): kotlin.Iterator<E>"),
pair("lastIndexOf(java.lang.Object)", "fun lastIndexOf(o: kotlin.Any?): kotlin.Int"),
pair("listIterator()", "fun listIterator(): kotlin.ListIterator<E>"),
pair("listIterator(int)", "fun listIterator(index: kotlin.Int): kotlin.ListIterator<E>"),
pair("size()", "fun size(): kotlin.Int"),
pair("subList(int, int)", "fun subList(fromIndex: kotlin.Int, toIndex: kotlin.Int): kotlin.List<E>")
);
put(b, "java.util.List", "MutableList",
pair("add(E)", "fun add(e: E): kotlin.Boolean"),
pair("add(int, E)", "fun add(index: kotlin.Int, element: E): kotlin.Unit"),
pair("addAll(int, java.util.Collection<? extends E>)", "fun addAll(index: kotlin.Int, c: kotlin.Collection<E>): kotlin.Boolean"),
pair("addAll(java.util.Collection<? extends E>)", "fun addAll(c: kotlin.Collection<E>): kotlin.Boolean"),
pair("clear()", "fun clear(): kotlin.Unit"),
pair("contains(java.lang.Object)", "fun contains(o: kotlin.Any?): kotlin.Boolean"),
pair("containsAll(java.util.Collection<?>)", "fun containsAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("get(int)", "fun get(index: kotlin.Int): E"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("indexOf(java.lang.Object)", "fun indexOf(o: kotlin.Any?): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("iterator()", "fun iterator(): kotlin.MutableIterator<E>"),
pair("lastIndexOf(java.lang.Object)", "fun lastIndexOf(o: kotlin.Any?): kotlin.Int"),
pair("listIterator()", "fun listIterator(): kotlin.MutableListIterator<E>"),
pair("listIterator(int)", "fun listIterator(index: kotlin.Int): kotlin.MutableListIterator<E>"),
pair("remove(int)", "fun remove(index: kotlin.Int): E"),
pair("remove(java.lang.Object)", "fun remove(o: kotlin.Any?): kotlin.Boolean"),
pair("removeAll(java.util.Collection<?>)", "fun removeAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("retainAll(java.util.Collection<?>)", "fun retainAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("set(int, E)", "fun set(index: kotlin.Int, element: E): E"),
pair("size()", "fun size(): kotlin.Int"),
pair("subList(int, int)", "fun subList(fromIndex: kotlin.Int, toIndex: kotlin.Int): kotlin.MutableList<E>")
);
put(b, "java.util.Set", "Set",
pair("contains(java.lang.Object)", "fun contains(o: kotlin.Any?): kotlin.Boolean"),
pair("containsAll(java.util.Collection<?>)", "fun containsAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("iterator()", "fun iterator(): kotlin.Iterator<E>"),
pair("size()", "fun size(): kotlin.Int")
);
put(b, "java.util.Set", "MutableSet",
pair("add(E)", "fun add(e: E): kotlin.Boolean"),
pair("addAll(java.util.Collection<? extends E>)", "fun addAll(c: kotlin.Collection<E>): kotlin.Boolean"),
pair("clear()", "fun clear(): kotlin.Unit"),
pair("contains(java.lang.Object)", "fun contains(o: kotlin.Any?): kotlin.Boolean"),
pair("containsAll(java.util.Collection<?>)", "fun containsAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("iterator()", "fun iterator(): kotlin.MutableIterator<E>"),
pair("remove(java.lang.Object)", "fun remove(o: kotlin.Any?): kotlin.Boolean"),
pair("removeAll(java.util.Collection<?>)", "fun removeAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("retainAll(java.util.Collection<?>)", "fun retainAll(c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean"),
pair("size()", "fun size(): kotlin.Int")
);
put(b, "java.util.Map", "Map",
pair("containsKey(java.lang.Object)", "fun containsKey(key: kotlin.Any?): kotlin.Boolean"),
pair("containsValue(java.lang.Object)", "fun containsValue(value: kotlin.Any?): kotlin.Boolean"),
pair("entrySet()", "fun entrySet(): kotlin.Set<kotlin.Map.Entry<K, V>>"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("get(java.lang.Object)", "fun get(key: kotlin.Any?): V?"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("keySet()", "fun keySet(): kotlin.Set<K>"),
pair("size()", "fun size(): kotlin.Int"),
pair("values()", "fun values(): kotlin.Collection<V>")
);
put(b, "java.util.Map", "MutableMap",
pair("clear()", "fun clear(): kotlin.Unit"),
pair("containsKey(java.lang.Object)", "fun containsKey(key: kotlin.Any?): kotlin.Boolean"),
pair("containsValue(java.lang.Object)", "fun containsValue(value: kotlin.Any?): kotlin.Boolean"),
pair("entrySet()", "fun entrySet(): kotlin.MutableSet<kotlin.MutableMap.MutableEntry<K, V>>"),
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("get(java.lang.Object)", "fun get(key: kotlin.Any?): V?"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("isEmpty()", "fun isEmpty(): kotlin.Boolean"),
pair("keySet()", "fun keySet(): kotlin.MutableSet<K>"),
pair("put(K, V)", "fun put(key: K, value: V): V?"),
pair("putAll(java.util.Map<? extends K,? extends V>)", "fun putAll(m: kotlin.Map<out K, V>): kotlin.Unit"),
pair("remove(java.lang.Object)", "fun remove(key: kotlin.Any?): V?"),
pair("size()", "fun size(): kotlin.Int"),
pair("values()", "fun values(): kotlin.MutableCollection<V>")
);
put(b, "java.util.Map.Entry", "Map.Entry",
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("getKey()", "fun getKey(): K"),
pair("getValue()", "fun getValue(): V"),
pair("hashCode()", "fun hashCode(): kotlin.Int")
);
put(b, "java.util.Map.Entry", "MutableMap.MutableEntry",
pair("equals(java.lang.Object)", "fun equals(other: kotlin.Any?): kotlin.Boolean"),
pair("getKey()", "fun getKey(): K"),
pair("getValue()", "fun getValue(): V"),
pair("hashCode()", "fun hashCode(): kotlin.Int"),
pair("setValue(V)", "fun setValue(value: V): V")
);
put(b, "java.util.ListIterator", "ListIterator",
pair("hasNext()", "fun hasNext(): kotlin.Boolean"),
pair("hasPrevious()", "fun hasPrevious(): kotlin.Boolean"),
pair("next()", "fun next(): T"),
pair("nextIndex()", "fun nextIndex(): kotlin.Int"),
pair("previous()", "fun previous(): T"),
pair("previousIndex()", "fun previousIndex(): kotlin.Int")
);
put(b, "java.util.ListIterator", "MutableListIterator",
pair("add(E)", "fun add(e: T): kotlin.Unit"),
pair("hasNext()", "fun hasNext(): kotlin.Boolean"),
pair("hasPrevious()", "fun hasPrevious(): kotlin.Boolean"),
pair("next()", "fun next(): T"),
pair("nextIndex()", "fun nextIndex(): kotlin.Int"),
pair("previous()", "fun previous(): T"),
pair("previousIndex()", "fun previousIndex(): kotlin.Int"),
pair("remove()", "fun remove(): kotlin.Unit"),
pair("set(E)", "fun set(e: T): kotlin.Unit")
);
map = b.build();
}
}
@@ -1,264 +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.generators.jvm;
import com.google.common.collect.Lists;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
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.impl.file.impl.JavaFileManager;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.di.GeneratorsFileUtil;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
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;
import org.jetbrains.jet.utils.Printer;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.CLASSPATH_KEY;
import static org.jetbrains.jet.lang.resolve.java.kotlinSignature.JavaToKotlinMethodMap.serializeFunction;
public class GenerateJavaToKotlinMethodMap {
public static final String BUILTINS_FQNAME_PREFIX = KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.asString() + ".";
public static final File TARGET_FILE =
new File("compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/JavaToKotlinMethodMapGenerated.java");
public static void main(String[] args) throws IOException {
GeneratorsFileUtil.writeFileIfContentChanged(TARGET_FILE, generateText().toString());
}
public static CharSequence generateText() throws IOException {
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.addAll(CLASSPATH_KEY, PathUtil.getJdkClassesRoots());
Disposable disposable = Disposer.newDisposable();
try {
JetCoreEnvironment coreEnvironment = JetCoreEnvironment.createForProduction(disposable, configuration);
StringBuilder buf = new StringBuilder();
Printer printer = new Printer(buf);
printer.print(FileUtil.loadFile(new File("injector-generator/copyright.txt")))
.println()
.println("package org.jetbrains.jet.lang.resolve.java.kotlinSignature;")
.println()
.println("import com.google.common.collect.ImmutableMultimap;")
.println()
.println("import static org.jetbrains.jet.lang.resolve.java.kotlinSignature.JavaToKotlinMethodMap.*;")
.println()
.println("/* This file is generated by ", GenerateJavaToKotlinMethodMap.class.getName(), ". DO NOT EDIT! */")
.println("@SuppressWarnings(\"unchecked\")")
.println("class JavaToKotlinMethodMapGenerated {").pushIndent()
.println("final ImmutableMultimap<String, JavaToKotlinMethodMap.ClassData> map;")
.println()
.println("JavaToKotlinMethodMapGenerated() {").pushIndent()
.println("ImmutableMultimap.Builder<String, JavaToKotlinMethodMap.ClassData> b = ImmutableMultimap.builder();")
.println();
MyMapBuilder builder = new MyMapBuilder(coreEnvironment.getProject());
printer.printWithNoIndent(builder.toString());
printer.println("map = b.build();");
printer.popIndent().println("}");
printer.popIndent().println("}");
return buf;
}
finally {
Disposer.dispose(disposable);
}
}
private static class MyMapBuilder extends JavaToKotlinClassMapBuilder {
private final Project project;
private final StringBuilder buf = new StringBuilder();
private final Printer printer = new Printer(buf).pushIndent().pushIndent();
public MyMapBuilder(@NotNull Project project) {
this.project = project;
init();
}
@Override
protected void register(@NotNull Class<?> javaClass, @NotNull ClassDescriptor kotlinDescriptor, @NotNull Direction direction) {
processClass(javaClass, kotlinDescriptor);
}
@Override
protected void register(@NotNull Class<?> javaClass,
@NotNull ClassDescriptor kotlinDescriptor,
@NotNull ClassDescriptor kotlinMutableDescriptor,
@NotNull Direction direction) {
processClass(javaClass, kotlinDescriptor);
processClass(javaClass, kotlinMutableDescriptor);
}
private void processClass(@NotNull Class<?> javaClass, @NotNull ClassDescriptor kotlinClass) {
JavaFileManager javaFileManager = ServiceManager.getService(project, JavaFileManager.class);
PsiClass psiClass = javaFileManager.findClass(javaClass.getCanonicalName(), GlobalSearchScope.allScope(project));
assert psiClass != null;
List<Pair<PsiMethod, FunctionDescriptor>> methods2Functions = getClassMethods2Functions(kotlinClass, psiClass);
if (!methods2Functions.isEmpty()) {
appendBeforeClass(kotlinClass, psiClass);
appendClass(methods2Functions);
appendAfterClass();
}
}
private static List<Pair<PsiMethod, FunctionDescriptor>> getClassMethods2Functions(
@NotNull ClassDescriptor kotlinClass,
@NotNull PsiClass psiClass
) {
PsiMethod[] methods = psiClass.getMethods();
List<Pair<PsiMethod, FunctionDescriptor>> result = Lists.newArrayList();
for (DeclarationDescriptor member : kotlinClass.getDefaultType().getMemberScope().getAllDescriptors()) {
if (!(member instanceof FunctionDescriptor) || member.getContainingDeclaration() != kotlinClass) {
continue;
}
FunctionDescriptor fun = (FunctionDescriptor) member;
PsiMethod foundMethod = findMethod(methods, fun);
if (foundMethod != null) {
result.add(Pair.create(foundMethod, fun));
}
}
Collections.sort(result, new Comparator<Pair<PsiMethod, FunctionDescriptor>>() {
@Override
public int compare(@NotNull Pair<PsiMethod, FunctionDescriptor> pair1, @NotNull Pair<PsiMethod, FunctionDescriptor> pair2) {
PsiMethod method1 = pair1.first;
PsiMethod method2 = pair2.first;
String name1 = method1.getName();
String name2 = method2.getName();
if (!name1.equals(name2)) {
return name1.compareTo(name2);
}
String serialized1 = serializePsiMethod(method1);
String serialized2 = serializePsiMethod(method2);
return serialized1.compareTo(serialized2);
}
});
return result;
}
private static boolean match(@NotNull PsiMethod method, @NotNull FunctionDescriptor fun) {
// Compare method an function by name and parameters count. For all methods except one (List.remove) it is enough.
// If this changes, there will be assertion error in findMethod()
if (method.getName().equals(fun.getName().getIdentifier())
&& method.getParameterList().getParametersCount() == fun.getValueParameters().size()) {
// "special case": remove(Int) and remove(Any?) in MutableList
if (method.getName().equals("remove") && method.getContainingClass().getName().equals("List")) {
String psiType = method.getParameterList().getParameters()[0].getType().getPresentableText();
String jetType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(fun.getValueParameters().get(0).getType());
String string = psiType + "|" + jetType;
return "int|Int".equals(string) || "Object|Any?".equals(string);
}
return true;
}
return false;
}
@NotNull
private static String serializePsiMethod(@NotNull PsiMethod psiMethod) {
return JavaSignatureFormatter.getInstance().formatMethod(new JavaMethodImpl(psiMethod));
}
@Nullable
private static PsiMethod findMethod(@NotNull PsiMethod[] methods, @NotNull FunctionDescriptor fun) {
PsiMethod found = null;
for (PsiMethod method : methods) {
if (match(method, fun)) {
if (found != null) {
throw new AssertionError("Duplicate for " + fun);
}
found = method;
}
}
return found;
}
private void appendBeforeClass(@NotNull ClassDescriptor kotlinClass, @NotNull PsiClass psiClass) {
String psiFqName = psiClass.getQualifiedName();
String kotlinFqName = DescriptorUtils.getFqNameSafe(kotlinClass).asString();
assert kotlinFqName.startsWith(BUILTINS_FQNAME_PREFIX);
String kotlinSubQualifiedName = kotlinFqName.substring(BUILTINS_FQNAME_PREFIX.length());
printer.println("put(b, \"", psiFqName, "\", \"", kotlinSubQualifiedName, "\",").pushIndent();
}
private void appendClass(@NotNull List<Pair<PsiMethod, FunctionDescriptor>> methods2Functions) {
int index = 0;
for (Pair<PsiMethod, FunctionDescriptor> method2Function : methods2Functions) {
printer.print("pair(\"", serializePsiMethod(method2Function.first), "\", \"", serializeFunction(method2Function.second),
"\")");
if (index != methods2Functions.size() - 1) {
printer.printWithNoIndent(",");
}
printer.println();
index++;
}
}
private void appendAfterClass() {
printer.popIndent().println(");").println();
}
public String toString() {
return buf.toString();
}
}
private GenerateJavaToKotlinMethodMap() {
}
}
@@ -1,30 +0,0 @@
/*
* Copyright 2010-2014 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.generators.jvm;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import junit.framework.TestCase;
import org.junit.Assert;
public class GenerateJavaToKotlinMethodMapTest extends TestCase {
public void testGenerateJavaToKotlinMethodMap() throws Exception {
String text = StringUtil.convertLineSeparators(GenerateJavaToKotlinMethodMap.generateText().toString());
String expected = FileUtil.loadFile(GenerateJavaToKotlinMethodMap.TARGET_FILE, true);
Assert.assertEquals("To fix this problem you need to run GenerateJavaToKotlinMethodMap", expected, text);
}
}