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));
}
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));
}
}
}
}
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;
}
Aggregations