Debian Codegraph @

compiler/src/main/java/android/databinding/tool/expr/MethodReferenceExpr.java

raw
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
17 package android.databinding.tool.expr;
18
19 import android.databinding.tool.reflection.ModelAnalyzer;
20 import android.databinding.tool.reflection.ModelClass;
21 import android.databinding.tool.util.L;
22 import android.databinding.tool.util.Preconditions;
23 import android.databinding.tool.writer.KCode;
24
25 public class MethodReferenceExpr extends MethodBaseExpr {
26
27 MethodReferenceExpr(Expr parent, String name) {
28 super(parent, name);
29 }
30
31 @Override
32 protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
33 return modelAnalyzer.findClass(Object.class);
34 }
35
36 @Override
37 protected String computeUniqueKey() {
38 return join(mName, "::", getTarget().getUniqueKey());
39 }
40
41 @Override
42 public String getInvertibleError() {
43 return "Listeners do not support two-way binding";
44 }
45
46 @Override
47 public boolean isDynamic() {
48 return true;
49 }
50
51 @Override
52 public Expr resolveListeners(ModelClass listener, Expr parent) {
53 try {
54 return resolveListenersAsMethodReference(listener, parent);
55 } catch (IllegalStateException e) {
56 L.e("%s", e.getMessage());
57 return this;
58 }
59 }
60
61 @Override
62 protected KCode generateCode() {
63 // once we can deprecate using Field.access for callbacks, we can get rid of this since
64 // it will be detected when resolve type is run.
65 Preconditions.check(false, "Cannot generate code for unresolved method reference %s", this);
66 return null;
67 }
68
69 @Override
70 public Expr generateInverse(ExprModel model, Expr value, String bindingClassName) {
71 Preconditions.check(false, "Method references do not have an inverse");
72 return this;
73 }
74
75 @Override
76 public Expr cloneToModel(ExprModel model) {
77 final Expr clonedTarget = getTarget().cloneToModel(model);
78 return model.methodReference(clonedTarget, mName);
79 }
80
81 @Override
82 public String toString() {
83 return getTarget().toString() + "::" + mName;
84 }
85 }