KT-9377 Support is-checks for read-only collections

Reorganize marker interfaces and is/as/as? intrinsics.
This commit is contained in:
Dmitry Petrov
2015-10-01 17:51:22 +03:00
parent 6cb0e5151c
commit e033d093d4
14 changed files with 352 additions and 315 deletions
@@ -18,6 +18,7 @@ package kotlin.jvm.internal;
import kotlin.KotlinNullPointerException;
import kotlin.UninitializedPropertyAccessException;
import kotlin.jvm.internal.markers.*;
import java.util.*;
@@ -34,10 +35,6 @@ public class Intrinsics {
throw sanitizeStackTrace(new KotlinNullPointerException());
}
public static void throwCce(String message) {
throw sanitizeStackTrace(new ClassCastException(message));
}
public static void throwUninitializedPropertyAccessException(String propertyName) {
throw sanitizeStackTrace(new UninitializedPropertyAccessException(propertyName));
}
@@ -126,12 +123,16 @@ public class Intrinsics {
}
private static <T extends Throwable> T sanitizeStackTrace(T throwable) {
return sanitizeStackTrace(throwable, Intrinsics.class.getName());
}
static <T extends Throwable> T sanitizeStackTrace(T throwable, String classNameToDrop) {
StackTraceElement[] stackTrace = throwable.getStackTrace();
int size = stackTrace.length;
int lastIntrinsic = -1;
for (int i = 0; i < size; i++) {
if (Intrinsics.class.getName().equals(stackTrace[i].getClassName())) {
if (classNameToDrop.equals(stackTrace[i].getClassName())) {
lastIntrinsic = i;
}
}
@@ -140,268 +141,4 @@ public class Intrinsics {
throwable.setStackTrace(list.toArray(new StackTraceElement[list.size()]));
return throwable;
}
public static boolean isMutableIterator(Object obj) {
return (obj instanceof Iterator) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableIterator));
}
public static Iterator asMutableIterator(Object obj) {
Iterator result = null;
try {
result = (Iterator) obj;
}
catch (ClassCastException e) {
throwCce("argument is not an Iterator");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterator)) {
throwCce("argument is not a MutableIterator");
}
return result;
}
public static Iterator safeAsMutableIterator(Object obj) {
Iterator result;
try {
result = (Iterator) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterator)) {
return null;
}
return result;
}
public static boolean isMutableListIterator(Object obj) {
return (obj instanceof ListIterator) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableListIterator));
}
public static ListIterator asMutableListIterator(Object obj) {
ListIterator result = null;
try {
result = (ListIterator) obj;
}
catch (ClassCastException e) {
throwCce("argument is not a ListIterator");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableListIterator)) {
throwCce("argument is not a MutableListIterator");
}
return result;
}
public static ListIterator safeAsMutableListIterator(Object obj) {
ListIterator result;
try {
result = (ListIterator) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableListIterator)) {
return null;
}
return result;
}
public static boolean isMutableIterable(Object obj) {
return (obj instanceof Iterable) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableIterable));
}
public static Iterable asMutableIterable(Object obj) {
Iterable result = null;
try {
result = (Iterable) obj;
}
catch (ClassCastException e) {
throwCce("argument is not an Iterable");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterable)) {
throwCce("argument is not a MutableIterable");
}
return result;
}
public static Iterable safeAsMutableIterable(Object obj) {
Iterable result;
try {
result = (Iterable) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterable)) {
return null;
}
return result;
}
public static boolean isMutableCollection(Object obj) {
return (obj instanceof Collection) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableCollection));
}
public static Collection asMutableCollection(Object obj) {
Collection result = null;
try {
result = (Collection) obj;
}
catch (ClassCastException e) {
throwCce("argument is not a Collection");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableCollection)) {
throwCce("argument is not a MutableCollection");
}
return result;
}
public static Collection safeAsMutableCollection(Object obj) {
Collection result;
try {
result = (Collection) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableCollection)) {
return null;
}
return result;
}
public static boolean isMutableList(Object obj) {
return (obj instanceof List) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableList));
}
public static List asMutableList(Object obj) {
List result = null;
try {
result = (List) obj;
}
catch (ClassCastException e) {
throwCce("argument is not a List");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableList)) {
throwCce("argument is not a MutableList");
}
return result;
}
public static List safeAsMutableList(Object obj) {
List result;
try {
result = (List) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableList)) {
return null;
}
return result;
}
public static boolean isMutableSet(Object obj) {
return (obj instanceof Set) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableSet));
}
public static Set asMutableSet(Object obj) {
Set result = null;
try {
result = (Set) obj;
}
catch (ClassCastException e) {
throwCce("argument is not a Set");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableSet)) {
throwCce("argument is not a MutableSet");
}
return result;
}
public static Set safeAsMutableSet(Object obj) {
Set result;
try {
result = (Set) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableSet)) {
return null;
}
return result;
}
public static boolean isMutableMap(Object obj) {
return (obj instanceof Map) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableMap));
}
public static Map asMutableMap(Object obj) {
Map result = null;
try {
result = (Map) obj;
}
catch (ClassCastException e) {
throwCce("argument is not a Map");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap)) {
throwCce("argument is not a MutableMap");
}
return result;
}
public static Map safeAsMutableMap(Object obj) {
Map result = null;
try {
result = (Map) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap)) {
return null;
}
return result;
}
public static boolean isMutableMapEntry(Object obj) {
return (obj instanceof Map.Entry) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableMap.Entry));
}
public static Map.Entry asMutableMapEntry(Object obj) {
Map.Entry result = null;
try {
result = (Map.Entry) obj;
}
catch (ClassCastException e) {
throwCce("argument is not a Map.Entry");
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap.Entry)) {
throwCce("argument is not a MutableMap.MutableEntry");
}
return result;
}
public static Map.Entry safeAsMutableMapEntry(Object obj) {
Map.Entry result = null;
try {
result = (Map.Entry) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap.Entry)) {
return null;
}
return result;
}
}
@@ -0,0 +1,299 @@
/*
* 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 kotlin.jvm.internal;
import kotlin.jvm.internal.markers.*;
import java.util.*;
@SuppressWarnings("unused")
public class TypeIntrinsics {
public static void throwCce(Object argument, String requestedClassName) {
String argumentClassName = argument == null ? "null" : argument.getClass().getName();
ClassCastException classCastException = new ClassCastException(argumentClassName + " cannot be cast to " + requestedClassName);
throw Intrinsics.sanitizeStackTrace(classCastException, TypeIntrinsics.class.getName());
}
public static void throwCce(ClassCastException e) {
throw Intrinsics.sanitizeStackTrace(e, TypeIntrinsics.class.getName());
}
public static boolean isMutableIterator(Object obj) {
return (obj instanceof Iterator) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableIterator));
}
public static Iterator asMutableIterator(Object obj) {
Iterator result = null;
try {
result = (Iterator) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterator)) {
throwCce(obj, "kotlin.MutableIterator");
}
return result;
}
public static Iterator safeAsMutableIterator(Object obj) {
Iterator result;
try {
result = (Iterator) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterator)) {
return null;
}
return result;
}
public static boolean isMutableListIterator(Object obj) {
return (obj instanceof ListIterator) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableListIterator));
}
public static ListIterator asMutableListIterator(Object obj) {
ListIterator result = null;
try {
result = (ListIterator) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableListIterator)) {
throwCce(obj, "kotlin.MutableListIterator");
}
return result;
}
public static ListIterator safeAsMutableListIterator(Object obj) {
ListIterator result;
try {
result = (ListIterator) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableListIterator)) {
return null;
}
return result;
}
public static boolean isMutableIterable(Object obj) {
return (obj instanceof Iterable) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableIterable));
}
public static Iterable asMutableIterable(Object obj) {
Iterable result = null;
try {
result = (Iterable) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterable)) {
throwCce(obj, "kotlin.MutableIterable");
}
return result;
}
public static Iterable safeAsMutableIterable(Object obj) {
Iterable result;
try {
result = (Iterable) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableIterable)) {
return null;
}
return result;
}
public static boolean isMutableCollection(Object obj) {
return (obj instanceof Collection) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableCollection));
}
public static Collection asMutableCollection(Object obj) {
Collection result = null;
try {
result = (Collection) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableCollection)) {
throwCce(obj, "kotlin.MutableCollection");
}
return result;
}
public static Collection safeAsMutableCollection(Object obj) {
Collection result;
try {
result = (Collection) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableCollection)) {
return null;
}
return result;
}
public static boolean isMutableList(Object obj) {
return (obj instanceof List) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableList));
}
public static List asMutableList(Object obj) {
List result = null;
try {
result = (List) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableList)) {
throwCce(obj, "kotlin.MutableList");
}
return result;
}
public static List safeAsMutableList(Object obj) {
List result;
try {
result = (List) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableList)) {
return null;
}
return result;
}
public static boolean isMutableSet(Object obj) {
return (obj instanceof Set) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableSet));
}
public static Set asMutableSet(Object obj) {
Set result = null;
try {
result = (Set) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableSet)) {
throwCce(obj, "kotlin.MutableSet");
}
return result;
}
public static Set safeAsMutableSet(Object obj) {
Set result;
try {
result = (Set) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableSet)) {
return null;
}
return result;
}
public static boolean isMutableMap(Object obj) {
return (obj instanceof Map) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableMap));
}
public static Map asMutableMap(Object obj) {
Map result = null;
try {
result = (Map) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap)) {
throwCce(obj, "kotlin.MutableMap");
}
return result;
}
public static Map safeAsMutableMap(Object obj) {
Map result;
try {
result = (Map) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap)) {
return null;
}
return result;
}
public static boolean isMutableMapEntry(Object obj) {
return (obj instanceof Map.Entry) &&
(!(obj instanceof KMappedMarker) || (obj instanceof KMutableMap.Entry));
}
public static Map.Entry asMutableMapEntry(Object obj) {
Map.Entry result = null;
try {
result = (Map.Entry) obj;
}
catch (ClassCastException e) {
throwCce(e);
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap.Entry)) {
throwCce(obj, "kotlin.MutableMap.MutableEntry");
}
return result;
}
public static Map.Entry safeAsMutableMapEntry(Object obj) {
Map.Entry result;
try {
result = (Map.Entry) obj;
}
catch (ClassCastException e) {
return null;
}
if ((obj instanceof KMappedMarker) && !(obj instanceof KMutableMap.Entry)) {
return null;
}
return result;
}
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMappedMarker {
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMutableCollection extends KMutableIterable {
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMutableIterable extends KMappedMarker {
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMutableIterator extends KMappedMarker {
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMutableList extends KMutableCollection {
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMutableListIterator extends KMutableIterator {
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMutableMap extends KMappedMarker {
interface Entry extends KMappedMarker {
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.jvm.internal;
package kotlin.jvm.internal.markers;
public interface KMutableSet extends KMutableCollection {
}