Refactoring: code duplication removed from accessor generators

This commit is contained in:
Mikhael Bogdanov
2013-03-13 12:03:42 +04:00
parent 86f5114b7d
commit d9fd04dd43
32 changed files with 1026 additions and 521 deletions
@@ -37,9 +37,7 @@ import org.jetbrains.jet.codegen.state.GenerationStateAware;
import org.jetbrains.jet.codegen.state.JetTypeMapper;
import org.jetbrains.jet.codegen.state.JetTypeMapperMode;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody;
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
@@ -81,13 +79,16 @@ public class FunctionCodegen extends GenerationStateAware {
generateMethod(f, method, true, null, functionDescriptor);
}
public void generateMethod(
JetDeclarationWithBody fun,
JvmMethodSignature jvmSignature,
@NotNull PsiElement declaration,
@NotNull JvmMethodSignature jvmSignature,
boolean needJetAnnotations,
@Nullable String propertyTypeSignature,
FunctionDescriptor functionDescriptor
@NotNull FunctionDescriptor functionDescriptor
) {
assert declaration instanceof JetDeclarationWithBody || declaration instanceof JetProperty || declaration instanceof JetParameter;
checkMustGenerateCode(functionDescriptor);
OwnerKind kind = owner.getContextKind();
@@ -96,9 +97,11 @@ public class FunctionCodegen extends GenerationStateAware {
needJetAnnotations = false;
}
boolean hasBodyExpression = hasBodyExpression(declaration);
MethodContext context = owner.intoFunction(functionDescriptor);
if (kind != OwnerKind.TRAIT_IMPL || fun.getBodyExpression() != null) {
generateMethodHeaderAndBody(fun, jvmSignature, needJetAnnotations, propertyTypeSignature, functionDescriptor, context);
if (kind != OwnerKind.TRAIT_IMPL || hasBodyExpression) {
generateMethodHeaderAndBody(declaration, jvmSignature, needJetAnnotations, propertyTypeSignature, functionDescriptor, context);
if (state.getClassBuilderMode() == ClassBuilderMode.FULL && !isAbstract(functionDescriptor, kind)) {
generateBridgeIfNeeded(owner, state, v, jvmSignature.getAsmMethod(), functionDescriptor);
@@ -109,7 +112,7 @@ public class FunctionCodegen extends GenerationStateAware {
}
private void generateMethodHeaderAndBody(
@NotNull JetDeclarationWithBody fun,
@NotNull PsiElement declaration,
@NotNull JvmMethodSignature jvmSignature,
boolean needJetAnnotations,
@Nullable String propertyTypeSignature,
@@ -119,7 +122,7 @@ public class FunctionCodegen extends GenerationStateAware {
OwnerKind kind = context.getContextKind();
Method asmMethod = jvmSignature.getAsmMethod();
MethodVisitor mv = v.newMethod(fun,
MethodVisitor mv = v.newMethod(declaration,
getMethodAsmFlags(functionDescriptor, kind),
asmMethod.getName(),
asmMethod.getDescriptor(),
@@ -142,14 +145,14 @@ public class FunctionCodegen extends GenerationStateAware {
LocalVariablesInfo localVariablesInfo = generateLocalVariablesInfo(functionDescriptor);
MethodBounds methodBounds = generateMethodBody(mv, fun, functionDescriptor, context, asmMethod, localVariablesInfo);
MethodBounds methodBounds = generateMethodBody(mv, declaration, functionDescriptor, context, asmMethod, localVariablesInfo);
Type thisType;
ReceiverParameterDescriptor expectedThisObject = functionDescriptor.getExpectedThisObject();
if (expectedThisObject != null) {
thisType = typeMapper.mapType(expectedThisObject.getType());
}
else if (fun instanceof JetFunctionLiteralExpression || isLocalFun(bindingContext, functionDescriptor)) {
else if (declaration instanceof JetFunctionLiteralExpression || isLocalFun(bindingContext, functionDescriptor)) {
thisType = typeMapper.mapType(context.getThisDescriptor());
}
else {
@@ -158,13 +161,13 @@ public class FunctionCodegen extends GenerationStateAware {
generateLocalVariableTable(typeMapper, mv, functionDescriptor, thisType, localVariablesInfo, methodBounds);
endVisit(mv, null, fun);
endVisit(mv, null, declaration);
}
@NotNull
private MethodBounds generateMethodBody(
@NotNull MethodVisitor mv,
@NotNull JetDeclarationWithBody fun,
@NotNull PsiElement funOrProperty,
@NotNull FunctionDescriptor functionDescriptor,
@NotNull MethodContext context,
@NotNull Method asmMethod,
@@ -201,10 +204,23 @@ public class FunctionCodegen extends GenerationStateAware {
genNotNullAssertionsForParameters(new InstructionAdapter(mv), state, functionDescriptor, frameMap);
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, asmMethod.getReturnType(), context, state);
codegen.returnExpression(fun.getBodyExpression());
boolean hasBodyExpression = hasBodyExpression(funOrProperty);
if (hasBodyExpression) {
JetDeclarationWithBody fun = (JetDeclarationWithBody)funOrProperty;
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, asmMethod.getReturnType(), context, state);
codegen.returnExpression(fun.getBodyExpression());
localVariablesInfo.names.addAll(codegen.getLocalVariableNamesForExpression());
localVariablesInfo.names.addAll(codegen.getLocalVariableNamesForExpression());
} else {
///generate default accessor
assert functionDescriptor instanceof PropertyAccessorDescriptor;
PropertyCodegen.generateDefaultAccessor(
(PropertyAccessorDescriptor) functionDescriptor,
new InstructionAdapter(mv),
kind,
typeMapper,
context);
}
}
Label methodEnd = new Label();
@@ -213,6 +229,12 @@ public class FunctionCodegen extends GenerationStateAware {
return new MethodBounds(methodBegin, methodEnd);
}
private static boolean hasBodyExpression(PsiElement funOrProperty) {
return (funOrProperty instanceof JetDeclarationWithBody
&& ((JetDeclarationWithBody)funOrProperty).getBodyExpression() != null);
}
public static class MethodBounds {
@NotNull private final Label begin;
@@ -24,14 +24,14 @@ import org.jetbrains.asm4.MethodVisitor;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.context.CodegenContext;
import org.jetbrains.jet.codegen.signature.JvmMethodSignature;
import org.jetbrains.jet.codegen.signature.JvmPropertyAccessorSignature;
import org.jetbrains.jet.codegen.signature.kotlin.JetMethodAnnotationWriter;
import org.jetbrains.jet.codegen.state.GenerationStateAware;
import org.jetbrains.jet.codegen.state.JetTypeMapper;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
@@ -40,9 +40,8 @@ import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.AsmUtil.*;
import static org.jetbrains.jet.codegen.AsmUtil.getDeprecatedAccessFlag;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isExternallyAccessible;
import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE;
@@ -72,16 +71,15 @@ public class PropertyCodegen extends GenerationStateAware {
if (kind != OwnerKind.TRAIT_IMPL && !(kind instanceof OwnerKind.StaticDelegateKind)) {
generateBackingField(p, propertyDescriptor);
}
generateGetter(p, propertyDescriptor);
generateSetter(p, propertyDescriptor);
generateGetter(p, propertyDescriptor, p.getGetter());
generateSetter(p, propertyDescriptor, p.getSetter());
}
public void generatePrimaryConstructorProperty(JetParameter p, PropertyDescriptor descriptor) {
generateBackingField(p, descriptor);
int accessFlags = getVisibilityAccessFlag(descriptor) | getModalityAccessFlag(descriptor) | getDeprecatedAccessFlag(descriptor);
generateDefaultGetter(descriptor, accessFlags, p);
generateGetter(p, descriptor, null);
if (descriptor.isVar()) {
generateDefaultSetter(descriptor, accessFlags, p);
generateSetter(p, descriptor, null);
}
}
@@ -121,107 +119,73 @@ public class PropertyCodegen extends GenerationStateAware {
}
}
private void generateGetter(JetProperty p, PropertyDescriptor propertyDescriptor) {
JetPropertyAccessor getter = p.getGetter();
PropertyGetterDescriptor getterDescriptor = propertyDescriptor.getGetter();
if (getter != null && getter.getBodyExpression() != null) {
private void generateGetter(JetNamedDeclaration p, PropertyDescriptor propertyDescriptor, JetPropertyAccessor getter) {
if (getter != null && getter.getBodyExpression() != null || isExternallyAccessible(propertyDescriptor)) {
JvmPropertyAccessorSignature signature = typeMapper.mapGetterSignature(propertyDescriptor, kind);
functionCodegen.generateMethod(getter, signature.getJvmMethodSignature(), true, signature.getPropertyTypeKotlinSignature(),
PropertyGetterDescriptor getterDescriptor = propertyDescriptor.getGetter();
getterDescriptor = getterDescriptor != null ? getterDescriptor : DescriptorResolver.createDefaultGetter(propertyDescriptor);
functionCodegen.generateMethod(getter != null ? getter : p,
signature.getJvmMethodSignature(),
true,
signature.getPropertyTypeKotlinSignature(),
getterDescriptor);
}
else if (isExternallyAccessible(propertyDescriptor)) {
int flags = getVisibilityAccessFlag(propertyDescriptor);
flags |= getModalityAccessFlag(propertyDescriptor);
flags |= getterDescriptor == null ? getDeprecatedAccessFlag(propertyDescriptor): getDeprecatedAccessFlag(getterDescriptor);
generateDefaultGetter(propertyDescriptor, flags, p);
}
}
private void generateSetter(JetProperty p, PropertyDescriptor propertyDescriptor) {
JetPropertyAccessor setter = p.getSetter();
if (setter != null && setter.getBodyExpression() != null) {
private void generateSetter(JetNamedDeclaration p, PropertyDescriptor propertyDescriptor, JetPropertyAccessor setter) {
if (setter != null && setter.getBodyExpression() != null
|| isExternallyAccessible(propertyDescriptor) && propertyDescriptor.isVar()) {
JvmPropertyAccessorSignature signature = typeMapper.mapSetterSignature(propertyDescriptor, kind);
functionCodegen.generateMethod(setter, signature.getJvmMethodSignature(), true, signature.getPropertyTypeKotlinSignature(),
propertyDescriptor.getSetter());
}
else if (isExternallyAccessible(propertyDescriptor) && propertyDescriptor.isVar()) {
PropertySetterDescriptor setterDescriptor = propertyDescriptor.getSetter();
int flags = getModalityAccessFlag(propertyDescriptor);
if (setterDescriptor == null) {
flags |= getVisibilityAccessFlag(propertyDescriptor);
flags |= getDeprecatedAccessFlag(propertyDescriptor);
}
else {
flags |= getVisibilityAccessFlag(setterDescriptor);
flags |= getDeprecatedAccessFlag(setterDescriptor);
}
generateDefaultSetter(propertyDescriptor, flags, p);
setterDescriptor = setterDescriptor != null ? setterDescriptor : DescriptorResolver.createDefaultSetter(propertyDescriptor);
functionCodegen.generateMethod(setter != null ? setter : p,
signature.getJvmMethodSignature(),
true,
signature.getPropertyTypeKotlinSignature(),
setterDescriptor);
}
}
private void generateDefaultGetter(PropertyDescriptor propertyDescriptor, int flags, PsiElement origin) {
checkMustGenerateCode(propertyDescriptor);
if (kind == OwnerKind.TRAIT_IMPL) {
return;
}
public static void generateDefaultAccessor(
@NotNull PropertyAccessorDescriptor accessorDescriptor,
@NotNull InstructionAdapter iv,
@NotNull OwnerKind kind,
@NotNull JetTypeMapper typeMapper,
@NotNull CodegenContext context) {
if (kind == OwnerKind.NAMESPACE || kind instanceof OwnerKind.StaticDelegateKind) {
flags |= ACC_STATIC;
}
PsiElement psiElement = descriptorToDeclaration(bindingContext, propertyDescriptor.getContainingDeclaration());
boolean isTrait = psiElement instanceof JetClass && ((JetClass) psiElement).isTrait();
if (isTrait) {
flags |= ACC_ABSTRACT;
}
JvmPropertyAccessorSignature signature = typeMapper.mapGetterSignature(propertyDescriptor, kind);
JvmMethodSignature jvmMethodSignature = signature.getJvmMethodSignature();
String descriptor = jvmMethodSignature.getAsmMethod().getDescriptor();
String getterName = getterName(propertyDescriptor.getName());
MethodVisitor mv = v.newMethod(origin, flags, getterName, descriptor, jvmMethodSignature.getGenericsSignature(), null);
PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
generateJetPropertyAnnotation(mv, signature.getPropertyTypeKotlinSignature(),
jvmMethodSignature.getKotlinTypeParameter(), propertyDescriptor,
getter == null
? propertyDescriptor.getVisibility()
: getter.getVisibility());
if (getter != null) {
//noinspection ConstantConditions
assert !getter.hasBody();
AnnotationCodegen.forMethod(mv, typeMapper).genAnnotations(getter);
}
if (state.getClassBuilderMode() != ClassBuilderMode.SIGNATURES && !isTrait) {
if (propertyDescriptor.getModality() != Modality.ABSTRACT) {
mv.visitCode();
if (state.getClassBuilderMode() == ClassBuilderMode.STUBS) {
genStubThrow(mv);
}
else if (kind instanceof OwnerKind.StaticDelegateKind) {
FunctionCodegen.generateStaticDelegateMethodBody(mv, jvmMethodSignature.getAsmMethod(), (OwnerKind.StaticDelegateKind) kind);
}
else {
InstructionAdapter iv = new InstructionAdapter(mv);
if (kind != OwnerKind.NAMESPACE) {
iv.load(0, OBJECT_TYPE);
}
Type type = typeMapper.mapType(propertyDescriptor);
iv.visitFieldInsn(
kind == OwnerKind.NAMESPACE ? GETSTATIC : GETFIELD,
typeMapper.getOwner(propertyDescriptor, kind, isCallInsideSameModuleAsDeclared(propertyDescriptor, context)).getInternalName(),
propertyDescriptor.getName().getName(),
type.getDescriptor());
iv.areturn(type);
}
PropertyDescriptor propertyDescriptor = accessorDescriptor.getCorrespondingProperty();
final Type type = typeMapper.mapType(propertyDescriptor);
if (accessorDescriptor instanceof PropertyGetterDescriptor) {
if (kind != OwnerKind.NAMESPACE) {
iv.load(0, OBJECT_TYPE);
}
}
FunctionCodegen.endVisit(mv, "getter", origin);
iv.visitFieldInsn(
kind == OwnerKind.NAMESPACE ? GETSTATIC : GETFIELD,
typeMapper.getOwner(propertyDescriptor, kind, isCallInsideSameModuleAsDeclared(propertyDescriptor, context)).getInternalName(),
propertyDescriptor.getName().getName(),
type.getDescriptor());
iv.areturn(type);
} else if (accessorDescriptor instanceof PropertySetterDescriptor) {
int paramCode = 0;
if (kind != OwnerKind.NAMESPACE) {
iv.load(0, OBJECT_TYPE);
paramCode = 1;
}
ReceiverParameterDescriptor receiverParameter = propertyDescriptor.getReceiverParameter();
if (receiverParameter != null) {
paramCode += typeMapper.mapType(receiverParameter.getType()).getSize();
}
iv.load(paramCode, type);
iv.visitFieldInsn(kind == OwnerKind.NAMESPACE ? PUTSTATIC : PUTFIELD,
typeMapper.getOwner(propertyDescriptor, kind, isCallInsideSameModuleAsDeclared(propertyDescriptor, context)).getInternalName(),
propertyDescriptor.getName().getName(),
type.getDescriptor());
FunctionCodegen.generateBridgeIfNeeded(context, state, v, jvmMethodSignature.getAsmMethod(), getter);
iv.visitInsn(RETURN);
} else {
assert false;
}
}
public static void generateJetPropertyAnnotation(
@@ -242,73 +206,6 @@ public class PropertyCodegen extends GenerationStateAware {
aw.visitEnd();
}
private void generateDefaultSetter(PropertyDescriptor propertyDescriptor, int flags, PsiElement origin) {
checkMustGenerateCode(propertyDescriptor);
if (kind == OwnerKind.TRAIT_IMPL) {
return;
}
if (kind == OwnerKind.NAMESPACE || kind instanceof OwnerKind.StaticDelegateKind) {
flags |= ACC_STATIC;
}
PsiElement psiElement = descriptorToDeclaration(bindingContext, propertyDescriptor.getContainingDeclaration());
boolean isTrait = psiElement instanceof JetClass && ((JetClass) psiElement).isTrait();
if (isTrait) {
flags |= ACC_ABSTRACT;
}
JvmPropertyAccessorSignature signature = typeMapper.mapSetterSignature(propertyDescriptor, kind);
assert true;
JvmMethodSignature jvmMethodSignature = signature.getJvmMethodSignature();
String descriptor = jvmMethodSignature.getAsmMethod().getDescriptor();
MethodVisitor mv = v.newMethod(origin, flags, setterName(propertyDescriptor.getName()), descriptor, jvmMethodSignature.getGenericsSignature(), null);
PropertySetterDescriptor setter = propertyDescriptor.getSetter();
assert setter != null;
generateJetPropertyAnnotation(mv, signature.getPropertyTypeKotlinSignature(),
jvmMethodSignature.getKotlinTypeParameter(), propertyDescriptor,
setter.getVisibility());
assert !setter.hasBody();
AnnotationCodegen.forMethod(mv, typeMapper).genAnnotations(setter);
if (state.getClassBuilderMode() != ClassBuilderMode.SIGNATURES && (!isTrait)) {
if (propertyDescriptor.getModality() != Modality.ABSTRACT) {
mv.visitCode();
if (state.getClassBuilderMode() == ClassBuilderMode.STUBS) {
genStubThrow(mv);
}
else if (kind instanceof OwnerKind.StaticDelegateKind) {
FunctionCodegen.generateStaticDelegateMethodBody(mv, jvmMethodSignature.getAsmMethod(), (OwnerKind.StaticDelegateKind) kind);
}
else {
InstructionAdapter iv = new InstructionAdapter(mv);
Type type = typeMapper.mapType(propertyDescriptor);
int paramCode = 0;
if (kind != OwnerKind.NAMESPACE) {
iv.load(0, OBJECT_TYPE);
paramCode = 1;
}
ReceiverParameterDescriptor receiverParameter = propertyDescriptor.getReceiverParameter();
if (receiverParameter != null) {
paramCode += typeMapper.mapType(receiverParameter.getType()).getSize();
}
iv.load(paramCode, type);
iv.visitFieldInsn(kind == OwnerKind.NAMESPACE ? PUTSTATIC : PUTFIELD,
typeMapper.getOwner(propertyDescriptor, kind, isCallInsideSameModuleAsDeclared(propertyDescriptor, context)).getInternalName(),
propertyDescriptor.getName().getName(),
type.getDescriptor());
iv.visitInsn(RETURN);
}
}
FunctionCodegen.endVisit(mv, "setter", origin);
FunctionCodegen.generateBridgeIfNeeded(context, state, v, jvmMethodSignature.getAsmMethod(), setter);
}
}
public static String getterName(Name propertyName) {
return JvmAbi.GETTER_PREFIX + StringUtil.capitalizeWithJavaBeanConvention(propertyName.getName());
}
@@ -0,0 +1,13 @@
package test
class ClassVal() {
val property1 = 1
internal val property2 = 1
private val property3 = Object()
protected val property4: String = ""
public val property5: Int = 1
}
@@ -0,0 +1,15 @@
package test
internal final class ClassVal {
/*primary*/ public constructor ClassVal()
internal final val property1 : jet.Int
internal final fun <get-property1>() : jet.Int
internal final val property2 : jet.Int
internal final fun <get-property2>() : jet.Int
private final val property3 : java.lang.Object
private final fun <get-property3>() : java.lang.Object
protected final val property4 : jet.String
protected final fun <get-property4>() : jet.String
public final val property5 : jet.Int
public final fun <get-property5>() : jet.Int
}
@@ -0,0 +1,11 @@
package test
class ClassValParams(
val pr1: String,
internal val pr2 : Int,
private val pr3: Long,
protected val pr4: java.util.Date,
public val pr5 : Any,
pr6: Object) {
}
@@ -0,0 +1,15 @@
package test
internal final class ClassValParams {
/*primary*/ public constructor ClassValParams(/*0*/ pr1 : jet.String, /*1*/ pr2 : jet.Int, /*2*/ pr3 : jet.Long, /*3*/ pr4 : java.util.Date, /*4*/ pr5 : jet.Any, /*5*/ pr6 : java.lang.Object)
internal final val pr1 : jet.String
internal final fun <get-pr1>() : jet.String
internal final val pr2 : jet.Int
internal final fun <get-pr2>() : jet.Int
private final val pr3 : jet.Long
private final fun <get-pr3>() : jet.Long
protected final val pr4 : java.util.Date
protected final fun <get-pr4>() : java.util.Date
public final val pr5 : jet.Any
public final fun <get-pr5>() : jet.Any
}
@@ -0,0 +1,20 @@
package test
class ClassVal() {
val property1 = 1
get
internal val property2 = 1
get
private val property3 = Object()
get
protected val property4: String = ""
get
public val property5: Int = 1
get
}
@@ -0,0 +1,15 @@
package test
internal final class ClassVal {
/*primary*/ public constructor ClassVal()
internal final val property1 : jet.Int
internal final fun <get-property1>() : jet.Int
internal final val property2 : jet.Int
internal final fun <get-property2>() : jet.Int
private final val property3 : java.lang.Object
private final fun <get-property3>() : java.lang.Object
protected final val property4 : jet.String
protected final fun <get-property4>() : jet.String
public final val property5 : jet.Int
public final fun <get-property5>() : jet.Int
}
@@ -0,0 +1,15 @@
package test
class ClassVar() {
var property1 = 1
internal var property2 = 1
private var property3 = Object()
protected var property4: String = ""
public var property5: Int = 1
}
@@ -0,0 +1,20 @@
package test
internal final class ClassVar {
/*primary*/ public constructor ClassVar()
internal final var property1 : jet.Int
internal final fun <get-property1>() : jet.Int
internal final fun <set-property1>(/*0*/ <set-?> : jet.Int) : Unit
internal final var property2 : jet.Int
internal final fun <get-property2>() : jet.Int
internal final fun <set-property2>(/*0*/ <set-?> : jet.Int) : Unit
private final var property3 : java.lang.Object
private final fun <get-property3>() : java.lang.Object
private final fun <set-property3>(/*0*/ <set-?> : java.lang.Object) : Unit
protected final var property4 : jet.String
protected final fun <get-property4>() : jet.String
protected final fun <set-property4>(/*0*/ <set-?> : jet.String) : Unit
public final var property5 : jet.Int
public final fun <get-property5>() : jet.Int
public final fun <set-property5>(/*0*/ <set-?> : jet.Int) : Unit
}
@@ -0,0 +1,18 @@
package test
open class ClassVarModality() {
open var property1 = 1
final internal var property2 = 1
open var property3 = 1
private set
final internal var property4 = 1
private set
}
abstract class ClassVarModalityAbstract {
abstract var property1 : java.util.Date
public set
}
@@ -0,0 +1,24 @@
package test
internal open class ClassVarModality {
/*primary*/ public constructor ClassVarModality()
internal open var property1 : jet.Int
internal open fun <get-property1>() : jet.Int
internal open fun <set-property1>(/*0*/ <set-?> : jet.Int) : Unit
internal final var property2 : jet.Int
internal final fun <get-property2>() : jet.Int
internal final fun <set-property2>(/*0*/ <set-?> : jet.Int) : Unit
internal open var property3 : jet.Int
internal open fun <get-property3>() : jet.Int
private open fun <set-property3>(/*0*/ <set-?> : jet.Int) : Unit
internal final var property4 : jet.Int
internal final fun <get-property4>() : jet.Int
private final fun <set-property4>(/*0*/ <set-?> : jet.Int) : Unit
}
internal abstract class ClassVarModalityAbstract {
/*primary*/ public constructor ClassVarModalityAbstract()
internal abstract var property1 : java.util.Date
internal abstract fun <get-property1>() : java.util.Date
public abstract fun <set-property1>(/*0*/ <set-?> : java.util.Date) : Unit
}
@@ -0,0 +1,10 @@
package test
class ClassVarParams(
var pr1: String,
internal var pr2 : Int,
private var pr3: Long,
protected var pr4: java.util.Date,
public var pr5 : Any,
pr6: Object) {
}
@@ -0,0 +1,20 @@
package test
internal final class ClassVarParams {
/*primary*/ public constructor ClassVarParams(/*0*/ pr1 : jet.String, /*1*/ pr2 : jet.Int, /*2*/ pr3 : jet.Long, /*3*/ pr4 : java.util.Date, /*4*/ pr5 : jet.Any, /*5*/ pr6 : java.lang.Object)
internal final var pr1 : jet.String
internal final fun <get-pr1>() : jet.String
internal final fun <set-pr1>(/*0*/ <set-?> : jet.String) : Unit
internal final var pr2 : jet.Int
internal final fun <get-pr2>() : jet.Int
internal final fun <set-pr2>(/*0*/ <set-?> : jet.Int) : Unit
private final var pr3 : jet.Long
private final fun <get-pr3>() : jet.Long
private final fun <set-pr3>(/*0*/ <set-?> : jet.Long) : Unit
protected final var pr4 : java.util.Date
protected final fun <get-pr4>() : java.util.Date
protected final fun <set-pr4>(/*0*/ <set-?> : java.util.Date) : Unit
public final var pr5 : jet.Any
public final fun <get-pr5>() : jet.Any
public final fun <set-pr5>(/*0*/ <set-?> : jet.Any) : Unit
}
@@ -0,0 +1,18 @@
package test
class ClassVal() {
var property1 = 1
get
internal var property2 = 1
get
private var property3 = Object()
get
protected var property4: String = ""
get
public var property5: Int = 1
get
}
@@ -0,0 +1,20 @@
package test
internal final class ClassVal {
/*primary*/ public constructor ClassVal()
internal final var property1 : jet.Int
internal final fun <get-property1>() : jet.Int
internal final fun <set-property1>(/*0*/ <set-?> : jet.Int) : Unit
internal final var property2 : jet.Int
internal final fun <get-property2>() : jet.Int
internal final fun <set-property2>(/*0*/ <set-?> : jet.Int) : Unit
private final var property3 : java.lang.Object
private final fun <get-property3>() : java.lang.Object
private final fun <set-property3>(/*0*/ <set-?> : java.lang.Object) : Unit
protected final var property4 : jet.String
protected final fun <get-property4>() : jet.String
protected final fun <set-property4>(/*0*/ <set-?> : jet.String) : Unit
public final var property5 : jet.Int
public final fun <get-property5>() : jet.Int
public final fun <set-property5>(/*0*/ <set-?> : jet.Int) : Unit
}
@@ -0,0 +1,36 @@
package test
class ClassVal() {
var property1 = 1
set
var property2 = Object()
protected set
var property3 = Object()
private set
protected var property4: String = ""
set
protected var property5: String = ""
private set
protected var property6: String = ""
internal set
protected var property7: java.util.Date = java.util.Date()
public set
public var property8: Int = 1
set
public var property9: Int = 1
private set
public var property10: Int = 1
protected set
public var property11: Int = 1
internal set
}
@@ -0,0 +1,38 @@
package test
internal final class ClassVal {
/*primary*/ public constructor ClassVal()
internal final var property1 : jet.Int
internal final fun <get-property1>() : jet.Int
internal final fun <set-property1>(/*0*/ <set-?> : jet.Int) : Unit
public final var property10 : jet.Int
public final fun <get-property10>() : jet.Int
protected final fun <set-property10>(/*0*/ <set-?> : jet.Int) : Unit
public final var property11 : jet.Int
public final fun <get-property11>() : jet.Int
internal final fun <set-property11>(/*0*/ <set-?> : jet.Int) : Unit
internal final var property2 : java.lang.Object
internal final fun <get-property2>() : java.lang.Object
protected final fun <set-property2>(/*0*/ <set-?> : java.lang.Object) : Unit
internal final var property3 : java.lang.Object
internal final fun <get-property3>() : java.lang.Object
private final fun <set-property3>(/*0*/ <set-?> : java.lang.Object) : Unit
protected final var property4 : jet.String
protected final fun <get-property4>() : jet.String
protected final fun <set-property4>(/*0*/ <set-?> : jet.String) : Unit
protected final var property5 : jet.String
protected final fun <get-property5>() : jet.String
private final fun <set-property5>(/*0*/ <set-?> : jet.String) : Unit
protected final var property6 : jet.String
protected final fun <get-property6>() : jet.String
internal final fun <set-property6>(/*0*/ <set-?> : jet.String) : Unit
protected final var property7 : java.util.Date
protected final fun <get-property7>() : java.util.Date
public final fun <set-property7>(/*0*/ <set-?> : java.util.Date) : Unit
public final var property8 : jet.Int
public final fun <get-property8>() : jet.Int
public final fun <set-property8>(/*0*/ <set-?> : jet.Int) : Unit
public final var property9 : jet.Int
public final fun <get-property9>() : jet.Int
private final fun <set-property9>(/*0*/ <set-?> : jet.Int) : Unit
}
@@ -0,0 +1,9 @@
package test
val Long.date1: Object = java.util.Date()
internal val Long.date12: Object = java.util.Date()
private val Long.date3: java.util.Date = java.util.Date()
public val Long.date4: java.util.Date = java.util.Date()
@@ -0,0 +1,10 @@
package test
internal val jet.Long.date1 : java.lang.Object
internal fun jet.Long.<get-date1>() : java.lang.Object
internal val jet.Long.date12 : java.lang.Object
internal fun jet.Long.<get-date12>() : java.lang.Object
private val jet.Long.date3 : java.util.Date
private fun jet.Long.<get-date3>() : java.util.Date
public val jet.Long.date4 : java.util.Date
public fun jet.Long.<get-date4>() : java.util.Date
@@ -0,0 +1,9 @@
package test
var Long.date1: Object = java.util.Date()
internal var Long.date12: Object = java.util.Date()
private var Long.date3: java.util.Date = java.util.Date()
public var Long.date5: java.util.Date = java.util.Date()
@@ -0,0 +1,14 @@
package test
internal var jet.Long.date1 : java.lang.Object
internal fun jet.Long.<get-date1>() : java.lang.Object
internal fun jet.Long.<set-date1>(/*0*/ <set-?> : java.lang.Object) : Unit
internal var jet.Long.date12 : java.lang.Object
internal fun jet.Long.<get-date12>() : java.lang.Object
internal fun jet.Long.<set-date12>(/*0*/ <set-?> : java.lang.Object) : Unit
private var jet.Long.date3 : java.util.Date
private fun jet.Long.<get-date3>() : java.util.Date
private fun jet.Long.<set-date3>(/*0*/ <set-?> : java.util.Date) : Unit
public var jet.Long.date5 : java.util.Date
public fun jet.Long.<get-date5>() : java.util.Date
public fun jet.Long.<set-date5>(/*0*/ <set-?> : java.util.Date) : Unit
@@ -0,0 +1,28 @@
package test
var Long.date1: Object = java.util.Date()
set
var Long.date2: Object = java.util.Date()
protected set
var Long.date3: Object = java.util.Date()
private set
private var Long.date4: java.util.Date = java.util.Date()
set
public var Long.date7: java.util.Date = java.util.Date()
set
public var Long.date8: java.util.Date = java.util.Date()
internal set
public var Long.date9: java.util.Date = java.util.Date()
private set
public var Long.date10: java.util.Date = java.util.Date()
protected set
public var Long.date11: java.util.Date = java.util.Date()
public set
@@ -0,0 +1,29 @@
package test
internal var jet.Long.date1 : java.lang.Object
internal fun jet.Long.<get-date1>() : java.lang.Object
internal fun jet.Long.<set-date1>(/*0*/ <set-?> : java.lang.Object) : Unit
public var jet.Long.date10 : java.util.Date
public fun jet.Long.<get-date10>() : java.util.Date
protected fun jet.Long.<set-date10>(/*0*/ <set-?> : java.util.Date) : Unit
public var jet.Long.date11 : java.util.Date
public fun jet.Long.<get-date11>() : java.util.Date
public fun jet.Long.<set-date11>(/*0*/ <set-?> : java.util.Date) : Unit
internal var jet.Long.date2 : java.lang.Object
internal fun jet.Long.<get-date2>() : java.lang.Object
protected fun jet.Long.<set-date2>(/*0*/ <set-?> : java.lang.Object) : Unit
internal var jet.Long.date3 : java.lang.Object
internal fun jet.Long.<get-date3>() : java.lang.Object
private fun jet.Long.<set-date3>(/*0*/ <set-?> : java.lang.Object) : Unit
private var jet.Long.date4 : java.util.Date
private fun jet.Long.<get-date4>() : java.util.Date
private fun jet.Long.<set-date4>(/*0*/ <set-?> : java.util.Date) : Unit
public var jet.Long.date7 : java.util.Date
public fun jet.Long.<get-date7>() : java.util.Date
public fun jet.Long.<set-date7>(/*0*/ <set-?> : java.util.Date) : Unit
public var jet.Long.date8 : java.util.Date
public fun jet.Long.<get-date8>() : java.util.Date
internal fun jet.Long.<set-date8>(/*0*/ <set-?> : java.util.Date) : Unit
public var jet.Long.date9 : java.util.Date
public fun jet.Long.<get-date9>() : java.util.Date
private fun jet.Long.<set-date9>(/*0*/ <set-?> : java.util.Date) : Unit
@@ -6,4 +6,4 @@ var p: M<X> = throw Exception()
// method: _DefaultPackage::setP
// jvm signature: (LM;)V
// generic signature: (LM<-LX;>;)V
// kotlin signature: null
// kotlin signature: (LM<LX;>;)null
@@ -6,4 +6,4 @@ var p: M<X> = throw Exception()
// method: _DefaultPackage::setP
// jvm signature: (LM;)V
// generic signature: (LM<+LX;>;)V
// kotlin signature: null
// kotlin signature: (LM<LX;>;)null
@@ -36,7 +36,16 @@ import static org.jetbrains.jet.test.util.NamespaceComparator.compareNamespaces;
*/
@SuppressWarnings("JUnitTestCaseWithNoTests")
public abstract class AbstractLoadCompiledKotlinTest extends TestCaseWithTmpdir {
public void doTest(@NotNull String ktFileName) throws Exception {
doTest(ktFileName, false);
}
public void doTestWithAccessors(@NotNull String ktFileName) throws Exception {
doTest(ktFileName, true);
}
private void doTest(@NotNull String ktFileName, boolean includeAccessors) throws Exception {
File ktFile = new File(ktFileName);
File txtFile = new File(ktFileName.replaceFirst("\\.kt$", ".txt"));
AnalyzeExhaust exhaust = compileKotlinToDirAndGetAnalyzeExhaust(ktFile, tmpdir, getTestRootDisposable(),
@@ -49,6 +58,9 @@ public abstract class AbstractLoadCompiledKotlinTest extends TestCaseWithTmpdir
NamespaceDescriptor namespaceFromClass = LoadDescriptorUtil.loadTestNamespaceAndBindingContextFromJavaRoot(
tmpdir, getTestRootDisposable(), ConfigurationKind.JDK_ONLY).first;
compareNamespaces(namespaceFromSource, namespaceFromClass,
NamespaceComparator.DONT_INCLUDE_METHODS_OF_OBJECT.checkPrimaryConstructors(true), txtFile);
NamespaceComparator.DONT_INCLUDE_METHODS_OF_OBJECT
.checkPrimaryConstructors(true)
.checkPropertyAccessors(includeAccessors),
txtFile);
}
}
File diff suppressed because it is too large Load Diff
@@ -42,14 +42,18 @@ public abstract class AbstractLazyResolveNamespaceComparingTest extends KotlinTe
}
protected void doTestCheckingPrimaryConstructors(String testFileName) throws IOException {
doTest(testFileName, true);
doTest(testFileName, true, false);
}
protected void doTestCheckingPrimaryConstructorsAndAccessors(String testFileName) throws IOException {
doTest(testFileName, true, true);
}
protected void doTestNotCheckingPrimaryConstructors(String testFileName) throws IOException {
doTest(testFileName, false);
doTest(testFileName, false, false);
}
private void doTest(String testFileName, boolean checkPrimaryConstructors) throws IOException {
private void doTest(String testFileName, boolean checkPrimaryConstructors, boolean checkPropertyAccessors) throws IOException {
List<JetFile> files = JetTestUtils
.createTestFiles(testFileName, FileUtil.loadFile(new File(testFileName), true),
new JetTestUtils.TestFileFactory<JetFile>() {
@@ -75,6 +79,6 @@ public abstract class AbstractLazyResolveNamespaceComparingTest extends KotlinTe
public boolean apply(FqNameUnsafe fqName) {
return !KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.toUnsafe().equals(fqName);
}
}).checkPrimaryConstructors(checkPrimaryConstructors), serializeResultsTo);
}).checkPrimaryConstructors(checkPrimaryConstructors).checkPropertyAccessors(checkPropertyAccessors), serializeResultsTo);
}
}
@@ -45,8 +45,8 @@ import java.util.Collections;
import java.util.List;
public class NamespaceComparator {
public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, Predicates.<FqNameUnsafe>alwaysTrue());
public static final Configuration RECURSIVE = new Configuration(false, true, Predicates.<FqNameUnsafe>alwaysTrue());
public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, false, Predicates.<FqNameUnsafe>alwaysTrue());
public static final Configuration RECURSIVE = new Configuration(false, false, true, Predicates.<FqNameUnsafe>alwaysTrue());
private static final DescriptorRenderer RENDERER = new DescriptorRendererBuilder()
.setWithDefinedIn(false)
@@ -98,6 +98,22 @@ public class NamespaceComparator {
printer.popIndent().println("}");
}
}
else if (conf.checkPropertyAccessors && descriptor instanceof PropertyDescriptor) {
printer.printlnWithNoIndent();
printer.pushIndent();
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
if (getter != null) {
printer.println(RENDERER.render(getter));
}
PropertySetterDescriptor setter = propertyDescriptor.getSetter();
if (setter != null) {
printer.println(RENDERER.render(setter));
}
printer.popIndent();
}
else {
printer.printlnWithNoIndent();
}
@@ -198,25 +214,32 @@ public class NamespaceComparator {
public static class Configuration {
private final boolean checkPrimaryConstructors;
private final boolean checkPropertyAccessors;
private final boolean includeMethodsOfJavaObject;
private final Predicate<FqNameUnsafe> recurseIntoPackage;
public Configuration(
boolean checkPrimaryConstructors,
boolean checkPropertyAccessors,
boolean includeMethodsOfJavaObject,
Predicate<FqNameUnsafe> recurseIntoPackage
) {
this.checkPrimaryConstructors = checkPrimaryConstructors;
this.checkPropertyAccessors = checkPropertyAccessors;
this.includeMethodsOfJavaObject = includeMethodsOfJavaObject;
this.recurseIntoPackage = recurseIntoPackage;
}
public Configuration filterRecursion(@NotNull Predicate<FqNameUnsafe> recurseIntoPackage) {
return new Configuration(checkPrimaryConstructors, includeMethodsOfJavaObject, recurseIntoPackage);
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage);
}
public Configuration checkPrimaryConstructors(boolean checkPrimaryConstructors) {
return new Configuration(checkPrimaryConstructors, includeMethodsOfJavaObject, recurseIntoPackage);
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage);
}
public Configuration checkPropertyAccessors(boolean checkPropertyAccessors) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage);
}
}
}
@@ -140,7 +140,7 @@ public class GenerateTests {
"compiler/tests/",
"LoadCompiledKotlinTestGenerated",
AbstractLoadCompiledKotlinTest.class,
testModel("compiler/testData/loadKotlin")
testModel("compiler/testData/loadKotlin", "doTestWithAccessors")
);
generateTest(
@@ -199,7 +199,7 @@ public class GenerateTests {
"compiler/tests/",
"LazyResolveNamespaceComparingTestGenerated",
AbstractLazyResolveNamespaceComparingTest.class,
testModel("compiler/testData/loadKotlin", "doTestCheckingPrimaryConstructors"),
testModel("compiler/testData/loadKotlin", "doTestCheckingPrimaryConstructorsAndAccessors"),
testModel("compiler/testData/loadJava/compiledJavaCompareWithKotlin", "doTestNotCheckingPrimaryConstructors"),
testModel("compiler/testData/lazyResolve/namespaceComparator", "doTestCheckingPrimaryConstructors")
);