1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * Unless required by applicable law or agreed to in writing, software 8 * distributed under the License is distributed on an "AS IS" BASIS, 9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 * See the License for the specific language governing permissions and 11 * limitations under the License. 12 */ 13 14 package android.databinding.tool.writer 15 16 import android.databinding.tool.expr.* 17 import android.databinding.tool.reflection.Callable 18 19 class CodeGenUtil { 20 companion object { toCodenull21 fun toCode(it : Expr, full : Boolean) : KCode { 22 if (it.isDynamic() && !full) { 23 return kcode(it.executePendingLocalName) 24 } 25 return when (it) { 26 is ComparisonExpr -> kcode("") { 27 app("", it.getLeft().toCode()) 28 app(" ", it.getOp()).app(" ") 29 app("", it.getRight().toCode()) 30 } 31 is InstanceOfExpr -> kcode("") { 32 app("", it.getExpr().toCode()) 33 app(" instanceof ") 34 app("", it.getType().toJavaCode()) 35 } 36 is FieldAccessExpr -> kcode("") { 37 if (it.isListener()) { 38 app("(") 39 if (it.getMinApi() > 1) { 40 app("", "(getBuildSdkInt() < ${it.getMinApi()}) ? null : ") 41 } 42 if (it.getChild().isDynamic()) { 43 val value = it.getChild().toCode().generate(); 44 app("", "((${it.fieldName} == null) ? (${it.fieldName} = (new ${it.listenerClassName}()).setValue(${value})) : ${it.fieldName}.setValue(${value}))") 45 } else { 46 app("", "((${it.fieldName} == null) ? (${it.fieldName} = new ${it.listenerClassName}()) : ${it.fieldName})") 47 } 48 app(")") 49 } else { 50 app("", it.getChild().toCode()) 51 if (it.getGetter().type == Callable.Type.FIELD) { 52 app(".", it.getGetter().name) 53 } else { 54 app(".", it.getGetter().name).app("()") 55 } 56 } 57 } 58 is GroupExpr -> kcode("(").app("", it.getWrapped().toCode()).app(")") 59 is StaticIdentifierExpr -> kcode(it.getResolvedType().toJavaCode()) 60 is IdentifierExpr -> kcode(it.executePendingLocalName) 61 is MathExpr -> kcode("") { 62 app("", it.getLeft().toCode()) 63 app(it.getOp()) 64 app("", it.getRight().toCode()) 65 } 66 is UnaryExpr -> kcode("") { 67 app(it.getOp(), it.getExpr().toCode()) 68 } 69 is BitShiftExpr -> kcode("") { 70 app("", it.getLeft().toCode()) 71 app(it.getOp()) 72 app("", it.getRight().toCode()) 73 } 74 is MethodCallExpr -> kcode("") { 75 app("", it.getTarget().toCode()) 76 app(".", it.getGetter().name) 77 app("(") 78 var first = true 79 it.getArgs().forEach { 80 apps(if (first) "" else ",", it.toCode()) 81 first = false 82 } 83 app(")") 84 } 85 is SymbolExpr -> kcode(it.getText()) // TODO 86 is TernaryExpr -> kcode("") { 87 app("", it.getPred().toCode()) 88 app(" ? ", it.getIfTrue().toCode()) 89 app(" : ", it.getIfFalse().toCode()) 90 } 91 is ResourceExpr -> kcode("") { 92 app("", it.toJava()) 93 } 94 is BracketExpr -> kcode("") { 95 app("", it.getTarget().toCode()) 96 val bracketType = it.getAccessor()!! 97 when (bracketType) { 98 BracketExpr.BracketAccessor.ARRAY -> { 99 app("[", it.getArg().toCode()) 100 app("]") 101 } 102 BracketExpr.BracketAccessor.LIST -> { 103 app(".get(") 104 if (it.argCastsInteger()) { 105 app("(Integer)") 106 } 107 app("", it.getArg().toCode()) 108 app(")") 109 } 110 BracketExpr.BracketAccessor.MAP -> { 111 app(".get(", it.getArg().toCode()) 112 app(")") 113 } 114 } 115 } 116 is CastExpr -> kcode("") { 117 app("(", it.getCastType()) 118 app(") ", it.getCastExpr().toCode()) 119 } 120 is ArgListExpr -> throw IllegalStateException("should never try to convert an argument expressions into code"); 121 else -> kcode("//NOT IMPLEMENTED YET") 122 } 123 } 124 } 125 }