<commit_before>/**
 * Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.seedstack.shed.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Executable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;

public abstract class StandardAnnotationResolver<E extends AnnotatedElement, A extends Annotation> implements AnnotationResolver<E, A> {
    private final Class<E> annotatedElementClass;
    private final Class<A> annotationClass;

    @SuppressWarnings("unchecked")
    protected StandardAnnotationResolver() {
        Type[] actualTypeArguments = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
        annotatedElementClass = (Class<E>) actualTypeArguments[0];
        annotationClass = (Class<A>) actualTypeArguments[1];
    }

    @Override
    public Optional<A> apply(E element) {
        return resolveAnnotation(element);
    }

    @Override
    public boolean test(E element) {
        return resolveAnnotation(element).isPresent();
    }

    private Optional<A> resolveAnnotation(E element) {
        Annotations.OnAnnotatedElement on;
        if (Executable.class.isAssignableFrom(annotatedElementClass)) {
            on = Annotations.on(((Executable) element)).traversingOverriddenMembers();
        } else {
            on = Annotations.on(element);
        }
        return on.fallingBackOnClasses()
                .includingMetaAnnotations()
                .find(annotationClass);
    }
}
<commit_msg>Avoid returning annotated annotations in standard resolver<commit_after>/**
 * Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.seedstack.shed.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Executable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;

public abstract class StandardAnnotationResolver<E extends AnnotatedElement, A extends Annotation> implements AnnotationResolver<E, A> {
    private final Class<A> annotationClass;

    @SuppressWarnings("unchecked")
    protected StandardAnnotationResolver() {
        Type[] actualTypeArguments = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
        annotationClass = (Class<A>) Types.rawClassOf(actualTypeArguments[1]);
    }

    @Override
    public Optional<A> apply(E element) {
        return resolveAnnotation(element);
    }

    @Override
    public boolean test(E element) {
        return resolveAnnotation(element).isPresent();
    }

    private Optional<A> resolveAnnotation(E element) {
        Annotations.OnAnnotatedElement on;
        if (element instanceof Class<?>) {
            if (Annotation.class.isAssignableFrom((Class<?>) element)) {
                // do not return annotated annotations
                return Optional.empty();
            } else {
                on = Annotations.on(element);
            }
        } else if (element instanceof Executable) {
            on = Annotations.on(((Executable) element)).traversingOverriddenMembers();
        } else {
            return Optional.empty();
        }
        return on.fallingBackOnClasses()
                .includingMetaAnnotations()
                .find(annotationClass);
    }
}

{'fraction_non_alphanumeric': 0.10135135135135136, 'fraction_numerical': 0.007016632016632017, 'mean_word_length': 3.271920088790233, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7661962', 'n_tokens_mistral': 1050, 'n_tokens_neox': 1001, 'n_words': 261}