<commit_before>/*
 * Copyright 2008 <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 net.sf.jpasecurity.persistence.mapping;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Map;

import javax.persistence.PersistenceException;

/**
 * Parses classes for persistence annotations and stores mapping information
 * in the provided map.
 * @author <NAME>
 */
public abstract class AbstractMappingParser {

    private static final String READ_PROPERTY_PREFIX = "get";

    private Map<Class<?>, ClassMappingInformation> classMappings;

    public AbstractMappingParser(Map<Class<?>, ClassMappingInformation> classMappings) {
        this.classMappings = classMappings;
    }

    public ClassMappingInformation parse(Class<?> mappedClass) {
        ClassMappingInformation classMapping = classMappings.get(mappedClass);
        if (classMapping == null) {
            Class<?> superclass = mappedClass.getSuperclass();
            ClassMappingInformation superclassMapping = null;
            if (superclass != null) {
                superclassMapping = parse(mappedClass.getSuperclass());
            }
            if (isMapped(mappedClass)) {
                boolean usesFieldAccess;
                if (superclassMapping != null) {
                    usesFieldAccess = superclassMapping.usesFieldAccess();
                } else {
                    usesFieldAccess = usesFieldAccess(mappedClass);
                }
                //TODO add extracting of the entity name here
                classMapping = new ClassMappingInformation(mappedClass.getSimpleName(), mappedClass, superclassMapping);
                classMappings.put(mappedClass, classMapping);
                if (usesFieldAccess) {
                    for (Field field: mappedClass.getDeclaredFields()) {
                        PropertyMappingInformation propertyMapping = parse(field);
                        classMapping.addPropertyMapping(propertyMapping);
                    }
                } else {
                    for (Method method: mappedClass.getDeclaredMethods()) {
                        if (method.getName().startsWith(READ_PROPERTY_PREFIX)) {
                            PropertyMappingInformation propertyMapping = parse(method);
                            classMapping.addPropertyMapping(propertyMapping);
                        }
                    }
                }
            } else {
                classMapping = superclassMapping;
            }
        }
        return classMapping;
    }

    private PropertyMappingInformation parse(Member property) {
        String name = getName(property);
        Class<?> type = getType(property);
        ClassMappingInformation classMapping = parse(property.getDeclaringClass());
        if (isSingleValuedRelationshipProperty(property)) {
            ClassMappingInformation typeMapping = parse(type);
            return new SingleValuedRelationshipMappingInformation(name, typeMapping, classMapping);
        } else if (isCollectionValuedRelationshipProperty(property)) {
            ClassMappingInformation targetMapping = parse(getTargetType(property));
            return new CollectionValuedRelationshipMappingInformation(name, type, targetMapping, classMapping);
        } else if (isSimpePropertyType(type)) {
            return new SimplePropertyMappingInformation(name, type, classMapping);
        } else {
            throw new PersistenceException("could not determine mapping for property \"" + name + "\" of class " + property.getDeclaringClass().getName());
        }
    }

    protected String getName(Member property) {
        if (property instanceof Method) {
            return Character.toUpperCase(property.getName().charAt(3)) + property.getName().substring(4);
        } else {
            return property.getName();
        }
    }

    private Class<?> getType(Member property) {
        if (property instanceof Method) {
            return ((Method)property).getReturnType();
        } else {
            return ((Field)property).getType();
        }
    }

