<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.client.solrj.beans;

import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;

import java.lang.reflect.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.concurrent.ConcurrentHashMap;
import java.nio.ByteBuffer;

/**
 * A class to map objects to and from solr documents.
 * 
 *
 * @since solr 1.3
 */
public class DocumentObjectBinder {
  
  private final Map<Class, List<DocField>> infocache = new ConcurrentHashMap<Class, List<DocField>>();

  public DocumentObjectBinder() {
  }

  public <T> List<T> getBeans(Class<T> clazz, SolrDocumentList solrDocList) {
    List<DocField> fields = getDocFields(clazz);
    List<T> result = new ArrayList<T>(solrDocList.size());

    for (SolrDocument sdoc : solrDocList) {
      result.add(getBean(clazz, fields, sdoc));
    }
    return result;
  }

  public <T> T getBean(Class<T> clazz, SolrDocument solrDoc) {
    return getBean(clazz, null, solrDoc);
  }
  
  private <T> T getBean(Class<T> clazz, List<DocField> fields, SolrDocument solrDoc) {
    if (fields == null) {
      fields = getDocFields(clazz);
    }

    try {
      T obj = clazz.newInstance();
      for (DocField docField : fields) {
        docField.inject(obj, solrDoc);
      }
      return obj;
    } catch (Exception e) {
      throw new BindingException("Could not instantiate object of " + clazz, e);
    }
  }
  
  public SolrInputDocument toSolrInputDocument(Object obj) {
    List<DocField> fields = getDocFields(obj.getClass());
    if (fields.isEmpty()) {
      throw new BindingException("class: " + obj.getClass() + " does not define any fields.");
    }

    SolrInputDocument doc = new SolrInputDocument();
    for (DocField field : fields) {
      if (field.dynamicFieldNamePatternMatcher != null &&
          field.get(obj) != null &&
          field.isContainedInMap) {
        Map<String, Object> mapValue = (Map<String, Object>) field.get(obj);

        for (Map.Entry<String, Object> e : mapValue.entrySet()) {
          doc.setField(e.getKey(), e.getValue(), 1.0f);
        }
      } else {
        doc.setField(field.name, field.get(obj), 1.0f);
      }
    }
    return doc;
  }
  
  private List<DocField> getDocFields(Class clazz) {
    List<DocField> fields = infocache.get(clazz);
    if (fields == null) {
      synchronized(infocache) {
        infocache.put(clazz, fields = collectInfo(clazz));
      }
    }
    return fields;
  }

  private List<DocField> collectInfo(Class clazz) {
    List<DocField> fields = new ArrayList<DocField>();
    Class superClazz = clazz;
    List<AccessibleObject> members = new ArrayList<AccessibleObject>();

    while (superClazz != null && superClazz != Object.class) {
      members.addAll(Arrays.asList(superClazz.getDeclaredFields()));
      members.addAll(Arrays.asList(superClazz.getDeclaredMethods()));
      superClazz = superClazz.getSuperclass();
    }

    for (AccessibleObject member : members) {
      // TODO: Fix below code to use c.isAnnotationPresent(). It was changed
      // to the null check to work around a bug in JDK 8 b78 (see LUCENE-4808).
      if (member.getAnnotation(Field.class) != null) {
        member.setAccessible(true);
        fields.add(new DocField(member));
      }
    }
    return fields;
  }

  private static class DocField {
    private String name;
    private java.lang.reflect.Field field;
    private Method setter;
    private Method getter;
    private Class type;
    private boolean isArray;
    private boolean isList;

    /*
     * dynamic fields may use a Map based data structure to bind a given field.
     * if a mapping is done using, "Map<String, List<String>> foo", <code>isContainedInMap</code>
     * is set to <code>TRUE</code> as well as <code>isList</code> is set to <code>TRUE</code>
     */
    private boolean isContainedInMap;<commit_msg>LUCENE-4808: Undo the workaround for compilation in Java 8, the bug is fixed in b82<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.client.solrj.beans;

import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;

import java.lang.reflect.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.concurrent.ConcurrentHashMap;
import java.nio.ByteBuffer;

/**
 * A class to map objects to and from solr documents.
 * 
 *
 * @since solr 1.3
 */
public class DocumentObjectBinder {
  
