Search in sources :

Example 1 with JavaTypeDescriptor

use of org.hibernate.type.descriptor.java.JavaTypeDescriptor in project hibernate-orm by hibernate.

the class RegistryHelper method resolveDescriptor.

@SuppressWarnings("unchecked")
public <J> JavaTypeDescriptor<J> resolveDescriptor(Map<Class, JavaTypeDescriptor> descriptorsByClass, Class<J> cls, Supplier<JavaTypeDescriptor<J>> defaultValueSupplier) {
    if (cls == null) {
        throw new IllegalArgumentException("Class passed to locate JavaTypeDescriptor cannot be null");
    }
    JavaTypeDescriptor<J> descriptor = descriptorsByClass.get(cls);
    if (descriptor != null) {
        return descriptor;
    }
    if (cls.isEnum()) {
        descriptor = new EnumJavaTypeDescriptor(cls);
        descriptorsByClass.put(cls, descriptor);
        return descriptor;
    }
    // find the first "assignable" match
    for (Map.Entry<Class, JavaTypeDescriptor> entry : descriptorsByClass.entrySet()) {
        if (entry.getKey().isAssignableFrom(cls)) {
            log.debugf("Using  cached JavaTypeDescriptor instance for Java class [%s]", cls.getName());
            return entry.getValue();
        }
    }
    return defaultValueSupplier.get();
}
Also used : EnumJavaTypeDescriptor(org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor) Map(java.util.Map) EnumJavaTypeDescriptor(org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor)

Example 2 with JavaTypeDescriptor

use of org.hibernate.type.descriptor.java.JavaTypeDescriptor in project hibernate-orm by hibernate.

the class Java8DateTimeTests method dump.

private void dump(PersistentClass entityBinding, TheEntity theEntity) {
    final Iterator propertyBindingIterator = entityBinding.getPropertyClosureIterator();
    while (propertyBindingIterator.hasNext()) {
        final Property propertyBinding = (Property) propertyBindingIterator.next();
        final JavaTypeDescriptor javaTypeDescriptor = ((AbstractStandardBasicType) propertyBinding.getType()).getJavaTypeDescriptor();
        System.out.println(String.format("%s (%s) -> %s", propertyBinding.getName(), javaTypeDescriptor.getJavaType().getSimpleName(), javaTypeDescriptor.toString(propertyBinding.getGetter(TheEntity.class).get(theEntity))));
    }
}
Also used : Iterator(java.util.Iterator) Property(org.hibernate.mapping.Property) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType)

Example 3 with JavaTypeDescriptor

use of org.hibernate.type.descriptor.java.JavaTypeDescriptor in project hibernate-orm by hibernate.

the class JavaTypeDescriptorRegistryTest method testRegisterJavaTypeDescriptorRegistry.

@Test
public void testRegisterJavaTypeDescriptorRegistry() {
    TypeConfiguration typeConfiguration = new TypeConfiguration();
    typeConfiguration.getJavaTypeDescriptorRegistry().addDescriptor(new CustomJavaTypeDescriptor());
    JavaTypeDescriptor descriptor = typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor(CustomType.class);
    assertThat(descriptor, instanceOf(CustomJavaTypeDescriptor.class));
}
Also used : TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor) Test(org.junit.Test)

Example 4 with JavaTypeDescriptor

use of org.hibernate.type.descriptor.java.JavaTypeDescriptor in project hibernate-orm by hibernate.

the class JavaTypeDescriptorRegistryTest method testAddDirectlyToJavaTypeDescriptorRegistry.

@Test
public void testAddDirectlyToJavaTypeDescriptorRegistry() {
    TypeConfiguration typeConfiguration = new TypeConfiguration();
    org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry.INSTANCE.addDescriptor(new CustomJavaTypeDescriptor());
    JavaTypeDescriptor descriptor = typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor(CustomType.class);
    assertThat(descriptor, instanceOf(CustomJavaTypeDescriptor.class));
}
Also used : TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor) Test(org.junit.Test)

