<commit_before>package com.codeaspect.puffer.helpers;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import com.codeaspect.puffer.annotations.PacketMessage;
import com.codeaspect.puffer.cache.Cache;
import com.codeaspect.puffer.cache.MapFieldCache;

/**
 * The Class FieldMetadataHelper is a helper that other classes use to get Field Metadata.
 */
public class FieldMetadataHelper {

	
	/** The field cache. */
	private static Cache<Class<?>,List<Field>> fieldCache = MapFieldCache.getInstance();

	/**
	 * Instantiates a new field metadata helper.
	 */
	private FieldMetadataHelper() {
	}

	/**
	 * Returns the ordered list of fields passed on the positions in @see com.codeaspect.puffer.annotations.PacketMessage .
	 *
	 * @param clazz the clazz
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	private static List<Field> createOrderedFieldList(Class<?> clazz) {
		List<Field> fields = new ArrayList<Field>();

		Collections.addAll(fields, clazz.getDeclaredFields());

		Class<?> superClazz = clazz.getSuperclass();
		while (superClazz != null) {
			Collections.addAll(fields, superClazz.getDeclaredFields());
			superClazz = superClazz.getSuperclass();
		}

		Field[] orderedFields = new Field[fields.size()];
		for (Field fld : fields) {
			if (fld.isAnnotationPresent(PacketMessage.class)) {
				PacketMessage msg = fld.getAnnotation(PacketMessage.class);
				orderedFields[msg.position() - 1]= fld;
			}
		}

		return (List<Field>) CollectionUtils.select(Arrays.asList(orderedFields),
				new Predicate() {
					public boolean evaluate(Object obj) {
						return obj != null;
					}
				});
	}

	/**
	 * Returns the ordered list of fields passed on the positions in {@link com.codeaspect.puffer.annotations.PacketMessage}<br />
	 * If a Field Cache is provided through {@link com.codeaspect.puffer.helpers.CacheSettings#setFieldCache} then the cache will be used for succesive calls
	 *
	 * @param clazz the clazz
	 * @return the ordered field list
	 */
	public static List<Field> getOrderedFieldList(Class<?> clazz) {
		if(fieldCache!=null && fieldCache.contains(clazz)){
			return fieldCache.get(clazz);
		}else{
			List<Field> orderedList = createOrderedFieldList(clazz);
			if(fieldCache!=null){
				synchronized (clazz) {
					fieldCache.put(clazz, orderedList);
				}
			}
			return orderedList;
		}
	}

	/**
	 * Sets the field cache.
	 *
	 * @param fieldCache the field cache
	 */
	static void setFieldCache(Cache<Class<?>,List<Field>> fieldCache) {
		FieldMetadataHelper.fieldCache = fieldCache;
	}

}<commit_msg>Corrected synchronization order<commit_after>package com.codeaspect.puffer.helpers;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import com.codeaspect.puffer.annotations.PacketMessage;
import com.codeaspect.puffer.cache.Cache;
import com.codeaspect.puffer.cache.MapFieldCache;

/**
 * The Class FieldMetadataHelper is a helper that other classes use to get Field Metadata.
 */
public class FieldMetadataHelper {

	
	/** The field cache. */
	private static Cache<Class<?>,List<Field>> fieldCache = MapFieldCache.getInstance();

	/**
	 * Instantiates a new field metadata helper.
	 */
	private FieldMetadataHelper() {
	}

	/**
	 * Returns the ordered list of fields passed on the positions in @see com.codeaspect.puffer.annotations.PacketMessage .
	 *
	 * @param clazz the clazz
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	private static List<Field> createOrderedFieldList(Class<?> clazz) {
		List<Field> fields = new ArrayList<Field>();

		Collections.addAll(fields, clazz.getDeclaredFields());

		Class<?> superClazz = clazz.getSuperclass();
		while (superClazz != null) {
			Collections.addAll(fields, superClazz.getDeclaredFields());
			superClazz = superClazz.getSuperclass();
		}

		Field[] orderedFields = new Field[fields.size()];
		for (Field fld : fields) {
			if (fld.isAnnotationPresent(PacketMessage.class)) {
				PacketMessage msg = fld.getAnnotation(PacketMessage.class);
				orderedFields[msg.position() - 1]= fld;
			}
		}

		return (List<Field>) CollectionUtils.select(Arrays.asList(orderedFields),
				new Predicate() {
					public boolean evaluate(Object obj) {
						return obj != null;
					}
				});
	}

	/**
	 * Returns the ordered list of fields passed on the positions in {@link com.codeaspect.puffer.annotations.PacketMessage}<br />
	 * If a Field Cache is provided through {@link com.codeaspect.puffer.helpers.CacheSettings#setFieldCache} then the cache will be used for succesive calls
	 *
	 * @param clazz the clazz
	 * @return the ordered field list
	 */
	public static List<Field> getOrderedFieldList(Class<?> clazz) {
		if(fieldCache!=null && fieldCache.contains(clazz)){
			return fieldCache.get(clazz);
		}else{
			List<Field> orderedList = createOrderedFieldList(clazz);
			synchronized (clazz) {
				if(fieldCache!=null){
					fieldCache.put(clazz, orderedList);
				}
			}
			return orderedList;
		}
	}

	/**
	 * Sets the field cache.
	 *
	 * @param fieldCache the field cache
	 */
	static void setFieldCache(Cache<Class<?>,List<Field>> fieldCache) {
		FieldMetadataHelper.fieldCache = fieldCache;
	}

}
{'fraction_non_alphanumeric': 0.11766848816029143, 'fraction_numerical': 0.00036429872495446266, 'mean_word_length': 4.336248785228377, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '378146', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1648, 'n_words': 402}