HHH-12584 Do not try to create a reflection optimizer for interfaces and abstract classes

It's already the behavior of the Javassist bytecode provider but the
ByteBuddy one was failing with an exception.
This commit is contained in:
Guillaume Smet 2018-05-24 15:32:06 +02:00
parent 5c6183066e
commit ea8ec69bac
2 changed files with 48 additions and 5 deletions

View File

@ -56,6 +56,11 @@ public class BytecodeProviderImpl implements BytecodeProvider {
final String[] getterNames,
final String[] setterNames,
final Class[] types) {
if ( clazz.isInterface() || Modifier.isAbstract( clazz.getModifiers() ) ) {
// we don't provide an optimizer for interfaces and abstract classes - similar to what we do with Javassist
return null;
}
final Method[] getters = new Method[getterNames.length];
final Method[] setters = new Method[setterNames.length];
findAccessors( clazz, getterNames, setterNames, types, getters, setters );

View File

@ -6,17 +6,17 @@
*/
package org.hibernate.test.bytecode;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.hibernate.bytecode.internal.javassist.BulkAccessor;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
/**
* @author Steve Ebersole
@ -55,10 +55,48 @@ public class ReflectionOptimizerTest extends BaseUnitTestCase {
assertEquivalent( values, BeanReflectionHelper.TEST_VALUES );
}
@Test
@TestForIssue(jiraKey = "HHH-12584")
public void testAbstractClass() {
BytecodeProvider provider = Environment.getBytecodeProvider();
ReflectionOptimizer reflectionOptimizer = provider.getReflectionOptimizer( AbstractClass.class, new String[]{ "getProperty" },
new String[]{ "setProperty" }, new Class[]{ String.class } );
assertNull( reflectionOptimizer );
}
@Test
@TestForIssue(jiraKey = "HHH-12584")
public void testInterface() {
BytecodeProvider provider = Environment.getBytecodeProvider();
ReflectionOptimizer reflectionOptimizer = provider.getReflectionOptimizer( Interface.class, new String[]{ "getProperty" },
new String[]{ "setProperty" }, new Class[]{ String.class } );
assertNull( reflectionOptimizer );
}
private void assertEquivalent(Object[] checkValues, Object[] values) {
assertEquals( "Different lengths", checkValues.length, values.length );
for ( int i = 0; i < checkValues.length; i++ ) {
assertEquals( "different values at index [" + i + "]", checkValues[i], values[i] );
}
}
public static abstract class AbstractClass {
private String property;
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
public interface Interface {
String getProperty();
void setProperty(String property);
}
}