<commit_before>package no.saua.remock.internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.StandardMethodMetadata;

import java.util.Set;

public class RemockBeanFactory extends DefaultListableBeanFactory {

    private final static Logger log = LoggerFactory.getLogger(RemockBeanFactory.class);

    private Set<Rejecter> rejecters;

    private boolean foundEagerAnnotation = false;

    public RemockBeanFactory(Set<Rejecter> rejecters, boolean foundEagerAnnotation) {
        this.rejecters = rejecters;
        this.foundEagerAnnotation = foundEagerAnnotation;
    }

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        try {
            Class<?> beanClazz = null;
            String beanClassName = beanDefinition.getBeanClassName();

            // Fix for @Bean annotations in @Configuration will return a "null" beanClassName. This
            // finds the class anyway:
            if (beanClassName == null && beanDefinition instanceof AnnotatedBeanDefinition) {
                MethodMetadata factoryMethodMetadata =
                                ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata();
                if (factoryMethodMetadata instanceof StandardMethodMetadata) {
                    beanClazz =
                                    ((StandardMethodMetadata) factoryMethodMetadata).getIntrospectedMethod()
                                                    .getReturnType();
                }
            }


            if (beanClassName != null) {
                beanClazz = Class.forName(beanClassName);
            }
            if (isBeanRejected(beanName, beanClazz)) {
                log.info("Rejected bean [{}] with definiton [{}]", beanName, beanDefinition);
            } else {
                if (!foundEagerAnnotation) {
                    beanDefinition.setLazyInit(true);
                }
                super.registerBeanDefinition(beanName, beanDefinition);
            }<commit_msg>Fix nullpointer when finding beanClass with Spring caching (and other AOP type spring libs)<commit_after>package no.saua.remock.internal;

import java.lang.reflect.Method;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.StandardMethodMetadata;

public class RemockBeanFactory extends DefaultListableBeanFactory {

    private final static Logger log = LoggerFactory.getLogger(RemockBeanFactory.class);

    private Set<Rejecter> rejecters;

    private boolean foundEagerAnnotation = false;

    public RemockBeanFactory(Set<Rejecter> rejecters, boolean foundEagerAnnotation) {
        this.rejecters = rejecters;
        this.foundEagerAnnotation = foundEagerAnnotation;
    }

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        try {
            Class<?> beanClazz = null;
            String beanClassName = beanDefinition.getBeanClassName();

            // Fix for @Bean annotations in @Configuration will return a "null" beanClassName. This
            // finds the class anyway:
            if (beanClassName == null && beanDefinition instanceof AnnotatedBeanDefinition) {
                MethodMetadata factoryMethodMetadata =
                                ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata();
                if (factoryMethodMetadata instanceof StandardMethodMetadata) {
                    StandardMethodMetadata methodMetaData = (StandardMethodMetadata) factoryMethodMetadata;
                    beanClazz = methodMetaData.getIntrospectedMethod().getReturnType();
                } else {
                    // Try to find the method directly from the defining class/method
                    String declaringClassName = factoryMethodMetadata.getDeclaringClassName();
                    Class<?> factoryClazz = Class.forName(declaringClassName);

                    // Note that MethodMetadata does not give us the exact method, we have to loop through to find
                    // one which matches the name -- this is not optimal. But it should not matter much, the only thing
                    // which may trip this up is having to classes with same name but different return values which is
                    // not something we would expect in (sane) @Configuration classes.
                    for (Method m: factoryClazz.getDeclaredMethods()) {
                        if (factoryMethodMetadata.getMethodName().equals(m.getName())) {
                            beanClazz = m.getReturnType();
                        }
                    }
                }
            }

            if (beanClassName != null) {
                beanClazz = Class.forName(beanClassName);
            }
            if (beanClazz != null && isBeanRejected(beanName, beanClazz)) {
                log.info("Rejected bean [{}] with definiton [{}]", beanName, beanDefinition);
            } else {
                if (beanClazz == null) {
                    log.warn("Unable to find beanclass for bean [{}] with definition [{}]", beanName, beanDefinition);
                }
                if (!foundEagerAnnotation) {
                    beanDefinition.setLazyInit(true);
                }
                super.registerBeanDefinition(beanName, beanDefinition);
            }
{'fraction_non_alphanumeric': 0.07534821278738044, 'fraction_numerical': 0.0006712535660345696, 'mean_word_length': 2.3501967397414276, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1216416', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1329, 'n_words': 345}