Skip to content

Commit

Permalink
HV-571: Implementing BeanDescriptor#getConstrainedConstructors()
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jun 17, 2012
1 parent 103d9da commit c9da8a0
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 18 deletions.
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;
import javax.validation.groups.Default;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstructorDescriptor;
import javax.validation.metadata.MethodDescriptor;
import javax.validation.metadata.PropertyDescriptor;

Expand Down Expand Up @@ -176,11 +177,13 @@ public BeanMetaDataImpl(Class<T> beanClass,
this.directMetaConstraints = buildDirectConstraintSets();

this.executableMetaData = Collections.unmodifiableMap( byIdentifier( executableMetaDataSet ) );

this.beanDescriptor = new BeanDescriptorImpl(
beanClass,
getClassLevelConstraintsAsDescriptors(),
getConstrainedPropertiesAsDescriptors(),
getConstrainedMethodsAsDescriptors(),
getConstrainedConstructorsAsDescriptors(),
defaultGroupSequenceIsRedefined(),
getDefaultGroupSequence( null )
);
Expand Down Expand Up @@ -277,7 +280,28 @@ private Map<String, MethodDescriptor> getConstrainedMethodsAsDescriptors() {
if ( oneExecutable.getKind() == ConstraintMetaDataKind.METHOD && oneExecutable.isConstrained() ) {
constrainedMethodDescriptors.put(
oneExecutable.getIdentifier(),
oneExecutable.asDescriptor( defaultGroupSequenceIsRedefined(), getDefaultGroupSequence( null ) )
(MethodDescriptor) oneExecutable.asDescriptor(
defaultGroupSequenceIsRedefined(),
getDefaultGroupSequence( null )
)
);
}
}

return constrainedMethodDescriptors;
}

