| 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.solver.ExecutionPath; |
| 22 |
import android.databinding.tool.writer.KCode; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
/** |
| 28 |
* This is used by inverse field access expressions to assign back to the field. |
| 29 |
* For example, <code>@={a.b}</code> is inverted to @{code a.b = value;} |
| 30 |
*/ |
| 31 |
public class FieldAssignmentExpr extends Expr { |
| 32 |
final String mName; |
| 33 |
|
| 34 |
public FieldAssignmentExpr(Expr target, String name, Expr value) { |
| 35 |
super(target, value); |
| 36 |
mName = name; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
protected String computeUniqueKey() { |
| 41 |
return join(getTarget().getUniqueKey(), mName, "=", getValueExpr().getUniqueKey()); |
| 42 |
} |
| 43 |
|
| 44 |
public Expr getTarget() { |
| 45 |
return getChildren().get(0); |
| 46 |
} |
| 47 |
|
| 48 |
public Expr getValueExpr() { |
| 49 |
return getChildren().get(1); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) { |
| 54 |
getTarget().resolveType(modelAnalyzer); |
| 55 |
return modelAnalyzer.findClass(void.class); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
protected List<Dependency> constructDependencies() { |
| 60 |
return constructDynamicChildrenDependencies(); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected KCode generateCode() { |
| 65 |
return new KCode() |
| 66 |
.app("", getTarget().toCode()) |
| 67 |
.app("." + mName + " = ", getValueExpr().toCode()); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public Expr cloneToModel(ExprModel model) { |
| 72 |
return model.assignment(getTarget().cloneToModel(model), mName, getValueExpr()); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
protected String getInvertibleError() { |
| 77 |
return "Assignment expressions are inverses of field access expressions."; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public List<ExecutionPath> toExecutionPath(List<ExecutionPath> paths) { |
| 82 |
Expr child = getTarget(); |
| 83 |
List<ExecutionPath> targetPaths = child.toExecutionPath(paths); |
| 84 |
|
| 85 |
// after this, we need a null check. |
| 86 |
List<ExecutionPath> result = new ArrayList<ExecutionPath>(); |
| 87 |
if (child instanceof StaticIdentifierExpr) { |
| 88 |
result.addAll(toExecutionPathInOrder(paths, child)); |
| 89 |
} else { |
| 90 |
for (ExecutionPath path : targetPaths) { |
| 91 |
final ComparisonExpr cmp = getModel() |
| 92 |
.comparison("!=", child, getModel().symbol("null", Object.class)); |
| 93 |
path.addPath(cmp); |
| 94 |
final ExecutionPath subPath = path.addBranch(cmp, true); |
| 95 |
if (subPath != null) { |
| 96 |
subPath.addPath(this); |
| 97 |
result.add(subPath); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public String toString() { |
| 106 |
return getTarget().toString() + '.' + mName + " = " + getValueExpr(); |
| 107 |
} |
| 108 |
} |