Search in sources :

Example 1 with ReflectionOptimizer

use of org.hibernate.bytecode.spi.ReflectionOptimizer in project hibernate-orm by hibernate.

the class ReflectionOptimizerTest method testInterface.

@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 });
    assertNotNull(reflectionOptimizer);
}
Also used : BytecodeProvider(org.hibernate.bytecode.spi.BytecodeProvider) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with ReflectionOptimizer

use of org.hibernate.bytecode.spi.ReflectionOptimizer in project hibernate-orm by hibernate.

the class ReflectionOptimizerTest method testAbstractClass.

@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 });
    assertNotNull(reflectionOptimizer);
}
Also used : BytecodeProvider(org.hibernate.bytecode.spi.BytecodeProvider) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with ReflectionOptimizer

use of org.hibernate.bytecode.spi.ReflectionOptimizer in project hibernate-orm by hibernate.

the class ReflectionOptimizerTest method testReflectionOptimization.

@Test
public void testReflectionOptimization() {
    BytecodeProvider provider = Environment.getBytecodeProvider();
    ReflectionOptimizer optimizer = provider.getReflectionOptimizer(Bean.class, BeanReflectionHelper.getGetterNames(), BeanReflectionHelper.getSetterNames(), BeanReflectionHelper.getTypes());
    assertNotNull(optimizer);
    assertNotNull(optimizer.getInstantiationOptimizer());
    assertNotNull(optimizer.getAccessOptimizer());
    Object instance = optimizer.getInstantiationOptimizer().newInstance();
    assertEquals(instance.getClass(), Bean.class);
    Bean bean = (Bean) instance;
    optimizer.getAccessOptimizer().setPropertyValues(bean, BeanReflectionHelper.TEST_VALUES);
    assertEquals(bean.getSomeString(), BeanReflectionHelper.TEST_VALUES[0]);
    Object[] values = optimizer.getAccessOptimizer().getPropertyValues(bean);
    assertEquivalent(values, BeanReflectionHelper.TEST_VALUES);
}
Also used : BytecodeProvider(org.hibernate.bytecode.spi.BytecodeProvider) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) Test(org.junit.Test)

Example 4 with ReflectionOptimizer

use of org.hibernate.bytecode.spi.ReflectionOptimizer in project micronaut-sql by micronaut-projects.

the class IntrospectedHibernateBytecodeProvider method getReflectionOptimizer.

@Override
public ReflectionOptimizer getReflectionOptimizer(Class clazz, String[] getterNames, String[] setterNames, Class[] types) {
    Optional<BeanIntrospection<?>> optionalBeanIntrospection = BeanIntrospector.SHARED.findIntrospection(clazz);
    return optionalBeanIntrospection.map(beanIntrospection -> new ReflectionOptimizer() {

        @Override
        public InstantiationOptimizer getInstantiationOptimizer() {
            return beanIntrospection::instantiate;
        }

        @Override
        public AccessOptimizer getAccessOptimizer() {
            BeanProperty[] beanProperties = beanIntrospection.getBeanProperties().toArray(new BeanProperty[0]);
            return new AccessOptimizer() {

                private final String[] propertyNames = Arrays.stream(beanProperties).map(BeanProperty::getName).toArray(String[]::new);

                @Override
                public String[] getPropertyNames() {
                    return propertyNames;
                }

                @Override
                public Object[] getPropertyValues(Object object) {
                    Object[] values = new Object[beanProperties.length];
                    for (int i = 0; i < beanProperties.length; i++) {
                        BeanProperty beanProperty = beanProperties[i];
                        values[i] = beanProperty.get(i);
                    }
                    return values;
                }

                @Override
                public void setPropertyValues(Object object, Object[] values) {
                    for (int i = 0; i < beanProperties.length; i++) {
                        BeanProperty beanProperty = beanProperties[i];
                        beanProperty.set(object, values[i]);
                    }
                }
            };
        }
    }).orElse(null);
}
Also used : ProxyFactoryFactory(org.hibernate.bytecode.spi.ProxyFactoryFactory) EnhancementContext(org.hibernate.bytecode.enhance.spi.EnhancementContext) BytecodeProvider(org.hibernate.bytecode.spi.BytecodeProvider) Arrays(java.util.Arrays) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) BeanProperty(io.micronaut.core.beans.BeanProperty) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) Singleton(jakarta.inject.Singleton) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) Optional(java.util.Optional) Internal(io.micronaut.core.annotation.Internal) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) BeanProperty(io.micronaut.core.beans.BeanProperty)

Example 5 with ReflectionOptimizer

use of org.hibernate.bytecode.spi.ReflectionOptimizer in project hibernate-orm by hibernate.

the class ReflectionOptimizerTest method testReflectionOptimization.

@Test
public void testReflectionOptimization() {
    BytecodeProvider provider = Environment.getBytecodeProvider();
    ReflectionOptimizer optimizer = provider.getReflectionOptimizer(Bean.class, BeanReflectionHelper.getGetterNames(), BeanReflectionHelper.getSetterNames(), BeanReflectionHelper.getTypes());
    assertNotNull(optimizer);
    assertNotNull(optimizer.getInstantiationOptimizer());
    assertNotNull(optimizer.getAccessOptimizer());
    Object instance = optimizer.getInstantiationOptimizer().newInstance();
    assertEquals(instance.getClass(), Bean.class);
    Bean bean = (Bean) instance;
    optimizer.getAccessOptimizer().setPropertyValues(bean, BeanReflectionHelper.TEST_VALUES);
    assertEquals(bean.getSomeString(), BeanReflectionHelper.TEST_VALUES[0]);
    Object[] values = optimizer.getAccessOptimizer().getPropertyValues(bean);
    assertEquivalent(values, BeanReflectionHelper.TEST_VALUES);
}
Also used : BytecodeProvider(org.hibernate.bytecode.spi.BytecodeProvider) ReflectionOptimizer(org.hibernate.bytecode.spi.ReflectionOptimizer) Test(org.junit.Test)

Aggregations

ReflectionOptimizer (org.hibernate.bytecode.spi.ReflectionOptimizer)9 BytecodeProvider (org.hibernate.bytecode.spi.BytecodeProvider)6 Test (org.junit.Test)5 EnhancementContext (org.hibernate.bytecode.enhance.spi.EnhancementContext)2 Enhancer (org.hibernate.bytecode.enhance.spi.Enhancer)2 ProxyFactoryFactory (org.hibernate.bytecode.spi.ProxyFactoryFactory)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Internal (io.micronaut.core.annotation.Internal)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)1 BeanProperty (io.micronaut.core.beans.BeanProperty)1 Singleton (jakarta.inject.Singleton)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Callable (java.util.concurrent.Callable)1 ClassFileVersion (net.bytebuddy.ClassFileVersion)1 NamingStrategy (net.bytebuddy.NamingStrategy)1