<commit_before>/**
 * Copyright 2008-2016 <NAME>
 *
 * 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 com.aspectran.core.context.bean;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import com.aspectran.core.context.ActivityContext;
import com.aspectran.core.context.bean.annotation.*;
import com.aspectran.core.context.expr.token.Token;
import com.aspectran.core.context.expr.token.TokenParser;
import com.aspectran.core.context.rule.AutowireRule;
import com.aspectran.core.context.rule.BeanRule;
import com.aspectran.core.context.rule.DispatchResponseRule;
import com.aspectran.core.context.rule.ForwardResponseRule;
import com.aspectran.core.context.rule.MethodActionRule;
import com.aspectran.core.context.rule.RedirectResponseRule;
import com.aspectran.core.context.rule.ResponseRule;
import com.aspectran.core.context.rule.TransformRule;
import com.aspectran.core.context.rule.TransletRule;
import com.aspectran.core.context.rule.type.AutowireTargetType;
import com.aspectran.core.context.rule.type.RequestMethodType;
import com.aspectran.core.context.rule.type.TransformType;
import com.aspectran.core.util.ProfilesUtils;
import com.aspectran.core.util.StringUtils;
import com.aspectran.core.util.logging.Log;
import com.aspectran.core.util.logging.LogFactory;

/**
 * The Class AnnotatedConfigParser.
 *
 * <p>Created: 2016. 2. 16.</p>
 *
 * @since 2.0.0
 * @author <NAME>
 */
public class AnnotatedConfigParser {

	private final Log log = LogFactory.getLog(AnnotatedConfigParser.class);

	private final BeanRuleRegistry beanRuleRegistry;
	
	private final AnnotatedConfigRelater relater;
	
	private final Map<String, BeanRule> idBasedBeanRuleMap;

	private final Map<Class<?>, Set<BeanRule>> typeBasedBeanRuleMap;

	private final Map<Class<?>, BeanRule> configBeanRuleMap;

	private String[] activeProfiles;

	public AnnotatedConfigParser(BeanRuleRegistry beanRuleRegistry, AnnotatedConfigRelater relater) {
		this.beanRuleRegistry = beanRuleRegistry;
		this.relater = relater;
		
		this.idBasedBeanRuleMap = beanRuleRegistry.getIdBasedBeanRuleMap();
		this.typeBasedBeanRuleMap = beanRuleRegistry.getTypeBasedBeanRuleMap();
		this.configBeanRuleMap = beanRuleRegistry.getConfigBeanRuleMap();
	}
	
	public void setActiveProfiles(String[] activeProfiles) {
		this.activeProfiles = activeProfiles;
	}

	public void parse() {
		if(log.isDebugEnabled())
			log.debug("Parsed bean rules for configuring: " + configBeanRuleMap.size());

		for(BeanRule beanRule : configBeanRuleMap.values()) {
			if(!beanRule.isOffered()) {
				parseConfigBean(beanRule);
			}
		}

		if(log.isDebugEnabled())
			log.debug("Parsed id based bean rules: " + idBasedBeanRuleMap.size());

		for(BeanRule beanRule : idBasedBeanRuleMap.values()) {
			if(!beanRule.isOffered()) {
				parseFieldAutowire(beanRule);
				parseMethodAutowire(beanRule);
			}
		}

		if(log.isDebugEnabled())
			log.debug("Parsed type based bean rules: " + typeBasedBeanRuleMap.size());

		for(Set<BeanRule> set : typeBasedBeanRuleMap.values()) {
			for(BeanRule beanRule : set) {
				if(!beanRule.isOffered()) {
					parseFieldAutowire(beanRule);
					parseMethodAutowire(beanRule);
				}
			}
		}
	}
	
