Skip to content

Commit ef6b3d3

Browse files
committed
Fix a bunch of FindBugs and IntelliJ inspections
1 parent 29a7575 commit ef6b3d3

File tree

10 files changed

+38
-34
lines changed

10 files changed

+38
-34
lines changed

unsafe/src/main/java/org/apache/spark/unsafe/array/LongArray.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
*/
3030
public final class LongArray {
3131

32-
private static final int WIDTH = 8;
32+
// This is a long so that we perform long multiplications when computing offsets.
33+
private static final long WIDTH = 8;
3334

3435
private final MemoryBlock memory;
3536
private final Object baseObj;

unsafe/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,13 @@ public BytesToBytesMap(
176176
}
177177

178178
@Override
179-
public void finalize() {
180-
// In case the programmer forgot to call `free()`, try to perform that cleanup now:
181-
free();
179+
protected void finalize() throws Throwable {
180+
try {
181+
// In case the programmer forgot to call `free()`, try to perform that cleanup now:
182+
free();
183+
} finally {
184+
super.finalize();
185+
}
182186
}
183187

184188
/**

unsafe/src/main/java/org/apache/spark/unsafe/memory/MemoryAllocator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public interface MemoryAllocator {
2323
* Allocates a contiguous block of memory. Note that the allocated memory is not guaranteed
2424
* to be zeroed out (call `zero()` on the result if this is necessary).
2525
*/
26-
public MemoryBlock allocate(long size) throws OutOfMemoryError;
26+
MemoryBlock allocate(long size) throws OutOfMemoryError;
2727

28-
public void free(MemoryBlock memory);
28+
void free(MemoryBlock memory);
2929

30-
public static final MemoryAllocator UNSAFE = new UnsafeMemoryAllocator();
30+
MemoryAllocator UNSAFE = new UnsafeMemoryAllocator();
3131

32-
public static final MemoryAllocator HEAP = new HeapMemoryAllocator();
32+
MemoryAllocator HEAP = new HeapMemoryAllocator();
3333
}

unsafe/src/main/java/org/apache/spark/unsafe/memory/MemoryBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
public class MemoryBlock extends MemoryLocation {
2828

29-
final long length;
29+
private final long length;
3030

3131
MemoryBlock(@Nullable Object obj, long offset, long length) {
3232
super(obj, offset);

unsafe/src/main/java/org/apache/spark/unsafe/memory/MemoryLocation.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
public class MemoryLocation {
2727

2828
@Nullable
29-
protected Object obj;
29+
Object obj;
3030

31-
protected long offset;
31+
long offset;
3232

3333
public MemoryLocation(@Nullable Object obj, long offset) {
3434
this.obj = obj;
@@ -44,10 +44,6 @@ public void setObjAndOffset(Object newObj, long newOffset) {
4444
this.offset = newOffset;
4545
}
4646

47-
public void setOffset(long newOffset) {
48-
this.offset = newOffset;
49-
}
50-
5147
public final Object getBaseObject() {
5248
return obj;
5349
}

unsafe/src/main/java/org/apache/spark/unsafe/string/UTF8StringMethods.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static int getLengthInCodePoints(Object baseObject, long baseOffset, int
107107
}
108108

109109
public static String toJavaString(Object baseObject, long baseOffset, int lengthInBytes) {
110-
final byte[] bytes = new byte[(int) lengthInBytes];
110+
final byte[] bytes = new byte[lengthInBytes];
111111
PlatformDependent.UNSAFE.copyMemory(
112112
baseObject,
113113
baseOffset,
@@ -129,8 +129,11 @@ public static String toJavaString(Object baseObject, long baseOffset, int length
129129
*
130130
* @return the number of bytes written, including the space for tracking the string's length.
131131
*/
132-
public static int createFromJavaString(Object baseObject, long baseOffset, String str) {
133-
final byte[] strBytes = str.getBytes();
132+
public static int createFromJavaString(
133+
Object baseObject,
134+
long baseOffset,
135+
String str) throws UnsupportedEncodingException {
136+
final byte[] strBytes = str.getBytes("utf-8");
134137
final int strLengthInBytes = strBytes.length;
135138
PlatformDependent.copyMemory(
136139
strBytes,
@@ -159,7 +162,7 @@ public static int numOfBytes(byte b) {
159162
* number of tailing bytes in a UTF8 sequence for a code point
160163
* see http://en.wikipedia.org/wiki/UTF-8, 192-256 of Byte 1
161164
*/
162-
private static int[] bytesOfCodePointInUTF8 = new int[] {
165+
private static final int[] bytesOfCodePointInUTF8 = new int[] {
163166
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
164167
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
165168
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,

unsafe/src/test/java/org/apache/spark/unsafe/array/TestLongArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class TestLongArray {
2626

27-
private LongArray createTestData() {
27+
private static LongArray createTestData() {
2828
byte[] bytes = new byte[16];
2929
LongArray arr = new LongArray(MemoryBlock.fromByteArray(bytes));
3030
arr.set(0, 1L);

unsafe/src/test/java/org/apache/spark/unsafe/bitset/TestBitSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public class TestBitSet {
2727

28-
private BitSet createBitSet(int capacity) {
28+
private static BitSet createBitSet(int capacity) {
2929
assert capacity % 64 == 0;
3030
return new BitSet(MemoryBlock.fromLongArray(new long[capacity / 64]).zero());
3131
}

unsafe/src/test/java/org/apache/spark/unsafe/hash/TestMurmur3_x86_32.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
public class TestMurmur3_x86_32 {
3232

33-
private static Murmur3_x86_32 hasher = new Murmur3_x86_32(0);
33+
private static final Murmur3_x86_32 hasher = new Murmur3_x86_32(0);
3434

3535
@Test
3636
public void testKnownIntegerInputs() {

unsafe/src/test/java/org/apache/spark/unsafe/map/AbstractTestBytesToBytesMap.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@
1717

1818
package org.apache.spark.unsafe.map;
1919

20+
import java.lang.Exception;
21+
import java.nio.ByteBuffer;
22+
import java.util.*;
23+
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
2027
import org.apache.spark.unsafe.array.ByteArrayMethods;
2128
import org.apache.spark.unsafe.PlatformDependent;
2229
import org.apache.spark.unsafe.memory.MemoryAllocator;
2330
import org.apache.spark.unsafe.memory.MemoryLocation;
2431
import static org.apache.spark.unsafe.PlatformDependent.BYTE_ARRAY_OFFSET;
25-
import org.junit.Assert;
26-
import org.junit.Test;
27-
28-
import java.lang.Exception;
29-
import java.lang.IllegalStateException;
30-
import java.nio.ByteBuffer;
31-
import java.util.*;
3232

3333
public abstract class AbstractTestBytesToBytesMap {
3434

35-
protected final Random rand = new Random(42);
35+
private final Random rand = new Random(42);
3636

37-
protected final MemoryAllocator allocator = getMemoryAllocator();
37+
private final MemoryAllocator allocator = getMemoryAllocator();
3838

3939
protected abstract MemoryAllocator getMemoryAllocator();
4040

41-
protected byte[] getByteArray(MemoryLocation loc, int size) {
41+
private static byte[] getByteArray(MemoryLocation loc, int size) {
4242
final byte[] arr = new byte[size];
4343
PlatformDependent.UNSAFE.copyMemory(
4444
loc.getBaseObject(),
@@ -50,7 +50,7 @@ protected byte[] getByteArray(MemoryLocation loc, int size) {
5050
return arr;
5151
}
5252

53-
protected byte[] getRandomByteArray(int numWords) {
53+
private byte[] getRandomByteArray(int numWords) {
5454
Assert.assertTrue(numWords > 0);
5555
final int lengthInBytes = numWords * 8;
5656
final byte[] bytes = new byte[lengthInBytes];
@@ -62,7 +62,7 @@ protected byte[] getRandomByteArray(int numWords) {
6262
* Fast equality checking for byte arrays, since these comparisons are a bottleneck
6363
* in our stress tests.
6464
*/
65-
protected boolean arrayEquals(
65+
private static boolean arrayEquals(
6666
byte[] expected,
6767
MemoryLocation actualAddr,
6868
long actualLengthBytes) {

0 commit comments

Comments
 (0)