Search in sources :

Example 1 with InvalidKindForAnnotationException

use of com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException in project kripton by xcesco.

the class BindDataSourceSubProcessor method createSQLDaoDefinition.

/**
 * Create DAO definition
 *
 * @param globalBeanElements
 * @param globalDaoElements
 * @param generatedDaoParts
 * @param daoItem
 */
protected void createSQLDaoDefinition(SQLiteDatabaseSchema schema, final Map<String, TypeElement> globalBeanElements, final Map<String, TypeElement> globalDaoElements, String daoItem) {
    Element daoElement = globalDaoElements.get(daoItem);
    if (daoElement.getKind() != ElementKind.INTERFACE) {
        String msg = String.format("Class %s: only interfaces can be annotated with @%s annotation", daoElement.getSimpleName().toString(), BindDao.class.getSimpleName());
        throw (new InvalidKindForAnnotationException(msg));
    }
    M2MEntity entity = M2MEntity.extractEntityManagedByDAO((TypeElement) daoElement);
    // add to current schema generated entities too
    for (GeneratedTypeElement genItem : this.generatedEntities) {
        if (genItem.getQualifiedName().equals(entity.getQualifiedName())) {
            schema.generatedEntities.add(genItem);
        }
    }
    boolean generated = daoElement.getAnnotation(BindGeneratedDao.class) != null;
    final SQLiteDaoDefinition currentDaoDefinition = new SQLiteDaoDefinition(schema, daoItem, (TypeElement) daoElement, entity.getClassName().toString(), generated);
    // content provider management
    BindContentProviderPath daoContentProviderPath = daoElement.getAnnotation(BindContentProviderPath.class);
    if (daoContentProviderPath != null) {
        currentDaoDefinition.contentProviderEnabled = true;
        currentDaoDefinition.contentProviderPath = daoContentProviderPath.path();
        currentDaoDefinition.contentProviderTypeName = daoContentProviderPath.typeName();
        if (StringUtils.isEmpty(currentDaoDefinition.contentProviderTypeName)) {
            Converter<String, String> convert = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
            AssertKripton.assertTrue(currentDaoDefinition.getParent().contentProvider != null, "DAO '%s' has an inconsistent content provider definition, perhaps you forget to use @%s in data source interface?", currentDaoDefinition.getElement().getQualifiedName(), BindContentProvider.class.getSimpleName());
            currentDaoDefinition.contentProviderTypeName = currentDaoDefinition.getParent().contentProvider.authority + "." + convert.convert(currentDaoDefinition.getSimpleEntityClassName());
        }
    }
    // set.
    if (!globalBeanElements.containsKey(currentDaoDefinition.getEntityClassName()) && !isGeneratedEntity(currentDaoDefinition.getEntityClassName())) {
        throw (new InvalidBeanTypeException(currentDaoDefinition));
    }
    schema.add(currentDaoDefinition);
    fillMethods(currentDaoDefinition, daoElement);
    /*
		 * if (generatedDaoPart != null) {
		 * currentDaoDefinition.addImplementedInterface(TypeUtility.typeName(
		 * generatedDaoPart)); fillMethods(currentDaoDefinition,
		 * generatedDaoPart); }
		 */
    // get @annotation associated to many 2 many relationship
    BindDaoMany2Many daoMany2Many = daoElement.getAnnotation(BindDaoMany2Many.class);
    // dao definition must have >0 method associated to query
    if (currentDaoDefinition.getCollection().size() == 0 && daoMany2Many == null) {
        throw (new DaoDefinitionWithoutAnnotatedMethodException(currentDaoDefinition));
    }
}
Also used : BindDaoMany2Many(com.abubusoft.kripton.android.annotation.BindDaoMany2Many) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) InvalidBeanTypeException(com.abubusoft.kripton.processor.exceptions.InvalidBeanTypeException) BindContentProvider(com.abubusoft.kripton.android.annotation.BindContentProvider) InvalidKindForAnnotationException(com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException) DaoDefinitionWithoutAnnotatedMethodException(com.abubusoft.kripton.processor.exceptions.DaoDefinitionWithoutAnnotatedMethodException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) BindDao(com.abubusoft.kripton.android.annotation.BindDao) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) BindGeneratedDao(com.abubusoft.kripton.android.annotation.BindGeneratedDao) M2MEntity(com.abubusoft.kripton.processor.bind.model.many2many.M2MEntity) BindContentProviderPath(com.abubusoft.kripton.android.annotation.BindContentProviderPath)

Example 2 with InvalidKindForAnnotationException

