Search in sources :

Example 56 with CannotCompileException

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;
}
Also used : CtClass(javassist.CtClass) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException)

Example 57 with CannotCompileException

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);
    }
}
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 58 with CannotCompileException

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);
    }
}
Also used : CtClass(javassist.CtClass) EntityInfrastructureException(org.motechproject.mds.exception.entity.EntityInfrastructureException) NotFoundException(javassist.NotFoundException) MotechDataRepository(org.motechproject.mds.repository.MotechDataRepository) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor)

Example 59 with CannotCompileException

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);
    }
}
Also used : EntityInfrastructureException(org.motechproject.mds.exception.entity.EntityInfrastructureException) Bundle(org.osgi.framework.Bundle) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 60 with CannotCompileException

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

Aggregations

CannotCompileException (javassist.CannotCompileException)65 CtClass (javassist.CtClass)45 NotFoundException (javassist.NotFoundException)42 CtMethod (javassist.CtMethod)30 IOException (java.io.IOException)22 ClassPool (javassist.ClassPool)22 CtField (javassist.CtField)15 CtConstructor (javassist.CtConstructor)10 FileNotFoundException (java.io.FileNotFoundException)8 File (java.io.File)6 Method (java.lang.reflect.Method)5 ArrayList (java.util.ArrayList)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileFilter (java.io.FileFilter)4 BadBytecode (javassist.bytecode.BadBytecode)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CodeIterator (javassist.bytecode.CodeIterator)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3