| 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 |
package android.databinding.tool.expr; |
| 17 |
|
| 18 |
import android.databinding.tool.CallbackWrapper; |
| 19 |
import android.databinding.tool.processing.ErrorMessages; |
| 20 |
import android.databinding.tool.reflection.ModelAnalyzer; |
| 21 |
import android.databinding.tool.reflection.ModelClass; |
| 22 |
import android.databinding.tool.util.Preconditions; |
| 23 |
import android.databinding.tool.writer.KCode; |
| 24 |
|
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
/** |
| 29 |
* This expressions that are used to reference arguments in callbacks. |
| 30 |
* <p |
| 31 |
* While the callback is being parsed, they get whatever the variable user defined in the lambda. |
| 32 |
* When the code is being generated, they get simple enumarated names so that multiple callbacks |
| 33 |
* can be handled in the same method. |
| 34 |
*/ |
| 35 |
public class CallbackArgExpr extends IdentifierExpr { |
| 36 |
|
| 37 |
private int mArgIndex; |
| 38 |
|
| 39 |
private String mName; |
| 40 |
|
| 41 |
private ModelClass mClassFromCallback; |
| 42 |
|
| 43 |
public CallbackArgExpr(int argIndex, String name) { |
| 44 |
super(name); |
| 45 |
mArgIndex = argIndex; |
| 46 |
mName = name; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public boolean isDynamic() { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
public void setClassFromCallback(ModelClass modelClass) { |
| 55 |
mClassFromCallback = modelClass; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) { |
| 60 |
Preconditions |
| 61 |
.checkNotNull(mClassFromCallback, ErrorMessages.UNDEFINED_CALLBACK_ARGUMENT, mName); |
| 62 |
return mClassFromCallback; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
protected List<Dependency> constructDependencies() { |
| 67 |
return Collections.emptyList(); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
protected KCode generateCode() { |
| 72 |
return new KCode(CallbackWrapper.ARG_PREFIX + mArgIndex); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
protected String computeUniqueKey() { |
| 77 |
return CallbackWrapper.ARG_PREFIX + mArgIndex; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public String getInvertibleError() { |
| 82 |
return "Callback arguments cannot be inverted"; |
| 83 |
} |
| 84 |
|
| 85 |
public String getName() { |
| 86 |
return mName; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public Expr cloneToModel(ExprModel model) { |
| 91 |
return new CallbackArgExpr(mArgIndex, mName); |
| 92 |
} |
| 93 |
} |