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));
}
}
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);
}
}
}
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;
}
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);
}
}
}
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);
}
Aggregations