CallbackWrapperWriter.kt revision 6047998943beebd81e0ae1068df39c0cbee38628
1/*
2 * 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
17package android.databinding.tool.writer
18
19import android.databinding.tool.CallbackWrapper
20
21fun CallbackWrapper.allArgsWithTypes() =
22        "int ${CallbackWrapper.SOURCE_ID} ${method.parameterTypes.withIndex().map { ", ${it.value.toJavaCode()} ${CallbackWrapper.ARG_PREFIX}${it.index}" }.joinToString("")}"
23
24fun CallbackWrapper.argsWithTypes() =
25        method.parameterTypes.withIndex().map { "${it.value.toJavaCode()} ${CallbackWrapper.ARG_PREFIX}${it.index}" }.joinToString(", ")
26
27fun CallbackWrapper.args() =
28        method.parameterTypes.withIndex().map { "${CallbackWrapper.ARG_PREFIX}${it.index}" }.joinToString(", ")
29
30fun CallbackWrapper.allArgs() =
31        "mSourceId ${method.parameterTypes.withIndex().map { ", ${CallbackWrapper.ARG_PREFIX}${it.index}" }.joinToString("")}"
32
33/**
34 * For any listener type we see, we create a class that can wrap around it. This wrapper has an
35 * interface which is implemented by the ViewDataBinding.
36 */
37public class CallbackWrapperWriter(val wrapper: CallbackWrapper) {
38
39    public fun write() = kcode("") {
40        with(wrapper) {
41            @Suppress("RemoveCurlyBracesFromTemplate")
42            app("package ${`package`};")
43            val extendsImplements = if (klass.isInterface) {
44                "implements"
45            } else {
46                "extends"
47            }
48            block("public final class $className $extendsImplements ${klass.canonicalName}") {
49                // declare the actual listener interface
50                nl("final $listenerInterfaceName mListener;")
51                nl("final int mSourceId;")
52                block("public $className($listenerInterfaceName listener, int sourceId)") {
53                    nl("mListener = listener;")
54                    nl("mSourceId = sourceId;")
55                }
56                nl("")
57                nl("@Override")
58                block("public ${method.returnType.canonicalName} ${method.name}(${wrapper.argsWithTypes()})") {
59                    val evaluate = "mListener.$listenerMethodName(${wrapper.allArgs()});"
60                    if (method.returnType.isVoid) {
61                        nl("$evaluate")
62                    } else {
63                        nl("return $evaluate")
64                    }
65                }
66                nl("")
67                block("public interface $listenerInterfaceName") {
68                    nl("${method.returnType} $listenerMethodName(${wrapper.allArgsWithTypes()});")
69                }
70            }
71        }
72    }.generate()
73}