Search in sources :

Example 1 with AnnotationProcessorManager

use of org.apache.felix.scrplugin.helper.AnnotationProcessorManager in project felix by apache.

the class SCRDescriptorGenerator method execute.

/**
 * Actually generates the Declarative Services and Metatype descriptors
 * scanning the java sources provided by the {@link #setProject(Project)}
 *
 * @return A list of generated file names, relative to the output directory
 *
 * @throws SCRDescriptorException
 * @throws SCRDescriptorFailureException
 */
public Result execute() throws SCRDescriptorException, SCRDescriptorFailureException {
    this.logger.debug("Starting SCR Descriptor Generator....");
    if (this.project == null) {
        throw new SCRDescriptorFailureException("Project has not been set!");
    }
    if (this.options == null) {
        // use default options
        this.options = new Options();
    }
    if (this.options.getOutputDirectory() == null) {
        throw new SCRDescriptorFailureException("Output directory has not been set!");
    }
    this.logger.debug("..using output directory: " + this.options.getOutputDirectory());
    this.logger.debug("..strict mode: " + this.options.isStrictMode());
    this.logger.debug("..generating accessors: " + this.options.isGenerateAccessors());
    // check speck version configuration
    SpecVersion specVersion = options.getSpecVersion();
    if (specVersion == null) {
        this.logger.debug("..auto detecting spec version");
    } else {
        this.logger.debug("..using spec version " + specVersion.getName());
    }
    // create a log
    this.iLog = new IssueLog(this.options.isStrictMode());
    // create the annotation processor manager
    final AnnotationProcessor aProcessor = new AnnotationProcessorManager(this.logger, this.project.getClassLoader());
    // create the class scanner - and start scanning
    this.scanner = new ClassScanner(logger, iLog, project, aProcessor);
    final List<ClassDescription> scannedDescriptions = scanner.scanSources();
    // create the result to hold the list of processed source files
    final Result result = new Result();
    final List<ComponentContainer> processedContainers = new ArrayList<ComponentContainer>();
    for (final ClassDescription desc : scannedDescriptions) {
        this.logger.debug("Processing component class " + desc.getSource());
        result.addProcessedSourceFile(desc.getSource());
        // check if there is more than one component definition
        if (desc.getDescriptions(ComponentDescription.class).size() > 1) {
            iLog.addError("Class has more than one component definition." + " Check the annotations and merge the definitions to a single definition.", desc.getSource());
        } else {
            final ComponentContainer container = this.createComponent(desc, iLog);
            if (container.getComponentDescription().getSpecVersion() != null) {
                if (specVersion == null) {
                    specVersion = container.getComponentDescription().getSpecVersion();
                    logger.debug("Setting used spec version to " + specVersion);
                } else if (container.getComponentDescription().getSpecVersion().ordinal() > specVersion.ordinal()) {
                    if (this.options.getSpecVersion() != null) {
                        // if a spec version has been configured and a component requires a higher
                        // version, this is considered an error!
                        iLog.addError("Component " + container + " requires spec version " + container.getComponentDescription().getSpecVersion().name() + " but plugin is configured to use version " + this.options.getSpecVersion(), desc.getSource());
                    } else {
                        specVersion = container.getComponentDescription().getSpecVersion();
                        logger.debug("Setting used spec version to " + specVersion);
                    }
                }
            } else {
                if (this.options.getSpecVersion() != null) {
                    container.getComponentDescription().setSpecVersion(options.getSpecVersion());
                } else {
                    container.getComponentDescription().setSpecVersion(SpecVersion.VERSION_1_0);
                }
            }
            processedContainers.add(container);
        }
    }
    // if spec version is still not set, we're using lowest available
    if (specVersion == null) {
        specVersion = SpecVersion.VERSION_1_0;
        logger.debug("Using default spec version " + specVersion);
    }
    this.logger.debug("Generating descriptor for spec version: " + specVersion);
    options.setSpecVersion(specVersion);
    // in order to create them if possible
    if (this.options.isGenerateAccessors()) {
        for (final ComponentContainer container : processedContainers) {
            this.generateMethods(container);
        }
    }
    // now validate
    final DescriptionContainer module = new DescriptionContainer(this.options);
    for (final ComponentContainer container : processedContainers) {
        final int errorCount = iLog.getNumberOfErrors();
        final Validator validator = new Validator(container, project, options, iLog);
        validator.validate();
        // ignore component if it has errors
        if (iLog.getNumberOfErrors() == errorCount) {
            module.add(container);
        }
    }
    // log issues
    iLog.logMessages(logger);
    // after checking all classes, throw if there were any failures
    if (iLog.hasErrors()) {
        throw new SCRDescriptorFailureException("SCR Descriptor parsing had failures (see log)");
    }
    // and generate files
    result.setMetatypeFiles(MetaTypeIO.generateDescriptors(module, this.project, this.options, this.logger));
    result.setScrFiles(ComponentDescriptorIO.generateDescriptorFiles(module, this.options, logger));
    return result;
}
Also used : ArrayList(java.util.ArrayList) AnnotationProcessor(org.apache.felix.scrplugin.annotations.AnnotationProcessor) ClassDescription(org.apache.felix.scrplugin.description.ClassDescription) AnnotationProcessorManager(org.apache.felix.scrplugin.helper.AnnotationProcessorManager) IssueLog(org.apache.felix.scrplugin.helper.IssueLog) ClassScanner(org.apache.felix.scrplugin.helper.ClassScanner) DescriptionContainer(org.apache.felix.scrplugin.helper.DescriptionContainer) ComponentContainer(org.apache.felix.scrplugin.helper.ComponentContainer) Validator(org.apache.felix.scrplugin.helper.Validator)

Aggregations

ArrayList (java.util.ArrayList)1 AnnotationProcessor (org.apache.felix.scrplugin.annotations.AnnotationProcessor)1 ClassDescription (org.apache.felix.scrplugin.description.ClassDescription)1 AnnotationProcessorManager (org.apache.felix.scrplugin.helper.AnnotationProcessorManager)1 ClassScanner (org.apache.felix.scrplugin.helper.ClassScanner)1 ComponentContainer (org.apache.felix.scrplugin.helper.ComponentContainer)1 DescriptionContainer (org.apache.felix.scrplugin.helper.DescriptionContainer)1 IssueLog (org.apache.felix.scrplugin.helper.IssueLog)1 Validator (org.apache.felix.scrplugin.helper.Validator)1