Skip to content

Commit

Permalink
HV-1623 Add additional checks before casting
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta authored and gsmet committed Jun 11, 2018
1 parent a9c4de8 commit e409264
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.validator.internal.metadata.aggregated;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
import java.util.Collections;
Expand Down Expand Up @@ -40,6 +41,8 @@
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
import org.hibernate.validator.internal.util.CollectionHelper;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.internal.util.stereotypes.Immutable;

/**
Expand All @@ -60,6 +63,8 @@
*/
public class PropertyMetaData extends AbstractConstraintMetaData {

private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );

@Immutable
private final Set<Cascadable> cascadables;

Expand Down Expand Up @@ -221,10 +226,20 @@ public final void add(ConstrainedElement constrainedElement) {

private Optional<Constrainable> getConstrainableFromConstrainedElement(ConstrainedElement constrainedElement) {
if ( constrainedElement.getKind() == ConstrainedElementKind.FIELD ) {
return Optional.of( ( (ConstrainedField) constrainedElement ).getProperty() );
if ( constrainedElement instanceof ConstrainedField ) {
return Optional.of( ( (ConstrainedField) constrainedElement ).getProperty() );
}
else {
LOG.getUnexpectedConstraintElementType( ConstrainedField.class, constrainedElement.getClass() );
}
}
else if ( constrainedElement.getKind() == ConstrainedElementKind.METHOD ) {
return Optional.of( ( (ConstrainedExecutable) constrainedElement ).getCallable() );
if ( constrainedElement instanceof ConstrainedExecutable ) {
return Optional.of( ( (ConstrainedExecutable) constrainedElement ).getCallable() );
}
else {
LOG.getUnexpectedConstraintElementType( ConstrainedExecutable.class, constrainedElement.getClass() );
}
}
return Optional.empty();
}
Expand Down
Expand Up @@ -63,6 +63,7 @@
import org.hibernate.validator.spi.scripting.ScriptEvaluationException;
import org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory;
import org.hibernate.validator.spi.scripting.ScriptEvaluatorNotFoundException;

import org.jboss.logging.BasicLogger;
import org.jboss.logging.annotations.Cause;
import org.jboss.logging.annotations.FormatWith;
Expand Down Expand Up @@ -857,4 +858,7 @@ ConstraintDefinitionException getConstraintValidatorDefinitionConstraintMismatch
@FormatWith(ClassObjectFormatter.class) Class<? extends ConstraintValidator<?, ?>> constraintValidatorImplementationType,
@FormatWith(ClassObjectFormatter.class) Class<? extends Annotation> registeredConstraintAnnotationType,
@FormatWith(TypeFormatter.class) Type declaredConstraintAnnotationType);

@Message(id = 244, value = "ConstrainedElement expected class was %1$s, but instead received %2$s.")
AssertionError getUnexpectedConstraintElementType(@FormatWith(ClassObjectFormatter.class) Class<?> expecting, @FormatWith(ClassObjectFormatter.class) Class<?> got);
}

0 comments on commit e409264

Please sign in to comment.