<commit_before>package br.com.fixturefactory;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import br.com.fixturefactory.util.CalendarTransformer;
import br.com.fixturefactory.util.ReflectionUtils;

public class ObjectFactory {

	private static final String PLACE_HOLDER_START = "${";
	private static final String PLACE_HOLDER_END = "}";
	
	private TemplateHolder templateHolder;
	
	private Object owner;
	
	public ObjectFactory(TemplateHolder templateHolder) {
		this.templateHolder = templateHolder;
	}
	
	public ObjectFactory(TemplateHolder templateHolder, Object owner) {
		this.templateHolder = templateHolder;
		this.owner = owner;
	}
	
	
	@SuppressWarnings("unchecked")
	public <T> T gimme(String label) {
		Rule rule = templateHolder.getRules().get(label);
		 	
		if (rule == null) {
			throw new IllegalArgumentException("No such label: " + label);
		}

		return (T) this.createObject(rule);
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> gimme(Integer quantity, String label) {
		Rule rule = templateHolder.getRules().get(label);
		
		if (rule == null) {
			throw new IllegalArgumentException("No such label: " + label);
		}

		List<T> results = new ArrayList<T>(quantity);
		for (int i = 0; i < quantity; i++) {
			results.add((T) this.createObject(rule));
		}	
		
		return results;
	}

	private Object createObject(Rule rule) {
		Object result;
		if (owner == null || !ReflectionUtils.isInnerClass(templateHolder.getClazz()))  {
			result = ReflectionUtils.newInstance(templateHolder.getClazz());	
		} else {
			result = ReflectionUtils.newInnerClassInstance(templateHolder.getClazz(), owner);	
		}
		
		
		for (Property property : rule.getProperties()) {
			Class<?> fieldType = ReflectionUtils.invokeRecursiveType(result, property.getName());
			
			Object value = property.hasRelationFunction() || fieldType.getEnclosingClass() != null ?
					property.getValue(result) : property.getValue();
			
			
			if (value instanceof String) {
				String baseValue = (String) value;
				int start = baseValue.indexOf(PLACE_HOLDER_START);
				if (start >= 0) {
					String propertyReference = baseValue.substring(start + PLACE_HOLDER_START.length(), baseValue.indexOf(PLACE_HOLDER_END));
					value = baseValue.replace(PLACE_HOLDER_START + propertyReference + PLACE_HOLDER_END, ReflectionUtils.invokeRecursiveGetter(result, propertyReference).toString());
				}
			}
		<commit_msg>fixing inner class verify on instatiate properties on ObjectFactory<commit_after>package br.com.fixturefactory;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import br.com.fixturefactory.util.CalendarTransformer;
import br.com.fixturefactory.util.ReflectionUtils;

public class ObjectFactory {

	private static final String PLACE_HOLDER_START = "${";
	private static final String PLACE_HOLDER_END = "}";
	
	private TemplateHolder templateHolder;
	
	private Object owner;
	
	public ObjectFactory(TemplateHolder templateHolder) {
		this.templateHolder = templateHolder;
	}
	
	public ObjectFactory(TemplateHolder templateHolder, Object owner) {
		this.templateHolder = templateHolder;
		this.owner = owner;
	}
	
	
	@SuppressWarnings("unchecked")
	public <T> T gimme(String label) {
		Rule rule = templateHolder.getRules().get(label);
		 	
		if (rule == null) {
			throw new IllegalArgumentException("No such label: " + label);
		}

		return (T) this.createObject(rule);
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> gimme(Integer quantity, String label) {
		Rule rule = templateHolder.getRules().get(label);
		
		if (rule == null) {
			throw new IllegalArgumentException("No such label: " + label);
		}

		List<T> results = new ArrayList<T>(quantity);
		for (int i = 0; i < quantity; i++) {
			results.add((T) this.createObject(rule));
		}	
		
		return results;
	}

	private Object createObject(Rule rule) {
		Object result;
		if (owner == null || !ReflectionUtils.isInnerClass(templateHolder.getClazz()))  {
			result = ReflectionUtils.newInstance(templateHolder.getClazz());	
		} else {
			result = ReflectionUtils.newInnerClassInstance(templateHolder.getClazz(), owner);	
		}
		
		
		for (Property property : rule.getProperties()) {
			Class<?> fieldType = ReflectionUtils.invokeRecursiveType(result, property.getName());
			
			Object value = property.hasRelationFunction() || ReflectionUtils.isInnerClass(fieldType) ?
					property.getValue(result) : property.getValue();
			
			
			if (value instanceof String) {
				String baseValue = (String) value;
				int start = baseValue.indexOf(PLACE_HOLDER_START);
				if (start >= 0) {
					String propertyReference = baseValue.substring(start + PLACE_HOLDER_START.length(), baseValue.indexOf(PLACE_HOLDER_END));
					value = baseValue.replace(PLACE_HOLDER_START + propertyReference + PLACE_HOLDER_END, ReflectionUtils.invokeRecursiveGetter(result, propertyReference).toString());
				}
			}
		
{'fraction_non_alphanumeric': 0.11441368078175895, 'fraction_numerical': 0.0008143322475570033, 'mean_word_length': 4.653624856156502, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '1082900', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1450, 'n_words': 321}