Search in sources :

Example 1 with PropertyNotFoundException

use of org.hibernate.PropertyNotFoundException in project hibernate-orm by hibernate.

the class ConstructorNode method resolveConstructor.

private Constructor resolveConstructor(String path) throws SemanticException {
    String importedClassName = getSessionFactoryHelper().getImportedClassName(path);
    String className = StringHelper.isEmpty(importedClassName) ? path : importedClassName;
    if (className == null) {
        throw new SemanticException("Unable to locate class [" + path + "]");
    }
    try {
        final Class holderClass = getSessionFactoryHelper().getFactory().getServiceRegistry().getService(ClassLoaderService.class).classForName(className);
        return ReflectHelper.getConstructor(holderClass, constructorArgumentTypes);
    } catch (ClassLoadingException e) {
        throw new DetailedSemanticException("Unable to locate class [" + className + "]", e);
    } catch (PropertyNotFoundException e) {
        // locate an appropriate constructor
        throw new DetailedSemanticException(formatMissingContructorExceptionMessage(className), e);
    }
}
Also used : PropertyNotFoundException(org.hibernate.PropertyNotFoundException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) DetailedSemanticException(org.hibernate.hql.internal.ast.DetailedSemanticException) SemanticException(antlr.SemanticException) DetailedSemanticException(org.hibernate.hql.internal.ast.DetailedSemanticException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 2 with PropertyNotFoundException

use of org.hibernate.PropertyNotFoundException in project hibernate-orm by hibernate.

the class ReflectHelper method findField.

public static Field findField(Class containerClass, String propertyName) {
    if (containerClass == null) {
        throw new IllegalArgumentException("Class on which to find field [" + propertyName + "] cannot be null");
    } else if (containerClass == Object.class) {
        throw new IllegalArgumentException("Illegal attempt to locate field [" + propertyName + "] on Object.class");
    }
    Field field = locateField(containerClass, propertyName);
    if (field == null) {
        throw new PropertyNotFoundException(String.format(Locale.ROOT, "Could not locate field name [%s] on class [%s]", propertyName, containerClass.getName()));
    }
    field.setAccessible(true);
    return field;
}
Also used : Field(java.lang.reflect.Field) PropertyNotFoundException(org.hibernate.PropertyNotFoundException)

Example 3 with PropertyNotFoundException

use of org.hibernate.PropertyNotFoundException in project hibernate-orm by hibernate.

the class ReflectHelper method getDefaultConstructor.

/**
	 * Retrieve the default (no arg) constructor from the given class.
	 *
	 * @param clazz The class for which to retrieve the default ctor.
	 * @return The default constructor.
	 * @throws PropertyNotFoundException Indicates there was not publicly accessible, no-arg constructor (todo : why PropertyNotFoundException???)
	 */
public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws PropertyNotFoundException {
    if (isAbstractClass(clazz)) {
        return null;
    }
    try {
        Constructor<T> constructor = clazz.getDeclaredConstructor(NO_PARAM_SIGNATURE);
        constructor.setAccessible(true);
        return constructor;
    } catch (NoSuchMethodException nme) {
        throw new PropertyNotFoundException("Object class [" + clazz.getName() + "] must declare a default (no-argument) constructor");
    }
}
Also used : PropertyNotFoundException(org.hibernate.PropertyNotFoundException)

Example 4 with PropertyNotFoundException

use of org.hibernate.PropertyNotFoundException in project hibernate-orm by hibernate.

the class AbstractProducedQuery method setProperties.

@Override
@SuppressWarnings("unchecked")
public QueryImplementor setProperties(Object bean) {
    Class clazz = bean.getClass();
    String[] params = getNamedParameters();
    for (String namedParam : params) {
        try {
            final PropertyAccess propertyAccess = BuiltInPropertyAccessStrategies.BASIC.getStrategy().buildPropertyAccess(clazz, namedParam);
            final Getter getter = propertyAccess.getGetter();
            final Class retType = getter.getReturnType();
            final Object object = getter.get(bean);
            if (Collection.class.isAssignableFrom(retType)) {
                setParameterList(namedParam, (Collection) object);
            } else if (retType.isArray()) {
                setParameterList(namedParam, (Object[]) object);
            } else {
                Type type = determineType(namedParam, retType);
                setParameter(namedParam, object, type);
            }
        } catch (PropertyNotFoundException pnfe) {
        // ignore
        }
    }
    return this;
}
Also used : FlushModeType(javax.persistence.FlushModeType) TemporalType(javax.persistence.TemporalType) LockModeType(javax.persistence.LockModeType) Type(org.hibernate.type.Type) PropertyNotFoundException(org.hibernate.PropertyNotFoundException) Getter(org.hibernate.property.access.spi.Getter) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess)

Example 5 with PropertyNotFoundException

use of org.hibernate.PropertyNotFoundException in project hibernate-orm by hibernate.

the class ReflectHelper method getConstructor.

/**
	 * Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping
	 * {@link Type types}.
	 *
	 * @param clazz The class needing instantiation
	 * @param types The types representing the required ctor param signature
	 * @return The matching constructor.
	 * @throws PropertyNotFoundException Indicates we could not locate an appropriate constructor (todo : again with PropertyNotFoundException???)
	 */
public static Constructor getConstructor(Class clazz, Type[] types) throws PropertyNotFoundException {
    final Constructor[] candidates = clazz.getConstructors();
    Constructor constructor = null;
    int numberOfMatchingConstructors = 0;
    for (final Constructor candidate : candidates) {
        final Class[] params = candidate.getParameterTypes();
        if (params.length == types.length) {
            boolean found = true;
            for (int j = 0; j < params.length; j++) {
                final boolean ok = types[j] == null || params[j].isAssignableFrom(types[j].getReturnedClass()) || (types[j] instanceof PrimitiveType && params[j] == ((PrimitiveType) types[j]).getPrimitiveClass());
                if (!ok) {
                    found = false;
                    break;
                }
            }
            if (found) {
                numberOfMatchingConstructors++;
                candidate.setAccessible(true);
                constructor = candidate;
            }
        }
    }
    if (numberOfMatchingConstructors == 1) {
        return constructor;
    }
    throw new PropertyNotFoundException("no appropriate constructor in class: " + clazz.getName());
}
Also used : PropertyNotFoundException(org.hibernate.PropertyNotFoundException) Constructor(java.lang.reflect.Constructor) PrimitiveType(org.hibernate.type.PrimitiveType)

Aggregations

PropertyNotFoundException (org.hibernate.PropertyNotFoundException)7 Method (java.lang.reflect.Method)2 SemanticException (antlr.SemanticException)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 FlushModeType (javax.persistence.FlushModeType)1 LockModeType (javax.persistence.LockModeType)1 TemporalType (javax.persistence.TemporalType)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 DetailedSemanticException (org.hibernate.hql.internal.ast.DetailedSemanticException)1 Getter (org.hibernate.property.access.spi.Getter)1 PropertyAccess (org.hibernate.property.access.spi.PropertyAccess)1 PrimitiveType (org.hibernate.type.PrimitiveType)1 Type (org.hibernate.type.Type)1