下面列出了怎么用org.hibernate.PropertyNotFoundException的API类实例代码及写法,或者点击链接到github查看源代码。
public PojoInstantiator(
Class mappedClass,
ReflectionOptimizer.InstantiationOptimizer optimizer,
boolean embeddedIdentifier) {
this.mappedClass = mappedClass;
this.optimizer = optimizer;
this.embeddedIdentifier = embeddedIdentifier;
this.isAbstract = ReflectHelper.isAbstractClass( mappedClass );
try {
constructor = ReflectHelper.getDefaultConstructor(mappedClass);
}
catch ( PropertyNotFoundException pnfe ) {
LOG.noDefaultConstructor( mappedClass.getName() );
constructor = null;
}
}
/**
* 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 );
ensureAccessibility( constructor );
return constructor;
}
catch ( NoSuchMethodException nme ) {
throw new PropertyNotFoundException(
"Object class [" + clazz.getName() + "] must declare a default (no-argument) constructor"
);
}
}
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()
)
);
}
ensureAccessibility( field );
return field;
}
/**
* Create a "getter" for the named attribute
*/
public Getter getGetter(Class theClass, String propertyName)
throws PropertyNotFoundException {
if (nodeName==null) {
throw new MappingException("no node name for property: " + propertyName);
}
if ( ".".equals(nodeName) ) {
return new TextGetter(propertyType, factory);
}
else if ( nodeName.indexOf('/')>-1 ) {
return new ElementAttributeGetter(nodeName, propertyType, factory);
}
else if ( nodeName.indexOf('@')>-1 ) {
return new AttributeGetter(nodeName, propertyType, factory);
}
else {
return new ElementGetter(nodeName, propertyType, factory);
}
}
/**
* Create a "setter" for the named attribute
*/
public Setter getSetter(Class theClass, String propertyName)
throws PropertyNotFoundException {
if (nodeName==null) {
throw new MappingException("no node name for property: " + propertyName);
}
if ( ".".equals(nodeName) ) {
return new TextSetter(propertyType);
}
else if ( nodeName.indexOf('/')>-1 ) {
return new ElementAttributeSetter(nodeName, propertyType);
}
else if ( nodeName.indexOf('@')>-1 ) {
return new AttributeSetter(nodeName, propertyType);
}
else {
return new ElementSetter(nodeName, propertyType);
}
}
public PojoInstantiator(Component component, ReflectionOptimizer.InstantiationOptimizer optimizer) {
this.mappedClass = component.getComponentClass();
this.optimizer = optimizer;
this.proxyInterface = null;
this.embeddedIdentifier = false;
try {
constructor = ReflectHelper.getDefaultConstructor(mappedClass);
}
catch ( PropertyNotFoundException pnfe ) {
log.info(
"no default (no-argument) constructor for class: " +
mappedClass.getName() +
" (class must be instantiated by Interceptor)"
);
constructor = null;
}
}
public PojoInstantiator(PersistentClass persistentClass, ReflectionOptimizer.InstantiationOptimizer optimizer) {
this.mappedClass = persistentClass.getMappedClass();
this.proxyInterface = persistentClass.getProxyInterface();
this.embeddedIdentifier = persistentClass.hasEmbeddedIdentifier();
this.optimizer = optimizer;
try {
constructor = ReflectHelper.getDefaultConstructor( mappedClass );
}
catch ( PropertyNotFoundException pnfe ) {
log.info(
"no default (no-argument) constructor for class: " +
mappedClass.getName() +
" (class must be instantiated by Interceptor)"
);
constructor = null;
}
}
public static Constructor getDefaultConstructor(Class clazz) throws PropertyNotFoundException {
if ( isAbstractClass(clazz) ) return null;
try {
Constructor constructor = clazz.getDeclaredConstructor(NO_CLASSES);
if ( !isPublic(clazz, constructor) ) {
constructor.setAccessible(true);
}
return constructor;
}
catch (NoSuchMethodException nme) {
throw new PropertyNotFoundException(
"Object class " + clazz.getName() +
" must declare a default (no-argument) constructor"
);
}
}
public static Constructor getConstructor(Class clazz, Type[] types) throws PropertyNotFoundException {
final Constructor[] candidates = clazz.getConstructors();
for ( int i=0; i<candidates.length; i++ ) {
final Constructor constructor = candidates[i];
final Class[] params = constructor.getParameterTypes();
if ( params.length==types.length ) {
boolean found = true;
for ( int j=0; j<params.length; j++ ) {
final boolean ok = params[j].isAssignableFrom( types[j].getReturnedClass() ) || (
types[j] instanceof PrimitiveType &&
params[j] == ( (PrimitiveType) types[j] ).getPrimitiveClass()
);
if (!ok) {
found = false;
break;
}
}
if (found) {
if ( !isPublic(clazz, constructor) ) constructor.setAccessible(true);
return constructor;
}
}
}
throw new PropertyNotFoundException( "no appropriate constructor in class: " + clazz.getName() );
}
public Query setProperties(Object bean) throws HibernateException {
Class clazz = bean.getClass();
String[] params = getNamedParameters();
for (int i = 0; i < params.length; i++) {
String namedParam = params[i];
try {
Getter getter = ReflectHelper.getGetter( clazz, namedParam );
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 {
setParameter( namedParam, object, determineType( namedParam, retType ) );
}
}
catch (PropertyNotFoundException pnfe) {
// ignore
}
}
return this;
}
protected static Field fieldOrNull(Class containerJavaType, String propertyName) {
try {
return ReflectHelper.findField( containerJavaType, propertyName );
}
catch (PropertyNotFoundException e) {
return null;
}
}
@Override
public PropertyAccess buildPropertyAccess(Class containerJavaType, String propertyName) {
for ( PropertyAccessStrategy candidate : chain ) {
try {
return candidate.buildPropertyAccess( containerJavaType, propertyName );
}
catch (Exception ignore) {
// ignore
}
}
throw new PropertyNotFoundException( "Could not resolve PropertyAccess for " + propertyName + " on " + containerJavaType );
}
@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;
}
public PojoInstantiator(Class componentClass, ReflectionOptimizer.InstantiationOptimizer optimizer) {
this.mappedClass = componentClass;
this.isAbstract = ReflectHelper.isAbstractClass( mappedClass );
this.optimizer = optimizer;
this.embeddedIdentifier = false;
try {
constructor = ReflectHelper.getDefaultConstructor(mappedClass);
}
catch ( PropertyNotFoundException pnfe ) {
LOG.noDefaultConstructor(mappedClass.getName());
constructor = null;
}
}
/**
* 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 ++;
ensureAccessibility( candidate );
constructor = candidate;
}
}
}
if ( numberOfMatchingConstructors == 1 ) {
return constructor;
}
throw new PropertyNotFoundException( "no appropriate constructor in class: " + clazz.getName() );
}
public static Method findGetterMethod(Class containerClass, String propertyName) {
Class checkClass = containerClass;
Method getter = null;
// check containerClass, and then its super types (if any)
while ( getter == null && checkClass != null ) {
if ( checkClass.equals( Object.class ) ) {
break;
}
getter = getGetterOrNull( checkClass, propertyName );
// if no getter found yet, check all implemented interfaces
if ( getter == null ) {
getter = getGetterOrNull( checkClass.getInterfaces(), propertyName );
}
checkClass = checkClass.getSuperclass();
}
if ( getter == null ) {
throw new PropertyNotFoundException(
String.format(
Locale.ROOT,
"Could not locate getter method for property [%s#%s]",
containerClass.getName(),
propertyName
)
);
}
ensureAccessibility( getter );
return getter;
}
public static Method getterMethodOrNull(Class containerJavaType, String propertyName) {
try {
return findGetterMethod( containerJavaType, propertyName );
}
catch (PropertyNotFoundException e) {
return null;
}
}
public static Method findSetterMethod(final Class containerClass, final String propertyName, final Class propertyType) {
final Method setter = setterMethodOrNull( containerClass, propertyName, propertyType );
if ( setter == null ) {
throw new PropertyNotFoundException(
String.format(
Locale.ROOT,
"Could not locate setter method for property [%s#%s]",
containerClass.getName(),
propertyName
)
);
}
return setter;
}
@Override
public int getPropertyIndex(String name) {
String[] names = getPropertyNames();
for ( int i = 0, max = names.length; i < max; i++ ) {
if ( names[i].equals( name ) ) {
return i;
}
}
throw new PropertyNotFoundException(
"Unable to locate property named " + name + " on " + getReturnedClass().getName()
);
}
@Override
public int getPropertyIndex(String name) {
String[] names = getPropertyNames();
for ( int i = 0, max = names.length; i < max; i++ ) {
if ( names[i].equals( name ) ) {
return i;
}
}
throw new PropertyNotFoundException(
"Unable to locate property named " + name + " on " + getReturnedClass().getName()
);
}
@Override
public int getPropertyIndex(String name) {
if ( PROPERTY_NAMES[0].equals( name ) ) {
return 0;
}
else if ( PROPERTY_NAMES[1].equals( name ) ) {
return 1;
}
throw new PropertyNotFoundException( "Unable to locate property named " + name + " on AnyType" );
}
private static Setter createSetter(Class theClass, String propertyName)
throws PropertyNotFoundException {
BasicSetter result = getSetterOrNull(theClass, propertyName);
if (result==null) {
throw new PropertyNotFoundException(
"Could not find a setter for property " +
propertyName +
" in class " +
theClass.getName()
);
}
return result;
}
public static Getter createGetter(Class theClass, String propertyName)
throws PropertyNotFoundException {
BasicGetter result = getGetterOrNull(theClass, propertyName);
if (result==null) {
throw new PropertyNotFoundException(
"Could not find a getter for " +
propertyName +
" in class " +
theClass.getName()
);
}
return result;
}
private static Field getField(Class clazz, String name) throws PropertyNotFoundException {
if ( clazz==null || clazz==Object.class ) {
throw new PropertyNotFoundException("field not found: " + name);
}
Field field;
try {
field = clazz.getDeclaredField(name);
}
catch (NoSuchFieldException nsfe) {
field = getField( clazz, clazz.getSuperclass(), name );
}
if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true);
return field;
}
private static Field getField(Class root, Class clazz, String name) throws PropertyNotFoundException {
if ( clazz==null || clazz==Object.class ) {
throw new PropertyNotFoundException("field [" + name + "] not found on " + root.getName());
}
Field field;
try {
field = clazz.getDeclaredField(name);
}
catch (NoSuchFieldException nsfe) {
field = getField( root, clazz.getSuperclass(), name );
}
if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true);
return field;
}
public Getter getGetter(Class theClass, String propertyName)
throws PropertyNotFoundException {
Getter result = null;
for (int i = 0; i < chain.length; i++) {
PropertyAccessor candidate = chain[i];
try {
result = candidate.getGetter(theClass, propertyName);
return result;
} catch (PropertyNotFoundException pnfe) {
// ignore
}
}
throw new PropertyNotFoundException("Could not find getter for " + propertyName + " on " + theClass);
}
public Setter getSetter(Class theClass, String propertyName)
throws PropertyNotFoundException {
Setter result = null;
for (int i = 0; i < chain.length; i++) {
PropertyAccessor candidate = chain[i];
try {
result = candidate.getSetter(theClass, propertyName);
return result;
} catch (PropertyNotFoundException pnfe) {
//
}
}
throw new PropertyNotFoundException("Could not find setter for " + propertyName + " on " + theClass);
}
private static Getter getter(Class clazz, String name) throws MappingException {
try {
return BASIC_PROPERTY_ACCESSOR.getGetter(clazz, name);
}
catch (PropertyNotFoundException pnfe) {
return DIRECT_PROPERTY_ACCESSOR.getGetter(clazz, name);
}
}
public Getter getGetter(Class clazz) throws PropertyNotFoundException, MappingException {
return getPropertyAccessStrategy( clazz ).buildPropertyAccess( clazz, name ).getGetter();
}
public Setter getSetter(Class clazz) throws PropertyNotFoundException, MappingException {
return getPropertyAccessStrategy( clazz ).buildPropertyAccess( clazz, name ).getSetter();
}