use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.
the class MapProxyTest method shouldGenerateClassWithAppropriateGetter.
@Test
public void shouldGenerateClassWithAppropriateGetter() throws Exception {
//given
Map<String, Object> map = new HashMap<String, Object>();
int ageExpected = 14;
map.put("age", ageExpected);
Map<String, Class<?>> properties = new HashMap<String, Class<?>>();
properties.put("age", Integer.class);
//when
Class testClass = MapProxyTool.classForName("TestClass1", properties, new ClassLoaderServiceImpl());
Object testClassInstance = testClass.getConstructor(Map.class).newInstance(map);
//then
Getter getter = ReflectionTools.getGetter(testClass, "age", "property", serviceRegistry);
int age = (Integer) getter.get(testClassInstance);
Assert.assertEquals(ageExpected, age);
}
use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.
the class ReflectionTools method getGetter.
public static Getter getGetter(Class cls, String propertyName, String accessorType, ServiceRegistry serviceRegistry) {
final Pair<Class, String> key = Pair.make(cls, propertyName);
Getter value = GETTER_CACHE.get(key);
if (value == null) {
value = getAccessStrategy(cls, serviceRegistry, accessorType).buildPropertyAccess(cls, propertyName).getGetter();
// It's ok if two getters are generated concurrently
GETTER_CACHE.put(key, value);
}
return value;
}
use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.
the class EnversServiceImpl method initializeAuditStrategy.
private static AuditStrategy initializeAuditStrategy(String auditStrategyName, Class<?> revisionInfoClass, PropertyData revisionInfoTimestampData, ServiceRegistry serviceRegistry) {
AuditStrategy strategy;
try {
final Class<?> auditStrategyClass = loadClass(auditStrategyName, serviceRegistry);
strategy = (AuditStrategy) ReflectHelper.getDefaultConstructor(auditStrategyClass).newInstance();
} catch (Exception e) {
throw new MappingException(String.format("Unable to create AuditStrategy [%s] instance.", auditStrategyName), e);
}
if (strategy instanceof ValidityAuditStrategy) {
// further initialization required
final Getter revisionTimestampGetter = ReflectionTools.getGetter(revisionInfoClass, revisionInfoTimestampData, serviceRegistry);
((ValidityAuditStrategy) strategy).setRevisionTimestampGetter(revisionTimestampGetter);
}
return strategy;
}
use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.
the class SingleIdMapper method mapToMapFromEntity.
@Override
public void mapToMapFromEntity(Map<String, Object> data, Object obj) {
if (obj == null) {
data.put(propertyData.getName(), null);
} else {
if (obj instanceof HibernateProxy) {
final HibernateProxy hibernateProxy = (HibernateProxy) obj;
data.put(propertyData.getName(), hibernateProxy.getHibernateLazyInitializer().getIdentifier());
} else {
final Getter getter = ReflectionTools.getGetter(obj.getClass(), propertyData, getServiceRegistry());
data.put(propertyData.getName(), getter.get(obj));
}
}
}
use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.
the class SingleIdMapper method mapToEntityFromEntity.
public void mapToEntityFromEntity(Object objTo, Object objFrom) {
if (objTo == null || objFrom == null) {
return;
}
final Getter getter = ReflectionTools.getGetter(objFrom.getClass(), propertyData, getServiceRegistry());
final Setter setter = ReflectionTools.getSetter(objTo.getClass(), propertyData, getServiceRegistry());
setter.set(objTo, getter.get(objFrom), null);
}
Aggregations