All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.bval.jsr.ValidatorImpl Maven / Gradle / Ivy

There is a newer version: 10.0.0-M3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.bval.jsr;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ValidationException;
import javax.validation.executable.ExecutableValidator;
import javax.validation.metadata.BeanDescriptor;

import org.apache.bval.jsr.job.ValidationJobFactory;
import org.apache.bval.util.Validate;
import org.apache.bval.util.reflection.Reflection;

public class ValidatorImpl implements CascadingPropertyValidator, ExecutableValidator {

    private final ApacheFactoryContext validatorContext;
    private final ValidationJobFactory validationJobFactory;

    ValidatorImpl(ApacheFactoryContext validatorContext) {
        super();
        this.validatorContext = Validate.notNull(validatorContext, "validatorContext");
        this.validationJobFactory = new ValidationJobFactory(validatorContext);
    }

    @Override
    public BeanDescriptor getConstraintsForClass(Class clazz) {
        return validatorContext.getDescriptorManager().getBeanDescriptor(clazz);
    }

    @Override
    public  Set> validate(T object, Class... groups) {
        return validationJobFactory.validateBean(object, groups).getResults();
    }

    @Override
    public  Set> validateProperty(T object, String propertyName, boolean cascade,
        Class... groups) {
        return validationJobFactory.validateProperty(object, propertyName, groups).cascade(cascade).getResults();
    }

    @Override
    public  Set> validateValue(Class beanType, String propertyName, Object value,
        boolean cascade, Class... groups) {
        return validationJobFactory.validateValue(beanType, propertyName, value, groups).cascade(cascade).getResults();
    }

    @Override
    public ExecutableValidator forExecutables() {
        return this;
    }

    @Override
    public  Set> validateParameters(T object, Method method, Object[] parameterValues,
        Class... groups) {
        return validationJobFactory.validateParameters(object, method, parameterValues, groups).getResults();
    }

    @Override
    public  Set> validateReturnValue(T object, Method method, Object returnValue,
        Class... groups) {
        return validationJobFactory.validateReturnValue(object, method, returnValue, groups).getResults();
    }

    @Override
    public  Set> validateConstructorParameters(Constructor constructor,
        Object[] parameterValues, Class... groups) {
        return validationJobFactory. validateConstructorParameters(constructor, parameterValues, groups)
            .getResults();
    }

    @Override
    public  Set> validateConstructorReturnValue(Constructor constructor,
        T createdObject, Class... groups) {
        return validationJobFactory. validateConstructorReturnValue(constructor, createdObject, groups).getResults();
    }

    @Override
    public  T unwrap(Class type) {
        // FIXME 2011-03-27 jw:
        // This code is unsecure.
        // It should allow only a fixed set of classes.
        // Can't fix this because don't know which classes this method should support.

        if (type.isAssignableFrom(getClass())) {
            @SuppressWarnings("unchecked")
            final T result = (T) this;
            return result;
        }
        if (!(type.isInterface() || Modifier.isAbstract(type.getModifiers()))) {
            return newInstance(type);
        }
        try {
            final Class cls = Reflection.toClass(type.getName() + "Impl");
            if (type.isAssignableFrom(cls)) {
                @SuppressWarnings("unchecked")
                final Class implClass = (Class) cls;
                return newInstance(implClass);
            }
        } catch (ClassNotFoundException e) {
        }
        throw new ValidationException("Type " + type + " not supported");
    }

    private  T newInstance(final Class cls) {
        final Constructor cons = Reflection.getDeclaredConstructor(cls, ApacheFactoryContext.class);
        if (cons == null) {
            throw new ValidationException("Cannot instantiate " + cls);
        }
        Reflection.makeAccessible(cons);
        try {
            return cons.newInstance(validatorContext);
        } catch (final Exception ex) {
            throw new ValidationException("Cannot instantiate " + cls, ex);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy