Add assert for null provided fields, fixes #7024
Added a test to ensure the assertion happens.
Repost from Rietveld issue 1604803
Thanks to stephenh for the original patch!
Review at http://gwt-code-reviews.appspot.com/1602805
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10783 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/uibinder/rebind/AbstractFieldWriter.java b/user/src/com/google/gwt/uibinder/rebind/AbstractFieldWriter.java
index 6ad0394..929dd55 100644
--- a/user/src/com/google/gwt/uibinder/rebind/AbstractFieldWriter.java
+++ b/user/src/com/google/gwt/uibinder/rebind/AbstractFieldWriter.java
@@ -255,6 +255,9 @@
} else {
w.write("final %s %s = %s;", getQualifiedSourceName(), name, initializer);
}
+ if (ownerField != null && ownerField.isProvided() && !designTime.isDesignTime()) {
+ w.write("assert %1$s != null : \"UiField %1$s with 'provided = true' was null\";", name);
+ }
w.write("// Setup section.");
for (String s : statements) {
diff --git a/user/test/com/google/gwt/uibinder/LazyWidgetBuilderSuite.java b/user/test/com/google/gwt/uibinder/LazyWidgetBuilderSuite.java
index 0a339b8..7198fed 100644
--- a/user/test/com/google/gwt/uibinder/LazyWidgetBuilderSuite.java
+++ b/user/test/com/google/gwt/uibinder/LazyWidgetBuilderSuite.java
@@ -22,6 +22,7 @@
import com.google.gwt.uibinder.test.client.LazyWidgetBuilderSafeUriIntegrationTest;
import com.google.gwt.uibinder.test.client.SafeHtmlAsComponentsTest;
import com.google.gwt.uibinder.test.client.UiBinderParserUiWithAttributesTest;
+import com.google.gwt.uibinder.test.client.UiProvidedNullTest;
import com.google.gwt.uibinder.test.client.UiRendererEventsTest;
import com.google.gwt.uibinder.test.client.UiRendererTest;
@@ -41,6 +42,7 @@
suite.addTestSuite(LazyWidgetBuilderSafeUriIntegrationTest.class);
suite.addTestSuite(SafeHtmlAsComponentsTest.class);
suite.addTestSuite(UiBinderParserUiWithAttributesTest.class);
+ suite.addTestSuite(UiProvidedNullTest.class);
suite.addTestSuite(UiRendererTest.class);
suite.addTestSuite(UiRendererEventsTest.class);
diff --git a/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullTest.java b/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullTest.java
new file mode 100644
index 0000000..6fbfdbb
--- /dev/null
+++ b/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * 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.google.gwt.uibinder.test.client;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Test UiFields(provided = true) give meaningful errors when a field
+ * is not initialized. This test is only meant to be run when
+ * assertions are turned on.
+ */
+public class UiProvidedNullTest extends GWTTestCase {
+
+ boolean assertionsEnabled;
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.uibinder.test.LazyWidgetBuilderSuite";
+ }
+
+ @Override
+ public void gwtSetUp() {
+ try {
+ assert false;
+ assertionsEnabled = false;
+ } catch (AssertionError e) {
+ assertionsEnabled = true;
+ }
+ }
+
+ public void testNullFieldAssertion() {
+ if (!assertionsEnabled) {
+ return;
+ }
+ try {
+ new UiProvidedNullUi();
+ fail("Expected an Assertion Error");
+ } catch (AssertionError e) {
+ assertEquals("UiField myButton with 'provided = true' was null", e.getMessage());
+ } catch (NullPointerException e) {
+ fail("Expected an Assertion Error");
+ }
+ }
+
+}
diff --git a/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullUi.java b/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullUi.java
new file mode 100644
index 0000000..00c7710
--- /dev/null
+++ b/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullUi.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * 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.google.gwt.uibinder.test.client;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.uibinder.client.UiField;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.VerticalPanel;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * {@code UiBinder} with an uninitialized field.
+ */
+public class UiProvidedNullUi extends Composite {
+
+ interface Binder extends UiBinder<Widget, UiProvidedNullUi> {
+ }
+ private static final Binder binder = GWT.create(Binder.class);
+
+ @UiField VerticalPanel root;
+ @UiField(provided = true)
+ Button myButton;
+
+ public UiProvidedNullUi() {
+ initWidget(binder.createAndBindUi(this));
+ }
+}
diff --git a/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullUi.ui.xml b/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullUi.ui.xml
new file mode 100644
index 0000000..f3e7908
--- /dev/null
+++ b/user/test/com/google/gwt/uibinder/test/client/UiProvidedNullUi.ui.xml
@@ -0,0 +1,22 @@
+<!--
+ Copyright 2011 Google Inc.
+ Licensed under the Apache License, Version 2.0 (the "License"); you
+ may not use this file except in compliance with the License. You may
+ 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. License for the specific language governing permissions and
+ limitations under the License.
+-->
+<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
+ xmlns:gwt='urn:import:com.google.gwt.user.client.ui'
+>
+
+<gwt:VerticalPanel ui:field="root">
+ <gwt:Button ui:field='myButton'></gwt:Button>
+</gwt:VerticalPanel>
+</ui:UiBinder>