Search in sources :

Example 21 with ClassData

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);
}
Also used : EntBuilderTestClass(org.motechproject.mds.testutil.EntBuilderTestClass) ClassData(org.motechproject.mds.domain.ClassData) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 22 with ClassData

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();
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) CtField(javassist.CtField) ClassPool(javassist.ClassPool) MotechClassPool(org.motechproject.mds.javassist.MotechClassPool) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with ClassData

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);
    }
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) NotFoundException(javassist.NotFoundException) TypeDto(org.motechproject.mds.dto.TypeDto) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) EntityCreationException(org.motechproject.mds.exception.entity.EntityCreationException)

Example 24 with ClassData

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);
}
Also used : ClassData(org.motechproject.mds.domain.ClassData) ClassWriter(org.objectweb.asm.ClassWriter)

Example 25 with ClassData

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);
}
Also used : ClassData(org.motechproject.mds.domain.ClassData)

Aggregations

ClassData (org.motechproject.mds.domain.ClassData)28 Test (org.junit.Test)9 FieldDto (org.motechproject.mds.dto.FieldDto)7 CtClass (javassist.CtClass)6 ArrayList (java.util.ArrayList)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 IOException (java.io.IOException)4 CannotCompileException (javassist.CannotCompileException)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 HashSet (java.util.HashSet)2 ByteArrayClassPath (javassist.ByteArrayClassPath)2 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)2 MetadataDto (org.motechproject.mds.dto.MetadataDto)2 TypeDto (org.motechproject.mds.dto.TypeDto)2 EntityCreationException (org.motechproject.mds.exception.entity.EntityCreationException)2 EntBuilderTestClass (org.motechproject.mds.testutil.EntBuilderTestClass)2 MDSClassLoader (org.motechproject.mds.util.MDSClassLoader)2 Bundle (org.osgi.framework.Bundle)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1