use of org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation in project eclipselink by eclipse-ee4j.
the class NonJDBCTestHelper method buildWorkbenchXMLProject.
public static ObjectPersistenceWorkbenchXMLProject buildWorkbenchXMLProject() {
ObjectPersistenceWorkbenchXMLProject workbenchXMLProject = null;
try {
workbenchXMLProject = new ObjectPersistenceWorkbenchXMLProject();
XMLTransformationMapping versionMapping = (XMLTransformationMapping) workbenchXMLProject.getDescriptor(Project.class).getMappings().firstElement();
TransformerBasedFieldTransformation versionTransformer = (TransformerBasedFieldTransformation) versionMapping.getFieldTransformations().get(0);
Field transformerField = TransformerBasedFieldTransformation.class.getDeclaredField("transformer");
transformerField.setAccessible(true);
ConstantTransformer constantTransformer = (ConstantTransformer) transformerField.get(versionTransformer);
constantTransformer.setValue(CONSTANT_PROJECT_BUILD_VERSION);
} catch (Exception e) {
e.printStackTrace();
}
return workbenchXMLProject;
}
use of org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation in project eclipselink by eclipse-ee4j.
the class AbstractTransformationMapping method initializeFieldToTransformers.
/**
* INTERNAL:
* Convert the field names and their corresponding method names to
* DatabaseFields and Methods.
*/
protected void initializeFieldToTransformers(AbstractSession session) throws DescriptorException {
for (Object[] pair : this.fieldToTransformers) {
pair[0] = getDescriptor().buildField(((DatabaseField) pair[0]));
((FieldTransformer) pair[1]).initialize(this);
}
for (FieldTransformation transformation : getFieldTransformations()) {
DatabaseField field = getDescriptor().buildField(transformation.getField());
String transformerClassName = "MethodBasedFieldTransformer";
FieldTransformer transformer = null;
try {
transformer = transformation.buildTransformer();
} catch (ConversionException ex) {
if (transformation instanceof TransformerBasedFieldTransformation) {
transformerClassName = ((TransformerBasedFieldTransformation) transformation).getTransformerClassName();
}
throw DescriptorException.fieldTransformerClassNotFound(transformerClassName, this, ex);
} catch (Exception ex) {
if (transformation instanceof TransformerBasedFieldTransformation) {
transformerClassName = ((TransformerBasedFieldTransformation) transformation).getTransformerClassName();
}
throw DescriptorException.fieldTransformerClassInvalid(transformerClassName, this, ex);
}
transformer.initialize(this);
// Attempt to ensure a type is set on the field.
if (field.getType() == null) {
if (transformer instanceof MethodBasedFieldTransformer) {
field.setType(((MethodBasedFieldTransformer) transformer).getFieldType());
} else if (field.getColumnDefinition() != null) {
// Search for the type for this field definition.
if (session.getDatasourcePlatform() instanceof DatabasePlatform) {
Iterator<Map.Entry<Class<?>, FieldTypeDefinition>> iterator = session.getPlatform().getFieldTypes().entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Class<?>, FieldTypeDefinition> entry = iterator.next();
if (entry.getValue().getName().equals(field.getColumnDefinition())) {
field.setType(entry.getKey());
break;
}
}
}
}
}
Object[] fieldToTransformer = new Object[2];
fieldToTransformer[0] = field;
fieldToTransformer[1] = transformer;
this.fieldToTransformers.add(fieldToTransformer);
}
}
use of org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation in project eclipselink by eclipse-ee4j.
the class AbstractTransformationMapping method convertClassNamesToClasses.
/**
* INTERNAL:
* Convert all the class-name-based settings in this mapping to actual class-based
* settings
*/
@Override
public void convertClassNamesToClasses(ClassLoader classLoader) {
super.convertClassNamesToClasses(classLoader);
if (attributeTransformerClassName != null) {
Class<?> attributeTransformerClass = null;
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
attributeTransformerClass = AccessController.doPrivileged(new PrivilegedClassForName<>(attributeTransformerClassName, true, classLoader));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(attributeTransformerClassName, exception.getException());
}
} else {
attributeTransformerClass = PrivilegedAccessHelper.getClassForName(attributeTransformerClassName, true, classLoader);
}
} catch (ClassNotFoundException exc) {
throw ValidationException.classNotFoundWhileConvertingClassNames(attributeTransformerClassName, exc);
}
this.setAttributeTransformerClass(attributeTransformerClass);
}
for (FieldTransformation transformation : getFieldTransformations()) {
if (transformation instanceof TransformerBasedFieldTransformation) {
TransformerBasedFieldTransformation transformer = (TransformerBasedFieldTransformation) transformation;
String transformerClassName = transformer.getTransformerClassName();
if (transformerClassName == null) {
return;
}
Class<?> transformerClass = null;
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
transformerClass = AccessController.doPrivileged(new PrivilegedClassForName<>(transformerClassName, true, classLoader));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(transformerClassName, exception.getException());
}
} else {
transformerClass = PrivilegedAccessHelper.getClassForName(transformerClassName, true, classLoader);
}
} catch (ClassNotFoundException exc) {
throw ValidationException.classNotFoundWhileConvertingClassNames(transformerClassName, exc);
}
transformer.setTransformerClass(transformerClass);
}
}
}
use of org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation in project eclipselink by eclipse-ee4j.
the class AbstractTransformationMapping method addFieldTransformer.
/**
* PUBLIC:
* Add the field and the transformer
* that returns the value to be placed in the field
* when the object is written to the database.
*/
public void addFieldTransformer(DatabaseField field, FieldTransformer transformer) {
TransformerBasedFieldTransformation transformation = new TransformerBasedFieldTransformation(transformer);
transformation.setField(field);
getFieldTransformations().add(transformation);
}
use of org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation in project eclipselink by eclipse-ee4j.
the class PLSQLrecordTestHelper method buildWorkbenchXMLProject.
public static ObjectPersistenceWorkbenchXMLProject buildWorkbenchXMLProject() {
ObjectPersistenceWorkbenchXMLProject workbenchXMLProject = null;
try {
workbenchXMLProject = new ObjectPersistenceWorkbenchXMLProject();
XMLTransformationMapping versionMapping = (XMLTransformationMapping) workbenchXMLProject.getDescriptor(Project.class).getMappings().firstElement();
TransformerBasedFieldTransformation versionTransformer = (TransformerBasedFieldTransformation) versionMapping.getFieldTransformations().get(0);
Field transformerField = TransformerBasedFieldTransformation.class.getDeclaredField("transformer");
transformerField.setAccessible(true);
ConstantTransformer constantTransformer = (ConstantTransformer) transformerField.get(versionTransformer);
constantTransformer.setValue(CONSTANT_PROJECT_BUILD_VERSION);
} catch (Exception e) {
e.printStackTrace();
}
return workbenchXMLProject;
}
Aggregations