Search in sources :

Example 1 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class JSFAnnotationProcessor method deploy.

public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final Map<Class<? extends Annotation>, Set<Class<?>>> instances = new HashMap<Class<? extends Annotation>, Set<Class<?>>>();
    final CompositeIndex compositeIndex = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
    if (compositeIndex == null) {
        // Can not continue without index
        return;
    }
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    if (module == null) {
        // Can not continue without module
        return;
    }
    final ClassLoader classLoader = module.getClassLoader();
    for (FacesAnnotation annotation : FacesAnnotation.values()) {
        final List<AnnotationInstance> annotationInstances = compositeIndex.getAnnotations(annotation.indexName);
        if (annotationInstances == null || annotationInstances.isEmpty()) {
            continue;
        }
        final Set<Class<?>> discoveredClasses = new HashSet<Class<?>>();
        instances.put(annotation.annotationClass, discoveredClasses);
        for (AnnotationInstance annotationInstance : annotationInstances) {
            final AnnotationTarget target = annotationInstance.target();
            if (target instanceof ClassInfo) {
                final DotName className = ClassInfo.class.cast(target).name();
                final Class<?> annotatedClass;
                try {
                    annotatedClass = classLoader.loadClass(className.toString());
                } catch (ClassNotFoundException e) {
                    throw new DeploymentUnitProcessingException(JSFLogger.ROOT_LOGGER.classLoadingFailed(className));
                }
                discoveredClasses.add(annotatedClass);
            } else {
                throw new DeploymentUnitProcessingException(JSFLogger.ROOT_LOGGER.invalidAnnotationLocation(annotation, target));
            }
        }
    }
    deploymentUnit.addToAttachmentList(ServletContextAttribute.ATTACHMENT_KEY, new ServletContextAttribute(FACES_ANNOTATIONS_SC_ATTR, instances));
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) DotName(org.jboss.jandex.DotName) Annotation(java.lang.annotation.Annotation) ServletContextAttribute(org.jboss.as.web.common.ServletContextAttribute) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) AnnotationInstance(org.jboss.jandex.AnnotationInstance) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class HibernateSearchProcessor method addSearchDependency.

private void addSearchDependency(ModuleSpecification moduleSpecification, ModuleLoader moduleLoader, DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    String searchModuleName = null;
    PersistenceUnitsInApplication persistenceUnitsInApplication = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(PersistenceUnitsInApplication.PERSISTENCE_UNITS_IN_APPLICATION);
    for (PersistenceUnitMetadataHolder holder : persistenceUnitsInApplication.getPersistenceUnitHolders()) {
        for (PersistenceUnitMetadata pu : holder.getPersistenceUnits()) {
            String providerModule = pu.getProperties().getProperty(Configuration.HIBERNATE_SEARCH_MODULE);
            if (providerModule != null) {
                // one persistence unit specifying the Hibernate search module is allowed
                if (searchModuleName == null) {
                    searchModuleName = providerModule;
                } else // more than one persistence unit specifying different Hibernate search module names is not allowed
                if (!providerModule.equals(searchModuleName)) {
                    throw JpaLogger.ROOT_LOGGER.differentSearchModuleDependencies(deploymentUnit.getName(), searchModuleName, providerModule);
                }
            }
        }
    }
    if (NONE.equals(searchModuleName)) {
        // Hibernate Search module will not be added to deployment
        ROOT_LOGGER.debugf("Not adding Hibernate Search dependency to deployment %s", deploymentUnit.getName());
        return;
    }
    // use Search module name specified in persistence unit definition
    if (searchModuleName != null && !IGNORE.equals(searchModuleName)) {
        ModuleIdentifier moduleIdentifier = ModuleIdentifier.fromString(searchModuleName);
        moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, moduleIdentifier, false, true, true, false));
        ROOT_LOGGER.debugf("added %s dependency to %s", moduleIdentifier, deploymentUnit.getName());
    } else {
        // add Hibernate Search module dependency if application is using the Hibernate Search Indexed annotation
        final CompositeIndex index = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
        List<AnnotationInstance> annotations = index.getAnnotations(SEARCH_INDEXED_ANNOTATION_NAME);
        if (annotations != null && annotations.size() > 0) {
            moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, defaultSearchModule, false, true, true, false));
            ROOT_LOGGER.debugf("deployment %s contains %s annotation, added %s dependency", deploymentUnit.getName(), SEARCH_INDEXED_ANNOTATION_NAME, defaultSearchModule);
        }
    }
}
Also used : ModuleDependency(org.jboss.as.server.deployment.module.ModuleDependency) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) PersistenceUnitsInApplication(org.jboss.as.jpa.config.PersistenceUnitsInApplication) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 3 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class JPAAnnotationProcessor method bindClassSources.