private Map<String, ConstructorDescriptor> getConstrainedConstructorsAsDescriptors() {
Map<String, ConstructorDescriptor> constrainedMethodDescriptors = newHashMap();

for ( ExecutableMetaData oneExecutable : executableMetaData.values() ) {
if ( oneExecutable.getKind() == ConstraintMetaDataKind.CONSTRUCTOR && oneExecutable.isConstrained() ) {
constrainedMethodDescriptors.put(
oneExecutable.getIdentifier(),
(ConstructorDescriptor) oneExecutable.asDescriptor(
defaultGroupSequenceIsRedefined(),
getDefaultGroupSequence( null )
)
);
}
}
Expand Down
Expand Up @@ -21,12 +21,13 @@
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintDeclarationException;
import javax.validation.metadata.MethodDescriptor;
import javax.validation.metadata.ElementDescriptor;
import javax.validation.metadata.ParameterDescriptor;
import javax.validation.metadata.ReturnValueDescriptor;

import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstructorDescriptorImpl;
import org.hibernate.validator.internal.metadata.descriptor.MethodDescriptorImpl;
import org.hibernate.validator.internal.metadata.descriptor.ReturnValueDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.MethodConstraintLocation;
Expand Down Expand Up @@ -365,16 +366,29 @@ public String getIdentifier() {
}

@Override
public MethodDescriptor asDescriptor(boolean defaultGroupSequenceRedefined, List<Class<?>> defaultGroupSequence) {
return new MethodDescriptorImpl(
getType(),
getName(),
asDescriptors( getConstraints() ),
returnValueAsDescriptor( defaultGroupSequenceRedefined, defaultGroupSequence ),
parametersAsDescriptors( defaultGroupSequenceRedefined, defaultGroupSequence ),
defaultGroupSequenceRedefined,
defaultGroupSequence
);
public ElementDescriptor asDescriptor(boolean defaultGroupSequenceRedefined, List<Class<?>> defaultGroupSequence) {

if ( super.getKind() == ConstraintMetaDataKind.METHOD ) {
return new MethodDescriptorImpl(
getType(),
getName(),
asDescriptors( getConstraints() ),
returnValueAsDescriptor( defaultGroupSequenceRedefined, defaultGroupSequence ),
parametersAsDescriptors( defaultGroupSequenceRedefined, defaultGroupSequence ),
defaultGroupSequenceRedefined,
defaultGroupSequence
);
}
else {
return new ConstructorDescriptorImpl(
getType(),
asDescriptors( getConstraints() ),
returnValueAsDescriptor( defaultGroupSequenceRedefined, defaultGroupSequence ),
parametersAsDescriptors( defaultGroupSequenceRedefined, defaultGroupSequence ),
defaultGroupSequenceRedefined,
defaultGroupSequence
);
}
}

private List<ParameterDescriptor> parametersAsDescriptors(boolean defaultGroupSequenceRedefined, List<Class<?>> defaultGroupSequence) {
Expand Down
Expand Up @@ -43,17 +43,20 @@
public class BeanDescriptorImpl extends ElementDescriptorImpl implements BeanDescriptor {
private final Map<String, PropertyDescriptor> constrainedProperties;
private final Map<String, MethodDescriptor> constrainedMethods;
private final Map<String, ConstructorDescriptor> constrainedConstructors;

public BeanDescriptorImpl(Type beanClass,
Set<ConstraintDescriptorImpl<?>> classLevelConstraints,
Map<String, PropertyDescriptor> properties,
Map<String, MethodDescriptor> methods,
Map<String, PropertyDescriptor> constrainedProperties,
Map<String, MethodDescriptor> constrainedMethods,
Map<String, ConstructorDescriptor> constrainedConstructors,
boolean defaultGroupSequenceRedefined,
List<Class<?>> defaultGroupSequence) {
super( beanClass, classLevelConstraints, defaultGroupSequenceRedefined, defaultGroupSequence );

this.constrainedProperties = Collections.unmodifiableMap( properties );
this.constrainedMethods = Collections.unmodifiableMap( methods );
this.constrainedProperties = Collections.unmodifiableMap( constrainedProperties );
this.constrainedMethods = Collections.unmodifiableMap( constrainedMethods );
this.constrainedConstructors = Collections.unmodifiableMap( constrainedConstructors );
}

@Override
Expand All @@ -80,8 +83,7 @@ public ConstructorDescriptor getConstraintsForConstructor(Class<?>... parameterT

@Override
public Set<ConstructorDescriptor> getConstrainedConstructors() {
// TODO HV-571
throw new IllegalArgumentException( "Not yet implemented" );
return new HashSet<ConstructorDescriptor>( constrainedConstructors.values() );
}

@Override
Expand Down
@@ -0,0 +1,68 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, 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.metadata.descriptor;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.validation.metadata.ConstructorDescriptor;
import javax.validation.metadata.ParameterDescriptor;
import javax.validation.metadata.ReturnValueDescriptor;

/**
* Describes a validated constructor.
*
* @author Gunnar Morling
*/
public class ConstructorDescriptorImpl extends ElementDescriptorImpl implements ConstructorDescriptor {
private final List<ParameterDescriptor> parameters;
private final ReturnValueDescriptor returnValueDescriptor;

public ConstructorDescriptorImpl(Type returnType,
Set<ConstraintDescriptorImpl<?>> returnValueConstraints,
ReturnValueDescriptor returnValueDescriptor,
List<ParameterDescriptor> parameters,
boolean defaultGroupSequenceRedefined,
List<Class<?>> defaultGroupSequence) {
super( returnType, returnValueConstraints, defaultGroupSequenceRedefined, defaultGroupSequence );

this.parameters = Collections.unmodifiableList( parameters );
this.returnValueDescriptor = returnValueDescriptor;
}

@Override
public List<ParameterDescriptor> getParameterDescriptors() {
return parameters;
}

@Override
public ReturnValueDescriptor getReturnValueDescriptor() {
return returnValueDescriptor;
}

@Override
public String toString() {
return "ConstructorDescriptorImpl [parameters=" + parameters
+ ", returnValueDescriptor=" + returnValueDescriptor + "]";
}

@Override
public Kind getKind() {
return Kind.CONSTRUCTOR;
}
}
Expand Up @@ -30,6 +30,9 @@ public static class CustomerExtension extends Customer {
public CustomerRepositoryExt(@NotNull String foo) {
}

public CustomerRepositoryExt(@NotNull String foo, @Min(0) int bar) {
}

public Customer createCustomer(CharSequence firstName, String lastName) {
return null;
}
Expand Down
Expand Up @@ -16,14 +16,17 @@
*/
package org.hibernate.validator.test.internal.metadata.descriptor;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.ConstructorDescriptor;
import javax.validation.metadata.ElementDescriptor;
import javax.validation.metadata.MethodDescriptor;
import javax.validation.metadata.ParameterDescriptor;

import org.testng.annotations.Test;

Expand All @@ -33,6 +36,7 @@
import org.hibernate.validator.test.internal.metadata.CustomerRepositoryExt;

import static org.fest.assertions.Assertions.assertThat;
import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
import static org.hibernate.validator.testutil.ValidatorUtil.getBeanDescriptor;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -199,6 +203,18 @@ public void testGetConstrainedMethodsForDerivedType() {
);
}

@Test
public void testGetConstrainedConstructors() {
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepositoryExt.class );
Set<ConstructorDescriptor> constrainedConstructors = descriptor.getConstrainedConstructors();

assertThat( constrainedConstructors ).isNotNull();
assertThat( getSignatures( constrainedConstructors ) ).containsOnly(
Arrays.<Class<?>>asList( String.class ),
Arrays.<Class<?>>asList( String.class, int.class )
);
}

private Set<String> getMethodNames(Set<MethodDescriptor> descriptors) {
Set<String> methodNames = newHashSet();

Expand All @@ -209,6 +225,21 @@ private Set<String> getMethodNames(Set<MethodDescriptor> descriptors) {
return methodNames;
}

private Set<List<Class<?>>> getSignatures(Set<ConstructorDescriptor> descriptors) {
Set<List<Class<?>>> signatures = newHashSet();

for ( ConstructorDescriptor methodDescriptor : descriptors ) {
List<Class<?>> parameterTypes = newArrayList();

for ( ParameterDescriptor oneParameter : methodDescriptor.getParameterDescriptors() ) {
parameterTypes.add( oneParameter.getElementClass() );
}
signatures.add( parameterTypes );
}

return signatures;
}

private static class UnconstrainedType {
@SuppressWarnings("unused")
public void foo(String foo) {
Expand Down

0 comments on commit c9da8a0

Please sign in to comment.