<commit_before>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.util.fudgemsg;

import org.fudgemsg.mapping.*;

import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Utilities for converting Beans to Fudge and vice versa.
 */
public final class InnerClassFudgeBuilderFactory extends FudgeBuilderFactoryAdapter {

  /**
   * Map of bean class to builder.
   */
  private final FudgeBuilder _innerClassFudgeBuilder = new InnerClassFudgeBuilder(getDelegate());

  /**
   * Initializes an instance of this factory.
   * This extracts the existing factory from the dictionary, wraps it, and updates it.
   * @param dictionary  the object dictionary to install into, not null
   */
  public static void init(final FudgeObjectDictionary dictionary) {
    FudgeBuilderFactory factory = new InnerClassFudgeBuilderFactory(dictionary.getDefaultBuilderFactory());
    dictionary.setDefaultBuilderFactory(factory);
  }

  /**
   * Constructor.
   * @param parent  the parent factory, not null
   */
  private InnerClassFudgeBuilderFactory(final FudgeBuilderFactory parent) {
    super(parent);
  }

  private <T> boolean canBeUsed(final Class<T> clazz) {
    return
      !(Map.class.isAssignableFrom(clazz) || Set.class.isAssignableFrom(clazz) || List.class.isAssignableFrom(clazz) || Collection.class.isAssignableFrom(clazz)) &&
      clazz.getEnclosingClass() != null                       // the class is inner class 
        && (constructorsCount(clazz) == 1)                    // and it have single only constructor
        && clazz.getSuperclass().getEnclosingClass() == null  // and its super class is not inner one    
        && hasSingleZeroArgConstructor(clazz.getSuperclass());// and its super class has single zero param constructor
  }

  private static boolean hasSingleZeroArgConstructor(final Class clazz) {
    return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
      @Override
      public Boolean run() {
        Constructor<?>[] ctors = clazz.getDeclaredConstructors();
        return ctors.length == 1 && ctors[0].getParameterTypes().length == 0;
      }
    });
  }

  private static int constructorsCount(final Class clazz) {
    return AccessController.doPrivileged(new PrivilegedAction<Integer>() {
      @Override
      public Integer run() {
        Constructor<?>[] ctors = clazz.getDeclaredConstructors();
        return ctors.length;
      }
    });
  }


  @Override
  public <T> FudgeMessageBuilder<T> createMessageBuilder(final Class<T> clazz) {
    if (canBeUsed(clazz)) {
      return _innerClassFudgeBuilder;
    } else {
      return super.createMessageBuilder(clazz);
    }
  }


  @Override
  public <T> FudgeObjectBuilder<T> createObjectBuilder(final Class<T> clazz) {
    if (canBeUsed(clazz)) {
      return _innerClassFudgeBuilder;
    } else {
      return super.createObjectBuilder(clazz);
    }
  }
<commit_msg>checking if there is a default non "org.fudgemsg.mapping.JavaBeanBuilder" builder first, before the InnerClassFudgeBuilder one<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.util.fudgemsg;

import org.fudgemsg.mapping.*;

import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Utilities for converting Beans to Fudge and vice versa.
 */
public final class InnerClassFudgeBuilderFactory extends FudgeBuilderFactoryAdapter {

  /**
   * Map of bean class to builder.
   */
  private final FudgeBuilder _innerClassFudgeBuilder = new InnerClassFudgeBuilder(getDelegate());

  /**
   * Initializes an instance of this factory.
   * This extracts the existing factory from the dictionary, wraps it, and updates it.
   * @param dictionary  the object dictionary to install into, not null
   */
  public static void init(final FudgeObjectDictionary dictionary) {
    FudgeBuilderFactory factory = new InnerClassFudgeBuilderFactory(dictionary.getDefaultBuilderFactory());
    dictionary.setDefaultBuilderFactory(factory);
  }

  /**
   * Constructor.
   * @param parent  the parent factory, not null
   */
  private InnerClassFudgeBuilderFactory(final FudgeBuilderFactory parent) {
    super(parent);
  }

  private <T> boolean canBeUsed(final Class<T> clazz) {
    return
      !(Map.class.isAssignableFrom(clazz) || Set.class.isAssignableFrom(clazz) || List.class.isAssignableFrom(clazz) || Collection.class.isAssignableFrom(clazz)) &&
        clazz.getEnclosingClass() != null                       // the class is inner class 
        && (constructorsCount(clazz) == 1)                    // and it have single only constructor
        && clazz.getSuperclass().getEnclosingClass() == null  // and its super class is not inner one    
        && hasSingleZeroArgConstructor(clazz.getSuperclass());// and its super class has single zero param constructor
  }

  private static boolean hasSingleZeroArgConstructor(final Class clazz) {
    return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
      @Override
      public Boolean run() {
        Constructor<?>[] ctors = clazz.getDeclaredConstructors();
        return ctors.length == 1 && ctors[0].getParameterTypes().length == 0;
      }
    });
  }

  private static int constructorsCount(final Class clazz) {
    return AccessController.doPrivileged(new PrivilegedAction<Integer>() {
      @Override
      public Integer run() {
        Constructor<?>[] ctors = clazz.getDeclaredConstructors();
        return ctors.length;
      }
    });
  }


  @Override
  public <T> FudgeMessageBuilder<T> createMessageBuilder(final Class<T> clazz) {
    if (canBeUsed(clazz)) {
      FudgeMessageBuilder defaultBuilder = super.createMessageBuilder(clazz);
      if (defaultBuilder.getClass().getName().equals("org.fudgemsg.mapping.JavaBeanBuilder")) {
        return _innerClassFudgeBuilder;
      } else {
        return defaultBuilder;
      }
    } else {
      return super.createMessageBuilder(clazz);
    }
  }


  @Override
  public <T> FudgeObjectBuilder<T> createObjectBuilder(final Class<T> clazz) {
    if (canBeUsed(clazz)) {
      FudgeObjectBuilder defaultBuilder = super.createObjectBuilder(clazz);
      if (defaultBuilder.getClass().getName().equals("org.fudgemsg.mapping.JavaBeanBuilder")) {
        return _innerClassFudgeBuilder;
      } else {
        return defaultBuilder;
      }
    } else {
      return super.createObjectBuilder(clazz);
    }
  }

{'fraction_non_alphanumeric': 0.09881941407958024, 'fraction_numerical': 0.0023320215711995335, 'mean_word_length': 3.5473823724320743, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '5531409', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1893, 'n_words': 489}