private void bindClassSources(final DeploymentUnit deploymentUnit, final AnnotationInstance annotation, final EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
    // handle PersistenceContext and PersistenceUnit annotations
    if (isPersistenceContext(annotation) || isPersistenceUnit(annotation)) {
        String injectionTypeName = getClassLevelInjectionType(annotation);
        InjectionSource injectionSource = getBindingSource(deploymentUnit, annotation, injectionTypeName, classDescription);
        if (injectionSource != null) {
            final AnnotationValue nameValue = annotation.value("name");
            if (nameValue == null || nameValue.asString().isEmpty()) {
                classDescription.setInvalid(JpaLogger.ROOT_LOGGER.classLevelAnnotationParameterRequired(annotation.name().toString(), classDescription.getClassName(), "name"));
                return;
            }
            final String name = nameValue.asString();
            final BindingConfiguration bindingConfiguration = new BindingConfiguration(name, injectionSource);
            classDescription.getBindingConfigurations().add(bindingConfiguration);
        }
    } else if (isPersistenceUnits(annotation)) {
        // handle PersistenceUnits (array of PersistenceUnit)
        AnnotationValue containedPersistenceUnits = annotation.value("value");
        AnnotationInstance[] arrayPersistenceUnits;
        if (containedPersistenceUnits != null && (arrayPersistenceUnits = containedPersistenceUnits.asNestedArray()) != null) {
            for (int source = 0; source < arrayPersistenceUnits.length; source++) {
                String injectionTypeName = getClassLevelInjectionType(arrayPersistenceUnits[source]);
                InjectionSource injectionSource = getBindingSource(deploymentUnit, arrayPersistenceUnits[source], injectionTypeName, classDescription);
                if (injectionSource != null) {
                    final AnnotationValue nameValue = arrayPersistenceUnits[source].value("name");
                    if (nameValue == null || nameValue.asString().isEmpty()) {
                        classDescription.setInvalid(JpaLogger.ROOT_LOGGER.classLevelAnnotationParameterRequired(arrayPersistenceUnits[source].name().toString(), classDescription.getClassName(), "name"));
                        return;
                    }
                    final String name = nameValue.asString();
                    final BindingConfiguration bindingConfiguration = new BindingConfiguration(name, injectionSource);
                    classDescription.getBindingConfigurations().add(bindingConfiguration);
                }
            }
        }
    } else if (isPersistenceContexts(annotation)) {
        // handle PersistenceContexts (array of PersistenceContext)
        AnnotationValue containedPersistenceContexts = annotation.value("value");
        AnnotationInstance[] arrayPersistenceContexts;
        if (containedPersistenceContexts != null && (arrayPersistenceContexts = containedPersistenceContexts.asNestedArray()) != null) {
            for (int source = 0; source < arrayPersistenceContexts.length; source++) {
                String injectionTypeName = getClassLevelInjectionType(arrayPersistenceContexts[source]);
                InjectionSource injectionSource = getBindingSource(deploymentUnit, arrayPersistenceContexts[source], injectionTypeName, classDescription);
                if (injectionSource != null) {
                    final AnnotationValue nameValue = arrayPersistenceContexts[source].value("name");
                    if (nameValue == null || nameValue.asString().isEmpty()) {
                        classDescription.setInvalid(JpaLogger.ROOT_LOGGER.classLevelAnnotationParameterRequired(arrayPersistenceContexts[source].name().toString(), classDescription.getClassName(), "name"));
                        return;
                    }
                    final String name = nameValue.asString();
                    final BindingConfiguration bindingConfiguration = new BindingConfiguration(name, injectionSource);
                    classDescription.getBindingConfigurations().add(bindingConfiguration);
                }
            }
        }
    }
}
Also used : LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) InjectionSource(org.jboss.as.ee.component.InjectionSource) PersistenceContextInjectionSource(org.jboss.as.jpa.injectors.PersistenceContextInjectionSource) PersistenceUnitInjectionSource(org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource) AnnotationValue(org.jboss.jandex.AnnotationValue) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 4 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class JPAAnnotationProcessor method getBindingSource.

