Skip to content

Commit

Permalink
HV-347: Added support for return value nodes to Path API
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jan 9, 2011
1 parent 2132b7d commit dd96fe5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
@@ -0,0 +1,47 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.engine;

import java.lang.reflect.Method;
import javax.validation.Path.Node;

/**
* A {@link Node} implementation representing a single method parameter.
*
* @author Gunnar Morling
*/
public class MethodReturnValueNodeImpl extends NodeImpl {

private static final long serialVersionUID = -1964614171714243780L;

private final static String NAME_TEMPLATE = "%s#%s()";

/**
* Creates a new {@link MethodReturnValueNodeImpl}.
*
* @param method The method hosting the parameter to represent.
* @param parent The parent node, representing the bean hosting the given
* method.
*/
MethodReturnValueNodeImpl(Method method, NodeImpl parent) {
super(
String.format( NAME_TEMPLATE, method.getDeclaringClass().getSimpleName(), method.getName() ),
parent, false, null, null
);
}

}
Expand Up @@ -26,6 +26,8 @@
import java.util.regex.Pattern;
import javax.validation.Path;

import org.hibernate.validator.util.Contracts;

/**
* @author Hardy Ferentschik
* @author Gunnar Morling
Expand Down Expand Up @@ -85,9 +87,7 @@ public static PathImpl createPathFromString(String propertyPath) {
*/
public static PathImpl createPathForMethodParameter(Method method, int parameterIndex) {

if ( method == null ) {
throw new IllegalArgumentException( "A method is required to create a method parameter path." );
}
Contracts.assertNotNull( method, "A method is required to create a method parameter path." );

if ( parameterIndex < 0 || parameterIndex > method.getParameterTypes().length - 1 ) {
throw new IllegalArgumentException(
Expand All @@ -101,6 +101,16 @@ public static PathImpl createPathForMethodParameter(Method method, int parameter
return path;
}

public static PathImpl createPathForMethodReturnValue(Method method) {

Contracts.assertNotNull( method, "A method is required to create a method return value path." );

PathImpl path = createRootPath();
path.addMethodReturnValueNode( method );

return path;
}


public static PathImpl createNewPath(String name) {
PathImpl path = new PathImpl();
Expand Down Expand Up @@ -140,6 +150,13 @@ private NodeImpl addMethodParameterNode(Method method, int parameterIndex) {
return currentLeafNode;
}

private NodeImpl addMethodReturnValueNode(Method method) {
NodeImpl parent = nodeList.size() == 0 ? null : ( NodeImpl ) nodeList.get( nodeList.size() - 1 );
currentLeafNode = new MethodReturnValueNodeImpl( method, parent );
nodeList.add( currentLeafNode );
hashCode = -1;
return currentLeafNode;
}

public final NodeImpl makeLeafNodeIterable() {
NodeImpl leafNode = getLeafNode();
Expand Down
Expand Up @@ -163,6 +163,25 @@ public void testCreationOfMethodParameterPathFailsDueToInvalidParameterIndex() t
);
}

@Test
public void testCreationOfReturnValuePath() throws Exception {

PathImpl methodParameterPath = PathImpl.createPathForMethodReturnValue(
Container.class.getMethod( "addItem", Key.class, Item.class )
);

assertEquals( methodParameterPath.toString(), "Container#addItem()" );
}

@Test(
expectedExceptions=IllegalArgumentException.class,
expectedExceptionsMessageRegExp="A method is required to create a method return value path."
)
public void creationOfReturnValuePathFailsDueToNullMethod() throws Exception {

PathImpl.createPathForMethodReturnValue(null);
}

class Container {
@Valid
Map<Key, Item> store = new HashMap<Key, Item>();
Expand Down

0 comments on commit dd96fe5

Please sign in to comment.