in lightning-core/src/main/java/org/apache/directmemory/lightning/internal/util/ClassUtil.java [148:295]
public static long calculateSerialVersionUID( Class<?> clazz )
{
Long serialVersion = SERIAL_VERSION_UID_CACHE.get( clazz );
if ( serialVersion != null )
{
return serialVersion;
}
if ( Serializable.class.isAssignableFrom( clazz ) )
{
serialVersion = ObjectStreamClass.lookup( clazz ).getSerialVersionUID();
SERIAL_VERSION_UID_CACHE.put( clazz, serialVersion );
return serialVersion;
}
serialVersion = getSerialVersionUIDFromField( clazz );
if ( serialVersion != null )
{
SERIAL_VERSION_UID_CACHE.put( clazz, serialVersion );
return serialVersion;
}
try
{
ClassReader reader = new ClassReader( Type.getInternalName( clazz ).replace( "/", "." ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream( baos );
SerialVersionClassVisitor classVisitor = new SerialVersionClassVisitor();
reader.accept( classVisitor, 0 );
// Classname
out.writeUTF( toJavaName( classVisitor.name ) );
// Modifiers
out.writeInt( clazz.getModifiers()
& ( Modifier.PUBLIC | Modifier.FINAL | Modifier.INTERFACE | Modifier.ABSTRACT ) );
// Interfaces
Collections.sort( classVisitor.interfaces );
for ( int i = 0; i < classVisitor.interfaces.size(); i++ )
{
out.writeUTF( toJavaName( classVisitor.interfaces.get( i ) ) );
}
// Fields
Field[] fields = clazz.getDeclaredFields();
Arrays.sort( fields, new Comparator<Field>()
{
@Override
public int compare( Field o1, Field o2 )
{
return o1.getName().compareTo( o2.getName() );
}
} );
for ( Field field : fields )
{
int mods = field.getModifiers();
if ( ( ( mods & Modifier.PRIVATE ) == 0 || ( mods & ( Modifier.STATIC | Modifier.TRANSIENT ) ) == 0 ) )
{
out.writeUTF( field.getName() );
out.writeInt( mods );
out.writeUTF( Type.getDescriptor( field.getType() ) );
}
}
// Static Initializer
if ( classVisitor.staticInitializerFound )
{
out.writeUTF( "<clinit>" );
out.writeInt( Modifier.STATIC );
out.writeUTF( "()V" );
}
// Constructors
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
Arrays.sort( constructors, new Comparator<Constructor<?>>()
{
@Override
public int compare( Constructor<?> o1, Constructor<?> o2 )
{
return Type.getConstructorDescriptor( o1 ).compareTo( Type.getConstructorDescriptor( o2 ) );
}
} );
for ( int i = 0; i < constructors.length; i++ )
{
Constructor<?> constructor = constructors[i];
int mods = constructor.getModifiers();
if ( ( mods & Modifier.PRIVATE ) == 0 )
{
out.writeUTF( "<init>" );
out.writeInt( mods );
out.writeUTF( toJavaName( Type.getConstructorDescriptor( constructor ) ) );
}
}
// Methods
Method[] methods = clazz.getDeclaredMethods();
Arrays.sort( methods, new Comparator<Method>()
{
@Override
public int compare( Method o1, Method o2 )
{
return Type.getMethodDescriptor( o1 ).compareTo( Type.getMethodDescriptor( o2 ) );
}
} );
for ( int i = 0; i < methods.length; i++ )
{
Method method = methods[i];
int mods = method.getModifiers();
if ( ( mods & Modifier.PRIVATE ) == 0 )
{
out.writeUTF( "<init>" );
out.writeInt( mods );
out.writeUTF( toJavaName( Type.getMethodDescriptor( method ) ) );
}
}
// Final calculation
out.flush();
MessageDigest digest = MessageDigest.getInstance( "SHA" );
byte[] checksum = digest.digest( baos.toByteArray() );
long hash = 0;
for ( int i = Math.min( checksum.length, 8 ) - 1; i >= 0; i-- )
{
hash = ( hash << 8 ) | ( checksum[i] & 0xFF );
}
SERIAL_VERSION_UID_CACHE.put( clazz, hash );
return hash;
}
catch ( IOException e )
{
}
catch ( NoSuchAlgorithmException e )
{
}
return -1L;
}