| 1 |
/* |
| 2 |
* Copyright (C) 2015 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.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 |
import android.databinding.tool.writer.LayoutBinderWriterKt; |
| 25 |
|
| 26 |
import com.google.common.collect.Lists; |
| 27 |
|
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
public class IdentifierExpr extends Expr { |
| 32 |
String mName; |
| 33 |
String mUserDefinedType; |
| 34 |
private boolean mIsDeclared; |
| 35 |
|
| 36 |
IdentifierExpr(String name) { |
| 37 |
mName = name; |
| 38 |
} |
| 39 |
|
| 40 |
public String getName() { |
| 41 |
return mName; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* If this is root, its type should be set while parsing the XML document |
| 46 |
* @param userDefinedType The type of this identifier |
| 47 |
*/ |
| 48 |
public void setUserDefinedType(String userDefinedType) { |
| 49 |
mUserDefinedType = userDefinedType; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected String computeUniqueKey() { |
| 54 |
return join(mName, super.computeUniqueKey()); |
| 55 |
} |
| 56 |
|
| 57 |
public String getUserDefinedType() { |
| 58 |
return mUserDefinedType; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public boolean isDynamic() { |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
protected ModelClass resolveType(final ModelAnalyzer modelAnalyzer) { |
| 68 |
Preconditions.checkNotNull(mUserDefinedType, ErrorMessages.UNDEFINED_VARIABLE, mName); |
| 69 |
return modelAnalyzer.findClass(mUserDefinedType, getModel().getImports()); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
protected List<Dependency> constructDependencies() { |
| 74 |
return new ArrayList<Dependency>(); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
protected String asPackage() { |
| 79 |
return mUserDefinedType == null ? mName : null; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected KCode generateCode() { |
| 84 |
return new KCode(LayoutBinderWriterKt.scopedName(this)); |
| 85 |
} |
| 86 |
|
| 87 |
public void setDeclared() { |
| 88 |
mIsDeclared = true; |
| 89 |
} |
| 90 |
|
| 91 |
public boolean isDeclared() { |
| 92 |
return mIsDeclared; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public String getInvertibleError() { |
| 97 |
return null; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public Expr generateInverse(ExprModel model, Expr value, String bindingClassName) { |
| 102 |
String thisType = bindingClassName + ".this"; |
| 103 |
Expr target = model.builtInVariable(thisType, bindingClassName, thisType); |
| 104 |
return model.methodCall(target, LayoutBinderWriterKt.getSetterName(this), |
| 105 |
Lists.newArrayList(value)); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public Expr cloneToModel(ExprModel model) { |
| 110 |
return model.identifier(mName); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public String toString() { |
| 115 |
return mName; |
| 116 |
} |
| 117 |
} |