    protected Class<?> getTargetType(Member property) {
        Type genericType;
        if (property instanceof Method) {
            genericType = ((Method)property).getGenericReturnType();
        } else {
            genericType = ((Field)property).getGenericType();
        }
        if (!(genericType instanceof ParameterizedType)) {
            throw new PersistenceException("no target entity specified for property \"" + property.getName() + "\" of class " + property.getDeclaringClass().getName());
        }
        Type[] genericTypeArguments = ((ParameterizedType)genericType).getActualTypeArguments();
        Type genericTypeArgument;
        if (genericTypeArguments.length == 1) {
            genericTypeArgument = genericTypeArguments[0];
        } else if (genericTypeArguments.length == 2) {
            //Must be a map, take the value
            genericTypeArgument = genericTypeArguments[1];
        } else {
            throw new PersistenceException("could not determine target entity for property \"" + property.getName() + "\" of class " + property.getDeclaringClass().getName());
        }
        if (genericTypeArgument instanceof Class) {
            return (Class<?>)genericTypeArgument;
        } else {
            Type[] bounds = null;
            if (genericTypeArgument instanceof TypeVariable) {
                bounds = ((TypeVariable)genericTypeArgument).getBounds();
            } else if (genericTypeArgument instanceof WildcardType) {
                bounds = ((WildcardType)genericTypeArgument).getUpperBounds();
            }
            if (bounds != null) {
                for (Type bound: ((TypeVariable)genericTypeArgument).getBounds()) {
                    if (bound instanceof Class) {
                        return (Class<?>)bound;
                    }
                }
            }
            throw new PersistenceException("could not determine target entity for property \"" + property.getName() + "\" of class " + property.getDeclaringClass().getName());
        }
    }

    protected boolean usesFieldAccess(Class<?> mappedClass) {
        Field[] fields = mappedClass.getDeclaredFields();
        for (Field field: fields) {
            if (isMappable(field) && isMapped(field)) {
                return true;
            }
        }
        return false;
    }

    protected abstract boolean isMapped(Class<?> mappedClass);

    protected abstract boolean isMapped(Member member);

    protected boolean isMappable(Member member) {
        return !Modifier.isStatic(member.getModifiers()) && !Modifier.isTransient(member.getModifiers());
    }
    
