Merge JavaToKotlinClassMap and JavaToKotlinClassMapBuilder
This commit is contained in:
+56
-17
@@ -24,11 +24,14 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.name.ClassId;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.*;
|
||||
|
||||
public class JavaToKotlinClassMap extends JavaToKotlinClassMapBuilder implements PlatformToKotlinClassMap {
|
||||
public class JavaToKotlinClassMap implements PlatformToKotlinClassMap {
|
||||
public static final JavaToKotlinClassMap INSTANCE = new JavaToKotlinClassMap();
|
||||
|
||||
private final Map<FqName, ClassDescriptor> javaToKotlin = new HashMap<FqName, ClassDescriptor>();
|
||||
@@ -39,7 +42,34 @@ public class JavaToKotlinClassMap extends JavaToKotlinClassMapBuilder implements
|
||||
private final Map<ClassDescriptor, ClassDescriptor> readOnlyToMutable = new HashMap<ClassDescriptor, ClassDescriptor>();
|
||||
|
||||
private JavaToKotlinClassMap() {
|
||||
init();
|
||||
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
|
||||
|
||||
add(Object.class, builtIns.getAny());
|
||||
add(String.class, builtIns.getString());
|
||||
add(CharSequence.class, builtIns.getCharSequence());
|
||||
add(Throwable.class, builtIns.getThrowable());
|
||||
add(Cloneable.class, builtIns.getCloneable());
|
||||
add(Number.class, builtIns.getNumber());
|
||||
add(Comparable.class, builtIns.getComparable());
|
||||
add(Enum.class, builtIns.getEnum());
|
||||
add(Annotation.class, builtIns.getAnnotation());
|
||||
|
||||
add(Iterable.class, builtIns.getIterable(), builtIns.getMutableIterable());
|
||||
add(Iterator.class, builtIns.getIterator(), builtIns.getMutableIterator());
|
||||
add(Collection.class, builtIns.getCollection(), builtIns.getMutableCollection());
|
||||
add(List.class, builtIns.getList(), builtIns.getMutableList());
|
||||
add(Set.class, builtIns.getSet(), builtIns.getMutableSet());
|
||||
add(Map.class, builtIns.getMap(), builtIns.getMutableMap());
|
||||
add(Map.Entry.class, builtIns.getMapEntry(), builtIns.getMutableMapEntry());
|
||||
add(ListIterator.class, builtIns.getListIterator(), builtIns.getMutableListIterator());
|
||||
|
||||
for (JvmPrimitiveType jvmType : JvmPrimitiveType.values()) {
|
||||
add(ClassId.topLevel(jvmType.getWrapperFqName()), builtIns.getPrimitiveClassDescriptor(jvmType.getPrimitiveType()));
|
||||
}
|
||||
|
||||
addJavaToKotlin(classId(Deprecated.class), builtIns.getDeprecatedAnnotation());
|
||||
|
||||
addKotlinToJava(classId(Void.class), builtIns.getNothing());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,31 +108,31 @@ public class JavaToKotlinClassMap extends JavaToKotlinClassMapBuilder implements
|
||||
return kotlinToJava.get(kotlinFqName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void register(@NotNull ClassId javaClassId, @NotNull ClassDescriptor kotlinDescriptor, @NotNull Direction direction) {
|
||||
if (direction == Direction.BOTH || direction == Direction.JAVA_TO_KOTLIN) {
|
||||
addJavaToKotlin(javaClassId, kotlinDescriptor);
|
||||
}
|
||||
if (direction == Direction.BOTH || direction == Direction.KOTLIN_TO_JAVA) {
|
||||
addKotlinToJava(javaClassId, kotlinDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void register(
|
||||
@NotNull ClassId javaClassId,
|
||||
private void add(
|
||||
@NotNull Class<?> javaClass,
|
||||
@NotNull ClassDescriptor kotlinDescriptor,
|
||||
@NotNull ClassDescriptor kotlinMutableDescriptor
|
||||
) {
|
||||
addJavaToKotlin(javaClassId, kotlinDescriptor);
|
||||
ClassId javaClassId = classId(javaClass);
|
||||
|
||||
add(javaClassId, kotlinDescriptor);
|
||||
|
||||
addJavaToKotlinCovariant(javaClassId, kotlinMutableDescriptor);
|
||||
addKotlinToJava(javaClassId, kotlinDescriptor);
|
||||
addKotlinToJava(javaClassId, kotlinMutableDescriptor);
|
||||
|
||||
mutableToReadOnly.put(kotlinMutableDescriptor, kotlinDescriptor);
|
||||
readOnlyToMutable.put(kotlinDescriptor, kotlinMutableDescriptor);
|
||||
}
|
||||
|
||||
private void add(@NotNull ClassId javaClassId, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
addJavaToKotlin(javaClassId, kotlinDescriptor);
|
||||
addKotlinToJava(javaClassId, kotlinDescriptor);
|
||||
}
|
||||
|
||||
private void add(@NotNull Class<?> javaClass, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
add(classId(javaClass), kotlinDescriptor);
|
||||
}
|
||||
|
||||
private void addJavaToKotlin(@NotNull ClassId javaClassId, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
javaToKotlin.put(javaClassId.asSingleFqName(), kotlinDescriptor);
|
||||
}
|
||||
@@ -115,6 +145,15 @@ public class JavaToKotlinClassMap extends JavaToKotlinClassMapBuilder implements
|
||||
kotlinToJava.put(DescriptorUtils.getFqName(kotlinDescriptor), javaClassId);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static ClassId classId(@NotNull Class<?> clazz) {
|
||||
assert !clazz.isPrimitive() && !clazz.isArray() : "Invalid class: " + clazz;
|
||||
Class<?> outer = clazz.getDeclaringClass();
|
||||
return outer == null
|
||||
? ClassId.topLevel(new FqName(clazz.getCanonicalName()))
|
||||
: classId(outer).createNestedClassId(Name.identifier(clazz.getSimpleName()));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Collection<ClassDescriptor> mapPlatformClass(@NotNull FqName fqName) {
|
||||
ClassDescriptor kotlinAnalog = mapJavaToKotlin(fqName);
|
||||
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.platform;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.name.ClassId;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class JavaToKotlinClassMapBuilder {
|
||||
|
||||
public enum Direction {
|
||||
JAVA_TO_KOTLIN,
|
||||
KOTLIN_TO_JAVA,
|
||||
BOTH
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
KotlinBuiltIns kotlinBuiltIns = KotlinBuiltIns.getInstance();
|
||||
|
||||
register(Object.class, KotlinBuiltIns.getInstance().getAny());
|
||||
register(String.class, kotlinBuiltIns.getString());
|
||||
register(CharSequence.class, kotlinBuiltIns.getCharSequence());
|
||||
register(Throwable.class, kotlinBuiltIns.getThrowable());
|
||||
register(Cloneable.class, kotlinBuiltIns.getCloneable());
|
||||
register(Number.class, kotlinBuiltIns.getNumber());
|
||||
register(Comparable.class, kotlinBuiltIns.getComparable());
|
||||
register(Enum.class, kotlinBuiltIns.getEnum());
|
||||
register(Annotation.class, kotlinBuiltIns.getAnnotation());
|
||||
|
||||
register(Iterable.class, kotlinBuiltIns.getIterable(), kotlinBuiltIns.getMutableIterable());
|
||||
register(Iterator.class, kotlinBuiltIns.getIterator(), kotlinBuiltIns.getMutableIterator());
|
||||
register(Collection.class, kotlinBuiltIns.getCollection(), kotlinBuiltIns.getMutableCollection());
|
||||
register(List.class, kotlinBuiltIns.getList(), kotlinBuiltIns.getMutableList());
|
||||
register(Set.class, kotlinBuiltIns.getSet(), kotlinBuiltIns.getMutableSet());
|
||||
register(Map.class, kotlinBuiltIns.getMap(), kotlinBuiltIns.getMutableMap());
|
||||
register(Map.Entry.class, kotlinBuiltIns.getMapEntry(), kotlinBuiltIns.getMutableMapEntry());
|
||||
register(ListIterator.class, kotlinBuiltIns.getListIterator(), kotlinBuiltIns.getMutableListIterator());
|
||||
|
||||
for (JvmPrimitiveType jvmType : JvmPrimitiveType.values()) {
|
||||
register(
|
||||
ClassId.topLevel(jvmType.getWrapperFqName()),
|
||||
kotlinBuiltIns.getPrimitiveClassDescriptor(jvmType.getPrimitiveType()),
|
||||
Direction.BOTH
|
||||
);
|
||||
}
|
||||
|
||||
register(classId(Deprecated.class), kotlinBuiltIns.getDeprecatedAnnotation(), Direction.JAVA_TO_KOTLIN);
|
||||
|
||||
register(classId(Void.class), kotlinBuiltIns.getNothing(), Direction.KOTLIN_TO_JAVA);
|
||||
}
|
||||
|
||||
protected abstract void register(@NotNull ClassId javaClassId, @NotNull ClassDescriptor kotlinDescriptor, @NotNull Direction direction);
|
||||
|
||||
protected abstract void register(
|
||||
@NotNull ClassId javaClassId,
|
||||
@NotNull ClassDescriptor kotlinDescriptor,
|
||||
@NotNull ClassDescriptor kotlinMutableDescriptor
|
||||
);
|
||||
|
||||
@NotNull
|
||||
private static ClassId classId(@NotNull Class<?> clazz) {
|
||||
assert !clazz.isPrimitive() && !clazz.isArray() : "Invalid class: " + clazz;
|
||||
Class<?> outer = clazz.getDeclaringClass();
|
||||
return outer == null
|
||||
? ClassId.topLevel(new FqName(clazz.getCanonicalName()))
|
||||
: classId(outer).createNestedClassId(Name.identifier(clazz.getSimpleName()));
|
||||
}
|
||||
|
||||
private void register(@NotNull Class<?> javaClass, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
register(classId(javaClass), kotlinDescriptor, Direction.BOTH);
|
||||
}
|
||||
|
||||
private void register(
|
||||
@NotNull Class<?> javaClass,
|
||||
@NotNull ClassDescriptor kotlinDescriptor,
|
||||
@NotNull ClassDescriptor kotlinMutableDescriptor
|
||||
) {
|
||||
register(classId(javaClass), kotlinDescriptor, kotlinMutableDescriptor);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user