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);
}
}
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;
}
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");
}
}
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;
}
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());
}
Aggregations