diff options
author | George Mount <mount@google.com> | 2015-09-29 14:33:38 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-09-29 14:33:38 +0000 |
commit | d7a88db363a66f8afe51d92f029fd139f5fc6cd3 (patch) | |
tree | 36fbadb853cb76e5e77aff77823bae3e0f0119e1 /compiler/src/main | |
parent | b90752c49ab83e20c7cd8fe27da94117ad332df8 (diff) | |
parent | 7c1b078ca84336caba7f811709836562bd5550d6 (diff) | |
download | data-binding-d7a88db363a66f8afe51d92f029fd139f5fc6cd3.tar.gz |
Merge "Add context as an automatic variable." into studio-master-dev
Diffstat (limited to 'compiler/src/main')
3 files changed, 70 insertions, 0 deletions
diff --git a/compiler/src/main/java/android/databinding/tool/LayoutBinder.java b/compiler/src/main/java/android/databinding/tool/LayoutBinder.java index b2457350..fc31affc 100644 --- a/compiler/src/main/java/android/databinding/tool/LayoutBinder.java +++ b/compiler/src/main/java/android/databinding/tool/LayoutBinder.java @@ -172,12 +172,23 @@ public class LayoutBinder implements FileScopeProvider { mBundle = layoutBundle; mModulePackage = layoutBundle.getModulePackage(); // copy over data. + boolean addContext = true; for (ResourceBundle.VariableDeclaration variable : mBundle.getVariables()) { addVariable(variable.name, variable.type, variable.location, variable.declared); + if ("context".equals(variable.name)) { + addContext = false; + } } for (ResourceBundle.NameTypeLocation userImport : mBundle.getImports()) { mExprModel.addImport(userImport.name, userImport.type, userImport.location); + if ("context".equals(userImport.name)) { + addContext = false; + } + } + if (addContext) { + mExprModel.builtInVariable("context", "android.content.Context", + "getRoot().getContext()"); } for (String javaLangClass : sJavaLangClasses) { mExprModel.addImport(javaLangClass, "java.lang." + javaLangClass, null); diff --git a/compiler/src/main/java/android/databinding/tool/expr/BuiltInVariableExpr.java b/compiler/src/main/java/android/databinding/tool/expr/BuiltInVariableExpr.java new file mode 100644 index 00000000..d9167bfa --- /dev/null +++ b/compiler/src/main/java/android/databinding/tool/expr/BuiltInVariableExpr.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.databinding.tool.expr; + +import android.databinding.tool.writer.KCode; + +import java.util.ArrayList; +import java.util.List; + +public class BuiltInVariableExpr extends IdentifierExpr { + private final String mAccessCode; + + BuiltInVariableExpr(String name, String type, String accessCode) { + super(name); + super.setUserDefinedType(type); + this.mAccessCode = accessCode; + } + + @Override + public boolean isDynamic() { + return false; + } + + @Override + protected List<Dependency> constructDependencies() { + return new ArrayList<>(); + } + + @Override + protected KCode generateCode() { + if (mAccessCode == null) { + return new KCode().app(mName); + } else { + return new KCode().app(mAccessCode); + } + } + + public boolean isDeclared() { + return false; + } +} diff --git a/compiler/src/main/java/android/databinding/tool/expr/ExprModel.java b/compiler/src/main/java/android/databinding/tool/expr/ExprModel.java index 57d19cf2..5939510f 100644 --- a/compiler/src/main/java/android/databinding/tool/expr/ExprModel.java +++ b/compiler/src/main/java/android/databinding/tool/expr/ExprModel.java @@ -162,6 +162,10 @@ public class ExprModel { return register(new StaticIdentifierExpr(name)); } + public BuiltInVariableExpr builtInVariable(String name, String type, String accessCode) { + return register(new BuiltInVariableExpr(name, type, accessCode)); + } + /** * Creates a static identifier for the given class or returns the existing one. */ |