<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.jpa.processor;

import org.jboss.as.ee.component.AbstractComponentDescription;
import org.jboss.as.ee.component.BindingDescription;
import org.jboss.as.ee.component.EEModuleDescription;
import org.jboss.as.ee.component.InjectionTargetDescription;
import org.jboss.as.ee.component.ServiceBindingSourceDescription;
import org.jboss.as.jpa.container.PersistenceUnitSearch;
import org.jboss.as.jpa.service.PersistenceContextInjectorService;
import org.jboss.as.jpa.service.PersistenceUnitInjectorService;
import org.jboss.as.jpa.service.PersistenceUnitService;
import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.annotation.CompositeIndex;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import java.util.Collection;
import java.util.List;

/**
 * Handle PersistenceContext and PersistenceUnit annotations.
 *
 * @author <NAME> (based on ResourceInjectionAnnotationParsingProcessor)
 */
public class JPAAnnotationParseProcessor
    implements DeploymentUnitProcessor {

    private static final DotName PERSISTENCE_CONTEXT_ANNOTATION_NAME = DotName.createSimple(PersistenceContext.class.getName());
    private static final DotName PERSISTENCE_UNIT_ANNOTATION_NAME = DotName.createSimple(PersistenceUnit.class.getName());

    /**
     * Check the deployment annotation index for all classes with the @PersistenceContext annotation.  For each class with the
     * annotation, collect all the required information to create a managed bean instance, and attach it to the context.
     *
     * @param phaseContext the deployment unit context
     * @throws org.jboss.as.server.deployment.DeploymentUnitProcessingException
     *
     */
    public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
        final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
        final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
        final String applicationName = deploymentUnit.getParent() == null ? deploymentUnit.getName() : deploymentUnit.getParent().getName();
        final CompositeIndex compositeIndex = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
        if (compositeIndex == null) {
            return;
        }

        addBindings(deploymentUnit, PERSISTENCE_CONTEXT_ANNOTATION_NAME, compositeIndex, moduleDescription, applicationName, phaseContext);

        addBindings(deploymentUnit, PERSISTENCE_UNIT_ANNOTATION_NAME, compositeIndex, moduleDescription, applicationName, phaseContext);

    }

    private void addBindings(final DeploymentUnit deploymentUnit,
                             final DotName annotationName,
                             final CompositeIndex compositeIndex,
                             final EEModuleDescription moduleDescription,
                             final String applicationName,
                             DeploymentPhaseContext phaseContext)
        throws DeploymentUnitProcessingException {

        final List<AnnotationInstance> instances = compositeIndex.getAnnotations(annotationName);
        if (instances != null) {
            for (AnnotationInstance instance : instances) {
                final BindingDescription binding = getResourceConfiguration(deploymentUnit, instance, phaseContext);
                final String componentName = getComponentName(instance);
                final AbstractComponentDescription componentDescription = moduleDescription.getComponentByClassName(componentName);

                // give hopefully enough information if component isn't found
                if (null == componentDescription) {
                    final Collection<AbstractComponentDescription> descriptions = moduleDescription.getComponentDescriptions();
                    String componentDetails = "{";
                    String separator = "";
                    if (descriptions != null) {
                        for (AbstractComponentDescription description : descriptions) {
                            componentDetails = componentDetails + separator + description.getComponentName();
                            separator = ",";
                        }
                    }
                    componentDetails = componentDetails + "}";
                    throw new DeploymentUnitProcessingException("Application " + applicationName + ": is missing component " + componentName +
                        " (components=" + componentDetails + ") for annotation " + instance.target().toString());
                }

                componentDescription.getBindings().add(binding);
            }
        }

    }

    private String getComponentName(AnnotationInstance annotation) {
        String name;
        final AnnotationTarget annotationTarget = annotation.target();
        if (annotationTarget instanceof FieldInfo) {
            name = ((FieldInfo) annotationTarget).declaringClass().name().toString();
        } else if (annotationTarget instanceof MethodInfo) {
            name = ((MethodInfo) annotationTarget).declaringClass().name().toString();
        } else if (annotationTarget instanceof ClassInfo) {
            name = ((ClassInfo) annotationTarget).name().toString();
        } else {
            // Don't expect to see this error thrown
            throw new RuntimeException("unexpected error: AnnotationTarget class of type (" + annotationTarget.getClass().getName() + ") is not handled.");
        }
        return name;
    }

    public void undeploy(DeploymentUnit context) {<commit_msg>Change JPAAnnotationParseProcessor to allow for CDI @PersistenceContext injection<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.jpa.processor;

import org.jboss.as.ee.component.AbstractComponentDescription;
import org.jboss.as.ee.component.BindingDescription;
import org.jboss.as.ee.component.EEModuleDescription;
import org.jboss.as.ee.component.InjectionTargetDescription;
import org.jboss.as.ee.component.ServiceBindingSourceDescription;
import org.jboss.as.jpa.container.PersistenceUnitSearch;
import org.jboss.as.jpa.service.PersistenceContextInjectorService;
import org.jboss.as.jpa.service.PersistenceUnitInjectorService;
import org.jboss.as.jpa.service.PersistenceUnitService;
import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.annotation.CompositeIndex;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import java.util.List;

/**
 * Handle PersistenceContext and PersistenceUnit annotations.
 *
 * TODO: This should iterate over components looking for annotations, not the other way around
 *
 * @author <NAME> (based on ResourceInjectionAnnotationParsingProcessor)
 */
public class JPAAnnotationParseProcessor
    implements DeploymentUnitProcessor {

    private static final DotName PERSISTENCE_CONTEXT_ANNOTATION_NAME = DotName.createSimple(PersistenceContext.class.getName());
    private static final DotName PERSISTENCE_UNIT_ANNOTATION_NAME = DotName.createSimple(PersistenceUnit.class.getName());

    /**
     * Check the deployment annotation index for all classes with the @PersistenceContext annotation.  For each class with the
     * annotation, collect all the required information to create a managed bean instance, and attach it to the context.
     *
     * @param phaseContext the deployment unit context
     * @throws org.jboss.as.server.deployment.DeploymentUnitProcessingException
     *
     */
    public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
        final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
        final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
        final String applicationName = deploymentUnit.getParent() == null ? deploymentUnit.getName() : deploymentUnit.getParent().getName();
        final CompositeIndex compositeIndex = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
        if (compositeIndex == null) {
            return;
        }

        addBindings(deploymentUnit, PERSISTENCE_CONTEXT_ANNOTATION_NAME, compositeIndex, moduleDescription, applicationName, phaseContext);

        addBindings(deploymentUnit, PERSISTENCE_UNIT_ANNOTATION_NAME, compositeIndex, moduleDescription, applicationName, phaseContext);

    }

    private void addBindings(final DeploymentUnit deploymentUnit,
                             final DotName annotationName,
                             final CompositeIndex compositeIndex,
                             final EEModuleDescription moduleDescription,
                             final String applicationName,
                             DeploymentPhaseContext phaseContext)
        throws DeploymentUnitProcessingException {

        final List<AnnotationInstance> instances = compositeIndex.getAnnotations(annotationName);
        if (instances != null) {
            for (AnnotationInstance instance : instances) {
                final BindingDescription binding = getResourceConfiguration(deploymentUnit, instance, phaseContext);
                final String componentName = getComponentName(instance);
                final AbstractComponentDescription componentDescription = moduleDescription.getComponentByClassName(componentName);

                // give hopefully enough information if component isn't found
                if (null == componentDescription) {
                    //if the component is not found it is probably a CDI component
                    //so just return
                    continue;
                }

                componentDescription.getBindings().add(binding);
            }
        }

    }

    private String getComponentName(AnnotationInstance annotation) {
        String name;
        final AnnotationTarget annotationTarget = annotation.target();
        if (annotationTarget instanceof FieldInfo) {
            name = ((FieldInfo) annotationTarget).declaringClass().name().toString();
        } else if (annotationTarget instanceof MethodInfo) {
            name = ((MethodInfo) annotationTarget).declaringClass().name().toString();
        } else if (annotationTarget instanceof ClassInfo) {
            name = ((ClassInfo) annotationTarget).name().toString();
        } else {
            // Don't expect to see this error thrown
            throw new RuntimeException("unexpected error: AnnotationTarget class of type (" + annotationTarget.getClass().getName() + ") is not handled.");
        }
        return name;
    }

    public void undeploy(DeploymentUnit context) {
{'fraction_non_alphanumeric': 0.07479303454182129, 'fraction_numerical': 0.002426491578646874, 'mean_word_length': 3.4727098627513566, 'pattern_counts': {'":': 1, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6990183', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3339, 'n_words': 954}