<commit_before>package water;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;

import water.api.Request;
import water.api.RequestArguments;
import water.fvec.Frame;

public abstract class Request2 extends Request {
  @Override protected void registered() {
    try {
      ArrayList<Class> classes = new ArrayList<Class>();
      {
        Class c = getClass();
        while( c != null ) {
          classes.add(c);
          c = c.getSuperclass();
        }
      }
      // Fields from parent classes first
      Collections.reverse(classes);
      ArrayList<Field> fields = new ArrayList<Field>();
      for( Class c : classes )
        for( Field field : c.getDeclaredFields() )
          if( !Modifier.isStatic(field.getModifiers()) )
            fields.add(field);

      for( Field f : fields ) {
        Annotation[] as = f.getAnnotations();
        API api = find(as, API.class);

        if( api != null && Helper.isInput(api) ) {
          f.setAccessible(true);
          Object defaultValue = f.get(this);

          // Create an Argument instance to reuse existing Web framework for now
          Argument arg = null;

          if( Argument.class.isAssignableFrom(api.filter()) )
            arg = (Argument) newInstance(api);
          else {
            // Real
            if( f.getType() == float.class || f.getType() == double.class ) {
              double val = ((Number) defaultValue).doubleValue();
              arg = new Real(f.getName(), api.required(), val, null, null, api.help());
            }

            // LongInt
            else if( f.getType() == int.class || f.getType() == long.class ) {
              long val = ((Number) defaultValue).longValue();
              arg = new LongInt(f.getName(), api.required(), val, null, null, api.help());
            }

            // Bool
            else if( f.getType() == boolean.class ) {
              boolean val = (Boolean) defaultValue;
              arg = new Bool(f.getName(), val, api.help());
            }

            // Key
            else if( f.getType() == Key.class )
              arg = new H2OKey(f.getName(), api.required());
          }

          if( ColumnSelect.class.isAssignableFrom(api.filter()) ) {
            ColumnSelect name = (ColumnSelect) newInstance(api);
            H2OHexKey key = null;
            for( Argument a : _arguments )
              if( a instanceof H2OHexKey && name._key.equals(((H2OHexKey) a)._name) )
                key = (H2OHexKey) a;
            arg = new HexAllColumnSelect(f.getName(), key);
          }
          if( VecSelect.class.isAssignableFrom(api.filter()) ) {
            VecSelect name = (VecSelect) newInstance(api);
            FrameKey key = null;
            for( Argument a : _arguments )
              if( a instanceof FrameKey && name._key.equals(((FrameKey) a)._name) )
                key = (FrameKey) a;
            arg = new FrameKeyVec(f.getName(), key);
          }

          if( arg != null ) {
            arg._name = f.getName();
            arg._required = api.required();
            arg._field = f;
          }
        }
      }
    } catch( Exception e ) {
      throw new RuntimeException(e);
    }
  }

  // Extracted in separate class as Weaver cannot load REquest during boot
  static final class Helper {
    static boolean isInput(API api) {
      return api.filter() != Filter.class || api.filters().length != 0;
    }
  }

  private static <T> T find(Annotation[] as, Class<T> c) {
    for( Annotation a : as )
      if( a.annotationType() == c )
        return (T) a;
    return null;
  }

  private Filter newInstance(API api) throws Exception {
    for( Constructor c : api.filter().getDeclaredConstructors() ) {
      c.setAccessible(true);
      Class[] ps = c.getParameterTypes();
      if( ps.length == 0 )
        return (Filter) c.newInstance();
      if( ps.length == 1 && RequestArguments.class.isAssignableFrom(ps[0]) )
        return (Filter) c.newInstance(this);
    }
    throw new Exception("Class " + api.filter().getName() + " must have an empty constructor");
  }