private InjectionSource getBindingSource(final DeploymentUnit deploymentUnit, final AnnotationInstance annotation, String injectionTypeName, final EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
    PersistenceUnitMetadata pu = getPersistenceUnit(deploymentUnit, annotation, classDescription);
    if (pu == null) {
        return null;
    }
    String scopedPuName = pu.getScopedPersistenceUnitName();
    ServiceName puServiceName = getPuServiceName(scopedPuName);
    if (isPersistenceContext(annotation)) {
        if (pu.getTransactionType() == PersistenceUnitTransactionType.RESOURCE_LOCAL) {
            classDescription.setInvalid(JpaLogger.ROOT_LOGGER.cannotInjectResourceLocalEntityManager());
            return null;
        }
        AnnotationValue pcType = annotation.value("type");
        PersistenceContextType type = (pcType == null || PersistenceContextType.TRANSACTION.name().equals(pcType.asString())) ? PersistenceContextType.TRANSACTION : PersistenceContextType.EXTENDED;
        AnnotationValue stType = annotation.value("synchronization");
        SynchronizationType synchronizationType = (stType == null || SynchronizationType.SYNCHRONIZED.name().equals(stType.asString())) ? SynchronizationType.SYNCHRONIZED : SynchronizationType.UNSYNCHRONIZED;
        Map<String, String> properties;
        AnnotationValue value = annotation.value("properties");
        AnnotationInstance[] props = value != null ? value.asNestedArray() : null;
        if (props != null) {
            properties = new HashMap<>();
            for (int source = 0; source < props.length; source++) {
                properties.put(props[source].value("name").asString(), props[source].value("value").asString());
            }
        } else {
            properties = null;
        }
        // get deployment settings from top level du (jboss-all.xml is only parsed at the top level).
        final JPADeploymentSettings jpaDeploymentSettings = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(JpaAttachments.DEPLOYMENT_SETTINGS_KEY);
        return new PersistenceContextInjectionSource(type, synchronizationType, properties, puServiceName, deploymentUnit.getServiceRegistry(), scopedPuName, injectionTypeName, pu, jpaDeploymentSettings);
    } else {
        return new PersistenceUnitInjectionSource(puServiceName, deploymentUnit.getServiceRegistry(), injectionTypeName, pu);
    }
}
Also used : PersistenceContextType(javax.persistence.PersistenceContextType) SynchronizationType(javax.persistence.SynchronizationType) JPADeploymentSettings(org.jboss.as.jpa.config.JPADeploymentSettings) ServiceName(org.jboss.msc.service.ServiceName) PersistenceContextInjectionSource(org.jboss.as.jpa.injectors.PersistenceContextInjectionSource) PersistenceUnitInjectionSource(org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) AnnotationValue(org.jboss.jandex.AnnotationValue) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 5 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class JPAAnnotationProcessor method processPersistenceAnnotations.

private void processPersistenceAnnotations(final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, List<AnnotationInstance> persistenceContexts, final EEApplicationClasses applicationClasses) throws DeploymentUnitProcessingException {
    for (AnnotationInstance annotation : persistenceContexts) {
        ClassInfo declaringClass;
        final AnnotationTarget annotationTarget = annotation.target();
        if (annotationTarget instanceof FieldInfo) {
            FieldInfo fieldInfo = (FieldInfo) annotationTarget;
            declaringClass = fieldInfo.declaringClass();
            EEModuleClassDescription eeModuleClassDescription = eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
            this.processField(deploymentUnit, annotation, fieldInfo, eeModuleClassDescription);
        } else if (annotationTarget instanceof MethodInfo) {
            MethodInfo methodInfo = (MethodInfo) annotationTarget;
            declaringClass = methodInfo.declaringClass();
            EEModuleClassDescription eeModuleClassDescription = eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
            this.processMethod(deploymentUnit, annotation, methodInfo, eeModuleClassDescription);
        } else if (annotationTarget instanceof ClassInfo) {
            declaringClass = (ClassInfo) annotationTarget;
            EEModuleClassDescription eeModuleClassDescription = eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
            this.processClass(deploymentUnit, annotation, eeModuleClassDescription);
        }
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) MethodInfo(org.jboss.jandex.MethodInfo) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) AnnotationInstance(org.jboss.jandex.AnnotationInstance) FieldInfo(org.jboss.jandex.FieldInfo) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

AnnotationInstance (org.jboss.jandex.AnnotationInstance)40 ClassInfo (org.jboss.jandex.ClassInfo)26 CompositeIndex (org.jboss.as.server.deployment.annotation.CompositeIndex)17 AnnotationTarget (org.jboss.jandex.AnnotationTarget)16 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)15 AnnotationValue (org.jboss.jandex.AnnotationValue)11 MethodInfo (org.jboss.jandex.MethodInfo)11 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)9 HashSet (java.util.HashSet)8 DotName (org.jboss.jandex.DotName)7 FieldInfo (org.jboss.jandex.FieldInfo)7 ArrayList (java.util.ArrayList)5 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)5 PropertyReplacer (org.jboss.metadata.property.PropertyReplacer)5 HashMap (java.util.HashMap)4 EEApplicationClasses (org.jboss.as.ee.component.EEApplicationClasses)4 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)3 Module (org.jboss.modules.Module)3 PersistenceUnitMetadata (org.jipijapa.plugin.spi.PersistenceUnitMetadata)3 URL (java.net.URL)2