  private final Map<Class, List<DocField>> infocache = new ConcurrentHashMap<Class, List<DocField>>();

  public DocumentObjectBinder() {
  }

  public <T> List<T> getBeans(Class<T> clazz, SolrDocumentList solrDocList) {
    List<DocField> fields = getDocFields(clazz);
    List<T> result = new ArrayList<T>(solrDocList.size());

    for (SolrDocument sdoc : solrDocList) {
      result.add(getBean(clazz, fields, sdoc));
    }
    return result;
  }

  public <T> T getBean(Class<T> clazz, SolrDocument solrDoc) {
    return getBean(clazz, null, solrDoc);
  }
  
  private <T> T getBean(Class<T> clazz, List<DocField> fields, SolrDocument solrDoc) {
    if (fields == null) {
      fields = getDocFields(clazz);
    }

    try {
      T obj = clazz.newInstance();
      for (DocField docField : fields) {
        docField.inject(obj, solrDoc);
      }
      return obj;
    } catch (Exception e) {
      throw new BindingException("Could not instantiate object of " + clazz, e);
    }
  }
  
  public SolrInputDocument toSolrInputDocument(Object obj) {
    List<DocField> fields = getDocFields(obj.getClass());
    if (fields.isEmpty()) {
      throw new BindingException("class: " + obj.getClass() + " does not define any fields.");
    }

    SolrInputDocument doc = new SolrInputDocument();
    for (DocField field : fields) {
      if (field.dynamicFieldNamePatternMatcher != null &&
          field.get(obj) != null &&
          field.isContainedInMap) {
        Map<String, Object> mapValue = (Map<String, Object>) field.get(obj);

        for (Map.Entry<String, Object> e : mapValue.entrySet()) {
          doc.setField(e.getKey(), e.getValue(), 1.0f);
        }
      } else {
        doc.setField(field.name, field.get(obj), 1.0f);
      }
    }
    return doc;
  }
  
  private List<DocField> getDocFields(Class clazz) {
    List<DocField> fields = infocache.get(clazz);
    if (fields == null) {
      synchronized(infocache) {
        infocache.put(clazz, fields = collectInfo(clazz));
      }
    }
    return fields;
  }

  private List<DocField> collectInfo(Class clazz) {
    List<DocField> fields = new ArrayList<DocField>();
    Class superClazz = clazz;
    List<AccessibleObject> members = new ArrayList<AccessibleObject>();

    while (superClazz != null && superClazz != Object.class) {
      members.addAll(Arrays.asList(superClazz.getDeclaredFields()));
      members.addAll(Arrays.asList(superClazz.getDeclaredMethods()));
      superClazz = superClazz.getSuperclass();
    }

    for (AccessibleObject member : members) {
      if (member.isAnnotationPresent(Field.class)) {
        member.setAccessible(true);
        fields.add(new DocField(member));
      }
    }
    return fields;
  }

  private static class DocField {
    private String name;
    private java.lang.reflect.Field field;
    private Method setter;
    private Method getter;
    private Class type;
    private boolean isArray;
    private boolean isList;

    /*
     * dynamic fields may use a Map based data structure to bind a given field.
     * if a mapping is done using, "Map<String, List<String>> foo", <code>isContainedInMap</code>
     * is set to <code>TRUE</code> as well as <code>isList</code> is set to <code>TRUE</code>
     */
    private boolean isContainedInMap;
{'fraction_non_alphanumeric': 0.10951315649302226, 'fraction_numerical': 0.0036220304676680515, 'mean_word_length': 3.292638317329675, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '421109', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2818, 'n_words': 831}