Skip to content
Permalink
Browse files

Merge branch 'findbugs' into spotbugs

 Conflicts:
	src/main/java/com/mebigfatguy/fbcontrib/utils/SerialVersionCalc.java
  • Loading branch information...
mebigfatguy committed Jul 25, 2019
2 parents 300ccf7 + 6afdad8 commit 706c90bb28674af772bd2543633f9d793638f649
Showing with 74 additions and 58 deletions.
  1. +1 −0 README.md
  2. +3 −0 pom.xml
  3. +70 −58 src/main/java/com/mebigfatguy/fbcontrib/utils/SerialVersionCalc.java
@@ -46,6 +46,7 @@ Contributors
* David Burström
* Venkata Gajavalli
* Rubén López
* Pavel Roskin


fb-contrib has two main branches, 'findbugs' and 'spotbugs'. Code is committed to findbugs, and then merged to spotbugs.
@@ -91,6 +91,9 @@
<contributor>
<name>Rubén López</name>
</contributor>
<contribnutor>
<name>Pavel Roskin</name>
</contribnutor>
</contributors>

<licenses>
@@ -19,22 +19,23 @@
package com.mebigfatguy.fbcontrib.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Comparator;

import org.apache.bcel.Const;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.FieldOrMethod;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;

import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;

public final class SerialVersionCalc {

enum ModifierType {
@@ -51,65 +52,15 @@ public static long uuid(JavaClass cls) throws IOException {
return 0;
}


try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos)) {

dos.writeUTF(cls.getClassName());
dos.writeInt(filterModifiers(cls.getModifiers(), ModifierType.CLASS));

String[] infs = cls.getInterfaceNames();
if (infs.length > 0) {
infs = infs.clone();
Arrays.sort(infs);
for (String inf : infs) {
dos.writeUTF(inf);
}
}

Field[] fields = cls.getFields();
if (fields.length > 0) {
fields = fields.clone();
Arrays.sort(fields, new FieldSorter());
for (Field field : fields) {
if (!field.isPrivate() || (!field.isStatic() && !field.isTransient())) {
dos.writeUTF(field.getName());
dos.writeInt(filterModifiers(field.getModifiers(), ModifierType.FIELD));
dos.writeUTF(field.getSignature());
}
}
}

Method[] methods = cls.getMethods();
if (methods.length > 0) {
methods = methods.clone();
Arrays.sort(methods, new MethodSorter());

for (Method sinit : methods) {
if ("<clinit>".equals(sinit.getName())) {
dos.writeUTF("<clinit>");
dos.writeInt(Const.ACC_STATIC);
dos.writeUTF("()V");
break;
}
}

for (Method init : methods) {
if ("<init>".equals(init.getName()) && !init.isPrivate()) {
dos.writeUTF("<init>");
dos.writeInt(filterModifiers(init.getModifiers(), ModifierType.METHOD));
dos.writeUTF(init.getSignature().replace('/', '.')); // how bazaar
}
}

for (Method method : methods) {
if (!"<clinit>".equals(method.getName()) && !"<init>".equals(method.getName()) && !method.isPrivate()) {
dos.writeUTF(method.getName());
dos.writeInt(filterModifiers(method.getModifiers(), ModifierType.METHOD));
dos.writeUTF(method.getSignature().replace('/', '.')); // how bazaar
}
}
}
writeInterfaces(dos, cls);
writeFields(dos, cls);
writeMethods(dos, cls);

dos.flush();
MessageDigest digest = MessageDigest.getInstance("SHA-1");
@@ -123,7 +74,66 @@ public static long uuid(JavaClass cls) throws IOException {
return 0;
}
}

private static void writeInterfaces(DataOutput out, JavaClass cls) throws IOException {
String[] infs = cls.getInterfaceNames();
if (infs.length > 0) {
infs = infs.clone();
Arrays.sort(infs);
for (String inf : infs) {
out.writeUTF(inf);
}
}
}

private static void writeFields(DataOutput out, JavaClass cls) throws IOException {
Field[] fields = cls.getFields();
if (fields.length > 0) {
fields = fields.clone();
Arrays.sort(fields, new FieldSorter());
for (Field field : fields) {
if (!field.isPrivate() || (!field.isStatic() && !field.isTransient())) {
out.writeUTF(field.getName());
out.writeInt(filterModifiers(field.getModifiers(), ModifierType.FIELD));
out.writeUTF(field.getSignature());
}
}
}
}

private static void writeMethods(DataOutput out, JavaClass cls) throws IOException {
Method[] methods = cls.getMethods();
if (methods.length > 0) {
methods = methods.clone();
Arrays.sort(methods, new MethodSorter());

for (Method sinit : methods) {
if ("<clinit>".equals(sinit.getName())) {
out.writeUTF("<clinit>");
out.writeInt(Const.ACC_STATIC);
out.writeUTF("()V");
break;
}
}

for (Method init : methods) {
if ("<init>".equals(init.getName()) && !init.isPrivate()) {
out.writeUTF("<init>");
out.writeInt(filterModifiers(init.getModifiers(), ModifierType.METHOD));
out.writeUTF(init.getSignature().replace('/', '.')); // how bazaar
}
}

for (Method method : methods) {
if (!"<clinit>".equals(method.getName()) && !"<init>".equals(method.getName()) && !method.isPrivate()) {
out.writeUTF(method.getName());
out.writeInt(filterModifiers(method.getModifiers(), ModifierType.METHOD));
out.writeUTF(method.getSignature().replace('/', '.')); // how bazaar
}
}
}
}

private static int filterModifiers(int modifier, ModifierType type) {

switch (type) {
@@ -145,15 +155,17 @@ private static int filterModifiers(int modifier, ModifierType type) {

}

static class FieldSorter implements Comparator<Field> {
static class FieldSorter implements Comparator<Field>, Serializable {
private static final long serialVersionUID = 1L;

@Override
public int compare(Field f1, Field f2) {
return f1.getName().compareTo(f2.getName());
}
}

static class MethodSorter implements Comparator<Method> {
static class MethodSorter implements Comparator<Method>, Serializable {
private static final long serialVersionUID = 1L;

@Override
public int compare(Method m1, Method m2) {

0 comments on commit 706c90b

Please sign in to comment.
You can’t perform that action at this time.