Permalink
Browse files

HV-775 Throwing class cast exception if Node#as() is called with wron…

…g type; Also disabling CheckStyle rule on complexity of OR statements
  • Loading branch information...
1 parent 590da97 commit 7e8ecfb323d065e6a4ebbc70c1fc59989de5e204 @gunnarmorling gunnarmorling committed Mar 19, 2013
@@ -22,6 +22,13 @@
import java.util.List;
import javax.validation.ElementKind;
import javax.validation.Path;
+import javax.validation.Path.BeanNode;
+import javax.validation.Path.ConstructorNode;
+import javax.validation.Path.CrossParameterNode;
+import javax.validation.Path.MethodNode;
+import javax.validation.Path.ParameterNode;
+import javax.validation.Path.PropertyNode;
+import javax.validation.Path.ReturnValueNode;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.logging.Log;
@@ -229,10 +236,17 @@ public ElementKind getKind() {
@Override
public <T extends Path.Node> T as(Class<T> nodeType) {
- if ( nodeType.isAssignableFrom( this.getClass() ) ) {
+ if ( ( kind == ElementKind.BEAN && nodeType == BeanNode.class ) ||
+ ( kind == ElementKind.CONSTRUCTOR && nodeType == ConstructorNode.class ) ||
+ ( kind == ElementKind.CROSS_PARAMETER && nodeType == CrossParameterNode.class ) ||
+ ( kind == ElementKind.METHOD && nodeType == MethodNode.class ) ||
+ ( kind == ElementKind.PARAMETER && nodeType == ParameterNode.class ) ||
+ ( kind == ElementKind.PROPERTY && nodeType == PropertyNode.class ) ||
+ ( kind == ElementKind.RETURN_VALUE && nodeType == ReturnValueNode.class ) ) {
return nodeType.cast( this );
}
- throw log.getUnableToNarrowNodeTypeException( this.getClass().getName(), nodeType.getName() );
+
+ throw log.getUnableToNarrowNodeTypeException( this.getClass().getName(), kind, nodeType.getName() );
}
@Override
@@ -29,6 +29,7 @@
import javax.validation.ConstraintDeclarationException;
import javax.validation.ConstraintDefinitionException;
import javax.validation.ConstraintTarget;
+import javax.validation.ElementKind;
import javax.validation.GroupDefinitionException;
import javax.validation.Path;
import javax.validation.UnexpectedTypeException;
@@ -429,8 +430,8 @@
@Message(id = 117, value = "Invalid value for property %s: %s")
ValidationException getInvalidPropertyValueException(String propertyName, String propertyValue, @Cause Exception e);
- @Message(id = 118, value = "Unable to cast %s to %s")
- ClassCastException getUnableToNarrowNodeTypeException(String actualDescriptorType, String expectedDescriptorType);
+ @Message(id = 118, value = "Unable to cast %s (with element kind %s) to %s")
+ ClassCastException getUnableToNarrowNodeTypeException(String actualDescriptorType, ElementKind kind, String expectedDescriptorType);
@LogMessage(level = INFO)
@Message(id = 119, value = "Using %s as parameter name provider.")
@@ -34,7 +34,6 @@
</module>
<!-- Miscellaneous other checks. -->
- <module name="BooleanExpressionComplexity"/>
<module name="UpperEll"/>
<module name="PackageAnnotation"/>
<module name="CovariantEquals"/>

0 comments on commit 7e8ecfb

Please sign in to comment.