Public: Generate BeanDescriptors for GWT Validation.
[JSR 303 TCK Result] 31 of 258 (12.02%) Pass with 10 Failures and 13 Errors.
Review at http://gwt-code-reviews.appspot.com/1212801
Review by: rchandia@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9421 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/ConstraintInheritanceTest.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/ConstraintInheritanceTest.java
new file mode 100644
index 0000000..357d78e
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/ConstraintInheritanceTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010 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.sample.validationtck.constraints.inheritance;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Test wrapper for
+ * {@link org.hibernate.jsr303.tck.tests.constraints.inheritance.ConstraintInheritanceTest}
+ * .
+ */
+public class ConstraintInheritanceTest extends GWTTestCase {
+ private final org.hibernate.jsr303.tck.tests.constraints.inheritance.ConstraintInheritanceTest delegate =
+ new org.hibernate.jsr303.tck.tests.constraints.inheritance.ConstraintInheritanceTest();
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.sample.validationtck.constraints.inheritance.TckTest";
+ }
+
+ public void testConstraintsOnInterfaceAreInherited() {
+ delegate.testConstraintsOnInterfaceAreInherited();
+ }
+
+ public void testConstraintsOnSuperClassAreInherited() {
+ delegate.testConstraintsOnSuperClassAreInherited();
+ }
+}
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/ConstraintsInheritanceGwtSuite.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/ConstraintsInheritanceGwtSuite.java
new file mode 100644
index 0000000..42a38e4
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/ConstraintsInheritanceGwtSuite.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2010 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.sample.validationtck.constraints.inheritance;
+
+import com.google.gwt.junit.tools.GWTTestSuite;
+
+import junit.framework.Test;
+
+/**
+ * Tck Tests for the {@code constraints inheritance} package.
+ */
+public class ConstraintsInheritanceGwtSuite {
+ public static Test suite() {
+ GWTTestSuite suite = new GWTTestSuite(
+ "TCK for GWT Validation, constraints inheritance package");
+ suite.addTestSuite(ConstraintInheritanceTest.class);
+ return suite;
+ }
+}
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/TckTest.gwt.xml b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/TckTest.gwt.xml
new file mode 100644
index 0000000..a28c2fe
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/TckTest.gwt.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.0.1/distro-source/core/src/gwt-module.dtd">
+<!--
+ Copyright 2010 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.
+-->
+<module>
+ <inherits name="com.google.gwt.sample.validationtck.ValidationTck" />
+ <source path="">
+ <include name="*.java" />
+ <exclude name="super" />
+ </source>
+ <replace-with class="com.google.gwt.sample.validationtck.constraints.inheritance.TckTestValidator">
+ <when-type-is class="javax.validation.Validator"/>
+ </replace-with>
+</module>
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/TckTestValidator.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/TckTestValidator.java
new file mode 100644
index 0000000..dd13747
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/inheritance/TckTestValidator.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010 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.sample.validationtck.constraints.inheritance;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.validation.client.AbstractValidator;
+import com.google.gwt.validation.client.GwtValidation;
+
+import org.hibernate.jsr303.tck.tests.constraints.inheritance.Bar;
+
+import javax.validation.Validator;
+
+/**
+ * Validator implementation that uses
+ * {@link com.google.gwt.validation.client.GwtValidation GwtValidation}.
+ */
+public final class TckTestValidator extends AbstractValidator {
+ /**
+ * Marker Interface to {@link GWT#create(Class)}.
+ */
+ @GwtValidation(value = {Bar.class})
+ public static interface GwtValidator extends Validator {
+ }
+
+ public TckTestValidator() {
+ super((Validator) GWT.create(GwtValidator.class));
+ }
+}
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/BeanDescriptorTest.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/BeanDescriptorTest.java
new file mode 100644
index 0000000..e08b65f
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/BeanDescriptorTest.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2010 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.sample.validationtck.metadata;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Test wrapper for
+ * {@link org.hibernate.jsr303.tck.tests.constraints.groups.GroupTest}.
+ */
+public class BeanDescriptorTest extends GWTTestCase {
+ private final org.hibernate.jsr303.tck.tests.metadata.BeanDescriptorTest delegate =
+ new org.hibernate.jsr303.tck.tests.metadata.BeanDescriptorTest();
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.sample.validationtck.metadata.TckTest";
+ }
+
+ public void testGetConstrainedProperties() {
+ delegate.testGetConstrainedProperties();
+ }
+
+ public void testGetConstrainedPropertiesForUnconstrainedEntity() {
+ delegate.testGetConstrainedPropertiesForUnconstrainedEntity();
+ }
+
+ public void testGetConstraintForConstrainedProperty() {
+ delegate.testGetConstraintForConstrainedProperty();
+ }
+
+ public void testGetConstraintForUnConstrainedProperty() {
+ delegate.testGetConstraintForUnConstrainedProperty();
+ }
+
+ public void testGetConstraintsForNonExistingProperty() {
+ delegate.testGetConstraintsForNonExistingProperty();
+ }
+
+ public void testGetConstraintsForNullProperty() {
+ delegate.testGetConstraintsForNullProperty();
+ }
+
+ public void testIsBeanConstrainedDueToConstraintOnEntity() {
+ delegate.testIsBeanConstrainedDueToConstraintOnEntity();
+ }
+
+ public void testIsBeanConstrainedDueToConstraintOnInterface() {
+ delegate.testIsBeanConstrainedDueToConstraintOnInterface();
+ }
+
+ public void testIsBeanConstrainedDueToConstraintProperty() {
+ delegate.testIsBeanConstrainedDueToConstraintProperty();
+ }
+
+ public void testIsBeanConstrainedDueToValidAnnotation() {
+ delegate.testIsBeanConstrainedDueToValidAnnotation();
+ }
+
+ public void testUnconstrainedClass() {
+ delegate.testUnconstrainedClass();
+ }
+}
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/MetadataGwtSuite.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/MetadataGwtSuite.java
new file mode 100644
index 0000000..22298c2
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/MetadataGwtSuite.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2010 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.sample.validationtck.metadata;
+
+import com.google.gwt.junit.tools.GWTTestSuite;
+
+import junit.framework.Test;
+
+/**
+ * Tck Tests for the {@code metadata} package.
+ */
+public class MetadataGwtSuite {
+ public static Test suite() {
+ GWTTestSuite suite = new GWTTestSuite(
+ "TCK for GWT Validation, metadata package");
+ suite.addTestSuite(BeanDescriptorTest.class);
+ return suite;
+ }
+}
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/TckTest.gwt.xml b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/TckTest.gwt.xml
new file mode 100644
index 0000000..93f82e8
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/TckTest.gwt.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.0.1/distro-source/core/src/gwt-module.dtd">
+<!--
+ Copyright 2010 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.
+-->
+<module>
+ <inherits name="com.google.gwt.sample.validationtck.ValidationTck" />
+ <source path="">
+ <include name="*.java" />
+ <exclude name="super" />
+ </source>
+ <replace-with class="com.google.gwt.sample.validationtck.metadata.TckTestValidator">
+ <when-type-is class="javax.validation.Validator"/>
+ </replace-with>
+</module>
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/TckTestValidator.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/TckTestValidator.java
new file mode 100644
index 0000000..4c5cb7d
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/metadata/TckTestValidator.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2010 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.sample.validationtck.metadata;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.validation.client.AbstractValidator;
+import com.google.gwt.validation.client.GwtValidation;
+
+import org.hibernate.jsr303.tck.tests.metadata.Account;
+import org.hibernate.jsr303.tck.tests.metadata.Customer;
+import org.hibernate.jsr303.tck.tests.metadata.Man;
+import org.hibernate.jsr303.tck.tests.metadata.Order;
+import org.hibernate.jsr303.tck.tests.metadata.UnconstraintEntity;
+
+import javax.validation.Validator;
+
+/**
+ * Test Validator for {@link MetadataGwtSuite}.
+ */
+public final class TckTestValidator extends AbstractValidator {
+ /**
+ * Marker Interface to {@link GWT#create(Class)}.
+ */
+ @GwtValidation(value = {
+ Account.class, Customer.class, Man.class, Order.class,
+ UnconstraintEntity.class})
+ public static interface GwtValidator extends Validator {
+ }
+
+ public TckTestValidator() {
+ super((Validator) GWT.create(GwtValidator.class));
+ }
+}
diff --git a/user/src/com/google/gwt/validation/client/impl/AbstractBeanDescriptor.java b/user/src/com/google/gwt/validation/client/impl/AbstractBeanDescriptor.java
deleted file mode 100644
index 9f42398..0000000
--- a/user/src/com/google/gwt/validation/client/impl/AbstractBeanDescriptor.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Copyright 2010 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.validation.client.impl;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import javax.validation.metadata.ConstraintDescriptor;
-import javax.validation.metadata.PropertyDescriptor;
-
-/**
- * Abstract BeanDescriptor for use by generated {@link GwtBeanDescriptor}.
- * <p>
- * Subclasses are expected to call setDescriptorMap from the constructor.
- *
- * @param <T> the bean Type
- */
-public abstract class AbstractBeanDescriptor<T> implements GwtBeanDescriptor<T> {
-
- private final Class<T> clazz;
-
- private final Set<ConstraintDescriptor<?>> constraints = new HashSet<ConstraintDescriptor<?>>();
- private final Map<String, PropertyDescriptor> descriptor = new HashMap<String, PropertyDescriptor>();
-
- /**
- * @param clazz
- */
- public AbstractBeanDescriptor(Class<T> clazz) {
- super();
- this.clazz = clazz;
- }
-
- public ConstraintFinder findConstraints() {
- // TODO(nchalko) implement
- return null;
- }
-
- public Set<PropertyDescriptor> getConstrainedProperties() {
- return new HashSet<PropertyDescriptor>(descriptor.values());
- }
-
- public Set<ConstraintDescriptor<?>> getConstraintDescriptors() {
- // Copy for safety
- return new HashSet<ConstraintDescriptor<?>>(constraints);
- }
-
- public PropertyDescriptor getConstraintsForProperty(String propertyName) {
- return descriptor.get(propertyName);
- }
-
- public Class<?> getElementClass() {
- return clazz;
- }
-
- public boolean hasConstraints() {
- return !constraints.isEmpty();
- }
-
- public boolean isBeanConstrained() {
- return true;
- }
-
- protected void setDescriptorMap(Map<String, PropertyDescriptor> map) {
- descriptor.clear();
- descriptor.putAll(map);
- constraints.clear();
- for (PropertyDescriptor p : descriptor.values()) {
- if (p.hasConstraints()) {
- constraints.addAll(p.getConstraintDescriptors());
- }
- }
- }
-}
\ No newline at end of file
diff --git a/user/src/com/google/gwt/validation/client/impl/GwtBeanDescriptorImpl.java b/user/src/com/google/gwt/validation/client/impl/GwtBeanDescriptorImpl.java
new file mode 100644
index 0000000..591a1b1
--- /dev/null
+++ b/user/src/com/google/gwt/validation/client/impl/GwtBeanDescriptorImpl.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2010 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.validation.client.impl;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.validation.metadata.ConstraintDescriptor;
+import javax.validation.metadata.PropertyDescriptor;
+
+/**
+ * Abstract BeanDescriptor for use by generated {@link GwtBeanDescriptor}.
+ * <p>
+ * Subclasses are expected to call setDescriptorMap from the constructor.
+ *
+ * @param <T> the bean Type
+ */
+public final class GwtBeanDescriptorImpl<T> implements GwtBeanDescriptor<T> {
+
+ /**
+ * Builder for {@link GwtBeanDescriptors}.
+ *
+ * @param <T> the bean Type
+ */
+ public static final class Builder<T> {
+
+ private final Class<T> clazz;
+ private final Map<String, PropertyDescriptor> descriptorMap = new HashMap<String, PropertyDescriptor>();
+ private boolean isConstrained;
+
+ private Builder(Class<T> clazz) {
+ this.clazz = clazz;
+ }
+
+ public GwtBeanDescriptorImpl<T> build() {
+ return new GwtBeanDescriptorImpl<T>(clazz, isConstrained, descriptorMap);
+ }
+
+ public Builder<T> put(String key, PropertyDescriptor value) {
+ descriptorMap.put(key, value);
+ return this;
+ }
+
+ public Builder<T> setConstrained(boolean isConstrained) {
+ this.isConstrained = isConstrained;
+ return this;
+ }
+ }
+
+ public static <T> Builder<T> builder(Class<T> clazz) {
+ return new Builder<T>(clazz);
+ }
+
+ private final Class<T> clazz;
+ private final Set<ConstraintDescriptor<?>> constraints = new HashSet<ConstraintDescriptor<?>>();
+
+ private final Map<String, PropertyDescriptor> descriptorMap = new HashMap<String, PropertyDescriptor>();
+ private final boolean isBeanConstrained;
+
+ private GwtBeanDescriptorImpl(Class<T> clazz, boolean isConstrained,
+ Map<String, PropertyDescriptor> descriptorMap) {
+ super();
+ this.clazz = clazz;
+ this.isBeanConstrained = isConstrained;
+ this.descriptorMap.putAll(descriptorMap);
+ }
+
+ public ConstraintFinder findConstraints() {
+ // TODO(nchalko) implement
+ return null;
+ }
+
+ public Set<PropertyDescriptor> getConstrainedProperties() {
+ return new HashSet<PropertyDescriptor>(descriptorMap.values());
+ }
+
+ public Set<ConstraintDescriptor<?>> getConstraintDescriptors() {
+ // Copy for safety
+ return new HashSet<ConstraintDescriptor<?>>(constraints);
+ }
+
+ public PropertyDescriptor getConstraintsForProperty(String propertyName) {
+ return descriptorMap.get(propertyName);
+ }
+
+ public Class<?> getElementClass() {
+ return clazz;
+ }
+
+ public boolean hasConstraints() {
+ return !constraints.isEmpty();
+ }
+
+ public boolean isBeanConstrained() {
+ return isBeanConstrained;
+ }
+
+ protected void setDescriptorMap(Map<String, PropertyDescriptor> map) {
+ descriptorMap.clear();
+ descriptorMap.putAll(map);
+ constraints.clear();
+ for (PropertyDescriptor p : descriptorMap.values()) {
+ if (p.hasConstraints()) {
+ constraints.addAll(p.getConstraintDescriptors());
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/user/src/com/google/gwt/validation/rebind/BeanHelper.java b/user/src/com/google/gwt/validation/rebind/BeanHelper.java
index cd9d2e3..e3c6d84 100644
--- a/user/src/com/google/gwt/validation/rebind/BeanHelper.java
+++ b/user/src/com/google/gwt/validation/rebind/BeanHelper.java
@@ -153,7 +153,7 @@
}
public String getValidatorName() {
- return makeJavaSafe(jClass.getName() + "Validator");
+ return makeJavaSafe("_" + jClass.getName() + "Validator");
}
@Override
diff --git a/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java b/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
index 0c08cf1..a7efab0 100644
--- a/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
+++ b/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
@@ -19,6 +19,7 @@
import com.google.gwt.core.client.UnsafeNativeLong;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.TreeLogger.Type;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JField;
@@ -37,19 +38,23 @@
import com.google.gwt.validation.client.impl.AbstractGwtSpecificValidator;
import com.google.gwt.validation.client.impl.ConstraintDescriptorImpl;
import com.google.gwt.validation.client.impl.GwtBeanDescriptor;
+import com.google.gwt.validation.client.impl.GwtBeanDescriptorImpl;
import com.google.gwt.validation.client.impl.GwtValidationContext;
+import com.google.gwt.validation.client.impl.PropertyDescriptorImpl;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintViolation;
import javax.validation.Payload;
+import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;
@@ -228,11 +233,16 @@
return annotation;
}
- private Class<? extends ConstraintValidator<? extends Annotation, ?>> getValidatorForType(
- ConstraintDescriptor<? extends Annotation> constraint, Class<?> clazz) {
+ private <T extends Annotation> Class<? extends ConstraintValidator<T, ?>> getValidatorForType(
+ ConstraintDescriptor<T> constraint, Class<?> clazz) {
// TODO(nchalko) implement per spec
- Class<? extends ConstraintValidator<? extends Annotation, ?>> validatorClass =
- constraint.getConstraintValidatorClasses().get(0);
+
+ List<Class<? extends ConstraintValidator<T, ?>>> constraintValidatorClasses
+ = constraint.getConstraintValidatorClasses();
+ if (constraintValidatorClasses.isEmpty()) {
+ return null;
+ }
+ Class<? extends ConstraintValidator<T, ?>> validatorClass = constraintValidatorClasses.get(0);
return validatorClass;
}
@@ -256,6 +266,12 @@
}
}
+ private boolean isIterable(Class<?> elementClass) {
+ // TODO(nchalko) handle iterables everywhere this is called.
+ return elementClass.isArray()
+ || Iterable.class.isAssignableFrom(elementClass);
+ }
+
private boolean isPropertyConstrained(BeanHelper helper, PropertyDescriptor p) {
Set<PropertyDescriptor> propertyDescriptors =
helper.getBeanDescriptor().getConstrainedProperties();
@@ -292,6 +308,42 @@
return "validateProperty_get" + p.getPropertyName();
}
+ private void writeBeanDescriptor(SourceWriter sw) {
+ BeanDescriptor beanDescriptor = beanHelper.getBeanDescriptor();
+
+ // GwtBeanDescriptor <MyBean> beanDescriptor =
+ sw.print(GwtBeanDescriptor.class.getCanonicalName());
+ sw.print("<" + beanHelper.getTypeCanonicalName() + ">");
+ sw.println(" beanDescriptor = ");
+ sw.indent();
+ sw.indent();
+
+ // GwtBeanDescriptorImpl.builder(Order.class)
+ sw.print(GwtBeanDescriptorImpl.class.getCanonicalName());
+ sw.println(".builder(" + beanHelper.getTypeCanonicalName() + ".class)");
+ sw.indent();
+ sw.indent();
+
+ // .setConstrained(true)
+ sw.println(".setConstrained(" + beanDescriptor.isBeanConstrained() + ")");
+
+ // .put("myProperty", myProperty_pd)
+ for (PropertyDescriptor p : beanDescriptor.getConstrainedProperties()) {
+ sw.print(".put(\"");
+ sw.print(p.getPropertyName());
+ sw.print("\", ");
+ sw.print(p.getPropertyName());
+ sw.println("_pd)");
+ }
+
+ // .build();
+ sw.println(".build();");
+ sw.outdent();
+ sw.outdent();
+ sw.outdent();
+ sw.outdent();
+ }
+
private void writeConstraintDescriptor(SourceWriter sw,
ConstraintDescriptor<? extends Annotation> constraint,
String constraintDescripotorVar) throws UnableToCompleteException {
@@ -388,32 +440,34 @@
}
private void writeFields(SourceWriter sw) throws UnableToCompleteException {
- // MyBeanDescriptor beanDescriptor = GWT.create(MyBeanDescriptor);
- sw.println(GwtBeanDescriptor.class.getCanonicalName());
- // TODO(nchalko) implement BeanDescriptor Generator
- sw.println(" beanDescriptor = null; //TODO(nchalko) GWT.create");
// Create a variable for each constraint of each property
for (PropertyDescriptor p :
beanHelper.getBeanDescriptor().getConstrainedProperties()) {
int count = 0;
for (ConstraintDescriptor<?> constraint : p.getConstraintDescriptors()) {
- count++; // index starts at one.
writeConstraintDescriptor(sw, constraint,
constraintDescriptorVar(p.getPropertyName(), count));
+ count++; // index starts at zero.
}
- if (p.isCascaded()) {
+ writePropertyDescriptor(sw, p);
+ if (p.isCascaded() && !isIterable(p.getElementClass())) {
beansToValidate.add(createBeanHelper(p.getElementClass()));
}
}
+ // Now write the BeanDescriptor after we already have the PropertyDescriptor
+ writeBeanDescriptor(sw);
+ sw.println();
+
// Create a variable for each constraint of this class.
int count = 0;
for (ConstraintDescriptor<?> constraint :
beanHelper.getBeanDescriptor().getConstraintDescriptors()) {
- count++; // index starts at one.
+
writeConstraintDescriptor(sw, constraint,
constraintDescriptorVar("this", count));
+ count++; // index starts at zero.
}
}
@@ -540,6 +594,48 @@
sw.outdent();
}
+ /**
+ * @param sw
+ * @param p
+ */
+ private void writePropertyDescriptor(SourceWriter sw, PropertyDescriptor p) {
+ // PropertyDescriptor myProperty_pd =
+ sw.print(PropertyDescriptor.class.getCanonicalName());
+ sw.print(" ");
+ sw.print(p.getPropertyName());
+ sw.println("_pd =");
+ sw.indent();
+ sw.indent();
+
+ // new PropertyDescriptorImpl(
+ sw.println("new " + PropertyDescriptorImpl.class.getCanonicalName() + "(");
+ sw.indent();
+ sw.indent();
+
+ // "myProperty",
+ sw.println("\"" + p.getPropertyName() + "\",");
+
+ // MyType.class,
+ sw.println(p.getElementClass().getCanonicalName() + ".class,");
+
+ // isCascaded,
+ sw.print(Boolean.toString(p.isCascaded()));
+
+ // myProperty_c0,
+ // myProperty_c1 );
+ int size = p.getConstraintDescriptors().size();
+ for (int i = 0; i < size; i++) {
+ sw.println(","); // Print the , for the previous line
+ sw.print(constraintDescriptorVar(p.getPropertyName(), i));
+ }
+ sw.println(");");
+
+ sw.outdent();
+ sw.outdent();
+ sw.outdent();
+ sw.outdent();
+ }
+
private void writePropertyValidators(SourceWriter sw) {
for (PropertyDescriptor p :
beanHelper.getBeanDescriptor().getConstrainedProperties()) {
@@ -591,9 +687,10 @@
int count = 0;
Class<?> clazz = beanHelper.getClazz();
for (ConstraintDescriptor<?> constraint : beanHelper.getBeanDescriptor().findConstraints().getConstraintDescriptors()) {
- count++; // index starts at 1
+
Class<? extends ConstraintValidator<? extends Annotation, ?>> validatorClass = getValidatorForType(
constraint, clazz);
+ if (validatorClass != null) {
// TODO(nchalko) handle constraint.isReportAsSingleViolation() and
// hasComposingConstraints
@@ -607,6 +704,12 @@
// safe
sw.print(constraintDescriptorVar("this", count));
sw.println(", groups);");
+ } else {
+ // TODO(nchalko) What does the spec say to do here.
+ logger.log(Type.WARN, "No ConstraintValidator of " + constraint
+ + " for type " + clazz);
+ }
+ count++; // index starts at 0
}
// validate all super classes and interfaces
@@ -783,6 +886,8 @@
private void writeValidatePropertyMethod(SourceWriter sw,
PropertyDescriptor p, boolean useField) {
+ Class<?> elementClass = p.getElementClass();
+
// private final <T> void validateProperty_{get}<p>(
sw.print("private final <T> void ");
if (useField) {
@@ -798,14 +903,15 @@
sw.println("GwtValidationContext<T> context,");
sw.println("Set<ConstraintViolation<T>> violations,");
sw.println(beanHelper.getTypeCanonicalName() + " object,");
- sw.print(p.getElementClass().getCanonicalName());
+ sw.print(elementClass.getCanonicalName());
sw.println(" value,");
sw.println("Class<?>... groups) {");
sw.outdent();
// TODO(nchalko) move this out of here to the Validate method
- if (p.isCascaded()) {
- BeanHelper helper = createBeanHelper(p.getElementClass());
+ if (p.isCascaded() && !isIterable(elementClass)) {
+
+ BeanHelper helper = createBeanHelper(elementClass);
// if(value != null) {
sw.println("if(value != null) {");
@@ -823,27 +929,33 @@
int count = 0;
for (ConstraintDescriptor<?> constraint : p.getConstraintDescriptors()) {
- count++; // index starts at 1
-
Annotation annotation = getAnnotation(p, useField, constraint);
if (annotation != null) {
// TODO(nchalko) check for annotation equality
Class<? extends ConstraintValidator<? extends Annotation, ?>> validatorClass = getValidatorForType(
- constraint, p.getElementClass());
- // TODO(nchalko) handle constraint.isReportAsSingleViolation() and
- // hasComposingConstraints
+ constraint, elementClass);
+ if (validatorClass == null) {
+ // TODO(nchalko) What does the spec say to do here.
+ logger.log(Type.WARN, "No ConstraintValidator of " + constraint
+ + " for " + p.getPropertyName() + " of type " + elementClass);
- // validate(context, violations, object, value, new MyValidator(),
- // constraintDescriptor, groups);
- sw.print("validate(context, violations, object, value, ");
- sw.print("new ");
- sw.print(validatorClass.getCanonicalName());
- sw.print("(), "); // new one each time because validators are not thread
- // safe
- sw.print(constraintDescriptorVar(p.getPropertyName(), count));
- sw.println(", groups);");
+ } else {
+ // TODO(nchalko) handle constraint.isReportAsSingleViolation() and
+ // hasComposingConstraints
+
+ // validate(context, violations, object, value, new MyValidator(),
+ // constraintDescriptor, groups);
+ sw.print("validate(context, violations, object, value, ");
+ sw.print("new "); // new one each time because validators are not
+ // thread safe
+ sw.print(validatorClass.getCanonicalName());
+ sw.print("(), ");
+ sw.print(constraintDescriptorVar(p.getPropertyName(), count));
+ sw.println(", groups);");
+ }
}
+ count++; // index starts at zero
}
sw.outdent();
sw.println("}");
@@ -915,7 +1027,7 @@
private void writeValidatorCall(SourceWriter sw, Class<?> type, Stage stage,
PropertyDescriptor p) {
- if (BeanHelper.isClassConstrained(type)) {
+ if (BeanHelper.isClassConstrained(type) && !isIterable(type)) {
BeanHelper helper = createBeanHelper(type);
beansToValidate.add(helper);
switch (stage) {
diff --git a/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java b/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
index cffacda..2cd8d5a 100644
--- a/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
+++ b/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
@@ -58,7 +58,7 @@
@Override
protected void compose(ClassSourceFileComposerFactory composerFactory) {
- addImports(composerFactory,
+ addImports(composerFactory,
GWT.class,
GwtBeanDescriptor.class,
GwtSpecificValidator.class,
@@ -124,12 +124,38 @@
sw.println(");");
}
- private void writeGetConstraintsForClass(SourceWriter sourceWriter) {
- sourceWriter.println("public BeanDescriptor getConstraintsForClass(Class<?> clazz) {");
- sourceWriter.indent();
- sourceWriter.println("return null;");
- sourceWriter.outdent();
- sourceWriter.println("}");
+ private void writeGetConstraintsForClass(SourceWriter sw) {
+ // public BeanDescriptor getConstraintsForClass(Class<?> clazz {
+ sw.println("public BeanDescriptor getConstraintsForClass(Class<?> clazz) {");
+ sw.indent();
+
+ // checkNotNull(clazz, "clazz");
+ sw.println("checkNotNull(clazz, \"clazz\");");
+
+ for (BeanHelper bean : beansToValidate.values()) {
+ writeGetConstraintsForClass(sw, bean);
+ }
+
+ writeThrowIllegalArgumnet(sw, "clazz.getName()");
+
+ // }
+ sw.outdent();
+ sw.println("}");
+ }
+
+ private void writeGetConstraintsForClass(SourceWriter sw,
+ BeanHelper bean) {
+ // if (clazz.eqals(MyBean.class)) {
+ sw.println("if (clazz.equals(" + bean.getTypeCanonicalName() + ".class)) {");
+ sw.indent();
+
+ // return myBeanValidator.getConstraints();
+ sw.print("return ");
+ sw.print(bean.getValidatorInstanceName() + ".getConstraints();");
+
+ // }
+ sw.outdent();
+ sw.println("}");
}
private void writeIfEqualsBeanType(SourceWriter sourceWriter, BeanHelper bean) {