【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-BasicPropertyAccessor.java,希望本篇文章对您学习有所帮助。
//$Id: BasicPropertyAccessor.java 7516 2005-07-16 22:20:48Z oneovthafew $
package org.hibernate.property;
import java.beans.Introspector;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.PropertyAccessException;
import org.hibernate.PropertyNotFoundException;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.util.ReflectHelper;
/**
* Accesses property values via a get/set pair, which may be nonpublic.
* The default (and recommended strategy).
* @author Gavin King
*/
public class BasicPropertyAccessor implements PropertyAccessor {
private static final Log log = LogFactory.getLog(BasicPropertyAccessor.class);
public static final class BasicSetter implements Setter {
private Class clazz;
private final transient Method method;
private final String propertyName;
private BasicSetter(Class clazz, Method method, String propertyName) {
this.clazz=clazz;
this.method=method;
this.propertyName=propertyName;
}
public void set(Object target, Object value, SessionFactoryImplementor factory)
throws HibernateException {
try {
method.invoke( target, new Object[] { value } );
}
catch (NullPointerException npe) {
if ( value==null && method.getParameterTypes()[0].isPrimitive() ) {
throw new PropertyAccessException(
npe,
"Null value was assigned to a property of primitive type",
true,
clazz,
propertyName
);
}
else {
throw new PropertyAccessException(
npe,
"NullPointerException occurred while calling",
true,
clazz,
propertyName
);
}
}
catch (InvocationTargetException ite) {
throw new PropertyAccessException(
ite,
"Exception occurred inside",
true,
clazz,
propertyName
);
}
catch (IllegalAccessException iae) {
throw new PropertyAccessException(
iae,
"IllegalAccessException occurred while calling",
true,
clazz,
propertyName
);
//cannot occur
}
catch (IllegalArgumentException iae) {
if ( value==null && method.getParameterTypes()[0].isPrimitive() ) {
throw new PropertyAccessException(
iae,
"Null value was assigned to a property of primitive type",
true,
clazz,
propertyName
);
}
else {
log.error(
"IllegalArgumentException in class: " + clazz.getName() +
", setter method of property: " + propertyName
);
log.error(
"expected type: " +
method.getParameterTypes()[0].getName() +
", actual value: " +
( value==null ? null : value.getClass().getName() )
);
throw new PropertyAccessException(
iae,
"IllegalArgumentException occurred while calling",
true,
clazz,
propertyName
);
}
}
}
public Method getMethod() {
return method;
}
public String getMethodName() {
return method.getName();
}
Object readResolve() {
return createSetter(clazz, propertyName);
}
public String toString() {
return "BasicSetter(" + clazz.getName() + '.' + propertyName + ')';
}
}
public static final class BasicGetter implements Getter {
private Class clazz;
private final transient Method method;
private