use of org.eclipse.persistence.internal.descriptors.FieldTransformation in project eclipselink by eclipse-ee4j.
the class ProjectClassGenerator method addTransformationMappingLines.
protected void addTransformationMappingLines(NonreflectiveMethodDefinition method, String mappingName, TransformationMapping mapping) {
if (!mapping.isWriteOnly()) {
if (mapping.getAttributeMethodName() != null) {
method.addLine(mappingName + ".setAttributeTransformation(\"" + mapping.getAttributeMethodName() + "\");");
} else {
method.addLine(mappingName + ".setAttributeTransformer(new " + mapping.getAttributeTransformerClassName() + "());");
}
}
Iterator<FieldTransformation> fieldTransformations = mapping.getFieldTransformations().iterator();
while (fieldTransformations.hasNext()) {
FieldTransformation trans = fieldTransformations.next();
String fieldName = trans.getFieldName();
if (trans instanceof MethodBasedFieldTransformation) {
String methodName = ((MethodBasedFieldTransformation) trans).getMethodName();
method.addLine(mappingName + ".addFieldTransformation(\"" + fieldName + "\", \"" + methodName + "\");");
} else {
String transformerClass = ((TransformerBasedFieldTransformation) trans).getTransformerClassName();
method.addLine(mappingName + ".addFieldTransformer(\"" + fieldName + "\", new " + transformerClass + "());");
}
}
IndirectionPolicy policy = mapping.getIndirectionPolicy();
if (policy instanceof ContainerIndirectionPolicy) {
String containerClassName = ((ContainerIndirectionPolicy) policy).getContainerClassName();
method.addLine(mappingName + ".useContainerIndirection(" + containerClassName + ".class);");
} else if (policy instanceof BasicIndirectionPolicy) {
method.addLine(mappingName + ".useBasicIndirection();");
}
method.addLine(mappingName + ".setIsMutable(" + mapping.isMutable() + ");");
}
use of org.eclipse.persistence.internal.descriptors.FieldTransformation 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.FieldTransformation 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.FieldTransformation in project eclipselink by eclipse-ee4j.
the class AbstractTransformationMapping method getFieldNameToMethodNameAssociations.
/**
* INTERNAL:
* Needed for backwards compatibility
*/
public Vector getFieldNameToMethodNameAssociations() {
Vector associations = new Vector();
for (Iterator<FieldTransformation> source = getFieldTransformations().iterator(); source.hasNext(); ) {
FieldTransformation tf = source.next();
if (tf instanceof MethodBasedFieldTransformation) {
Association ass = new Association();
ass.setKey(tf.getField().getQualifiedName());
ass.setValue(((MethodBasedFieldTransformation) tf).getMethodName());
associations.addElement(ass);
}
}
return associations;
}
use of org.eclipse.persistence.internal.descriptors.FieldTransformation in project eclipselink by eclipse-ee4j.
the class AbstractTransformationMapping method getFieldNameToMethodNames.
/**
* INTERNAL:
* Required for reverse compatibility and test cases:
* @return a hash table containing the fieldName and their respective method names
*/
public Hashtable getFieldNameToMethodNames() {
Hashtable table = new Hashtable(getFieldTransformations().size());
Iterator<FieldTransformation> transformations = getFieldTransformations().iterator();
while (transformations.hasNext()) {
FieldTransformation transformation = transformations.next();
if (transformation instanceof MethodBasedFieldTransformation) {
table.put(transformation.getField().getQualifiedName(), ((MethodBasedFieldTransformation) transformation).getMethodName());
}
}
return table;
}
Aggregations