<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.bootstrap;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.ProcessAnnotatedType;

import org.jboss.weld.bootstrap.api.helpers.AbstractBootstrapService;
import org.jboss.weld.event.ExtensionObserverMethodImpl;
import org.jboss.weld.resolution.TypeSafeObserverResolver;
import org.jboss.weld.resources.spi.ClassFileInfo;
import org.jboss.weld.resources.spi.ClassFileServices;
import org.jboss.weld.util.Types;
import org.jboss.weld.util.reflection.Reflections;

import com.google.common.base.Predicate;
import com.google.common.collect.Sets;

/**
 * ProcessAnnotatedType observer method resolver. It uses {@link ClassFileServices} for resolution and thus entirely avoids loading the classes which speeds up
 * especially large deployments.
 *
 * Although this resolver covers most of the possible PAT observer method types, there are several cases when {@link ClassFileInfo} used by this resolver is not
 * sufficient to perform observer method resolution correctly. If such observer method is present in the deployment, the constructor of this class throws
 * {@link UnsupportedObserverMethodException}. This exception is expected to be caught by the deployer and observer method resolution using the default
 * {@link TypeSafeObserverResolver} is performed instead.
 *
 * @author <NAME>
 *
 */
public class FastProcessAnnotatedTypeResolver extends AbstractBootstrapService {

    private static class ExactTypePredicate implements Predicate<ClassFileInfo> {
        private final Class<?> type;

        public ExactTypePredicate(Class<?> type) {
            this.type = type;
        }

        @Override
        public boolean apply(ClassFileInfo input) {
            return type.getName().equals(input.getClassName());
        }
    }

    private static class AssignableToPredicate implements Predicate<ClassFileInfo> {

        private final Class<?> type;

        public AssignableToPredicate(Class<?> type) {
            this.type = type;
        }

        @Override
        public boolean apply(ClassFileInfo input) {
            return input.isAssignableTo(type);
        }
    }

    @SuppressWarnings("unchecked")
    private static class CompositePredicate implements Predicate<ClassFileInfo> {

        private static CompositePredicate assignable(Class<?>[] classes) {
            Predicate<ClassFileInfo>[] predicates = new Predicate[classes.length];
            for (int i = 0; i < classes.length; i++) {
                predicates[i] = new AssignableToPredicate(classes[i]);
            }
            return new CompositePredicate(predicates);
        }

        private final Predicate<ClassFileInfo>[] predicates;

        public CompositePredicate(Predicate<ClassFileInfo>[] predicates) {
            this.predicates = predicates;
        }

        @Override
        public boolean apply(ClassFileInfo input) {
            for (Predicate<ClassFileInfo> predicate : predicates) {
                if (!predicate.apply(input)) {
                    return false;
                }
            }
            return true;
        }
    }

    private final Set<ExtensionObserverMethodImpl<?, ?>> catchAllObservers;
    private final Map<ExtensionObserverMethodImpl<?, ?>, Predicate<ClassFileInfo>> observers;

    public FastProcessAnnotatedTypeResolver(Iterable<ObserverMethod<?>> observers) throws UnsupportedObserverMethodException {
        this.catchAllObservers = Sets.newHashSet();
        this.observers = new LinkedHashMap<ExtensionObserverMethodImpl<?, ?>, Predicate<ClassFileInfo>>();
        for (ObserverMethod<?> o : observers) {
            if (o instanceof ExtensionObserverMethodImpl<?, ?>) {
                process((ExtensionObserverMethodImpl<?, ?>) o, o.getObservedType());
            }
        }
    }

