Search in sources :

Example 1 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class MdsWeavingHookTest method shouldWeaveClassesForDdeAndAddImports.

@Test
public void shouldWeaveClassesForDdeAndAddImports() {
    MotechClassPool.registerEnhancedClassData(new ClassData(TEST_CLASS, "testClassContent".getBytes()));
    List<String> dynamicImports = new ArrayList<>(asList("one.two.three"));
    when(wovenClass.getClassName()).thenReturn(TEST_CLASS);
    when(wovenClass.getDynamicImports()).thenReturn(dynamicImports);
    mdsWeavingHook.weave(wovenClass);
    ArgumentCaptor<byte[]> captor = ArgumentCaptor.forClass(byte[].class);
    verify(wovenClass).setBytes(captor.capture());
    assertArrayEquals("testClassContent".getBytes(), captor.getValue());
    assertThat(dynamicImports, hasItem("one.two.three"));
    for (String sdi : MdsWeavingHook.STANDARD_DYNAMIC_IMPORTS) {
        assertThat(dynamicImports, hasItem(sdi));
    }
}
Also used : ClassData(org.motechproject.mds.domain.ClassData) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class JavassistLoader method doWhenClassNotFound.

@Override
public void doWhenClassNotFound(String name) {
    CtClass ctClass = MotechClassPool.getDefault().getOrNull(name);
    if (null != ctClass) {
        try {
            ctClass.defrost();
            byte[] bytecode = ctClass.toBytecode();
            loadClass(new ClassData(name, bytecode));
        } catch (IOException | CannotCompileException ex) {
            throw new LoaderException("Unable to load class using Javassist: " + name, ex);
        }
    }
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) LoaderException(org.motechproject.mds.exception.loader.LoaderException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException)

Example 3 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class EntityInfrastructureBuilderImpl method build.

private List<ClassData> build(String className, EntityDto entity, SchemaHolder schemaHolder) {
    List<ClassData> list = new ArrayList<>();
    // create a repository(dao) for the entity
    String repositoryClassName = MotechClassPool.getRepositoryName(className);
    byte[] repositoryCode = getRepositoryCode(repositoryClassName, className, entity.getMaxFetchDepth());
    list.add(new ClassData(repositoryClassName, repositoryCode));
    // create an interface for the service
    String interfaceClassName = MotechClassPool.getInterfaceName(className);
    byte[] interfaceCode = getInterfaceCode(interfaceClassName, className, entity, schemaHolder);
    list.add(new ClassData(interfaceClassName, entity.getModule(), entity.getNamespace(), interfaceCode, true));
    // create the implementation of the service
    String serviceClassName = MotechClassPool.getServiceImplName(className);
    byte[] serviceCode = getServiceCode(serviceClassName, interfaceClassName, className, entity, schemaHolder);
    list.add(new ClassData(serviceClassName, serviceCode));
    return list;
}
Also used : ClassData(org.motechproject.mds.domain.ClassData) ArrayList(java.util.ArrayList)

Example 4 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class MDSConstructorImpl method buildInterfaces.

private void buildInterfaces(JavassistLoader loader, MdsJDOEnhancer enhancer, EntityDto entity) {
    List<ClassData> interfaces = new LinkedList<>();
    if (entity.isDDE()) {
        Bundle declaringBundle = MdsBundleHelper.searchForBundle(bundleContext, entity);
        try {
            Class<?> definition = declaringBundle.loadClass(entity.getClassName());
            for (Class interfaceClass : definition.getInterfaces()) {
                String classpath = JavassistUtil.toClassPath(interfaceClass.getName());
                URL classResource = declaringBundle.getResource(classpath);
                if (classResource != null) {
                    try (InputStream in = classResource.openStream()) {
                        interfaces.add(new ClassData(interfaceClass.getName(), IOUtils.toByteArray(in), true));
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            LOGGER.error("Class {} not found in {} bundle", entity.getClassName(), declaringBundle.getSymbolicName());
        } catch (IOException ioExc) {
            LOGGER.error("Could not load interface for {} class", entity.getClassName());
        }
    }
    for (ClassData data : interfaces) {
        try {
            MDSClassLoader.getInstance().loadClass(data.getClassName());
        } catch (ClassNotFoundException e) {
            // interfaces should be defined in the MDS class loader only if it does not exist
            MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
            ByteArrayClassPath classPath = new ByteArrayClassPath(data.getClassName(), data.getBytecode());
            MotechClassPool.getDefault().appendClassPath(classPath);
            MotechClassPool.registerEnhancedClassData(data);
            addClassData(loader, enhancer, data);
        }
    }
}
Also used : ByteArrayClassPath(javassist.ByteArrayClassPath) ClassData(org.motechproject.mds.domain.ClassData) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) CtClass(javassist.CtClass) IOException(java.io.IOException) LinkedList(java.util.LinkedList) URL(java.net.URL)

Example 5 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class MDSConstructorImpl method registerClass.

private void registerClass(MdsJDOEnhancer enhancer, EntityDto entity) {
    byte[] enhancedBytes = enhancer.getEnhancedBytes(entity.getClassName());
    ClassData classData = new ClassData(entity, enhancedBytes);
    // register with the classloader so that we avoid issues with the persistence manager
    MDSClassLoader.getInstance().safeDefineClass(classData.getClassName(), classData.getBytecode());
    MotechClassPool.registerEnhancedClassData(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