use of com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException in project kripton by xcesco.

the class BindDataSourceSubProcessor method analyzeSecondRound.

public boolean analyzeSecondRound(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    parseBindType(roundEnv);
    // Put all @BindTable elements in beanElements
    for (Element item : roundEnv.getElementsAnnotatedWith(BindTable.class)) {
        if (item.getKind() != ElementKind.CLASS) {
            String msg = String.format("%s %s, only class can be annotated with @%s annotation", item.getKind(), item, BindTable.class.getSimpleName());
            throw (new InvalidKindForAnnotationException(msg));
        }
        globalBeanElements.put(item.toString(), (TypeElement) item);
    }
    Set<? extends Element> generatedDaos = roundEnv.getElementsAnnotatedWith(BindGeneratedDao.class);
    for (Element item : generatedDaos) {
        String keyToReplace = AnnotationUtility.extractAsClassName(item, BindGeneratedDao.class, AnnotationAttributeType.DAO);
        globalDaoElements.put(keyToReplace, (TypeElement) item);
        globalDaoGenerated.add(keyToReplace);
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) InvalidKindForAnnotationException(com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException) BindTable(com.abubusoft.kripton.android.annotation.BindTable)

Example 3 with InvalidKindForAnnotationException

use of com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException in project kripton by xcesco.

the class BindDataSourceSubProcessor method createDataSource.

/**
 * @param databaseSchema
 * @return databaseSchema
 */
protected SQLiteDatabaseSchema createDataSource(Element databaseSchema) {
    if (databaseSchema.getKind() != ElementKind.INTERFACE) {
        String msg = String.format("Class %s: only interfaces can be annotated with @%s annotation", databaseSchema.getSimpleName().toString(), BindDataSource.class.getSimpleName());
        throw (new InvalidKindForAnnotationException(msg));
    }
    if (!databaseSchema.getSimpleName().toString().endsWith(BindDataSourceBuilder.SUFFIX)) {
        String msg = String.format("Interface %s marked with @%s annotation must have a typeName with suffix \"" + BindDataSourceBuilder.SUFFIX + "\" to be used with @BindDataSource", databaseSchema.getSimpleName().toString(), BindDataSource.class.getSimpleName());
        throw (new InvalidNameException(msg));
    }
    // go ahead to dataSource analysis
    // ASSERT: daoElement and beanElement is element for dao and bean
    // associated
    String schemaFileName = AnnotationUtility.extractAsString(databaseSchema, BindDataSource.class, AnnotationAttributeType.FILENAME);
    int schemaVersion = AnnotationUtility.extractAsInt(databaseSchema, BindDataSource.class, AnnotationAttributeType.VERSION);
    boolean generateLog = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_LOG);
    boolean generateSchema = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_SCHEMA);
    boolean generateAsyncTask = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_ASYNC_TASK);
    boolean generateCursorWrapper = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_CURSOR_WRAPPER);
    boolean generateRx = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSource.class, AnnotationAttributeType.GENERATE_RX);
    // get all dao used within SQLDatabaseSchema annotation
    List<String> daoIntoDataSource = AnnotationUtility.extractAsClassNameArray(elementUtils, databaseSchema, BindDataSource.class, AnnotationAttributeType.DAO_SET);
    String configCursorFactory = NoCursorFactory.class.getName();
    String configDatabaseErrorHandler = NoDatabaseErrorHandler.class.getName();
    String configDatabaseLifecycleHandler = NoDatabaseLifecycleHandler.class.getName();
    boolean configInMemory = false;
    boolean configLogEnabled = true;
    String configPopulatorClass = NoPopulator.class.getName();
    // manage for annotated data-source options
    BindDataSourceOptions dataSourceOptionsAnnotation = databaseSchema.getAnnotation(BindDataSourceOptions.class);
    if (dataSourceOptionsAnnotation != null) {
        configInMemory = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.IN_MEMORY);
        configLogEnabled = AnnotationUtility.extractAsBoolean(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.LOG_ENABLED);
        configPopulatorClass = AnnotationUtility.extractAsClassName(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.POPULATOR);
        configCursorFactory = AnnotationUtility.extractAsClassName(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.CURSOR_FACTORY);
        configDatabaseLifecycleHandler = AnnotationUtility.extractAsClassName(databaseSchema, BindDataSourceOptions.class, AnnotationAttributeType.DATABASE_LIFECYCLE_HANDLER);
    }
    SQLiteDatabaseSchema schema = new SQLiteDatabaseSchema((TypeElement) databaseSchema, schemaFileName, schemaVersion, generateSchema, generateLog, generateAsyncTask, generateCursorWrapper, generateRx, daoIntoDataSource, configCursorFactory, configDatabaseErrorHandler, configDatabaseLifecycleHandler, configInMemory, configLogEnabled, configPopulatorClass);
    // manage for content provider generation
    BindContentProvider contentProviderAnnotation = databaseSchema.getAnnotation(BindContentProvider.class);
    if (contentProviderAnnotation != null) {
        schema.generateContentProvider = true;
        schema.contentProvider = new SQLiteModelContentProvider();
        schema.contentProvider.authority = contentProviderAnnotation.authority();
    } else {
        schema.generateContentProvider = false;
    }
    return schema;
}
Also used : SQLiteDatabaseSchema(com.abubusoft.kripton.processor.sqlite.model.SQLiteDatabaseSchema) BindDataSource(com.abubusoft.kripton.android.annotation.BindDataSource) InvalidNameException(com.abubusoft.kripton.processor.exceptions.InvalidNameException) BindDataSourceOptions(com.abubusoft.kripton.android.annotation.BindDataSourceOptions) BindContentProvider(com.abubusoft.kripton.android.annotation.BindContentProvider) InvalidKindForAnnotationException(com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException) SQLiteModelContentProvider(com.abubusoft.kripton.processor.sqlite.model.SQLiteModelContentProvider)

