Permalink
Browse files

HV-878 Extracting common base class for unwrappers using ClassMate

* Also using shared instance of TypeResolver
* Moving unwrappers to "internal" package
  • Loading branch information...
1 parent ad7c228 commit 78937b31388c6c894851e7822ee43d90437f74d1 @gunnarmorling gunnarmorling committed Jun 30, 2014
@@ -28,7 +28,6 @@
import javax.validation.MessageInterpolator;
import javax.validation.ParameterNameProvider;
import javax.validation.TraversableResolver;
-import javax.validation.ValidationException;
import javax.validation.ValidationProviderResolver;
import javax.validation.ValidatorFactory;
import javax.validation.spi.BootstrapState;
@@ -40,9 +39,12 @@
import org.hibernate.validator.internal.cfg.DefaultConstraintMapping;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl;
import org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver;
+import org.hibernate.validator.internal.engine.valuehandling.JavaFXPropertyValueUnwrapper;
+import org.hibernate.validator.internal.engine.valuehandling.OptionalValueUnwrapper;
import org.hibernate.validator.internal.util.CollectionHelper;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.ReflectionHelper;
+import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.Version;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
@@ -52,8 +54,6 @@
import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
import org.hibernate.validator.spi.valuehandling.ValidatedValueUnwrapper;
-import org.hibernate.validator.spi.valuehandling.wrapper.JavaFXPropertyValueUnwrapper;
-import org.hibernate.validator.spi.valuehandling.wrapper.OptionalValueUnwrapper;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
@@ -108,11 +108,20 @@ public ConfigurationImpl(ValidationProvider<?> provider) {
private ConfigurationImpl() {
this.validationBootstrapParameters = new ValidationBootstrapParameters();
- if (isJavaFxInClasspath()) {
- this.validationBootstrapParameters.addValidatedValueHandler( new JavaFXPropertyValueUnwrapper() );
+ TypeResolutionHelper typeResolutionHelper = new TypeResolutionHelper();
+ if ( isJavaFxInClasspath() ) {
+ this.validationBootstrapParameters.addValidatedValueHandler(
+ new JavaFXPropertyValueUnwrapper(
+ typeResolutionHelper
+ )
+ );
}
- if (Version.getJavaRelease() >= 8) {
- this.validationBootstrapParameters.addValidatedValueHandler( new OptionalValueUnwrapper() );
+ if ( Version.getJavaRelease() >= 8 ) {
+ this.validationBootstrapParameters.addValidatedValueHandler(
+ new OptionalValueUnwrapper(
+ typeResolutionHelper
+ )
+ );
}
this.defaultResourceBundleLocator = new PlatformResourceBundleLocator( ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES );
this.defaultTraversableResolver = new DefaultTraversableResolver();
@@ -14,24 +14,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.hibernate.validator.spi.valuehandling.wrapper;
+package org.hibernate.validator.internal.engine.valuehandling;
-import java.lang.reflect.Type;
-
-import com.fasterxml.classmate.ResolvedType;
-import com.fasterxml.classmate.TypeResolver;
import javafx.beans.value.ObservableValue;
-import org.hibernate.validator.spi.valuehandling.ValidatedValueUnwrapper;
+import org.hibernate.validator.internal.util.TypeResolutionHelper;
/**
* Unwraps a JavaFX {@code ObservableValue} and returns the wrapped value and type.
*
* @author Khalid Alqinyah
*/
-public class JavaFXPropertyValueUnwrapper extends ValidatedValueUnwrapper<ObservableValue<?>> {
+public class JavaFXPropertyValueUnwrapper extends TypeResolverBasedValueUnwrapper<ObservableValue<?>> {
- private final TypeResolver typeResolver = new TypeResolver();
+ public JavaFXPropertyValueUnwrapper(TypeResolutionHelper typeResolutionHelper) {
+ super( typeResolutionHelper );
+ }
@Override
public Object handleValidatedValue(ObservableValue<?> value) {
@@ -40,10 +38,4 @@ public Object handleValidatedValue(ObservableValue<?> value) {
}
return value;
}
-
- @Override
- public Type getValidatedValueType(Type valueType) {
- ResolvedType resolvedType = typeResolver.resolve( valueType );
- return resolvedType.typeParametersFor( ObservableValue.class ).get( 0 ).getErasedType();
- }
}
@@ -14,25 +14,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.hibernate.validator.spi.valuehandling.wrapper;
+package org.hibernate.validator.internal.engine.valuehandling;
-import java.lang.reflect.Type;
import java.util.Optional;
-import com.fasterxml.classmate.ResolvedType;
-import com.fasterxml.classmate.TypeResolver;
-
-import org.hibernate.validator.spi.valuehandling.ValidatedValueUnwrapper;
+import org.hibernate.validator.internal.util.TypeResolutionHelper;
/**
* Unwraps an {@code Optional} and returns the wrapped value and type. Empty {@code Optional} value is returned as
* {@code null}.
*
* @author Khalid Alqinyah
*/
-public class OptionalValueUnwrapper extends ValidatedValueUnwrapper<Optional<?>> {
+public class OptionalValueUnwrapper extends TypeResolverBasedValueUnwrapper<Optional<?>> {
- private final TypeResolver typeResolver = new TypeResolver();
+ public OptionalValueUnwrapper(TypeResolutionHelper typeResolutionHelper) {
+ super( typeResolutionHelper );
+ }
@Override
public Object handleValidatedValue(Optional<?> value) {
@@ -42,10 +40,4 @@ public Object handleValidatedValue(Optional<?> value) {
return null;
}
-
- @Override
- public Type getValidatedValueType(Type valueType) {
- ResolvedType resolvedType = typeResolver.resolve( valueType );
- return resolvedType.typeParametersFor( Optional.class ).get( 0 ).getErasedType();
- }
}
@@ -0,0 +1,54 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* 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 org.hibernate.validator.internal.engine.valuehandling;
+
+import java.lang.reflect.Type;
+
+import com.fasterxml.classmate.ResolvedType;
+import com.fasterxml.classmate.TypeResolver;
+
+import org.hibernate.validator.internal.util.TypeResolutionHelper;
+import org.hibernate.validator.spi.valuehandling.ValidatedValueUnwrapper;
+
+/**
+ * Base class for {@link ValidatedValueUnwrapper}s based on ClassMate's type resolver.
+ *
+ * @author Gunnar Morling
+ */
+public abstract class TypeResolverBasedValueUnwrapper<T> extends ValidatedValueUnwrapper<T> {
+
+ private final Class<?> clazz;
+ private final TypeResolver typeResolver;
+
+ TypeResolverBasedValueUnwrapper(TypeResolutionHelper typeResolutionHelper) {
+ this.typeResolver = typeResolutionHelper.getTypeResolver();
+ clazz = resolveSingleTypeParameter( typeResolver, this.getClass(), ValidatedValueUnwrapper.class );
+ }
+
+ @Override
+ public Type getValidatedValueType(Type valueType) {
+ return resolveSingleTypeParameter( typeResolver, valueType, clazz );
+ }
+
+ /**
+ * Resolves the single type parameter of the given target class, using the given sub-type.
+ */
+ private static Class<?> resolveSingleTypeParameter(TypeResolver typeResolver, Type subType, Class<?> target) {
+ ResolvedType resolvedType = typeResolver.resolve( subType );
+ return resolvedType.typeParametersFor( target ).get( 0 ).getErasedType();
+ }
+}
@@ -18,4 +18,4 @@
/**
* Implementations for {@link org.hibernate.validator.spi.valuehandling.ValidatedValueUnwrapper}
*/
-package org.hibernate.validator.spi.valuehandling.wrapper;
+package org.hibernate.validator.internal.engine.valuehandling;
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.hibernate.validator.test.internal.engine.valuehandling.wrapper;
+package org.hibernate.validator.test.internal.engine.valuehandling;
import java.util.Set;
import javax.validation.ConstraintViolation;
@@ -56,7 +56,7 @@
import static org.hibernate.validator.testutil.ValidatorUtil.getValidator;
/**
- * Tests for {@link org.hibernate.validator.spi.valuehandling.wrapper.JavaFXPropertyValueUnwrapper}.
+ * Tests for {@link org.hibernate.validator.internal.engine.valuehandling.JavaFXPropertyValueUnwrapper}.
*
* @author Khalid Alqinyah
*/
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.hibernate.validator.test.internal.engine.valuehandling.wrapper;
+package org.hibernate.validator.test.internal.engine.valuehandling;
import java.lang.reflect.Method;
import java.util.Optional;
@@ -39,7 +39,7 @@
import static org.hibernate.validator.testutil.ValidatorUtil.getValidator;
/**
- * Tests for {@link org.hibernate.validator.spi.valuehandling.wrapper.OptionalValueUnwrapper}.
+ * Tests for {@link org.hibernate.validator.internal.engine.valuehandling.OptionalValueUnwrapper}.
*
* @author Khalid Alqinyah
*/
@@ -56,7 +56,13 @@ public void setup() {
public void testOptionalUnwrappedValueViolations() {
Set<ConstraintViolation<Foo>> constraintViolations = validator.validate( new Foo() );
assertNumberOfViolations( constraintViolations, 4 );
- assertCorrectPropertyPaths( constraintViolations, "integerOptional", "stringOptional", "barOptional.number", "optionalLong" );
+ assertCorrectPropertyPaths(
+ constraintViolations,
+ "integerOptional",
+ "stringOptional",
+ "barOptional.number",
+ "optionalLong"
+ );
assertCorrectConstraintTypes( constraintViolations, Min.class, NotBlank.class, Min.class, Max.class );
}
@@ -78,8 +84,12 @@ public void testOptionalUnwrappedExecutableReturnValue() throws Exception {
public void testOptionalUnwrappedExecutableParameter() throws Exception {
ExecutableValidator executableValidator = validator.forExecutables();
Method method = Foo.class.getMethod( "setOptionalLong", Optional.class );
- Object [] values = {Optional.of( 2L )};
- Set<ConstraintViolation<Foo>> constraintViolations = executableValidator.validateParameters( new Foo(), method, values );
+ Object[] values = { Optional.of( 2L ) };
+ Set<ConstraintViolation<Foo>> constraintViolations = executableValidator.validateParameters(
+ new Foo(),
+ method,
+ values
+ );
assertNumberOfViolations( constraintViolations, 1 );
assertCorrectPropertyPaths( constraintViolations, "setOptionalLong.arg0" );
assertCorrectConstraintTypes( constraintViolations, Min.class );
@@ -103,8 +113,12 @@ public void testOptionalUnwrappedCascadableExecutableReturnValue() throws Except
public void testOptionalUnwrappedCascadableExecutableParameter() throws Exception {
ExecutableValidator executableValidator = validator.forExecutables();
Method method = Foo.class.getMethod( "setBar", Optional.class );
- Object [] values = {Optional.of( new Bar() )};
- Set<ConstraintViolation<Foo>> constraintViolations = executableValidator.validateParameters( new Foo(), method, values );
+ Object[] values = { Optional.of( new Bar() ) };
+ Set<ConstraintViolation<Foo>> constraintViolations = executableValidator.validateParameters(
+ new Foo(),
+ method,
+ values
+ );
assertNumberOfViolations( constraintViolations, 1 );
assertCorrectPropertyPaths( constraintViolations, "setBar.arg0.number" );
assertCorrectConstraintTypes( constraintViolations, Min.class );
@@ -153,7 +167,6 @@ public void setBar(@UnwrapValidatedValue @Valid Optional<Bar> optionalBarPara) {
}
}
- @SuppressWarnings("unused")
private class Bar {
@Min(value = 5)
int number = 3;

0 comments on commit 78937b3

Please sign in to comment.