<commit_before>package com.thoughtworks.xstream.converters.composite;

import com.thoughtworks.xstream.alias.ClassMapper;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.objecttree.reflection.ObjectFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ObjectWithFieldsConverter implements Converter {

    private ClassMapper classMapper;
    private String classAttributeIdentifier;
    private ObjectFactory objectFactory;

    public ObjectWithFieldsConverter(ClassMapper classMapper,String classAttributeIdentifier, ObjectFactory objectFactory) {
        this.classMapper = classMapper;
        this.classAttributeIdentifier = classAttributeIdentifier;
        this.objectFactory = objectFactory;
    }

    public boolean canConvert(Class type) {
        return true;
    }

    public void toXML(MarshallingContext context) {
        Object obj = context.currentObject();
        Iterator fields = getFields(obj.getClass());
        while (fields.hasNext()) {
            Field field = (Field) fields.next();
            field.setAccessible(true);
            try {
                Object newObj = field.get(obj);
                if (newObj != null) {
                    writeFieldAsXML(context, field, newObj);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e); // TODO... should it recover?
            }
        }
    }

    private void writeFieldAsXML(MarshallingContext context, Field field, Object obj) {
        context.xmlStartElement(classMapper.mapNameToXML(field.getName()));

        Class actualType = obj.getClass();

        Class defaultType = classMapper.lookupDefaultType(field.getType());
        if (!actualType.equals(defaultType)) {
            context.xmlAddAttribute(classAttributeIdentifier, classMapper.lookupName(actualType));
        }

        context.convert(obj);

        context.xmlEndElement();
    }

    public Object fromXML(UnmarshallingContext context) {
        Object result = context.currentObject();

        if (result == null) {
            result = objectFactory.create(context.getRequiredType());
        }

        while (context.xmlNextChild()) {
            String fieldName = classMapper.mapNameFromXML(context.xmlElementName());
            Iterator fields = getFields(result.getClass());
            Field field = null;
            while (fields.hasNext()) {
                Field tmp = (Field) fields.next();
                if (tmp.getName().equals(fieldName)) {
                    field = tmp;
                    break;
                }
            }
            if (field == null) {
                throw new RuntimeException("No such field " + result.getClass() + "." + fieldName);
            }

            Class type;
            String classAttribute = context.xmlAttribute(classAttributeIdentifier);
            if (classAttribute == null) {
                type = field.getType();
            } else {
                type = classMapper.lookupType(classAttribute);
            }

            Object fieldValue = context.convertAnother(type);

            try {
                field.setAccessible(true);
                field.set(result, fieldValue);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e); // TODO... should it recover?
            }

            context.xmlPop();
        }
        return result;
    }

    private Iterator getFields(Class cls) {
        List result = new LinkedList();

        while (!Object.class.equals(cls)) {
            Field[] fields = cls.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                int modifiers = field.getModifiers();
                if (field.getName().startsWith("this$")) {<commit_msg>Decent exception<commit_after>package com.thoughtworks.xstream.converters.composite;

import com.thoughtworks.xstream.alias.ClassMapper;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.objecttree.reflection.ObjectFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ObjectWithFieldsConverter implements Converter {

    private ClassMapper classMapper;
    private String classAttributeIdentifier;
    private ObjectFactory objectFactory;

    public ObjectWithFieldsConverter(ClassMapper classMapper,String classAttributeIdentifier, ObjectFactory objectFactory) {
        this.classMapper = classMapper;
        this.classAttributeIdentifier = classAttributeIdentifier;
        this.objectFactory = objectFactory;
    }

    public boolean canConvert(Class type) {
        return true;
    }

    public void toXML(MarshallingContext context) {
        Object obj = context.currentObject();
        Iterator fields = getFields(obj.getClass());
        while (fields.hasNext()) {
            Field field = (Field) fields.next();
            field.setAccessible(true);
            try {
                Object newObj = field.get(obj);
                if (newObj != null) {
                    writeFieldAsXML(context, field, newObj);
                }
            } catch (IllegalAccessException e) {
                throw new ConversionException(
                        "Cannot access field " + obj.getClass() + "." + field.getName(), e);
            }
        }
    }

    private void writeFieldAsXML(MarshallingContext context, Field field, Object obj) {
        context.xmlStartElement(classMapper.mapNameToXML(field.getName()));

        Class actualType = obj.getClass();

        Class defaultType = classMapper.lookupDefaultType(field.getType());
        if (!actualType.equals(defaultType)) {
            context.xmlAddAttribute(classAttributeIdentifier, classMapper.lookupName(actualType));
        }

        context.convert(obj);

        context.xmlEndElement();
    }

    public Object fromXML(UnmarshallingContext context) {
        Object result = context.currentObject();

        if (result == null) {
            result = objectFactory.create(context.getRequiredType());
        }

        while (context.xmlNextChild()) {
            String fieldName = classMapper.mapNameFromXML(context.xmlElementName());
            Iterator fields = getFields(result.getClass());
            Field field = null;
            while (fields.hasNext()) {
                Field tmp = (Field) fields.next();
                if (tmp.getName().equals(fieldName)) {
                    field = tmp;
                    break;
                }
            }
            if (field == null) {
                throw new RuntimeException("No such field " + result.getClass() + "." + fieldName);
            }

            Class type;
            String classAttribute = context.xmlAttribute(classAttributeIdentifier);
            if (classAttribute == null) {
                type = field.getType();
            } else {
                type = classMapper.lookupType(classAttribute);
            }

            Object fieldValue = context.convertAnother(type);

            try {
                field.setAccessible(true);
                field.set(result, fieldValue);
            } catch (IllegalAccessException e) {
                throw new ConversionException(
                        "Cannot access field " + type + "." + field.getName(), e);
            }

            context.xmlPop();
        }
        return result;
    }

    private Iterator getFields(Class cls) {
        List result = new LinkedList();

        while (!Object.class.equals(cls)) {
            Field[] fields = cls.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                int modifiers = field.getModifiers();
                if (field.getName().startsWith("this$")) {
{'fraction_non_alphanumeric': 0.09771284876062747, 'fraction_numerical': 0.00023949227637408692, 'mean_word_length': 2.186570011446013, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '3756078', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2028, 'n_words': 443}