Skip to content

Commit

Permalink
HV-1032 Avoid infinite loop in TypeHelper.isAssignable
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Mar 15, 2017
1 parent 192dddb commit 8b5e5da
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Expand Up @@ -571,7 +571,10 @@ private static Map<Type, Type> getActualTypeArgumentsByParameterInternal(Type ty
Map<Type, Type> actualTypeArgumentsByParameter = new LinkedHashMap<Type, Type>();

for ( int i = 0; i < typeParameters.length; i++ ) {
actualTypeArgumentsByParameter.put( typeParameters[i], typeArguments[i] );
// we only add the mapping if it is not a cyclic dependency (see HV-1032)
if ( !typeParameters[i].equals( typeArguments[i] ) ) {
actualTypeArgumentsByParameter.put( typeParameters[i], typeArguments[i] );
}
}

return actualTypeArgumentsByParameter;
Expand Down
@@ -0,0 +1,14 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.util;

/**
* @author Guillaume Smet
*/
public class ChildEntity<T> extends ParentEntity<T> {

}
@@ -0,0 +1,14 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.util;

/**
* @author Guillaume Smet
*/
public class ParentEntity<T> {

}
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
Expand All @@ -37,8 +38,8 @@

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import org.hibernate.validator.internal.util.TypeHelper;
import org.hibernate.validator.testutil.TestForIssue;

import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -866,6 +867,15 @@ public void testTypeDiscovery() {
assertNull( validatorsTypes.get( String.class ) );
}

@Test
@TestForIssue(jiraKey = "HV-1032")
public void testTypeHelperDoesntGoIntoInfiniteLoop() {
Type parentEntityType = ChildEntity.class.getGenericSuperclass();
ParameterizedType childEntityType = TypeHelper.parameterizedType( ChildEntity.class, ChildEntity.class.getTypeParameters() );

assertTrue( TypeHelper.isAssignable( parentEntityType, childEntityType ) );
}

private static void assertAsymmetricallyAssignable(Type supertype, Type type) {
assertAssignable( supertype, type );
assertUnassignable( type, supertype );
Expand Down

0 comments on commit 8b5e5da

Please sign in to comment.