--- /dev/null
+/*
+ * 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 com.android.databinding.testapp;
+
+import com.android.databinding.testapp.generated.CastTestBinder;
+
+import android.support.v4.util.ArrayMap;
+import android.test.UiThreadTest;
+
+import java.util.ArrayList;
+
+public class CastTest extends BaseDataBinderTest<CastTestBinder> {
+ ArrayList<String> mValues = new ArrayList<>();
+ ArrayMap<String, String> mMap = new ArrayMap<>();
+
+ public CastTest() {
+ super(CastTestBinder.class, R.layout.cast_test);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ try {
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ mValues.clear();
+ mValues.add("hello");
+ mValues.add("world");
+ mValues.add("not seen");
+ mMap.clear();
+ mMap.put("hello", "world");
+ mMap.put("world", "hello");
+ mBinder.setList(mValues);
+ mBinder.setMap(mMap);
+ mBinder.rebindDirty();
+ }
+ });
+ } catch (Throwable throwable) {
+ throw new Exception(throwable);
+ }
+ }
+
+ @UiThreadTest
+ public void testCast() throws Throwable {
+ assertEquals("hello", mBinder.getTextView0().getText().toString());
+ assertEquals("world", mBinder.getTextView1().getText().toString());
+ }
+}
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+ <variable name="list" type="java.util.Collection<java.lang.String>"/>
+ <variable name="map" type="java.lang.Object"/>
+
+ <TextView
+ android:id="@+id/textView0"
+ android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:text="@{((java.util.ArrayList<java.lang.String>)list)[0]}"/>
+ <TextView
+ android:id="@+id/textView1"
+ android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:text="@{((java.util.Map<java.lang.String,java.lang.String>)map)[`hello`]}"/>
+</LinearLayout>
\ No newline at end of file
import com.android.databinding.reflection.ModelAnalyzer;
+import org.apache.commons.io.IOUtils;
+
import android.binding.Bindable;
import java.io.File;
properties = (Intermediate) in.readObject();
}
} catch (IOException e) {
- System.err.println("Could not read Binding properties intermediate file: " +
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "Could not read Binding properties intermediate file: " +
e.getLocalizedMessage());
} catch (ClassNotFoundException e) {
- System.err.println("Could not read Binding properties intermediate file: " +
- e.getLocalizedMessage());
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "Could not read Binding properties intermediate file: " +
+ e.getLocalizedMessage());
} finally {
try {
if (in != null) {
.getResources(resourcePath);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
- System.out.println("Merging binding adapters from " + url);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
+ "Merging binding adapters from " + url);
InputStream inputStream = null;
try {
inputStream = url.openStream();
properties.captureProperties(intermediateProperties);
}
} catch (IOException e) {
- System.err.println("Could not merge in Bindables from " + url + ": " +
- e.getLocalizedMessage());
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "Could not merge in Bindables from " + url + ": " +
+ e.getLocalizedMessage());
} catch (ClassNotFoundException e) {
- System.err.println("Could not read Binding properties intermediate file: " +
- e.getLocalizedMessage());
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "Could not read Binding properties intermediate file: " +
+ e.getLocalizedMessage());
} finally {
- try {
- inputStream.close();
- } catch (IOException e2) {
- System.err.println("Error closing intermediate Bindables store: " +
- e2.getLocalizedMessage());
- }
+ IOUtils.closeQuietly(inputStream);
}
}
} catch (IOException e) {
- System.err.println("Could not read Binding properties intermediate file: " +
- e.getLocalizedMessage());
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "Could not read Binding properties intermediate file: " +
+ e.getLocalizedMessage());
}
}
return mModel.bracketExpr(visit(ctx.expression(0)), visit(ctx.expression(1)));
}
+ @Override
+ public Expr visitCastOp(@NotNull BindingExpressionParser.CastOpContext ctx) {
+ return mModel.castExpr(ctx.type().getText(), visit(ctx.expression()));
+ }
+
// @Override
// public Expr visitIdentifier(@NotNull BindingExpressionParser.IdentifierContext ctx) {
// final String identifier = ctx.Identifier().getText();
--- /dev/null
+/*
+ * 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 com.android.databinding.expr;
+
+import com.android.databinding.reflection.ModelAnalyzer;
+import com.android.databinding.reflection.ModelClass;
+
+import java.util.List;
+
+public class CastExpr extends Expr {
+
+ final String mType;
+
+ CastExpr(String type, Expr expr) {
+ super(expr);
+ mType = type;
+ }
+
+ @Override
+ protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
+ return modelAnalyzer.findClass(mType);
+ }
+
+ @Override
+ protected List<Dependency> constructDependencies() {
+ final List<Dependency> dependencies = constructDynamicChildrenDependencies();
+ for (Dependency dependency : dependencies) {
+ dependency.setMandatory(true);
+ }
+ return dependencies;
+ }
+
+ protected String computeUniqueKey() {
+ return sUniqueKeyJoiner.join(mType, getCastExpr().computeUniqueKey());
+ }
+
+ public Expr getCastExpr() {
+ return getChildren().get(0);
+ }
+
+ public String getCastType() {
+ return mType;
+ }
+}
return register(new BracketExpr(variableExpr, argExpr));
}
+ public Expr castExpr(String type, Expr expr) {
+ return register(new CastExpr(type, expr));
+ }
+
public List<Expr> getBindingExpressions() {
return mBindingExpressions;
}
declaredType = (DeclaredType) typeElement.asType();
} else {
int templateCloseIndex = className.lastIndexOf('>');
- String paramStr = className.substring(templateOpenIndex + 1, templateCloseIndex - 1);
+ String paramStr = className.substring(templateOpenIndex + 1, templateCloseIndex);
Elements elementUtils = getElementUtils();
String baseClassName = className.substring(0, templateOpenIndex);
openCount--;
}
}
+ index++;
}
list.add(arg.toString());
return list;
TypeMirror foundInterface = null;
if (typeUtil.isSameType(interfaceType, typeUtil.erasure(mTypeMirror))) {
foundInterface = mTypeMirror;
- } else if (mTypeMirror.getKind() == TypeKind.DECLARED) {
- DeclaredType declaredType = (DeclaredType) mTypeMirror;
- TypeElement typeElement = (TypeElement) declaredType.asElement();
- for (TypeMirror type : typeElement.getInterfaces()) {
- if (typeUtil.isSameType(interfaceType, typeUtil.erasure(type))) {
- foundInterface = type;
+ } else {
+ ArrayList<TypeMirror> toCheck = new ArrayList<>();
+ toCheck.add(mTypeMirror);
+ while (!toCheck.isEmpty()) {
+ TypeMirror typeMirror = toCheck.remove(0);
+ if (typeUtil.isSameType(interfaceType, typeUtil.erasure(typeMirror))) {
+ foundInterface = typeMirror;
break;
+ } else {
+ toCheck.addAll(typeUtil.directSupertypes(typeMirror));
}
}
if (foundInterface == null) {
", but it isn't a declared type: " + foundInterface);
return null;
}
+ System.err.println("found interface: " + foundInterface);
return (DeclaredType) foundInterface;
}
@Override
public boolean isAssignableFrom(ModelClass that) {
+ if (that == null) {
+ return false;
+ }
TypeMirror thatType = ((AnnotationClass)that).mTypeMirror;
return getTypeUtils().isAssignable(thatType, mTypeMirror);
}
*/
package com.android.databinding.store;
+import com.android.databinding.reflection.AnnotationAnalyzer;
import com.android.databinding.reflection.ModelAnalyzer;
import com.android.databinding.reflection.ModelClass;
import com.android.databinding.reflection.ModelMethod;
}
return new SetterStore(modelAnalyzer, store);
} catch (IOException e) {
- System.err.println("Could not read SetterStore intermediate file: " +
+ printMessage(Diagnostic.Kind.ERROR, "Could not read SetterStore intermediate file: " +
e.getLocalizedMessage());
e.printStackTrace();
} catch (ClassNotFoundException e) {
- System.err.println("Could not read SetterStore intermediate file: " +
+ printMessage(Diagnostic.Kind.ERROR, "Could not read SetterStore intermediate file: " +
e.getLocalizedMessage());
e.printStackTrace();
}
Filer filer = processingEnvironment.getFiler();
FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT,
SetterStore.class.getPackage().getName(), "setter_store.bin");
- processingEnvironment.getMessager().printMessage(Diagnostic.Kind.NOTE,
- "============= Writing intermediate file: " + resource.getName());
+ printMessage(Diagnostic.Kind.NOTE, "============= Writing intermediate file: " +
+ resource.getName());
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(resource.openOutputStream());
}
} catch (Exception e) {
- System.out.println("Unknown class: " + key.valueType);
+ printMessage(Diagnostic.Kind.NOTE, "Unknown class: " + key.valueType);
}
}
} catch (Exception e) {
- System.out.println("Unknown class: " + key.viewType);
+ printMessage(Diagnostic.Kind.NOTE, "Unknown class: " + key.viewType);
}
}
}
break;
}
} catch (Exception e) {
- //System.out.println("Unknown class: " + className);
+ //printMessage(Diagnostic.Kind.NOTE, "Unknown class: " + className);
}
}
}
return conversion.get(toClassName);
}
} catch (Exception e) {
- System.out.println("Unknown class: " + toClassName);
+ printMessage(Diagnostic.Kind.NOTE, "Unknown class: " + toClassName);
}
}
}
} catch (Exception e) {
- System.out.println("Unknown class: " + fromClassName);
+ printMessage(Diagnostic.Kind.NOTE, "Unknown class: " + fromClassName);
}
}
}
}
}
+ private static void printMessage(Diagnostic.Kind kind, String message) {
+ ModelAnalyzer modelAnalyzer = ModelAnalyzer.getInstance();
+ if (modelAnalyzer instanceof AnnotationAnalyzer) {
+ ((AnnotationAnalyzer) modelAnalyzer).printMessage(kind, message);
+ } else {
+ System.out.println(message);
+ }
+ }
+
private static class MethodDescription implements Serializable {
private static final long serialVersionUID = 1;
import com.android.databinding.expr.ResourceExpr
import com.android.databinding.expr.BracketExpr
import com.android.databinding.reflection.Callable
+import com.android.databinding.expr.CastExpr
fun String.stripNonJava() = this.split("[^a-zA-Z0-9]").map{ it.trim() }.joinToCamelCaseAsVar()
}
}
}
+ is CastExpr -> kcode("") {
+ app("(", it.getCastType())
+ app(") ", it.getCastExpr().toCode())
+ }
else -> kcode("//NOT IMPLEMENTED YET")
}