<commit_before>/*
 * Copyright DataStax, Inc.
 *
 * Licensed 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 com.datastax.oss.driver.internal.mapper.processor;

import com.datastax.oss.driver.api.mapper.annotations.Dao;
import com.datastax.oss.driver.api.mapper.annotations.Entity;
import com.datastax.oss.driver.api.mapper.annotations.Mapper;
import com.datastax.oss.driver.shaded.guava.common.base.Strings;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
import com.google.auto.service.AutoService;
import com.google.common.base.Throwables;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

@AutoService(Processor.class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class MapperProcessor extends AbstractProcessor {

  private static final String INDENT_AMOUNT_OPTION = "com.datastax.oss.driver.mapper.indent";
  private static final String INDENT_WITH_TABS_OPTION =
      "com.datastax.oss.driver.mapper.indentWithTabs";

  private DecoratedMessager messager;
  private Types typeUtils;
  private Elements elementUtils;
  private Filer filer;
  private String indent;

  @Override
  public synchronized void init(ProcessingEnvironment processingEnvironment) {
    super.init(processingEnvironment);
    messager = new DecoratedMessager(processingEnvironment.getMessager());
    typeUtils = processingEnvironment.getTypeUtils();
    elementUtils = processingEnvironment.getElementUtils();
    filer = processingEnvironment.getFiler();
    indent = computeIndent(processingEnvironment.getOptions());
  }

  @Override
  public boolean process(
      Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
    ProcessorContext context = buildContext(messager, typeUtils, elementUtils, filer, indent);

    CodeGeneratorFactory generatorFactory = context.getCodeGeneratorFactory();
    processAnnotatedTypes(
        roundEnvironment, Entity.class, ElementKind.CLASS, generatorFactory::newEntity);
    processAnnotatedTypes(
        roundEnvironment, Dao.class, ElementKind.INTERFACE, generatorFactory::newDao);
    processAnnotatedTypes(
        roundEnvironment, Mapper.class, ElementKind.INTERFACE, generatorFactory::newMapper);
    return true;
  }

  protected ProcessorContext buildContext(
      DecoratedMessager messager,
      Types typeUtils,
      Elements elementUtils,
      Filer filer,
      String indent) {
    return new DefaultProcessorContext(messager, typeUtils, elementUtils, filer, indent);
  }

  protected void processAnnotatedTypes(
      RoundEnvironment roundEnvironment,
      Class<? extends Annotation> annotationClass,
      ElementKind expectedKind,
      Function<TypeElement, CodeGenerator> generatorFactory) {
    for (Element element : roundEnvironment.getElementsAnnotatedWith(annotationClass)) {
      if (element.getKind() != expectedKind) {
        messager.error(
            element,
            "Only %s elements can be annotated with %s",
            expectedKind,
            annotationClass.getSimpleName());
      } else {
        // Safe cast given that we checked the kind above
        TypeElement typeElement = (TypeElement) element;
        try {
          generatorFactory.apply(typeElement).generate();
        } catch (Exception e) {
          messager.error(
              element,
              "Unexpected error while writing generated code: %s",
              Throwables.getStackTraceAsString(e));
        }
      }
    }
  }

  @Override
  public Set<String> getSupportedAnnotationTypes() {
    return ImmutableSet.of(Entity.class.getName(), Mapper.class.getName(), Dao.class.getName());
  }

  @Override
  public Set<String> getSupportedOptions() {
    return ImmutableSet.of(INDENT_AMOUNT_OPTION, INDENT_WITH_TABS_OPTION);
  }

  private String computeIndent(Map<String, String> options) {
    boolean tabs = options.containsKey(INDENT_WITH_TABS_OPTION);
    String amountSpec = options.get(INDENT_AMOUNT_OPTION);
    if (amountSpec != null) {
      try {
        int amount = Integer.parseInt(amountSpec);
        return Strings.repeat(tabs ? "\t" : " ", amount);
      } catch (NumberFormatException e) {
        messager.warn(
            "Could not parse %s: expected a number, got '%s'. Defaulting to %s.",
            INDENT_AMOUNT_OPTION, amountSpec, tabs ? "1 tab" : "2 spaces");
      }
    }
    return tabs ? "\t" : "  ";
  }
}<commit_msg>Change mapper processor's supported source version to latest<commit_after>/*
 * Copyright DataStax, Inc.
 *
 * Licensed 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 com.datastax.oss.driver.internal.mapper.processor;

import com.datastax.oss.driver.api.mapper.annotations.Dao;
import com.datastax.oss.driver.api.mapper.annotations.Entity;
import com.datastax.oss.driver.api.mapper.annotations.Mapper;
import com.datastax.oss.driver.shaded.guava.common.base.Strings;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
import com.google.auto.service.AutoService;
import com.google.common.base.Throwables;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

@AutoService(Processor.class)
public class MapperProcessor extends AbstractProcessor {

  private static final String INDENT_AMOUNT_OPTION = "com.datastax.oss.driver.mapper.indent";
  private static final String INDENT_WITH_TABS_OPTION =
      "com.datastax.oss.driver.mapper.indentWithTabs";

  private DecoratedMessager messager;
  private Types typeUtils;
  private Elements elementUtils;
  private Filer filer;
  private String indent;

  @Override
  public synchronized void init(ProcessingEnvironment processingEnvironment) {
    super.init(processingEnvironment);
    messager = new DecoratedMessager(processingEnvironment.getMessager());
    typeUtils = processingEnvironment.getTypeUtils();
    elementUtils = processingEnvironment.getElementUtils();
    filer = processingEnvironment.getFiler();
    indent = computeIndent(processingEnvironment.getOptions());
  }

  @Override
  public boolean process(
      Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
    ProcessorContext context = buildContext(messager, typeUtils, elementUtils, filer, indent);

    CodeGeneratorFactory generatorFactory = context.getCodeGeneratorFactory();
    processAnnotatedTypes(
        roundEnvironment, Entity.class, ElementKind.CLASS, generatorFactory::newEntity);
    processAnnotatedTypes(
        roundEnvironment, Dao.class, ElementKind.INTERFACE, generatorFactory::newDao);
    processAnnotatedTypes(
        roundEnvironment, Mapper.class, ElementKind.INTERFACE, generatorFactory::newMapper);
    return true;
  }

  protected ProcessorContext buildContext(
      DecoratedMessager messager,
      Types typeUtils,
      Elements elementUtils,
      Filer filer,
      String indent) {
    return new DefaultProcessorContext(messager, typeUtils, elementUtils, filer, indent);
  }

  protected void processAnnotatedTypes(
      RoundEnvironment roundEnvironment,
      Class<? extends Annotation> annotationClass,
      ElementKind expectedKind,
      Function<TypeElement, CodeGenerator> generatorFactory) {
    for (Element element : roundEnvironment.getElementsAnnotatedWith(annotationClass)) {
      if (element.getKind() != expectedKind) {
        messager.error(
            element,
            "Only %s elements can be annotated with %s",
            expectedKind,
            annotationClass.getSimpleName());
      } else {
        // Safe cast given that we checked the kind above
        TypeElement typeElement = (TypeElement) element;
        try {
          generatorFactory.apply(typeElement).generate();
        } catch (Exception e) {
          messager.error(
              element,
              "Unexpected error while writing generated code: %s",
              Throwables.getStackTraceAsString(e));
        }
      }
    }
  }

  @Override
  public Set<String> getSupportedAnnotationTypes() {
    return ImmutableSet.of(Entity.class.getName(), Mapper.class.getName(), Dao.class.getName());
  }

  @Override
  public Set<String> getSupportedOptions() {
    return ImmutableSet.of(INDENT_AMOUNT_OPTION, INDENT_WITH_TABS_OPTION);
  }

  @Override
  public SourceVersion getSupportedSourceVersion() {
    return SourceVersion.latest();
  }

  private String computeIndent(Map<String, String> options) {
    boolean tabs = options.containsKey(INDENT_WITH_TABS_OPTION);
    String amountSpec = options.get(INDENT_AMOUNT_OPTION);
    if (amountSpec != null) {
      try {
        int amount = Integer.parseInt(amountSpec);
        return Strings.repeat(tabs ? "\t" : " ", amount);
      } catch (NumberFormatException e) {
        messager.warn(
            "Could not parse %s: expected a number, got '%s'. Defaulting to %s.",
            INDENT_AMOUNT_OPTION, amountSpec, tabs ? "1 tab" : "2 spaces");
      }
    }
    return tabs ? "\t" : "  ";
  }
}
{'fraction_non_alphanumeric': 0.09143581386927395, 'fraction_numerical': 0.0011768966141589715, 'mean_word_length': 3.964943820224719, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5401460', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2943, 'n_words': 738}