    private void process(ExtensionObserverMethodImpl<?, ?> observer, Type observedType) throws UnsupportedObserverMethodException {
        if (Object.class.equals(observedType)) {
            // void observe(Object event)
            catchAllObservers.add(observer);
        } else if (ProcessAnnotatedType.class.equals(observedType)) {
            // void observe(ProcessAnnotatedType event)
            catchAllObservers.add(observer);
        } else if (observedType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) observedType;
            if (ProcessAnnotatedType.class.equals(type.getRawType())) {<commit_msg>Fix FastProcessAnnotatedTypeResolver to only resolve methods with no additional qualifiers<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.bootstrap;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.ProcessAnnotatedType;

import org.jboss.weld.bootstrap.api.helpers.AbstractBootstrapService;
import org.jboss.weld.event.ExtensionObserverMethodImpl;
import org.jboss.weld.resolution.TypeSafeObserverResolver;
import org.jboss.weld.resources.spi.ClassFileInfo;
import org.jboss.weld.resources.spi.ClassFileServices;
import org.jboss.weld.util.Types;
import org.jboss.weld.util.reflection.Reflections;

import com.google.common.base.Predicate;
import com.google.common.collect.Sets;

/**
 * ProcessAnnotatedType observer method resolver. It uses {@link ClassFileServices} for resolution and thus entirely avoids loading the classes which speeds up
 * especially large deployments.
 *
 * Although this resolver covers most of the possible PAT observer method types, there are several cases when {@link ClassFileInfo} used by this resolver is not
 * sufficient to perform observer method resolution correctly. If such observer method is present in the deployment, the constructor of this class throws
 * {@link UnsupportedObserverMethodException}. This exception is expected to be caught by the deployer and observer method resolution using the default
 * {@link TypeSafeObserverResolver} is performed instead.
 *
 * @author <NAME>
 *
 */
public class FastProcessAnnotatedTypeResolver extends AbstractBootstrapService {

    private static class ExactTypePredicate implements Predicate<ClassFileInfo> {
        private final Class<?> type;

        public ExactTypePredicate(Class<?> type) {
            this.type = type;
        }

        @Override
        public boolean apply(ClassFileInfo input) {
            return type.getName().equals(input.getClassName());
        }
    }

    private static class AssignableToPredicate implements Predicate<ClassFileInfo> {

        private final Class<?> type;

        public AssignableToPredicate(Class<?> type) {
            this.type = type;
        }

        @Override
        public boolean apply(ClassFileInfo input) {
            return input.isAssignableTo(type);
        }
    }

    @SuppressWarnings("unchecked")
    private static class CompositePredicate implements Predicate<ClassFileInfo> {

        private static CompositePredicate assignable(Class<?>[] classes) {
            Predicate<ClassFileInfo>[] predicates = new Predicate[classes.length];
            for (int i = 0; i < classes.length; i++) {
                predicates[i] = new AssignableToPredicate(classes[i]);
            }
            return new CompositePredicate(predicates);
        }

        private final Predicate<ClassFileInfo>[] predicates;

        public CompositePredicate(Predicate<ClassFileInfo>[] predicates) {
            this.predicates = predicates;
        }

        @Override
        public boolean apply(ClassFileInfo input) {
            for (Predicate<ClassFileInfo> predicate : predicates) {
                if (!predicate.apply(input)) {
                    return false;
                }
            }
            return true;
        }
    }

    private final Set<ExtensionObserverMethodImpl<?, ?>> catchAllObservers;
    private final Map<ExtensionObserverMethodImpl<?, ?>, Predicate<ClassFileInfo>> observers;

    public FastProcessAnnotatedTypeResolver(Iterable<ObserverMethod<?>> observers) throws UnsupportedObserverMethodException {
        this.catchAllObservers = Sets.newHashSet();
        this.observers = new LinkedHashMap<ExtensionObserverMethodImpl<?, ?>, Predicate<ClassFileInfo>>();
        for (ObserverMethod<?> o : observers) {
            if (o instanceof ExtensionObserverMethodImpl<?, ?>) {
                final Set<Annotation> qualifiers = o.getObservedQualifiers();
                // only process observer methods with no qualifiers or with @Any
                if (qualifiers.isEmpty() || (qualifiers.size() == 1 && Any.class.equals(qualifiers.iterator().next().annotationType()))) {
                    process((ExtensionObserverMethodImpl<?, ?>) o, o.getObservedType());
                }
            }
        }
    }

    private void process(ExtensionObserverMethodImpl<?, ?> observer, Type observedType) throws UnsupportedObserverMethodException {
        if (Object.class.equals(observedType)) {
            // void observe(Object event)
            catchAllObservers.add(observer);
        } else if (ProcessAnnotatedType.class.equals(observedType)) {
            // void observe(ProcessAnnotatedType event)
            catchAllObservers.add(observer);
        } else if (observedType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) observedType;
            if (ProcessAnnotatedType.class.equals(type.getRawType())) {
{'fraction_non_alphanumeric': 0.08592733894043181, 'fraction_numerical': 0.0016474464579901153, 'mean_word_length': 3.3705949223190603, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 58, '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': '7491000', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2890, 'n_words': 856}