package org.hibernate.metamodel.internal;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;
import org.hibernate.internal.util.ReflectHelper;
public abstract class AbstractAttribute<X, Y>
implements Attribute<X, Y>, AttributeImplementor<X,Y>, Serializable {
private final String name;
private final Class<Y> javaType;
private final AbstractManagedType<X> declaringType;
private transient Member member;
private final PersistentAttributeType persistentAttributeType;
public AbstractAttribute(
String name,
Class<Y> javaType,
AbstractManagedType<X> declaringType,
Member member,
PersistentAttributeType persistentAttributeType) {
this.name = name;
this.javaType = javaType;
this.declaringType = declaringType;
this.member = member;
this.persistentAttributeType = persistentAttributeType;
}
@Override
public String getName() {
return name;
}
@Override
public ManagedType<X> getDeclaringType() {
return declaringType;
}
@Override
public Class<Y> getJavaType() {
return javaType;
}
@Override
public Member getJavaMember() {
return member;
}
@Override
public PersistentAttributeType getPersistentAttributeType() {
return persistentAttributeType;
}
protected void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
final String memberDeclaringClassName = ( String ) ois.readObject();
final String memberName = ( String ) ois.readObject();
final String memberType = ( String ) ois.readObject();
final Class memberDeclaringClass = Class.forName(
memberDeclaringClassName,
false,
declaringType.getJavaType().getClassLoader()
);
try {
this.member = "method".equals( memberType )
? memberDeclaringClass.getMethod( memberName, ReflectHelper.NO_PARAM_SIGNATURE )
: memberDeclaringClass.getField( memberName );
}
catch ( Exception e ) {
throw new IllegalStateException(
"Unable to locate member [" + memberDeclaringClassName + "#"
+ memberName + "]"
);
}
}
protected void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
oos.writeObject( getJavaMember().getDeclaringClass().getName() );
oos.writeObject( getJavaMember().getName() );
oos.writeObject( Method.class.isInstance( getJavaMember() ) ? "method" : "field" );
}
}