Example 4 with InvalidKindForAnnotationException

use of com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException in project kripton by xcesco.

the class BindMany2ManySubProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    try {
        model = new M2MModel();
        for (Element daoItem : roundEnv.getElementsAnnotatedWith(BindDaoMany2Many.class)) {
            if (daoItem.getKind() != ElementKind.INTERFACE) {
                String msg = String.format("%s %s, only interface can be annotated with @%s annotation", daoItem.getKind(), daoItem, BindDaoMany2Many.class.getSimpleName());
                throw (new InvalidKindForAnnotationException(msg));
            }
            M2MEntity entity = M2MEntity.extractEntityManagedByDAO((TypeElement) daoItem);
            model.entityAdd(entity);
        }
        result = BindM2MBuilder.generate(filer, model);
    } catch (Exception e) {
        String msg = e.getMessage();
        error(null, msg);
        if (DEBUG_MODE) {
            logger.log(Level.SEVERE, msg);
            e.printStackTrace();
        }
    }
    return true;
}
Also used : BindDaoMany2Many(com.abubusoft.kripton.android.annotation.BindDaoMany2Many) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) M2MModel(com.abubusoft.kripton.processor.bind.model.many2many.M2MModel) M2MEntity(com.abubusoft.kripton.processor.bind.model.many2many.M2MEntity) InvalidKindForAnnotationException(com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException) InvalidKindForAnnotationException(com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException)

Aggregations

InvalidKindForAnnotationException (com.abubusoft.kripton.processor.exceptions.InvalidKindForAnnotationException)4 GeneratedTypeElement (com.abubusoft.kripton.processor.element.GeneratedTypeElement)3 Element (javax.lang.model.element.Element)3 TypeElement (javax.lang.model.element.TypeElement)3 BindContentProvider (com.abubusoft.kripton.android.annotation.BindContentProvider)2 BindDaoMany2Many (com.abubusoft.kripton.android.annotation.BindDaoMany2Many)2 M2MEntity (com.abubusoft.kripton.processor.bind.model.many2many.M2MEntity)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 BindContentProviderPath (com.abubusoft.kripton.android.annotation.BindContentProviderPath)1 BindDao (com.abubusoft.kripton.android.annotation.BindDao)1 BindDataSource (com.abubusoft.kripton.android.annotation.BindDataSource)1 BindDataSourceOptions (com.abubusoft.kripton.android.annotation.BindDataSourceOptions)1 BindGeneratedDao (com.abubusoft.kripton.android.annotation.BindGeneratedDao)1 BindTable (com.abubusoft.kripton.android.annotation.BindTable)1 M2MModel (com.abubusoft.kripton.processor.bind.model.many2many.M2MModel)1 DaoDefinitionWithoutAnnotatedMethodException (com.abubusoft.kripton.processor.exceptions.DaoDefinitionWithoutAnnotatedMethodException)1 InvalidBeanTypeException (com.abubusoft.kripton.processor.exceptions.InvalidBeanTypeException)1 InvalidNameException (com.abubusoft.kripton.processor.exceptions.InvalidNameException)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 SQLiteDatabaseSchema (com.abubusoft.kripton.processor.sqlite.model.SQLiteDatabaseSchema)1