<commit_before>/*
 * Copyright 2016 requery.io
 *
 * 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 io.requery.processor;

import io.requery.Embedded;
import io.requery.Entity;
import io.requery.Superclass;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static io.requery.processor.EntityProcessor.GENERATE_ALWAYS;
import static io.requery.processor.EntityProcessor.GENERATE_JPA;
import static io.requery.processor.EntityProcessor.GENERATE_MODEL;

/**
 * {@link javax.annotation.processing.Processor} for generating entities based on annotations.
 *
 * @author <NAME>
 */
@SupportedAnnotationTypes({"io.requery.*", "javax.persistence.*"})
@SupportedOptions({GENERATE_MODEL, GENERATE_ALWAYS, GENERATE_JPA})
public final class EntityProcessor extends AbstractProcessor {

    static final String GENERATE_MODEL = "generate.model";
    static final String GENERATE_ALWAYS = "generate.always";
    static final String GENERATE_JPA = "generate.jpa";

    private Map<String, EntityGraph> graphs;
    private Map<TypeElement, EntityType> superTypes;
    private Map<TypeElement, EntityType> embeddedTypes;

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

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        graphs = new LinkedHashMap<>();
        superTypes = new LinkedHashMap<>();
        embeddedTypes = new LinkedHashMap<>();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        // types to generate in this round
        Map<TypeElement, EntityType> entities = new HashMap<>();
        SourceLanguage.map(processingEnv);
        Types types = processingEnv.getTypeUtils();

        for (TypeElement annotation : annotations) {
            for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
                typeElementOf(element).ifPresent(typeElement -> {
                    EntityType entity = null;
                    if (isEntity(typeElement)) {
                        // create or get the entity for the annotation
                        entity = computeType(entities, typeElement);
                        // create or get the graph for it
                        String model = entity.modelName();
                        graphs.computeIfAbsent(model, key -> new EntityGraph(types, embeddedTypes)).add(entity);
                    } else if (isSuperclass(typeElement)) {
                        entity = computeType(superTypes, typeElement);
                    } else if (isEmbeddable(typeElement)) {
                        entity = computeType(embeddedTypes, typeElement);
                    }
                    if (entity != null) {<commit_msg>Fix formatting<commit_after>/*
 * Copyright 2016 requery.io
 *
 * 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 io.requery.processor;

import io.requery.Embedded;
import io.requery.Entity;
import io.requery.Superclass;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static io.requery.processor.EntityProcessor.GENERATE_ALWAYS;
import static io.requery.processor.EntityProcessor.GENERATE_JPA;
import static io.requery.processor.EntityProcessor.GENERATE_MODEL;

/**
 * {@link javax.annotation.processing.Processor} for generating entities based on annotations.
 *
 * @author <NAME>
 */
@SupportedAnnotationTypes({"io.requery.*", "javax.persistence.*"})
@SupportedOptions({GENERATE_MODEL, GENERATE_ALWAYS, GENERATE_JPA})
public final class EntityProcessor extends AbstractProcessor {

    static final String GENERATE_MODEL = "generate.model";
    static final String GENERATE_ALWAYS = "generate.always";
    static final String GENERATE_JPA = "generate.jpa";

    private Map<String, EntityGraph> graphs;
    private Map<TypeElement, EntityType> superTypes;
    private Map<TypeElement, EntityType> embeddedTypes;

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

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        graphs = new LinkedHashMap<>();
        superTypes = new LinkedHashMap<>();
        embeddedTypes = new LinkedHashMap<>();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        // types to generate in this round
        Map<TypeElement, EntityType> entities = new HashMap<>();
        SourceLanguage.map(processingEnv);
        Types types = processingEnv.getTypeUtils();

        for (TypeElement annotation : annotations) {
            for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
                typeElementOf(element).ifPresent(typeElement -> {
                    EntityType entity = null;
                    if (isEntity(typeElement)) {
                        // create or get the entity for the annotation
                        entity = computeType(entities, typeElement);
                        // create or get the graph for it
                        String model = entity.modelName();
                        graphs.computeIfAbsent(model,
                                key -> new EntityGraph(types, embeddedTypes)).add(entity);
                    } else if (isSuperclass(typeElement)) {
                        entity = computeType(superTypes, typeElement);
                    } else if (isEmbeddable(typeElement)) {
                        entity = computeType(embeddedTypes, typeElement);
                    }
                    if (entity != null) {
{'fraction_non_alphanumeric': 0.08921036769138035, 'fraction_numerical': 0.0019288728149487644, 'mean_word_length': 3.245649948822927, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '260537', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2030, 'n_words': 585}