use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityBuilderTest method shouldBuildRelationshipFields.
@Test
public void shouldBuildRelationshipFields() throws Exception {
FieldDto oneToOneField = fieldDto("oto", OneToOneRelationship.class);
oneToOneField.setReadOnly(true);
oneToOneField.addMetadata(new MetadataDto(Constants.MetadataKeys.RELATED_CLASS, RelatedClass.class.getName()));
FieldDto oneToManyField = fieldDto("otm", OneToManyRelationship.class);
oneToManyField.setReadOnly(true);
oneToManyField.addMetadata(new MetadataDto(Constants.MetadataKeys.RELATED_CLASS, RelatedClass.class.getName()));
fields.addAll(asList(oneToOneField, oneToManyField));
when(entity.getClassName()).thenReturn(EntBuilderTestClass.class.getName());
ClassData classData = entityBuilder.buildDDE(entity, fields, bundle);
Class<?> builtClass = MDSClassLoader.getStandaloneInstance().defineClass(classData.getClassName(), classData.getBytecode());
assertField(builtClass, "oto", RelatedClass.class);
assertField(builtClass, "otm", List.class);
assertGenericType(builtClass, "otm", List.class, RelatedClass.class);
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityMetadataBuilderTest method shouldAddBaseEntityMetadata.
@Test
public void shouldAddBaseEntityMetadata() throws Exception {
CtField ctField = mock(CtField.class);
CtClass ctClass = mock(CtClass.class);
CtClass superClass = mock(CtClass.class);
ClassData classData = mock(ClassData.class);
ClassPool pool = mock(ClassPool.class);
PowerMockito.mockStatic(MotechClassPool.class);
PowerMockito.when(MotechClassPool.getDefault()).thenReturn(pool);
when(classData.getClassName()).thenReturn(CLASS_NAME);
when(classData.getModule()).thenReturn(MODULE);
when(classData.getNamespace()).thenReturn(NAMESPACE);
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(pool.getOrNull(CLASS_NAME)).thenReturn(ctClass);
when(ctClass.getField("id")).thenReturn(ctField);
when(ctClass.getSuperclass()).thenReturn(superClass);
when(superClass.getName()).thenReturn(Object.class.getName());
when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
entityMetadataBuilder.addBaseMetadata(jdoMetadata, classData, EntityType.STANDARD, Sample.class);
verify(jdoMetadata).newPackageMetadata(PACKAGE);
verify(packageMetadata).newClassMetadata(ENTITY_NAME);
verify(classMetadata).setTable(TABLE_NAME_3);
verifyCommonClassMetadata();
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityBuilderImpl method build.
private ClassData build(EntityDto entity, List<FieldDto> fields, EntityType type, Bundle bundle) {
try {
CtClass declaring = makeClass(entity, fields, type, bundle);
switch(type) {
case HISTORY:
String className = type.getClassName(entity.getClassName());
String simpleName = ClassName.getSimpleName(className);
TypeDto idType = TypeDto.LONG;
// add 4 extra fields to history class definition
// this field is related with id field in entity
addProperty(declaring, idType.getTypeClass(), simpleName + Constants.Util.CURRENT_VERSION, null);
// this field contains information about the schema version of an entity
addProperty(declaring, Long.class.getName(), simpleName + StringUtils.capitalize(Constants.Util.SCHEMA_VERSION_FIELD_NAME), null);
break;
case TRASH:
// this field contains information about the schema version of an entity
addProperty(declaring, Long.class.getName(), Constants.Util.SCHEMA_VERSION_FIELD_NAME, null);
break;
default:
}
return new ClassData(declaring.getName(), entity.getModule(), entity.getNamespace(), declaring.toBytecode(), type);
} catch (ReflectiveOperationException | CannotCompileException | IOException | NotFoundException e) {
throw new EntityCreationException("Unable to create entity " + entity.getName(), e);
}
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EnumBuilderImpl method build.
@Override
public ClassData build(ComboboxHolder holder) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassHelper helper = new ClassHelper(holder);
start(classWriter, helper);
visitFields(classWriter, helper);
visitValues(classWriter, helper);
visitValueOf(classWriter, helper);
visitInit(classWriter, helper);
visitClinit(classWriter, helper);
end(classWriter);
return new ClassData(helper.className, null, null, classWriter.toByteArray(), false, EntityType.STANDARD, true);
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class MDSConstructorImpl method registerTrashClass.
private void registerTrashClass(MdsJDOEnhancer enhancer, String className) {
String trashClassName = ClassName.getTrashClassName(className);
byte[] enhancedBytes = enhancer.getEnhancedBytes(trashClassName);
ClassData classData = new ClassData(trashClassName, enhancedBytes);
// register with the classloader so that we avoid issues with the persistence manager
MDSClassLoader.getInstance().safeDefineClass(classData.getClassName(), classData.getBytecode());
MotechClassPool.registerTrashClassData(classData);
}
Aggregations