  // Create an instance per call instead of ThreadLocals
  @Override protected Request create(Properties parms) {
    Request2 request;
    try {
      request = getClass().newInstance();
      request._arguments = _arguments;
    } catch( Exception e ) {
      throw new RuntimeException(e);
    }
    return request;<commit_msg>Ignore constructors order in Request2<commit_after>package water;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;

import water.api.Request;
import water.api.RequestArguments;
import water.fvec.Frame;

public abstract class Request2 extends Request {
  @Override protected void registered() {
    try {
      ArrayList<Class> classes = new ArrayList<Class>();
      {
        Class c = getClass();
        while( c != null ) {
          classes.add(c);
          c = c.getSuperclass();
        }
      }
      // Fields from parent classes first
      Collections.reverse(classes);
      ArrayList<Field> fields = new ArrayList<Field>();
      for( Class c : classes )
        for( Field field : c.getDeclaredFields() )
          if( !Modifier.isStatic(field.getModifiers()) )
            fields.add(field);

      for( Field f : fields ) {
        Annotation[] as = f.getAnnotations();
        API api = find(as, API.class);

        if( api != null && Helper.isInput(api) ) {
          f.setAccessible(true);
          Object defaultValue = f.get(this);

          // Create an Argument instance to reuse existing Web framework for now
          Argument arg = null;

          if( Argument.class.isAssignableFrom(api.filter()) )
            arg = (Argument) newInstance(api);
          else {
            // Real
            if( f.getType() == float.class || f.getType() == double.class ) {
              double val = ((Number) defaultValue).doubleValue();
              arg = new Real(f.getName(), api.required(), val, null, null, api.help());
            }

            // LongInt
            else if( f.getType() == int.class || f.getType() == long.class ) {
              long val = ((Number) defaultValue).longValue();
              arg = new LongInt(f.getName(), api.required(), val, null, null, api.help());
            }

            // Bool
            else if( f.getType() == boolean.class ) {
              boolean val = (Boolean) defaultValue;
              arg = new Bool(f.getName(), val, api.help());
            }

            // Key
            else if( f.getType() == Key.class )
              arg = new H2OKey(f.getName(), api.required());
          }

          if( ColumnSelect.class.isAssignableFrom(api.filter()) ) {
            ColumnSelect name = (ColumnSelect) newInstance(api);
            H2OHexKey key = null;
            for( Argument a : _arguments )
              if( a instanceof H2OHexKey && name._key.equals(((H2OHexKey) a)._name) )
                key = (H2OHexKey) a;
            arg = new HexAllColumnSelect(f.getName(), key);
          }
          if( VecSelect.class.isAssignableFrom(api.filter()) ) {
            VecSelect name = (VecSelect) newInstance(api);
            FrameKey key = null;
            for( Argument a : _arguments )
              if( a instanceof FrameKey && name._key.equals(((FrameKey) a)._name) )
                key = (FrameKey) a;
            arg = new FrameKeyVec(f.getName(), key);
          }

          if( arg != null ) {
            arg._name = f.getName();
            arg._required = api.required();
            arg._field = f;
          }
        }
      }
    } catch( Exception e ) {
      throw new RuntimeException(e);
    }
  }

  // Extracted in separate class as Weaver cannot load REquest during boot
  static final class Helper {
    static boolean isInput(API api) {
      return api.filter() != Filter.class || api.filters().length != 0;
    }
  }

  private static <T> T find(Annotation[] as, Class<T> c) {
    for( Annotation a : as )
      if( a.annotationType() == c )
        return (T) a;
    return null;
  }

  private Filter newInstance(API api) throws Exception {
    for( Constructor c : api.filter().getDeclaredConstructors() ) {
      c.setAccessible(true);
      Class[] ps = c.getParameterTypes();
      if( ps.length == 1 && RequestArguments.class.isAssignableFrom(ps[0]) )
        return (Filter) c.newInstance(this);
    }
    for( Constructor c : api.filter().getDeclaredConstructors() ) {
      Class[] ps = c.getParameterTypes();
      if( ps.length == 0 )
        return (Filter) c.newInstance();
    }
    throw new Exception("Class " + api.filter().getName() + " must have an empty constructor");
  }

  // Create an instance per call instead of ThreadLocals
  @Override protected Request create(Properties parms) {
    Request2 request;
    try {
      request = getClass().newInstance();
      request._arguments = _arguments;
    } catch( Exception e ) {
      throw new RuntimeException(e);
    }
    return request;
{'fraction_non_alphanumeric': 0.1283303730017762, 'fraction_numerical': 0.0025532859680284193, 'mean_word_length': 2.000999333777482, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4208114', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2490, 'n_words': 678}