<commit_before>/**
 * Copyright 2017 <NAME>
 *
 * 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 org.jfleet;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JpaEntityInspector {

    private static Logger logger = LoggerFactory.getLogger(JpaEntityInspector.class);

    private final Class<?> entityClass;

    public JpaEntityInspector(Class<?> entityClass) {
        this.entityClass = entityClass;
        ensureHasEntity();
    }

    public EntityInfo inspect() {
        EntityInfo info = new EntityInfo();
        info.setEntityClass(entityClass);
        info.setTableName(getTableName());
        info.addFields(getFieldsFromClass(entityClass));
        return info;
    }

    private boolean ensureHasEntity() {
        Entity annotation = entityClass.getAnnotation(Entity.class);
        if (annotation != null) {
            return true;
        }
        throw new RuntimeException(entityClass.getName() + " has no Entity annotation");
    }

    private String getTableName() {
        Table annotation = entityClass.getAnnotation(Table.class);
        if (annotation != null) {
            String tableName = annotation.name();
            if (tableName != null && tableName.trim().length() > 0) {
                return tableName;
            }
        }
        return entityClass.getSimpleName().toLowerCase();
    }

    private FieldsCollection getFieldsFromClass(Class<?> entityClass) {
        if (entityClass == Object.class) {
            return EMPTY_FIELD_COLLECTION;
        }
        FieldsCollection parentClassFields = getFieldsFromClass(entityClass.getSuperclass());
        if (!isEntityOrMapped(entityClass)) {
            return parentClassFields;
        }
        MappingOverride mappingOverride = new MappingOverride(entityClass);
        FieldsCollection overwrittenClassFields = mappingOverride.override(parentClassFields);

        FieldsCollection currentClassFields = new FieldsCollection(Stream.of(entityClass.getDeclaredFields())
                .map(FieldInspector::new).map(FieldInspector::inspect).flatMap(List::stream));

        currentClassFields.addNotPresent(overwrittenClassFields);
        return currentClassFields;
    }

    private boolean isEntityOrMapped(Class<?> entityClass) {
        Entity entity = entityClass.getAnnotation(Entity.class);
        if (entity != null) {
            return true;
        }
        MappedSuperclass mapped = entityClass.getAnnotation(MappedSuperclass.class);
        if (mapped != null) {
            return true;
        }
        return false;
    }

    private static class FieldInspector {

        private final Field field;

        FieldInspector(Field field) {
            this.field = field;
        }

        public List<FieldInfo> inspect() {
            if (isSkippable()) {
                return Collections.emptyList();
            }
            Embedded embedded = field.getAnnotation(Embedded.class);
            if (embedded != null) {
                EmbeddedInspector embeddedInspector = new EmbeddedInspector(field);
                return embeddedInspector.getFields();
            }<commit_msg>Fix missed EmbddedId annotation code<commit_after>/**
 * Copyright 2017 <NAME>
 *
 * 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 org.jfleet;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JpaEntityInspector {

    private static Logger logger = LoggerFactory.getLogger(JpaEntityInspector.class);

    private final Class<?> entityClass;

    public JpaEntityInspector(Class<?> entityClass) {
        this.entityClass = entityClass;
        ensureHasEntity();
    }

    public EntityInfo inspect() {
        EntityInfo info = new EntityInfo();
        info.setEntityClass(entityClass);
        info.setTableName(getTableName());
        info.addFields(getFieldsFromClass(entityClass));
        return info;
    }

    private boolean ensureHasEntity() {
        Entity annotation = entityClass.getAnnotation(Entity.class);
        if (annotation != null) {
            return true;
        }
        throw new RuntimeException(entityClass.getName() + " has no Entity annotation");
    }

    private String getTableName() {
        Table annotation = entityClass.getAnnotation(Table.class);
        if (annotation != null) {
            String tableName = annotation.name();
            if (tableName != null && tableName.trim().length() > 0) {
                return tableName;
            }
        }
        return entityClass.getSimpleName().toLowerCase();
    }

    private FieldsCollection getFieldsFromClass(Class<?> entityClass) {
        if (entityClass == Object.class) {
            return EMPTY_FIELD_COLLECTION;
        }
        FieldsCollection parentClassFields = getFieldsFromClass(entityClass.getSuperclass());
        if (!isEntityOrMapped(entityClass)) {
            return parentClassFields;
        }
        MappingOverride mappingOverride = new MappingOverride(entityClass);
        FieldsCollection overwrittenClassFields = mappingOverride.override(parentClassFields);

        FieldsCollection currentClassFields = new FieldsCollection(Stream.of(entityClass.getDeclaredFields())
                .map(FieldInspector::new).map(FieldInspector::inspect).flatMap(List::stream));

        currentClassFields.addNotPresent(overwrittenClassFields);
        return currentClassFields;
    }

    private boolean isEntityOrMapped(Class<?> entityClass) {
        Entity entity = entityClass.getAnnotation(Entity.class);
        if (entity != null) {
            return true;
        }
        MappedSuperclass mapped = entityClass.getAnnotation(MappedSuperclass.class);
        if (mapped != null) {
            return true;
        }
        return false;
    }

    private static class FieldInspector {

        private final Field field;

        FieldInspector(Field field) {
            this.field = field;
        }

        public List<FieldInfo> inspect() {
            if (isSkippable()) {
                return Collections.emptyList();
            }
            Embedded embedded = field.getAnnotation(Embedded.class);
            EmbeddedId embeddedId = field.getAnnotation(EmbeddedId.class);
            if (embedded != null || embeddedId != null) {
                EmbeddedInspector embeddedInspector = new EmbeddedInspector(field);
                return embeddedInspector.getFields();
            }
{'fraction_non_alphanumeric': 0.08912339358478738, 'fraction_numerical': 0.002298610385539651, 'mean_word_length': 3.1671745755333043, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '4228361', 'n_tokens_mistral': 2500, 'n_tokens_neox': 2455, 'n_words': 619}