<commit_before>package com.hubspot.rosetta.internal;

import java.lang.reflect.Type;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.hubspot.rosetta.annotations.RosettaCreator;
import com.hubspot.rosetta.annotations.RosettaNaming;
import com.hubspot.rosetta.annotations.RosettaProperty;
import com.hubspot.rosetta.annotations.RosettaValue;
import com.hubspot.rosetta.annotations.StoredAsJson;

public class RosettaAnnotationIntrospector extends NopAnnotationIntrospector {
  private static final long serialVersionUID = 1L;

  private final ObjectMapper objectMapper;

  public RosettaAnnotationIntrospector(ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
  }

  @Override
  public Object findNamingStrategy(AnnotatedClass ac) {
    RosettaNaming ann = ac.getAnnotation(RosettaNaming.class);
    return ann == null ? null : ann.value();
  }

  @Override
  @SuppressWarnings("unchecked")
  public JsonSerializer<?> findSerializer(Annotated a) {
    StoredAsJson storedAsJson = a.getAnnotation(StoredAsJson.class);
    if (storedAsJson == null) {
      return null;
    } else {
      Class<?> type = a.getRawType();
      return storedAsJson.binary() ? new StoredAsJsonBinarySerializer(type) : new StoredAsJsonSerializer(type);
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public JsonDeserializer<?> findDeserializer(Annotated a) {
    StoredAsJson storedAsJson = a.getAnnotation(StoredAsJson.class);
    if (storedAsJson == null) {
      return null;
    } else {
      if (a instanceof AnnotatedMethod) {
        a = ((AnnotatedMethod) a).getParameter(0);
      }

      String empty = StoredAsJson.NULL.equals(storedAsJson.empty()) ? "null" : storedAsJson.empty();
      return new StoredAsJsonDeserializer(a.getRawType(), getType(a), empty, objectMapper);
    }
  }

  @Override
  public boolean hasAsValueAnnotation(AnnotatedMethod am) {
    return am.hasAnnotation(RosettaValue.class);
  }

  @Override
  public boolean hasCreatorAnnotation(Annotated a) {
    return a.hasAnnotation(RosettaCreator.class);
  }

  @Override
  public PropertyName findNameForSerialization(Annotated a) {
    PropertyName propertyName = findRosettaPropertyName(a);
    if (propertyName == null) {
      propertyName = super.findNameForSerialization(a);
    }
    return propertyName;
  }

  @Override
  public PropertyName findNameForDeserialization(Annotated a) {
    PropertyName propertyName = findRosettaPropertyName(a);
    if (propertyName == null) {
      propertyName = super.findNameForDeserialization(a);
    }
    return propertyName;
  }

  @Override
  public Version version() {
    return Version.unknownVersion();
  }

  private PropertyName findRosettaPropertyName(Annotated a) {
    RosettaProperty ann = a.getAnnotation(RosettaProperty.class);
    if (ann != null) {
      return ann.value().isEmpty() ? PropertyName.USE_DEFAULT : new PropertyName(ann.value());
    }
    return null;
  }

  private Type getType(Annotated a) {
    try {
      // Jackson 2.7+
      return a.getType();
    } catch (Throwable t) {
      return a.getGenericType();
    }
  }<commit_msg>Force inclusion to ALWAYS<commit_after>package com.hubspot.rosetta.internal;

import java.lang.reflect.Type;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonInclude.Value;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.hubspot.rosetta.annotations.RosettaCreator;
import com.hubspot.rosetta.annotations.RosettaNaming;
import com.hubspot.rosetta.annotations.RosettaProperty;
import com.hubspot.rosetta.annotations.RosettaValue;
import com.hubspot.rosetta.annotations.StoredAsJson;

public class RosettaAnnotationIntrospector extends NopAnnotationIntrospector {
  private static final long serialVersionUID = 1L;

  private final ObjectMapper objectMapper;

  public RosettaAnnotationIntrospector(ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
  }

  @Override
  public Object findNamingStrategy(AnnotatedClass ac) {
    RosettaNaming ann = ac.getAnnotation(RosettaNaming.class);
    return ann == null ? null : ann.value();
  }

  @Override
  @SuppressWarnings("unchecked")
  public JsonSerializer<?> findSerializer(Annotated a) {
    StoredAsJson storedAsJson = a.getAnnotation(StoredAsJson.class);
    if (storedAsJson == null) {
      return null;
    } else {
      Class<?> type = a.getRawType();
      return storedAsJson.binary() ? new StoredAsJsonBinarySerializer(type) : new StoredAsJsonSerializer(type);
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public JsonDeserializer<?> findDeserializer(Annotated a) {
    StoredAsJson storedAsJson = a.getAnnotation(StoredAsJson.class);
    if (storedAsJson == null) {
      return null;
    } else {
      if (a instanceof AnnotatedMethod) {
        a = ((AnnotatedMethod) a).getParameter(0);
      }

      String empty = StoredAsJson.NULL.equals(storedAsJson.empty()) ? "null" : storedAsJson.empty();
      return new StoredAsJsonDeserializer(a.getRawType(), getType(a), empty, objectMapper);
    }
  }

  @Override
  public boolean hasAsValueAnnotation(AnnotatedMethod am) {
    return am.hasAnnotation(RosettaValue.class);
  }

  @Override
  public boolean hasCreatorAnnotation(Annotated a) {
    return a.hasAnnotation(RosettaCreator.class);
  }

  @Override
  public PropertyName findNameForSerialization(Annotated a) {
    PropertyName propertyName = findRosettaPropertyName(a);
    if (propertyName == null) {
      propertyName = super.findNameForSerialization(a);
    }
    return propertyName;
  }

  @Override
  public PropertyName findNameForDeserialization(Annotated a) {
    PropertyName propertyName = findRosettaPropertyName(a);
    if (propertyName == null) {
      propertyName = super.findNameForDeserialization(a);
    }
    return propertyName;
  }

  @Override
  public Include findSerializationInclusion(Annotated a, Include defValue) {
    return Include.ALWAYS;
  }

  @Override
  public Include findSerializationInclusionForContent(Annotated a, Include defValue) {
    return Include.ALWAYS;
  }

  @Override
  public Value findPropertyInclusion(Annotated a) {
    return Value.construct(Include.ALWAYS, Include.ALWAYS);
  }

  @Override
  public Version version() {
    return Version.unknownVersion();
  }

  private PropertyName findRosettaPropertyName(Annotated a) {
    RosettaProperty ann = a.getAnnotation(RosettaProperty.class);
    if (ann != null) {
      return ann.value().isEmpty() ? PropertyName.USE_DEFAULT : new PropertyName(ann.value());
    }
    return null;
  }

  private Type getType(Annotated a) {
    try {
      // Jackson 2.7+
      return a.getType();
    } catch (Throwable t) {
      return a.getGenericType();
    }
  }
{'fraction_non_alphanumeric': 0.09720597790773229, 'fraction_numerical': 0.0010396361273554255, 'mean_word_length': 4.427362482369534, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '5760734', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2286, 'n_words': 439}