    protected boolean isSimpePropertyType(Class<?> type) {
        return isEmbeddable(type)
            || type.isPrimitive()
            || type.equals(Boolean.class)
            || type.equals(Byte.class)
            || type.equals(Short.class)<commit_msg>Fixed typo<commit_after>/*
 * Copyright 2008 <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 net.sf.jpasecurity.persistence.mapping;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Map;

import javax.persistence.PersistenceException;

/**
 * Parses classes for persistence annotations and stores mapping information
 * in the provided map.
 * @author <NAME>
 */
public abstract class AbstractMappingParser {

    private static final String READ_PROPERTY_PREFIX = "get";

    private Map<Class<?>, ClassMappingInformation> classMappings;

    public AbstractMappingParser(Map<Class<?>, ClassMappingInformation> classMappings) {
        this.classMappings = classMappings;
    }

    public ClassMappingInformation parse(Class<?> mappedClass) {
        ClassMappingInformation classMapping = classMappings.get(mappedClass);
        if (classMapping == null) {
            Class<?> superclass = mappedClass.getSuperclass();
            ClassMappingInformation superclassMapping = null;
            if (superclass != null) {
                superclassMapping = parse(mappedClass.getSuperclass());
            }
            if (isMapped(mappedClass)) {
                boolean usesFieldAccess;
                if (superclassMapping != null) {
                    usesFieldAccess = superclassMapping.usesFieldAccess();
                } else {
                    usesFieldAccess = usesFieldAccess(mappedClass);
                }
                //TODO add extracting of the entity name here
                classMapping = new ClassMappingInformation(mappedClass.getSimpleName(), mappedClass, superclassMapping);
                classMappings.put(mappedClass, classMapping);
                if (usesFieldAccess) {
                    for (Field field: mappedClass.getDeclaredFields()) {
                        PropertyMappingInformation propertyMapping = parse(field);
                        classMapping.addPropertyMapping(propertyMapping);
                    }
                } else {
                    for (Method method: mappedClass.getDeclaredMethods()) {
                        if (method.getName().startsWith(READ_PROPERTY_PREFIX)) {
                            PropertyMappingInformation propertyMapping = parse(method);
                            classMapping.addPropertyMapping(propertyMapping);
                        }
                    }
                }
            } else {
                classMapping = superclassMapping;
            }
        }
        return classMapping;
    }

    private PropertyMappingInformation parse(Member property) {
        String name = getName(property);
        Class<?> type = getType(property);
        ClassMappingInformation classMapping = parse(property.getDeclaringClass());
        if (isSingleValuedRelationshipProperty(property)) {
            ClassMappingInformation typeMapping = parse(type);
            return new SingleValuedRelationshipMappingInformation(name, typeMapping, classMapping);
        } else if (isCollectionValuedRelationshipProperty(property)) {
            ClassMappingInformation targetMapping = parse(getTargetType(property));
            return new CollectionValuedRelationshipMappingInformation(name, type, targetMapping, classMapping);
        } else if (isSimplePropertyType(type)) {
            return new SimplePropertyMappingInformation(name, type, classMapping);
        } else {
            throw new PersistenceException("could not determine mapping for property \"" + name + "\" of class " + property.getDeclaringClass().getName());
        }
    }

    protected String getName(Member property) {
        if (property instanceof Method) {
            return Character.toUpperCase(property.getName().charAt(3)) + property.getName().substring(4);
        } else {
            return property.getName();
        }
    }

    private Class<?> getType(Member property) {
        if (property instanceof Method) {
            return ((Method)property).getReturnType();
        } else {
            return ((Field)property).getType();
        }
    }

    protected Class<?> getTargetType(Member property) {
        Type genericType;
        if (property instanceof Method) {
            genericType = ((Method)property).getGenericReturnType();
        } else {
            genericType = ((Field)property).getGenericType();
        }
        if (!(genericType instanceof ParameterizedType)) {
            throw new PersistenceException("no target entity specified for property \"" + property.getName() + "\" of class " + property.getDeclaringClass().getName());
        }
        Type[] genericTypeArguments = ((ParameterizedType)genericType).getActualTypeArguments();
        Type genericTypeArgument;
        if (genericTypeArguments.length == 1) {
            genericTypeArgument = genericTypeArguments[0];
        } else if (genericTypeArguments.length == 2) {
            //Must be a map, take the value
            genericTypeArgument = genericTypeArguments[1];
        } else {
            throw new PersistenceException("could not determine target entity for property \"" + property.getName() + "\" of class " + property.getDeclaringClass().getName());
        }
        if (genericTypeArgument instanceof Class) {
            return (Class<?>)genericTypeArgument;
        } else {
            Type[] bounds = null;
            if (genericTypeArgument instanceof TypeVariable) {
                bounds = ((TypeVariable)genericTypeArgument).getBounds();
            } else if (genericTypeArgument instanceof WildcardType) {
                bounds = ((WildcardType)genericTypeArgument).getUpperBounds();
            }
            if (bounds != null) {
                for (Type bound: ((TypeVariable)genericTypeArgument).getBounds()) {
                    if (bound instanceof Class) {
                        return (Class<?>)bound;
                    }
                }
            }
            throw new PersistenceException("could not determine target entity for property \"" + property.getName() + "\" of class " + property.getDeclaringClass().getName());
        }
    }

    protected boolean usesFieldAccess(Class<?> mappedClass) {
        Field[] fields = mappedClass.getDeclaredFields();
        for (Field field: fields) {
            if (isMappable(field) && isMapped(field)) {
                return true;
            }
        }
        return false;
    }

    protected abstract boolean isMapped(Class<?> mappedClass);

    protected abstract boolean isMapped(Member member);

    protected boolean isMappable(Member member) {
        return !Modifier.isStatic(member.getModifiers()) && !Modifier.isTransient(member.getModifiers());
    }
    
    protected boolean isSimplePropertyType(Class<?> type) {
        return isEmbeddable(type)
            || type.isPrimitive()
            || type.equals(Boolean.class)
            || type.equals(Byte.class)
            || type.equals(Short.class)
{'fraction_non_alphanumeric': 0.09351739932565685, 'fraction_numerical': 0.001781283796679178, 'mean_word_length': 2.4564643799472297, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '1121119', 'n_tokens_mistral': 3729, 'n_tokens_neox': 3653, 'n_words': 940}