Merge CollectionClassMapping into JavaToKotlinClassMap

This commit is contained in:
Alexander Udalov
2015-04-22 05:10:32 +03:00
parent a59fa5015e
commit 43c11b2925
4 changed files with 43 additions and 96 deletions
@@ -1,84 +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.resolve.jvm.kotlinSignature;
import com.google.common.collect.ImmutableBiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.name.ClassId;
import org.jetbrains.kotlin.platform.JavaToKotlinClassMapBuilder;
public class CollectionClassMapping extends JavaToKotlinClassMapBuilder {
private static CollectionClassMapping instance = null;
@NotNull
public static CollectionClassMapping getInstance() {
if (instance == null) {
instance = new CollectionClassMapping();
}
return instance;
}
private ImmutableBiMap.Builder<ClassDescriptor, ClassDescriptor> mapBuilder = ImmutableBiMap.builder();
private final ImmutableBiMap<ClassDescriptor, ClassDescriptor> mutableToReadOnlyMap;
private CollectionClassMapping() {
init();
mutableToReadOnlyMap = mapBuilder.build();
mapBuilder = null;
}
@Override
protected void register(@NotNull ClassId javaClassId, @NotNull ClassDescriptor kotlinDescriptor, @NotNull Direction direction) {
// do nothing
}
@Override
protected void register(
@NotNull ClassId javaClassId,
@NotNull ClassDescriptor kotlinDescriptor,
@NotNull ClassDescriptor kotlinMutableDescriptor
) {
mapBuilder.put(kotlinMutableDescriptor, kotlinDescriptor);
}
public boolean isMutableCollection(@NotNull ClassDescriptor mutable) {
return mutableToReadOnlyMap.containsKey(mutable);
}
public boolean isReadOnlyCollection(@NotNull ClassDescriptor immutable) {
return mutableToReadOnlyMap.containsValue(immutable);
}
@NotNull
public ClassDescriptor convertMutableToReadOnly(@NotNull ClassDescriptor mutable) {
ClassDescriptor readOnly = mutableToReadOnlyMap.get(mutable);
if (readOnly == null) {
throw new IllegalArgumentException("Given class " + mutable + " is not a mutable collection");
}
return readOnly;
}
@NotNull
public ClassDescriptor convertReadOnlyToMutable(@NotNull ClassDescriptor readOnly) {
ClassDescriptor mutable = mutableToReadOnlyMap.inverse().get(readOnly);
if (mutable == null) {
throw new IllegalArgumentException("Given class " + readOnly + " is not a read-only collection");
}
return mutable;
}
}
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor;
import org.jetbrains.kotlin.load.java.structure.JavaMethod;
import org.jetbrains.kotlin.name.FqNameUnsafe;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.jvm.JavaResolverUtils;
@@ -619,8 +620,6 @@ public class SignaturesPropagationData {
}
ClassDescriptor klass = (ClassDescriptor) classifier;
CollectionClassMapping collectionMapping = CollectionClassMapping.getInstance();
boolean someSupersMutable = false;
boolean someSupersCovariantReadOnly = false;
boolean someSupersNotCovariantReadOnly = false;
@@ -629,10 +628,10 @@ public class SignaturesPropagationData {
if (classifierFromSuper instanceof ClassDescriptor) {
ClassDescriptor classFromSuper = (ClassDescriptor) classifierFromSuper;
if (collectionMapping.isMutableCollection(classFromSuper)) {
if (JavaToKotlinClassMap.INSTANCE.isMutableCollection(classFromSuper)) {
someSupersMutable = true;
}
else if (collectionMapping.isReadOnlyCollection(classFromSuper)) {
else if (JavaToKotlinClassMap.INSTANCE.isReadOnlyCollection(classFromSuper)) {
if (typeFromSuper.varianceOfPosition == Variance.OUT_VARIANCE) {
someSupersCovariantReadOnly = true;
}
@@ -648,13 +647,13 @@ public class SignaturesPropagationData {
return classifier;
}
else if (someSupersMutable) {
if (collectionMapping.isReadOnlyCollection(klass)) {
return collectionMapping.convertReadOnlyToMutable(klass);
if (JavaToKotlinClassMap.INSTANCE.isReadOnlyCollection(klass)) {
return JavaToKotlinClassMap.INSTANCE.convertReadOnlyToMutable(klass);
}
}
else if (someSupersNotCovariantReadOnly || someSupersCovariantReadOnly) {
if (collectionMapping.isMutableCollection(klass)) {
return collectionMapping.convertMutableToReadOnly(klass);
if (JavaToKotlinClassMap.INSTANCE.isMutableCollection(klass)) {
return JavaToKotlinClassMap.INSTANCE.convertMutableToReadOnly(klass);
}
}