Extract, rename and konvert TypeMappingMode
This commit is contained in:
@@ -112,40 +112,6 @@ public class JetTypeMapper {
|
|||||||
return bindingContext;
|
return bindingContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum JetTypeMapperMode {
|
|
||||||
/**
|
|
||||||
* kotlin.Int is mapped to I
|
|
||||||
*/
|
|
||||||
DEFAULT,
|
|
||||||
/**
|
|
||||||
* kotlin.Int is mapped to Ljava/lang/Integer;
|
|
||||||
*/
|
|
||||||
TYPE_PARAMETER,
|
|
||||||
/**
|
|
||||||
* kotlin.Int is mapped to Ljava/lang/Integer;
|
|
||||||
* No projections allowed in immediate arguments
|
|
||||||
*/
|
|
||||||
SUPER_TYPE,
|
|
||||||
/**
|
|
||||||
* kotlin.reflect.KClass mapped to java.lang.Class
|
|
||||||
* Other types mapped as DEFAULT
|
|
||||||
*/
|
|
||||||
VALUE_FOR_ANNOTATION,
|
|
||||||
/**
|
|
||||||
* kotlin.reflect.KClass mapped to java.lang.Class
|
|
||||||
* Other types mapped as TYPE_PARAMETER
|
|
||||||
*/
|
|
||||||
TYPE_PARAMETER_FOR_ANNOTATION;
|
|
||||||
|
|
||||||
private boolean isForAnnotation() {
|
|
||||||
return this == VALUE_FOR_ANNOTATION || this == TYPE_PARAMETER_FOR_ANNOTATION;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean needPrimitiveBoxing() {
|
|
||||||
return this == TYPE_PARAMETER || this == SUPER_TYPE || this == TYPE_PARAMETER_FOR_ANNOTATION;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Type mapOwner(@NotNull DeclarationDescriptor descriptor) {
|
public Type mapOwner(@NotNull DeclarationDescriptor descriptor) {
|
||||||
return mapOwner(descriptor, true);
|
return mapOwner(descriptor, true);
|
||||||
@@ -357,40 +323,40 @@ public class JetTypeMapper {
|
|||||||
else if (descriptor instanceof FunctionDescriptor && forceBoxedReturnType((FunctionDescriptor) descriptor)) {
|
else if (descriptor instanceof FunctionDescriptor && forceBoxedReturnType((FunctionDescriptor) descriptor)) {
|
||||||
// TYPE_PARAMETER is a hack to automatically box the return type
|
// TYPE_PARAMETER is a hack to automatically box the return type
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
return mapType(descriptor.getReturnType(), sw, JetTypeMapperMode.TYPE_PARAMETER);
|
return mapType(descriptor.getReturnType(), sw, TypeMappingMode.TYPE_PARAMETER);
|
||||||
}
|
}
|
||||||
else if (DescriptorUtils.isAnnotationClass(descriptor.getContainingDeclaration())) {
|
else if (DescriptorUtils.isAnnotationClass(descriptor.getContainingDeclaration())) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
return mapType(descriptor.getReturnType(), sw, JetTypeMapperMode.VALUE_FOR_ANNOTATION);
|
return mapType(descriptor.getReturnType(), sw, TypeMappingMode.VALUE_FOR_ANNOTATION);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return mapType(returnType, sw, JetTypeMapperMode.DEFAULT, Variance.OUT_VARIANCE);
|
return mapType(returnType, sw, TypeMappingMode.DEFAULT, Variance.OUT_VARIANCE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Type mapType(@NotNull KotlinType jetType, @NotNull JetTypeMapperMode mode) {
|
private Type mapType(@NotNull KotlinType jetType, @NotNull TypeMappingMode mode) {
|
||||||
return mapType(jetType, null, mode);
|
return mapType(jetType, null, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Type mapSupertype(@NotNull KotlinType jetType, @Nullable BothSignatureWriter signatureVisitor) {
|
public Type mapSupertype(@NotNull KotlinType jetType, @Nullable BothSignatureWriter signatureVisitor) {
|
||||||
return mapType(jetType, signatureVisitor, JetTypeMapperMode.SUPER_TYPE);
|
return mapType(jetType, signatureVisitor, TypeMappingMode.SUPER_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Type mapTypeParameter(@NotNull KotlinType jetType, @Nullable BothSignatureWriter signatureVisitor) {
|
public Type mapTypeParameter(@NotNull KotlinType jetType, @Nullable BothSignatureWriter signatureVisitor) {
|
||||||
return mapType(jetType, signatureVisitor, JetTypeMapperMode.TYPE_PARAMETER);
|
return mapType(jetType, signatureVisitor, TypeMappingMode.TYPE_PARAMETER);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Type mapClass(@NotNull ClassifierDescriptor classifier) {
|
public Type mapClass(@NotNull ClassifierDescriptor classifier) {
|
||||||
return mapType(classifier.getDefaultType(), null, JetTypeMapperMode.DEFAULT);
|
return mapType(classifier.getDefaultType(), null, TypeMappingMode.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Type mapType(@NotNull KotlinType jetType) {
|
public Type mapType(@NotNull KotlinType jetType) {
|
||||||
return mapType(jetType, null, JetTypeMapperMode.DEFAULT);
|
return mapType(jetType, null, TypeMappingMode.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -403,7 +369,7 @@ public class JetTypeMapper {
|
|||||||
public JvmMethodSignature mapAnnotationParameterSignature(@NotNull PropertyDescriptor descriptor) {
|
public JvmMethodSignature mapAnnotationParameterSignature(@NotNull PropertyDescriptor descriptor) {
|
||||||
BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
|
BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
|
||||||
sw.writeReturnType();
|
sw.writeReturnType();
|
||||||
mapType(descriptor.getType(), sw, JetTypeMapperMode.VALUE_FOR_ANNOTATION);
|
mapType(descriptor.getType(), sw, TypeMappingMode.VALUE_FOR_ANNOTATION);
|
||||||
sw.writeReturnTypeEnd();
|
sw.writeReturnTypeEnd();
|
||||||
return sw.makeJvmMethodSignature(descriptor.getName().asString(), false);
|
return sw.makeJvmMethodSignature(descriptor.getName().asString(), false);
|
||||||
}
|
}
|
||||||
@@ -414,7 +380,7 @@ public class JetTypeMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Type mapType(@NotNull KotlinType jetType, @Nullable BothSignatureWriter signatureVisitor, @NotNull JetTypeMapperMode mode) {
|
private Type mapType(@NotNull KotlinType jetType, @Nullable BothSignatureWriter signatureVisitor, @NotNull TypeMappingMode mode) {
|
||||||
return mapType(jetType, signatureVisitor, mode, Variance.INVARIANT);
|
return mapType(jetType, signatureVisitor, mode, Variance.INVARIANT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,14 +388,14 @@ public class JetTypeMapper {
|
|||||||
private Type mapType(
|
private Type mapType(
|
||||||
@NotNull KotlinType jetType,
|
@NotNull KotlinType jetType,
|
||||||
@Nullable BothSignatureWriter signatureVisitor,
|
@Nullable BothSignatureWriter signatureVisitor,
|
||||||
@NotNull JetTypeMapperMode kind,
|
@NotNull TypeMappingMode kind,
|
||||||
@NotNull Variance howThisTypeIsUsed
|
@NotNull Variance howThisTypeIsUsed
|
||||||
) {
|
) {
|
||||||
Type builtinType = mapBuiltinType(jetType);
|
Type builtinType = mapBuiltinType(jetType);
|
||||||
|
|
||||||
boolean projectionsAllowed = kind != JetTypeMapperMode.SUPER_TYPE;
|
boolean projectionsAllowed = kind != TypeMappingMode.SUPER_TYPE;
|
||||||
if (builtinType != null) {
|
if (builtinType != null) {
|
||||||
Type asmType = kind.needPrimitiveBoxing() ? boxType(builtinType) : builtinType;
|
Type asmType = kind.getNeedPrimitiveBoxing() ? boxType(builtinType) : builtinType;
|
||||||
writeGenericType(jetType, asmType, signatureVisitor, howThisTypeIsUsed, projectionsAllowed);
|
writeGenericType(jetType, asmType, signatureVisitor, howThisTypeIsUsed, projectionsAllowed);
|
||||||
return asmType;
|
return asmType;
|
||||||
}
|
}
|
||||||
@@ -475,9 +441,9 @@ public class JetTypeMapper {
|
|||||||
arrayElementType = boxType(mapType(memberType, kind));
|
arrayElementType = boxType(mapType(memberType, kind));
|
||||||
if (signatureVisitor != null) {
|
if (signatureVisitor != null) {
|
||||||
signatureVisitor.writeArrayType();
|
signatureVisitor.writeArrayType();
|
||||||
JetTypeMapperMode newMode = kind.isForAnnotation() ?
|
TypeMappingMode newMode = kind.isForAnnotationParameter() ?
|
||||||
JetTypeMapperMode.TYPE_PARAMETER_FOR_ANNOTATION :
|
TypeMappingMode.TYPE_PARAMETER_FOR_ANNOTATION :
|
||||||
JetTypeMapperMode.TYPE_PARAMETER;
|
TypeMappingMode.TYPE_PARAMETER;
|
||||||
mapType(memberType, signatureVisitor, newMode, memberProjection.getProjectionKind());
|
mapType(memberType, signatureVisitor, newMode, memberProjection.getProjectionKind());
|
||||||
signatureVisitor.writeArrayEnd();
|
signatureVisitor.writeArrayEnd();
|
||||||
}
|
}
|
||||||
@@ -487,7 +453,7 @@ public class JetTypeMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (descriptor instanceof ClassDescriptor) {
|
if (descriptor instanceof ClassDescriptor) {
|
||||||
Type asmType = kind.isForAnnotation() && KotlinBuiltIns.isKClass((ClassDescriptor) descriptor) ?
|
Type asmType = kind.isForAnnotationParameter() && KotlinBuiltIns.isKClass((ClassDescriptor) descriptor) ?
|
||||||
AsmTypes.JAVA_CLASS_TYPE :
|
AsmTypes.JAVA_CLASS_TYPE :
|
||||||
computeAsmType((ClassDescriptor) descriptor.getOriginal());
|
computeAsmType((ClassDescriptor) descriptor.getOriginal());
|
||||||
writeGenericType(jetType, asmType, signatureVisitor, howThisTypeIsUsed, projectionsAllowed);
|
writeGenericType(jetType, asmType, signatureVisitor, howThisTypeIsUsed, projectionsAllowed);
|
||||||
@@ -689,7 +655,7 @@ public class JetTypeMapper {
|
|||||||
: Variance.INVARIANT;
|
: Variance.INVARIANT;
|
||||||
signatureVisitor.writeTypeArgument(projectionKind);
|
signatureVisitor.writeTypeArgument(projectionKind);
|
||||||
|
|
||||||
mapType(argument.getType(), signatureVisitor, JetTypeMapperMode.TYPE_PARAMETER);
|
mapType(argument.getType(), signatureVisitor, TypeMappingMode.TYPE_PARAMETER);
|
||||||
signatureVisitor.writeTypeArgumentEnd();
|
signatureVisitor.writeTypeArgumentEnd();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1132,7 +1098,7 @@ public class JetTypeMapper {
|
|||||||
@Nullable
|
@Nullable
|
||||||
public String mapFieldSignature(@NotNull KotlinType backingFieldType) {
|
public String mapFieldSignature(@NotNull KotlinType backingFieldType) {
|
||||||
BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.TYPE);
|
BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.TYPE);
|
||||||
mapType(backingFieldType, sw, JetTypeMapperMode.DEFAULT);
|
mapType(backingFieldType, sw, TypeMappingMode.DEFAULT);
|
||||||
return sw.makeJavaGenericSignature();
|
return sw.makeJavaGenericSignature();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1184,7 +1150,7 @@ public class JetTypeMapper {
|
|||||||
for (KotlinType jetType : typeParameterDescriptor.getUpperBounds()) {
|
for (KotlinType jetType : typeParameterDescriptor.getUpperBounds()) {
|
||||||
if (jetType.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) {
|
if (jetType.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) {
|
||||||
if (!isJvmInterface(jetType)) {
|
if (!isJvmInterface(jetType)) {
|
||||||
mapType(jetType, sw, JetTypeMapperMode.TYPE_PARAMETER);
|
mapType(jetType, sw, TypeMappingMode.TYPE_PARAMETER);
|
||||||
break classBound;
|
break classBound;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1202,13 +1168,13 @@ public class JetTypeMapper {
|
|||||||
if (classifier instanceof ClassDescriptor) {
|
if (classifier instanceof ClassDescriptor) {
|
||||||
if (isJvmInterface(jetType)) {
|
if (isJvmInterface(jetType)) {
|
||||||
sw.writeInterfaceBound();
|
sw.writeInterfaceBound();
|
||||||
mapType(jetType, sw, JetTypeMapperMode.TYPE_PARAMETER);
|
mapType(jetType, sw, TypeMappingMode.TYPE_PARAMETER);
|
||||||
sw.writeInterfaceBoundEnd();
|
sw.writeInterfaceBoundEnd();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (classifier instanceof TypeParameterDescriptor) {
|
else if (classifier instanceof TypeParameterDescriptor) {
|
||||||
sw.writeInterfaceBound();
|
sw.writeInterfaceBound();
|
||||||
mapType(jetType, sw, JetTypeMapperMode.TYPE_PARAMETER);
|
mapType(jetType, sw, TypeMappingMode.TYPE_PARAMETER);
|
||||||
sw.writeInterfaceBoundEnd();
|
sw.writeInterfaceBoundEnd();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -1223,7 +1189,7 @@ public class JetTypeMapper {
|
|||||||
|
|
||||||
private void writeParameter(@NotNull BothSignatureWriter sw, @NotNull JvmMethodParameterKind kind, @NotNull KotlinType type) {
|
private void writeParameter(@NotNull BothSignatureWriter sw, @NotNull JvmMethodParameterKind kind, @NotNull KotlinType type) {
|
||||||
sw.writeParameterType(kind);
|
sw.writeParameterType(kind);
|
||||||
mapType(type, sw, JetTypeMapperMode.DEFAULT);
|
mapType(type, sw, TypeMappingMode.DEFAULT);
|
||||||
sw.writeParameterTypeEnd();
|
sw.writeParameterTypeEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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.codegen.state
|
||||||
|
|
||||||
|
internal enum class TypeMappingMode(
|
||||||
|
val needPrimitiveBoxing: Boolean = false,
|
||||||
|
val isForAnnotationParameter: Boolean = false
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* kotlin.Int is mapped to I
|
||||||
|
*/
|
||||||
|
DEFAULT(),
|
||||||
|
/**
|
||||||
|
* kotlin.Int is mapped to Ljava/lang/Integer;
|
||||||
|
*/
|
||||||
|
TYPE_PARAMETER(needPrimitiveBoxing = true),
|
||||||
|
/**
|
||||||
|
* kotlin.Int is mapped to Ljava/lang/Integer;
|
||||||
|
* No projections allowed in immediate arguments
|
||||||
|
*/
|
||||||
|
SUPER_TYPE(needPrimitiveBoxing = true),
|
||||||
|
/**
|
||||||
|
* kotlin.reflect.KClass mapped to java.lang.Class
|
||||||
|
* Other types mapped as DEFAULT
|
||||||
|
*/
|
||||||
|
VALUE_FOR_ANNOTATION(isForAnnotationParameter = true),
|
||||||
|
/**
|
||||||
|
* kotlin.reflect.KClass mapped to java.lang.Class
|
||||||
|
* Other types mapped as TYPE_PARAMETER
|
||||||
|
*/
|
||||||
|
TYPE_PARAMETER_FOR_ANNOTATION(isForAnnotationParameter = true, needPrimitiveBoxing = true);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user