Search in sources :

Example 1 with KtParameter

use of org.jetbrains.kotlin.psi.KtParameter in project kotlin by JetBrains.

the class JsDataClassGenerator method generateCopyFunction.

@Override
public void generateCopyFunction(@NotNull FunctionDescriptor function, @NotNull List<? extends KtParameter> constructorParameters) {
    JsFunction functionObj = generateJsMethod(function);
    assert function.getValueParameters().size() == constructorParameters.size();
    List<JsExpression> constructorArguments = new ArrayList<JsExpression>(constructorParameters.size());
    for (int i = 0; i < constructorParameters.size(); i++) {
        KtParameter constructorParam = constructorParameters.get(i);
        ValueParameterDescriptor parameterDescriptor = (ValueParameterDescriptor) BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.VALUE_PARAMETER, constructorParam);
        PropertyDescriptor propertyDescriptor = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.VALUE_PARAMETER_AS_PROPERTY, parameterDescriptor);
        JsName fieldName = context.getNameForDescriptor(propertyDescriptor);
        JsName paramName = context.getNameForDescriptor(parameterDescriptor);
        functionObj.getParameters().add(new JsParameter(paramName));
        JsExpression argumentValue;
        JsExpression parameterValue = new JsNameRef(paramName);
        if (!constructorParam.hasValOrVar()) {
            assert !DescriptorUtilsKt.hasDefaultValue(function.getValueParameters().get(i));
            // Caller cannot rely on default value and pass undefined here.
            argumentValue = parameterValue;
        } else {
            JsExpression defaultCondition = JsAstUtils.equality(new JsNameRef(paramName), Namer.getUndefinedExpression());
            argumentValue = new JsConditional(defaultCondition, new JsNameRef(fieldName, JsLiteral.THIS), parameterValue);
        }
        constructorArguments.add(argumentValue);
    }
    ClassDescriptor classDescriptor = (ClassDescriptor) function.getContainingDeclaration();
    ClassConstructorDescriptor constructor = classDescriptor.getConstructors().iterator().next();
    JsExpression constructorRef = context.getInnerReference(constructor);
    JsNew returnExpression = new JsNew(constructorRef, constructorArguments);
    if (context.shouldBeDeferred(constructor)) {
        context.deferConstructorCall(constructor, returnExpression.getArguments());
    }
    functionObj.getBody().getStatements().add(new JsReturn(returnExpression));
}
Also used : ArrayList(java.util.ArrayList) KtParameter(org.jetbrains.kotlin.psi.KtParameter)

Example 2 with KtParameter

use of org.jetbrains.kotlin.psi.KtParameter in project kotlin by JetBrains.

the class CompileTimeConstantUtils method checkConstructorParametersType.

public static void checkConstructorParametersType(@NotNull List<KtParameter> parameters, @NotNull BindingTrace trace) {
    for (KtParameter parameter : parameters) {
        VariableDescriptor parameterDescriptor = trace.getBindingContext().get(VALUE_PARAMETER, parameter);
        if (parameterDescriptor == null)
            continue;
        KotlinType parameterType = parameterDescriptor.getType();
        KtTypeReference typeReference = parameter.getTypeReference();
        if (typeReference != null) {
            if (parameterType.isMarkedNullable()) {
                trace.report(NULLABLE_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
            } else if (!isAcceptableTypeForAnnotationParameter(parameterType)) {
                trace.report(INVALID_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
            }
        }
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KtParameter(org.jetbrains.kotlin.psi.KtParameter) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) VariableDescriptor(org.jetbrains.kotlin.descriptors.VariableDescriptor)

Example 3 with KtParameter

use of org.jetbrains.kotlin.psi.KtParameter in project kotlin by JetBrains.

the class ClassInitializerTranslator method translatePrimaryConstructorParameters.

@NotNull
private List<JsParameter> translatePrimaryConstructorParameters() {
    List<KtParameter> parameterList = getPrimaryConstructorParameters(classDeclaration);
    List<JsParameter> result = new ArrayList<JsParameter>();
    for (KtParameter jetParameter : parameterList) {
        result.add(translateParameter(jetParameter));
    }
    return result;
}
Also used : KtParameter(org.jetbrains.kotlin.psi.KtParameter) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

KtParameter (org.jetbrains.kotlin.psi.KtParameter)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)1 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)1 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1