use of javassist.CannotCompileException in project motech by motech.
the class JarGeneratorServiceImpl method addClass.
private boolean addClass(JarOutputStream output, String name) {
CtClass clazz = MotechClassPool.getDefault().getOrNull(name);
boolean added = false;
if (null != clazz) {
try {
addEntry(output, JavassistUtil.toClassPath(name), clazz.toBytecode());
added = true;
} catch (IOException | CannotCompileException e) {
LOGGER.error("There were problems with adding entry: ", e);
added = false;
}
}
return added;
}
use of javassist.CannotCompileException 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 javassist.CannotCompileException in project motech by motech.
the class EntityInfrastructureBuilderImpl method getRepositoryCode.
private byte[] getRepositoryCode(String repositoryClassName, String typeName, Integer fetchDepth) {
try {
CtClass superClass = classPool.getCtClass(MotechDataRepository.class.getName());
superClass.setGenericSignature(getGenericSignature(typeName));
CtClass subClass = createOrRetrieveClass(repositoryClassName, superClass);
String repositoryName = ClassName.getSimpleName(repositoryClassName);
removeDefaultConstructor(subClass);
String constructorAsString;
// the fetch depth parameter is optional
if (fetchDepth == null) {
constructorAsString = String.format("public %s(){super(%s.class);}", repositoryName, typeName);
} else {
constructorAsString = String.format("public %s(){super(%s.class, %d);}", repositoryName, typeName, fetchDepth);
}
CtConstructor constructor = CtNewConstructor.make(constructorAsString, subClass);
subClass.addConstructor(constructor);
return subClass.toBytecode();
} catch (NotFoundException | CannotCompileException | IOException e) {
throw new EntityInfrastructureException(repositoryClassName, e);
}
}
use of javassist.CannotCompileException in project motech by motech.
the class EntityInfrastructureBuilderImpl method getInterfaceCode.
private byte[] getInterfaceCode(String interfaceClassName, String className, EntityDto entity, SchemaHolder schemaHolder) {
try {
// the interface can come from the developer for DDE, but it doesn't have to
// in which case it will be generated from scratch
CtClass superInterface = null;
if (null != entity && MotechClassPool.isServiceInterfaceRegistered(className)) {
String ddeInterfaceName = MotechClassPool.getInterfaceName(className);
Bundle declaringBundle = MdsBundleHelper.searchForBundle(bundleContext, entity);
if (declaringBundle == null) {
LOGGER.error("Unable to find bundle declaring the DDE interface for {}", className);
} else {
superInterface = JavassistUtil.loadClass(declaringBundle, ddeInterfaceName, classPool);
}
}
// standard super interface - MotechDataService, for EUDE or DDE without an interface
if (superInterface == null) {
superInterface = classPool.getCtClass(MotechDataService.class.getName());
superInterface.setGenericSignature(getGenericSignature(className));
}
CtClass interfaceClass = createOrRetrieveInterface(interfaceClassName, superInterface);
List<CtMethod> methods = new ArrayList<>();
// a count method for the lookup
if (null != entity) {
List<LookupDto> lookups = schemaHolder.getLookups(entity);
for (LookupDto lookup : lookups) {
for (LookupType lookupType : LookupType.values()) {
LookupBuilder lookupBuilder = new LookupBuilder(entity, lookup, interfaceClass, lookupType, schemaHolder);
methods.add(lookupBuilder.buildSignature());
}
}
}
// clear lookup methods before adding the new ones
removeExistingMethods(interfaceClass);
for (CtMethod method : methods) {
interfaceClass.addMethod(method);
}
return interfaceClass.toBytecode();
} catch (NotFoundException | IOException | CannotCompileException e) {
throw new EntityInfrastructureException(interfaceClassName, e);
}
}
use of javassist.CannotCompileException in project motech by motech.
the class MDSConstructorImpl method buildEnum.
private void buildEnum(JavassistLoader loader, MdsJDOEnhancer enhancer, EntityDto entity, SchemaHolder schemaHolder) {
for (FieldDto field : schemaHolder.getFields(entity.getClassName())) {
TypeDto type = field.getType();
if (!type.isCombobox()) {
continue;
}
ComboboxHolder holder = new ComboboxHolder(entity, field);
if (holder.isEnum() || holder.isEnumCollection()) {
if (field.isReadOnly()) {
String enumName = holder.getEnumName();
Class<?> definition = loadClass(entity, enumName);
if (null != definition) {
MotechClassPool.registerEnum(enumName);
CtClass ctClass = MotechClassPool.getDefault().getOrNull(enumName);
if (null != ctClass) {
try {
ctClass.defrost();
byte[] bytecode = ctClass.toBytecode();
ClassData data = new ClassData(enumName, bytecode);
// register with the classloader so that we avoid issues with the persistence manager
MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
addClassData(loader, enhancer, data);
} catch (IOException | CannotCompileException e) {
LOGGER.error("Could not load enum: {}", enumName);
}
}
}
} else {
buildEnum(loader, enhancer, holder);
}
}
}
}
Aggregations