From dd96fe527d8230abe15d6470cd251ee55fcca4d6 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Fri, 17 Dec 2010 00:20:00 +0100 Subject: [PATCH] HV-347: Added support for return value nodes to Path API --- .../engine/MethodReturnValueNodeImpl.java | 47 +++++++++++++++++++ .../hibernate/validator/engine/PathImpl.java | 23 +++++++-- .../validator/test/engine/PathImplTest.java | 19 ++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 hibernate-validator/src/main/java/org/hibernate/validator/engine/MethodReturnValueNodeImpl.java diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/engine/MethodReturnValueNodeImpl.java b/hibernate-validator/src/main/java/org/hibernate/validator/engine/MethodReturnValueNodeImpl.java new file mode 100644 index 0000000000..94fec56950 --- /dev/null +++ b/hibernate-validator/src/main/java/org/hibernate/validator/engine/MethodReturnValueNodeImpl.java @@ -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 + ); + } + +} diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java b/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java index 4420a1061d..d0df98fda1 100644 --- a/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java +++ b/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java @@ -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 @@ -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( @@ -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(); @@ -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(); diff --git a/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/PathImplTest.java b/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/PathImplTest.java index 136eee85be..c1d23b7f8a 100644 --- a/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/PathImplTest.java +++ b/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/PathImplTest.java @@ -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 store = new HashMap();