From 93fed78f319e058168e4620d55dd72ffa9572716 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 24 May 2018 15:32:06 +0200 Subject: [PATCH] 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. --- .../bytebuddy/BytecodeProviderImpl.java | 5 ++ .../bytecode/ReflectionOptimizerTest.java | 48 +++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java index 5b27e82fa5..cc9c668380 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java @@ -53,6 +53,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 ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/ReflectionOptimizerTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/ReflectionOptimizerTest.java index 2ec09c75e1..3d47fed608 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/ReflectionOptimizerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/ReflectionOptimizerTest.java @@ -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); + } }