Example 5 with JavaTypeDescriptor

use of org.hibernate.type.descriptor.java.JavaTypeDescriptor in project hibernate-orm by hibernate.

the class MetadataBuildingProcess method handleTypes.

// todo (7.0) : buildJandexInitializer
// private static JandexInitManager buildJandexInitializer(
// MetadataBuildingOptions options,
// ClassLoaderAccess classLoaderAccess) {
// final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
// org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
// options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
// false
// );
// 
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static void handleTypes(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
    final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
    final TypeContributions typeContributions = new TypeContributions() {

        @Override
        public void contributeType(org.hibernate.type.BasicType type) {
            getBasicTypeRegistry().register(type);
        }

        @Override
        public void contributeType(BasicType type, String... keys) {
            getBasicTypeRegistry().register(type, keys);
        }

        @Override
        public void contributeType(UserType type, String[] keys) {
            getBasicTypeRegistry().register(type, keys);
        }

        @Override
        public void contributeType(CompositeUserType type, String[] keys) {
            getBasicTypeRegistry().register(type, keys);
        }

        @Override
        public void contributeJavaTypeDescriptor(JavaTypeDescriptor descriptor) {
            bootstrapContext.getTypeConfiguration().getJavaTypeDescriptorRegistry().addDescriptor(descriptor);
        }

        @Override
        public void contributeSqlTypeDescriptor(SqlTypeDescriptor descriptor) {
            bootstrapContext.getTypeConfiguration().getSqlTypeDescriptorRegistry().addDescriptor(descriptor);
        }

        @Override
        public TypeConfiguration getTypeConfiguration() {
            return bootstrapContext.getTypeConfiguration();
        }

        final BasicTypeRegistry getBasicTypeRegistry() {
            return bootstrapContext.getTypeConfiguration().getBasicTypeRegistry();
        }
    };
    // add Dialect contributed types
    final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
    dialect.contributeTypes(typeContributions, options.getServiceRegistry());
    // add TypeContributor contributed types.
    for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
        contributor.contribute(typeContributions, options.getServiceRegistry());
    }
    // add explicit application registered types
    for (BasicTypeRegistration basicTypeRegistration : options.getBasicTypeRegistrations()) {
        bootstrapContext.getTypeConfiguration().getBasicTypeRegistry().register(basicTypeRegistration.getBasicType(), basicTypeRegistration.getRegistrationKeys());
    }
}
Also used : BasicType(org.hibernate.type.BasicType) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) BasicTypeRegistration(org.hibernate.boot.spi.BasicTypeRegistration) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor) TypeContributor(org.hibernate.boot.model.TypeContributor) SqlTypeDescriptor(org.hibernate.type.descriptor.sql.SqlTypeDescriptor) TypeContributions(org.hibernate.boot.model.TypeContributions) Dialect(org.hibernate.dialect.Dialect) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType) CompositeUserType(org.hibernate.usertype.CompositeUserType) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Aggregations

JavaTypeDescriptor (org.hibernate.type.descriptor.java.JavaTypeDescriptor)5 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)2 Test (org.junit.Test)2 Iterator (java.util.Iterator)1 Map (java.util.Map)1 TypeContributions (org.hibernate.boot.model.TypeContributions)1 TypeContributor (org.hibernate.boot.model.TypeContributor)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 BasicTypeRegistration (org.hibernate.boot.spi.BasicTypeRegistration)1 Dialect (org.hibernate.dialect.Dialect)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 Property (org.hibernate.mapping.Property)1 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)1 BasicType (org.hibernate.type.BasicType)1 EnumJavaTypeDescriptor (org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor)1 SqlTypeDescriptor (org.hibernate.type.descriptor.sql.SqlTypeDescriptor)1 CompositeUserType (org.hibernate.usertype.CompositeUserType)1 UserType (org.hibernate.usertype.UserType)1