Search in sources :

Example 16 with AnnotationInstance

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

the class JandexAnnotationRepositoryImpl method getAnnotation.

@Override
public Collection<Annotation> getAnnotation(Class<?> annotationClass) {
    List<AnnotationInstance> instances = backingRepository.getAnnotations(DotName.createSimple(annotationClass.getName()));
    ArrayList<Annotation> annotations = new ArrayList<Annotation>(instances.size());
    for (AnnotationInstance instance : instances) {
        AnnotationTarget target = instance.target();
        Annotation annotation = null;
        if (target instanceof MethodInfo) {
            MethodInfo m = (MethodInfo) target;
            List<String> parameterTypes = new ArrayList<String>(m.args().length);
            for (Type type : m.args()) {
                parameterTypes.add(type.toString());
            }
            String declaringClass = m.declaringClass().name().toString();
            annotation = new AnnotationImpl(declaringClass, cl, parameterTypes, m.name(), true, false, annotationClass);
        }
        if (target instanceof FieldInfo) {
            FieldInfo f = (FieldInfo) target;
            String declaringClass = f.declaringClass().name().toString();
            annotation = new AnnotationImpl(declaringClass, cl, null, f.name(), false, true, annotationClass);
        }
        if (target instanceof ClassInfo) {
            ClassInfo c = (ClassInfo) target;
            annotation = new AnnotationImpl(c.name().toString(), cl, null, null, false, false, annotationClass);
        }
        if (annotation != null) {
            annotations.add(annotation);
        }
    }
    annotations.trimToSize();
    if (annotations.size() == 0) {
        return null;
    } else {
        return Collections.unmodifiableList(annotations);
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) ArrayList(java.util.ArrayList) Annotation(org.jboss.jca.common.spi.annotations.repository.Annotation) Type(org.jboss.jandex.Type) MethodInfo(org.jboss.jandex.MethodInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) FieldInfo(org.jboss.jandex.FieldInfo) ClassInfo(org.jboss.jandex.ClassInfo)

Example 17 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 18 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 19 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 20 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