1 /*
<lambda>null2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.room.vo
18 
19 import androidx.room.compiler.codegen.CodeLanguage
20 import androidx.room.compiler.codegen.XCodeBlock
21 import androidx.room.compiler.codegen.XCodeBlock.Builder.Companion.applyTo
22 import androidx.room.compiler.processing.XType
23 import androidx.room.ext.capitalize
24 import androidx.room.solver.CodeGenScope
25 import androidx.room.solver.types.StatementValueReader
26 import java.util.Locale
27 
28 data class PropertySetter(
29     val propertyName: String,
30     val jvmName: String,
31     val type: XType,
32     val callType: CallType
33 ) {
34     fun writeSet(ownerVar: String, inVar: String, builder: XCodeBlock.Builder) {
35         if (callType == CallType.CONSTRUCTOR) {
36             return
37         }
38         builder.applyTo { language ->
39             when (language) {
40                 CodeLanguage.JAVA -> {
41                     val stmt =
42                         when (callType) {
43                             CallType.PROPERTY -> "%L.%L = %L"
44                             CallType.FUNCTION,
45                             CallType.SYNTHETIC_FUNCTION -> "%L.%L(%L)"
46                             else -> error("Unknown call type: $callType")
47                         }
48                     addStatement(stmt, ownerVar, jvmName, inVar)
49                 }
50                 CodeLanguage.KOTLIN -> addStatement("%L.%L = %L", ownerVar, propertyName, inVar)
51             }
52         }
53     }
54 
55     fun writeSetFromStatement(
56         ownerVar: String,
57         stmtVar: String,
58         indexVar: String,
59         reader: StatementValueReader,
60         scope: CodeGenScope
61     ) {
62         when (scope.language) {
63             CodeLanguage.JAVA ->
64                 when (callType) {
65                     CallType.PROPERTY -> {
66                         val outPropertyName = "$ownerVar.$jvmName"
67                         reader.readFromStatement(outPropertyName, stmtVar, indexVar, scope)
68                     }
69                     CallType.FUNCTION,
70                     CallType.SYNTHETIC_FUNCTION -> {
71                         val tmpProperty =
72                             scope.getTmpVar("_tmp${propertyName.capitalize(Locale.US)}")
73                         scope.builder.apply {
74                             addLocalVariable(tmpProperty, type.asTypeName())
75                             reader.readFromStatement(tmpProperty, stmtVar, indexVar, scope)
76                             addStatement("%L.%L(%L)", ownerVar, jvmName, tmpProperty)
77                         }
78                     }
79                     CallType.CONSTRUCTOR -> {
80                         // no code, property is set via constructor
81                     }
82                 }
83             CodeLanguage.KOTLIN ->
84                 when (callType) {
85                     CallType.PROPERTY,
86                     CallType.SYNTHETIC_FUNCTION -> {
87                         val outPropertyName = "$ownerVar.$propertyName"
88                         reader.readFromStatement(outPropertyName, stmtVar, indexVar, scope)
89                     }
90                     CallType.FUNCTION -> {
91                         val tmpProperty =
92                             scope.getTmpVar("_tmp${propertyName.capitalize(Locale.US)}")
93                         scope.builder.apply {
94                             addLocalVariable(tmpProperty, type.asTypeName())
95                             reader.readFromStatement(tmpProperty, stmtVar, indexVar, scope)
96                             addStatement("%L.%L(%L)", ownerVar, jvmName, tmpProperty)
97                         }
98                     }
99                     CallType.CONSTRUCTOR -> {
100                         // no code, property is set via constructor
101                     }
102                 }
103         }
104     }
105 }
106