	private void parseConfigBean(BeanRule beanRule) {
		Class<?> beanClass = beanRule.getBeanClass();
		Configuration configAnno = beanClass.getAnnotation(Configuration.class);

		if(configAnno != null) {
			Profile profileAnno = beanClass.getAnnotation(Profile.class);
			if(!ProfilesUtils.acceptsProfiles(activeProfiles, profileAnno.value()))
				return;

			String[] nameArray = splitNamespace(configAnno.namespace());

			for(Method method : beanClass.getMethods()) {
				if(method.isAnnotationPresent(Profile.class)) {
					profileAnno = method.getAnnotation(Profile.class);
					if(!ProfilesUtils.acceptsProfiles(activeProfiles, profileAnno.value()))
						continue;
				}
				if(method.isAnnotationPresent(Bean.class)) {
					parseBeanRule(beanClass, method, nameArray);
				} else if(method.isAnnotationPresent(Request.class)) {<commit_msg>Fixed null pointer exception when profileAnno is null<commit_after>/**
 * Copyright 2008-2016 <NAME>
 *
 * 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 com.aspectran.core.context.bean;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import com.aspectran.core.context.ActivityContext;
import com.aspectran.core.context.bean.annotation.*;
import com.aspectran.core.context.expr.token.Token;
import com.aspectran.core.context.expr.token.TokenParser;
import com.aspectran.core.context.rule.AutowireRule;
import com.aspectran.core.context.rule.BeanRule;
import com.aspectran.core.context.rule.DispatchResponseRule;
import com.aspectran.core.context.rule.ForwardResponseRule;
import com.aspectran.core.context.rule.MethodActionRule;
import com.aspectran.core.context.rule.RedirectResponseRule;
import com.aspectran.core.context.rule.ResponseRule;
import com.aspectran.core.context.rule.TransformRule;
import com.aspectran.core.context.rule.TransletRule;
import com.aspectran.core.context.rule.type.AutowireTargetType;
import com.aspectran.core.context.rule.type.RequestMethodType;
import com.aspectran.core.context.rule.type.TransformType;
import com.aspectran.core.util.ProfilesUtils;
import com.aspectran.core.util.StringUtils;
import com.aspectran.core.util.logging.Log;
import com.aspectran.core.util.logging.LogFactory;

/**
 * The Class AnnotatedConfigParser.
 *
 * <p>Created: 2016. 2. 16.</p>
 *
 * @since 2.0.0
 * @author <NAME>
 */
public class AnnotatedConfigParser {

	private final Log log = LogFactory.getLog(AnnotatedConfigParser.class);

	private final BeanRuleRegistry beanRuleRegistry;
	
	private final AnnotatedConfigRelater relater;
	
	private final Map<String, BeanRule> idBasedBeanRuleMap;

	private final Map<Class<?>, Set<BeanRule>> typeBasedBeanRuleMap;

	private final Map<Class<?>, BeanRule> configBeanRuleMap;

	private String[] activeProfiles;

	public AnnotatedConfigParser(BeanRuleRegistry beanRuleRegistry, AnnotatedConfigRelater relater) {
		this.beanRuleRegistry = beanRuleRegistry;
		this.relater = relater;
		
		this.idBasedBeanRuleMap = beanRuleRegistry.getIdBasedBeanRuleMap();
		this.typeBasedBeanRuleMap = beanRuleRegistry.getTypeBasedBeanRuleMap();
		this.configBeanRuleMap = beanRuleRegistry.getConfigBeanRuleMap();
	}
	
	public void setActiveProfiles(String[] activeProfiles) {
		this.activeProfiles = activeProfiles;
	}

	public void parse() {
		if(log.isDebugEnabled())
			log.debug("Parsed bean rules for configuring: " + configBeanRuleMap.size());

		for(BeanRule beanRule : configBeanRuleMap.values()) {
			if(!beanRule.isOffered()) {
				parseConfigBean(beanRule);
			}
		}

		if(log.isDebugEnabled())
			log.debug("Parsed id based bean rules: " + idBasedBeanRuleMap.size());

		for(BeanRule beanRule : idBasedBeanRuleMap.values()) {
			if(!beanRule.isOffered()) {
				parseFieldAutowire(beanRule);
				parseMethodAutowire(beanRule);
			}
		}

		if(log.isDebugEnabled())
			log.debug("Parsed type based bean rules: " + typeBasedBeanRuleMap.size());

		for(Set<BeanRule> set : typeBasedBeanRuleMap.values()) {
			for(BeanRule beanRule : set) {
				if(!beanRule.isOffered()) {
					parseFieldAutowire(beanRule);
					parseMethodAutowire(beanRule);
				}
			}
		}
	}
	
	private void parseConfigBean(BeanRule beanRule) {
		Class<?> beanClass = beanRule.getBeanClass();
		Configuration configAnno = beanClass.getAnnotation(Configuration.class);

		if(configAnno != null) {
			if(beanClass.isAnnotationPresent(Profile.class)) {
				Profile profileAnno = beanClass.getAnnotation(Profile.class);
				if(!ProfilesUtils.acceptsProfiles(activeProfiles, profileAnno.value()))
					return;
			}

			String[] nameArray = splitNamespace(configAnno.namespace());

			for(Method method : beanClass.getMethods()) {
				if(method.isAnnotationPresent(Profile.class)) {
					Profile profileAnno = method.getAnnotation(Profile.class);
					if(!ProfilesUtils.acceptsProfiles(activeProfiles, profileAnno.value()))
						continue;
				}
				if(method.isAnnotationPresent(Bean.class)) {
					parseBeanRule(beanClass, method, nameArray);
				} else if(method.isAnnotationPresent(Request.class)) {
{'fraction_non_alphanumeric': 0.11064718162839249, 'fraction_numerical': 0.004592901878914405, 'mean_word_length': 5.1064372211599744, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '1696017', 'n_tokens_mistral': 3173, 'n_tokens_neox': 2895, 'n_words': 585}