Search in sources :

Example 1 with JavassistLoader

use of org.motechproject.mds.javassist.JavassistLoader in project motech by motech.

the class MDSConstructorImpl method constructEntities.

@Override
public synchronized boolean constructEntities(SchemaHolder schemaHolder) {
    // To be able to register updated class, we need to reload class loader
    // and therefore add all the classes again
    MotechClassPool.clearEnhancedData();
    MDSClassLoader.reloadClassLoader();
    // we need an jdo enhancer and a temporary classLoader
    // to define classes in before enhancement
    MDSClassLoader tmpClassLoader = MDSClassLoader.getStandaloneInstance();
    MdsJDOEnhancer enhancer = createEnhancer(tmpClassLoader);
    JavassistLoader loader = new JavassistLoader(tmpClassLoader);
    // process only entities that are not drafts
    List<EntityDto> entities = schemaHolder.getAllEntities();
    filterEntities(entities);
    sortEntities(entities, schemaHolder);
    // create enum for appropriate combobox fields
    for (EntityDto entity : entities) {
        buildEnum(loader, enhancer, entity, schemaHolder);
    }
    // load entities interfaces
    for (EntityDto entity : entities) {
        buildInterfaces(loader, enhancer, entity);
    }
    // generate jdo metadata from scratch for our entities
    JDOMetadata jdoMetadata = metadataHolder.reloadMetadata();
    // since we just fetch fields from existing definition
    for (EntityDto entity : entities) {
        if (entity.isRecordHistory()) {
            entityBuilder.prepareHistoryClass(entity);
        }
        entityBuilder.prepareTrashClass(entity);
    }
    // Build classes
    Map<String, ClassData> classDataMap = buildClasses(entities, schemaHolder);
    List<Class> classes = new ArrayList<>();
    // the temporary ClassLoader and enhancer
    for (EntityDto entity : entities) {
        String className = entity.getClassName();
        Class<?> definition = addClassData(loader, enhancer, classDataMap.get(className));
        if (entity.isRecordHistory()) {
            addClassData(loader, enhancer, classDataMap.get(ClassName.getHistoryClassName(className)));
        }
        addClassData(loader, enhancer, classDataMap.get(ClassName.getTrashClassName(className)));
        classes.add(definition);
        LOGGER.debug("Generated classes for {}", entity.getClassName());
    }
    for (Class<?> definition : classes) {
        loader.loadFieldsAndMethodsOfClass(definition);
    }
    // Prepare metadata
    buildMetadata(entities, jdoMetadata, classDataMap, classes, schemaHolder);
    // after the classes are defined, we register their metadata
    enhancer.registerMetadata(jdoMetadata);
    // then, we commence with enhancement
    enhancer.enhance();
    // we register the enhanced class bytes
    // and build the infrastructure classes
    registerEnhancedClassBytes(entities, enhancer, schemaHolder);
    metadataBuilder.fixEnhancerIssuesInMetadata(jdoMetadata, schemaHolder);
    return CollectionUtils.isNotEmpty(entities);
}
Also used : MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) JDOMetadata(javax.jdo.metadata.JDOMetadata) ArrayList(java.util.ArrayList) JavassistLoader(org.motechproject.mds.javassist.JavassistLoader) EntityDto(org.motechproject.mds.dto.EntityDto) MdsJDOEnhancer(org.motechproject.mds.enhancer.MdsJDOEnhancer) ClassData(org.motechproject.mds.domain.ClassData) CtClass(javassist.CtClass)

Aggregations

ArrayList (java.util.ArrayList)1 CtClass (javassist.CtClass)1 JDOMetadata (javax.jdo.metadata.JDOMetadata)1 ClassData (org.motechproject.mds.domain.ClassData)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 MdsJDOEnhancer (org.motechproject.mds.enhancer.MdsJDOEnhancer)1 JavassistLoader (org.motechproject.mds.javassist.JavassistLoader)1 MDSClassLoader (org.motechproject.mds.util.MDSClassLoader)1