-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ROASTER-101: Added SerialVersionUIDComputer
- Loading branch information
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
api/src/main/java/org/jboss/forge/roaster/model/util/SerialVersionUIDComputer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.roaster.model.util; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Modifier; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.List; | ||
|
||
import org.jboss.forge.roaster.model.source.JavaClassSource; | ||
import org.jboss.forge.roaster.model.source.MemberSource; | ||
|
||
/** | ||
* Computes the SerialVersion UID value | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class SerialVersionUIDComputer | ||
{ | ||
|
||
private SerialVersionUIDComputer() | ||
{ | ||
} | ||
|
||
/** | ||
* This is not the official algorithm as defined in the JLS spec, but it is close | ||
* | ||
* @see {@link java.io.ObjectStreamClass#getSerialVersionUID()} | ||
* @return the computed serialVersionUID value | ||
*/ | ||
public static long compute(JavaClassSource source) | ||
{ | ||
if (!source.hasInterface(Serializable.class)) | ||
{ | ||
return 0L; | ||
} | ||
try | ||
{ | ||
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
DataOutputStream dout = new DataOutputStream(bout); | ||
|
||
dout.writeUTF(source.getQualifiedName()); | ||
int classMods = 0; | ||
if (source.isPublic()) | ||
{ | ||
classMods |= Modifier.PUBLIC; | ||
} | ||
if (source.isFinal()) | ||
{ | ||
classMods |= Modifier.FINAL; | ||
} | ||
if (source.isInterface()) | ||
{ | ||
classMods |= Modifier.INTERFACE; | ||
} | ||
if (source.isAbstract()) | ||
{ | ||
classMods |= Modifier.ABSTRACT; | ||
} | ||
dout.writeInt(classMods); | ||
|
||
List<MemberSource<JavaClassSource, ?>> members = source.getMembers(); | ||
for (MemberSource<JavaClassSource, ?> member : members) | ||
{ | ||
if (!member.isPrivate()) | ||
{ | ||
dout.writeUTF(member.getName()); | ||
dout.writeUTF(member.toString().replace('/', '.')); | ||
} | ||
} | ||
dout.flush(); | ||
System.out.println(bout); | ||
MessageDigest md = MessageDigest.getInstance("SHA"); | ||
byte[] hashBytes = md.digest(bout.toByteArray()); | ||
long hash = 0; | ||
for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) | ||
{ | ||
hash = (hash << 8) | (hashBytes[i] & 0xFF); | ||
} | ||
return hash; | ||
} | ||
catch (IOException ex) | ||
{ | ||
throw new InternalError(ex); | ||
} | ||
catch (NoSuchAlgorithmException ex) | ||
{ | ||
throw new SecurityException(ex.getMessage()); | ||
} | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...s/src/test/java/org/jboss/forge/test/roaster/model/util/SerialVersionUIDComputerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.test.roaster.model.util; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.jboss.forge.roaster.Roaster; | ||
import org.jboss.forge.roaster.model.source.JavaClassSource; | ||
import org.jboss.forge.roaster.model.util.SerialVersionUIDComputer; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class SerialVersionUIDComputerTest | ||
{ | ||
|
||
@Test | ||
public void testNonSerializableSerialVersionUID() throws Exception | ||
{ | ||
JavaClassSource type = Roaster.create(JavaClassSource.class).setName("Foo"); | ||
type.addMethod().setName("foo"); | ||
type.addField().setType(String.class).setName("bar"); | ||
Assert.assertEquals(0, SerialVersionUIDComputer.compute(type)); | ||
} | ||
|
||
@Test | ||
public void testSerialVersionUID1() throws Exception | ||
{ | ||
JavaClassSource type = Roaster.create(JavaClassSource.class).setName("Foo").addInterface(Serializable.class); | ||
type.addMethod().setName("foo"); | ||
type.addField().setType(String.class).setName("bar"); | ||
long result = 8297639620252560729L; | ||
Assert.assertEquals(result, SerialVersionUIDComputer.compute(type)); | ||
} | ||
|
||
@Test | ||
public void testSerialVersionUID2() throws Exception | ||
{ | ||
JavaClassSource type = Roaster.create(JavaClassSource.class).setName("Bar").addInterface(Serializable.class); | ||
type.addMethod().setName("foo"); | ||
type.addField().setType(String.class).setName("bar"); | ||
long result = 6931690417956417438L; | ||
Assert.assertEquals(result, SerialVersionUIDComputer.compute(type)); | ||
} | ||
} |