Supported propagating read-onliness.
#KT-2776 in progress
This commit is contained in:
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2010-2012 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;
|
||||
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
|
||||
public class MutableReadOnlyCollectionsMap extends JavaToKotlinClassMapBuilder {
|
||||
private static MutableReadOnlyCollectionsMap instance = null;
|
||||
|
||||
@NotNull
|
||||
public static MutableReadOnlyCollectionsMap getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new MutableReadOnlyCollectionsMap();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private ImmutableBiMap.Builder<ClassDescriptor, ClassDescriptor> mapBuilder = ImmutableBiMap.builder();
|
||||
private final ImmutableBiMap<ClassDescriptor, ClassDescriptor> mutableToReadOnlyMap;
|
||||
|
||||
private MutableReadOnlyCollectionsMap() {
|
||||
init();
|
||||
mutableToReadOnlyMap = mapBuilder.build();
|
||||
mapBuilder = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
void register(@NotNull Class<?> javaClass, @NotNull ClassDescriptor kotlinDescriptor, @NotNull Direction direction) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
void register(
|
||||
@NotNull Class<?> javaClass,
|
||||
@NotNull ClassDescriptor kotlinDescriptor,
|
||||
@NotNull ClassDescriptor kotlinMutableDescriptor,
|
||||
@NotNull Direction direction
|
||||
) {
|
||||
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 immutable = mutableToReadOnlyMap.get(mutable);
|
||||
if (immutable == null) {
|
||||
throw new IllegalArgumentException("Given class " + mutable + " is not a mutable collection");
|
||||
}
|
||||
return immutable;
|
||||
}
|
||||
}
|
||||
+37
-2
@@ -304,7 +304,7 @@ public final class JavaFunctionResolver {
|
||||
boolean resultNullable = returnTypeMustBeNullable(autoType, typesFromSuper, covariantPosition);
|
||||
List<TypeProjection> resultArguments = getTypeArgsOfReturnType(autoType, typesFromSuper);
|
||||
JetScope resultScope;
|
||||
ClassifierDescriptor classifierDescriptor = autoType.getConstructor().getDeclarationDescriptor();
|
||||
ClassifierDescriptor classifierDescriptor = getReturnTypeClassifier(autoType, typesFromSuper);
|
||||
if (classifierDescriptor instanceof ClassDescriptor) {
|
||||
resultScope = ((ClassDescriptor) classifierDescriptor).getMemberScope(resultArguments);
|
||||
}
|
||||
@@ -313,7 +313,7 @@ public final class JavaFunctionResolver {
|
||||
}
|
||||
|
||||
return new JetTypeImpl(autoType.getAnnotations(),
|
||||
autoType.getConstructor(),
|
||||
classifierDescriptor.getTypeConstructor(),
|
||||
resultNullable,
|
||||
resultArguments,
|
||||
resultScope);
|
||||
@@ -414,6 +414,41 @@ public final class JavaFunctionResolver {
|
||||
return someSupersNullable && autoType.isNullable();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static ClassifierDescriptor getReturnTypeClassifier(@NotNull JetType autoType, @NotNull Collection<JetType> typesFromSuper) {
|
||||
ClassifierDescriptor classifier = autoType.getConstructor().getDeclarationDescriptor();
|
||||
if (!(classifier instanceof ClassDescriptor)) {
|
||||
return classifier;
|
||||
}
|
||||
ClassDescriptor clazz = (ClassDescriptor) classifier;
|
||||
|
||||
MutableReadOnlyCollectionsMap collectionsMap = MutableReadOnlyCollectionsMap.getInstance();
|
||||
|
||||
if (collectionsMap.isMutableCollection(clazz)) {
|
||||
|
||||
boolean someSupersMutable = false;
|
||||
boolean someSupersReadOnly = false;
|
||||
for (JetType typeFromSuper : typesFromSuper) {
|
||||
ClassifierDescriptor classifierFromSuper = typeFromSuper.getConstructor().getDeclarationDescriptor();
|
||||
if (classifierFromSuper instanceof ClassDescriptor) {
|
||||
ClassDescriptor classFromSuper = (ClassDescriptor) classifierFromSuper;
|
||||
|
||||
if (collectionsMap.isMutableCollection(classFromSuper)) {
|
||||
someSupersMutable = true;
|
||||
}
|
||||
else if (collectionsMap.isReadOnlyCollection(classFromSuper)) {
|
||||
someSupersReadOnly = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (someSupersReadOnly && !someSupersMutable) {
|
||||
return collectionsMap.convertMutableToReadOnly(clazz);
|
||||
}
|
||||
}
|
||||
return classifier;
|
||||
}
|
||||
|
||||
private static boolean isEnumSpecialMethod(@NotNull FunctionDescriptor functionDescriptor) {
|
||||
List<ValueParameterDescriptor> methodTypeParameters = functionDescriptor.getValueParameters();
|
||||
String methodName = functionDescriptor.getName().getName();
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
|
||||
import jet.runtime.typeinfo.KotlinSignature;
|
||||
|
||||
public class InheritReadOnlinessOfArgument {
|
||||
@KotlinSignature("fun foo(): List<List<String>>>")
|
||||
public List<List<String>> foo() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public class Sub extends InheritReadOnlinessOfArgument {
|
||||
public List<List<String>> foo() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
import org.jetbrains.annotations.NotNull
|
||||
|
||||
public open class InheritReadOnlinessOfArgument : java.lang.Object() {
|
||||
public open fun foo(): List<List<String>> = throw UnsupportedOperationException()
|
||||
|
||||
public open class Sub: InheritReadOnlinessOfArgument() {
|
||||
override fun foo(): List<List<String>> = throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
namespace test
|
||||
|
||||
public open class test.InheritReadOnlinessOfArgument : java.lang.Object {
|
||||
public final /*constructor*/ fun <init>(): test.InheritReadOnlinessOfArgument
|
||||
public open fun foo(): jet.List<jet.List<jet.String>>
|
||||
public open class test.InheritReadOnlinessOfArgument.Sub : test.InheritReadOnlinessOfArgument {
|
||||
public final /*constructor*/ fun <init>(): test.InheritReadOnlinessOfArgument.Sub
|
||||
public open override /*1*/ fun foo(): jet.List<jet.List<jet.String>>
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
|
||||
import jet.runtime.typeinfo.KotlinSignature;
|
||||
|
||||
public class InheritReadOnlinessSameClass {
|
||||
@KotlinSignature("fun foo(): List<String>")
|
||||
public List<String> foo() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public class Sub extends InheritReadOnlinessSameClass {
|
||||
public List<String> foo() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
import org.jetbrains.annotations.NotNull
|
||||
|
||||
public open class InheritReadOnlinessSameClass : java.lang.Object() {
|
||||
public open fun foo(): List<String> = throw UnsupportedOperationException()
|
||||
|
||||
public open class Sub: InheritReadOnlinessSameClass() {
|
||||
override fun foo(): List<String> = throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
namespace test
|
||||
|
||||
public open class test.InheritReadOnlinessSameClass : java.lang.Object {
|
||||
public final /*constructor*/ fun <init>(): test.InheritReadOnlinessSameClass
|
||||
public open fun foo(): jet.List<jet.String>
|
||||
public open class test.InheritReadOnlinessSameClass.Sub : test.InheritReadOnlinessSameClass {
|
||||
public final /*constructor*/ fun <init>(): test.InheritReadOnlinessSameClass.Sub
|
||||
public open override /*1*/ fun foo(): jet.List<jet.String>
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
|
||||
import jet.runtime.typeinfo.KotlinSignature;
|
||||
|
||||
public class InheritReadOnlinessSubclass {
|
||||
@KotlinSignature("fun foo(): Collection<String>")
|
||||
public Collection<String> foo() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public class Sub extends InheritReadOnlinessSubclass {
|
||||
public List<String> foo() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
import org.jetbrains.annotations.NotNull
|
||||
|
||||
public open class InheritReadOnlinessSubclass : java.lang.Object() {
|
||||
public open fun foo(): Collection<String> = throw UnsupportedOperationException()
|
||||
|
||||
public open class Sub: InheritReadOnlinessSubclass() {
|
||||
override fun foo(): List<String> = throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
namespace test
|
||||
|
||||
public open class test.InheritReadOnlinessSubclass : java.lang.Object {
|
||||
public final /*constructor*/ fun <init>(): test.InheritReadOnlinessSubclass
|
||||
public open fun foo(): jet.Collection<jet.String>
|
||||
public open class test.InheritReadOnlinessSubclass.Sub : test.InheritReadOnlinessSubclass {
|
||||
public final /*constructor*/ fun <init>(): test.InheritReadOnlinessSubclass.Sub
|
||||
public open override /*1*/ fun foo(): jet.List<jet.String>
|
||||
}
|
||||
}
|
||||
@@ -496,6 +496,21 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
|
||||
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritNullabilitySameJavaType.java");
|
||||
}
|
||||
|
||||
@TestMetadata("InheritReadOnlinessOfArgument.java")
|
||||
public void testInheritReadOnlinessOfArgument() throws Exception {
|
||||
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritReadOnlinessOfArgument.java");
|
||||
}
|
||||
|
||||
@TestMetadata("InheritReadOnlinessSameClass.java")
|
||||
public void testInheritReadOnlinessSameClass() throws Exception {
|
||||
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritReadOnlinessSameClass.java");
|
||||
}
|
||||
|
||||
@TestMetadata("InheritReadOnlinessSubclass.java")
|
||||
public void testInheritReadOnlinessSubclass() throws Exception {
|
||||
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritReadOnlinessSubclass.java");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test innerSuite() {
|
||||
|
||||
+15
@@ -1391,6 +1391,21 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso
|
||||
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritNullabilitySameJavaType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("InheritReadOnlinessOfArgument.kt")
|
||||
public void testInheritReadOnlinessOfArgument() throws Exception {
|
||||
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritReadOnlinessOfArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("InheritReadOnlinessSameClass.kt")
|
||||
public void testInheritReadOnlinessSameClass() throws Exception {
|
||||
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritReadOnlinessSameClass.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("InheritReadOnlinessSubclass.kt")
|
||||
public void testInheritReadOnlinessSubclass() throws Exception {
|
||||
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritReadOnlinessSubclass.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test innerSuite() {
|
||||
|
||||
Reference in New Issue
Block a user