Skip to content

Commit ca9a877

Browse files
committed
Merge branch '49-spotbugs-issues' into 'dev'
Fix some SpotBugs issues See merge request objectbox/objectbox-java!65
2 parents 02fa97a + 4a1bdce commit ca9a877

File tree

7 files changed

+45
-23
lines changed

7 files changed

+45
-23
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
public class BoxStore implements Closeable {
6161

6262
/** On Android used for native library loading. */
63-
@Nullable public static Object context;
64-
@Nullable public static Object relinker;
63+
@Nullable private static Object context;
64+
@Nullable private static Object relinker;
6565

6666
/** Change so ReLinker will update native library when using workaround loading. */
6767
public static final String JNI_VERSION = "2.6.0-RC";
@@ -73,6 +73,18 @@ public class BoxStore implements Closeable {
7373
private static final Set<String> openFiles = new HashSet<>();
7474
private static volatile Thread openFilesCheckerThread;
7575

76+
@Nullable
77+
@Internal
78+
public static synchronized Object getContext() {
79+
return context;
80+
}
81+
82+
@Nullable
83+
@Internal
84+
public static synchronized Object getRelinker() {
85+
return relinker;
86+
}
87+
7688
/**
7789
* Convenience singleton instance which gets set up using {@link BoxStoreBuilder#buildDefault()}.
7890
* <p>
@@ -276,16 +288,19 @@ static boolean isFileOpen(final String canonicalPath) {
276288
synchronized (openFiles) {
277289
if (!openFiles.contains(canonicalPath)) return false;
278290
}
279-
if (openFilesCheckerThread == null || !openFilesCheckerThread.isAlive()) {
291+
Thread checkerThread = BoxStore.openFilesCheckerThread;
292+
if (checkerThread == null || !checkerThread.isAlive()) {
280293
// Use a thread to avoid finalizers that block us
281-
openFilesCheckerThread = new Thread(() -> {
294+
checkerThread = new Thread(() -> {
282295
isFileOpenSync(canonicalPath, true);
283-
openFilesCheckerThread = null; // Clean ref to itself
296+
BoxStore.openFilesCheckerThread = null; // Clean ref to itself
284297
});
285-
openFilesCheckerThread.setDaemon(true);
286-
openFilesCheckerThread.start();
298+
checkerThread.setDaemon(true);
299+
300+
BoxStore.openFilesCheckerThread = checkerThread;
301+
checkerThread.start();
287302
try {
288-
openFilesCheckerThread.join(500);
303+
checkerThread.join(500);
289304
} catch (InterruptedException e) {
290305
e.printStackTrace();
291306
}

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.OutputStream;
2828
import java.lang.reflect.Method;
2929
import java.util.ArrayList;
30+
import java.util.Arrays;
3031
import java.util.List;
3132

3233
import javax.annotation.Nonnull;
@@ -107,10 +108,11 @@ private BoxStoreBuilder() {
107108
/** Called internally from the generated class "MyObjectBox". Check MyObjectBox.builder() to get an instance. */
108109
@Internal
109110
public BoxStoreBuilder(byte[] model) {
110-
this.model = model;
111111
if (model == null) {
112112
throw new IllegalArgumentException("Model may not be null");
113113
}
114+
// Future-proofing: copy to prevent external modification.
115+
this.model = Arrays.copyOf(model, model.length);
114116
}
115117

116118
/**

objectbox-java/src/main/java/io/objectbox/internal/NativeLibraryLoader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,22 @@ private static void checkUnpackLib(String filename) {
181181
}
182182

183183
private static boolean loadLibraryAndroid() {
184-
if (BoxStore.context == null) {
184+
if (BoxStore.getContext() == null) {
185185
return false;
186186
}
187187

188188
//noinspection TryWithIdenticalCatches
189189
try {
190190
Class<?> context = Class.forName("android.content.Context");
191-
if (BoxStore.relinker == null) {
191+
if (BoxStore.getRelinker() == null) {
192192
// use default ReLinker
193193
Class<?> relinker = Class.forName("com.getkeepsafe.relinker.ReLinker");
194194
Method loadLibrary = relinker.getMethod("loadLibrary", context, String.class, String.class);
195-
loadLibrary.invoke(null, BoxStore.context, OBJECTBOX_JNI, BoxStore.JNI_VERSION);
195+
loadLibrary.invoke(null, BoxStore.getContext(), OBJECTBOX_JNI, BoxStore.JNI_VERSION);
196196
} else {
197197
// use custom ReLinkerInstance
198-
Method loadLibrary = BoxStore.relinker.getClass().getMethod("loadLibrary", context, String.class, String.class);
199-
loadLibrary.invoke(BoxStore.relinker, BoxStore.context, OBJECTBOX_JNI, BoxStore.JNI_VERSION);
198+
Method loadLibrary = BoxStore.getRelinker().getClass().getMethod("loadLibrary", context, String.class, String.class);
199+
loadLibrary.invoke(BoxStore.getRelinker(), BoxStore.getContext(), OBJECTBOX_JNI, BoxStore.JNI_VERSION);
200200
}
201201
} catch (NoSuchMethodException e) {
202202
return false;

objectbox-java/src/main/java/io/objectbox/model/EntityFlags.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ private EntityFlags() { }
2828
*/
2929
public static final int USE_NO_ARG_CONSTRUCTOR = 1;
3030

31-
public static final String[] names = { "USE_NO_ARG_CONSTRUCTOR", };
31+
// Private to protect contents from getting modified.
32+
private static final String[] names = { "USE_NO_ARG_CONSTRUCTOR", };
3233

3334
public static String name(int e) { return names[e - USE_NO_ARG_CONSTRUCTOR]; }
3435
}

objectbox-java/src/main/java/io/objectbox/model/PropertyType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ private PropertyType() { }
6969
public static final short DateVector = 31;
7070
public static final short DateNanoVector = 32;
7171

72-
public static final String[] names = { "Unknown", "Bool", "Byte", "Short", "Char", "Int", "Long", "Float", "Double", "String", "Date", "Relation", "DateNano", "Reserved2", "Reserved3", "Reserved4", "Reserved5", "Reserved6", "Reserved7", "Reserved8", "Reserved9", "Reserved10", "BoolVector", "ByteVector", "ShortVector", "CharVector", "IntVector", "LongVector", "FloatVector", "DoubleVector", "StringVector", "DateVector", "DateNanoVector", };
72+
// Private to protect contents from getting modified.
73+
private static final String[] names = { "Unknown", "Bool", "Byte", "Short", "Char", "Int", "Long", "Float", "Double", "String", "Date", "Relation", "DateNano", "Reserved2", "Reserved3", "Reserved4", "Reserved5", "Reserved6", "Reserved7", "Reserved8", "Reserved9", "Reserved10", "BoolVector", "ByteVector", "ShortVector", "CharVector", "IntVector", "LongVector", "FloatVector", "DoubleVector", "StringVector", "DateVector", "DateNanoVector", };
7374

7475
public static String name(int e) { return names[e]; }
7576
}

objectbox-java/src/main/java/io/objectbox/query/OrderFlags.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ private OrderFlags() { }
4646
*/
4747
public static final int NULLS_ZERO = 16;
4848

49-
public static final String[] names = { "DESCENDING", "CASE_SENSITIVE", "", "UNSIGNED", "", "", "", "NULLS_LAST", "", "", "", "", "", "", "", "NULLS_ZERO", };
49+
// Private to protect contents from getting modified.
50+
private static final String[] names = { "DESCENDING", "CASE_SENSITIVE", "", "UNSIGNED", "", "", "", "NULLS_LAST", "", "", "", "", "", "", "", "NULLS_ZERO", };
5051

5152
public static String name(int e) { return names[e - DESCENDING]; }
5253
}

objectbox-java/src/main/java/io/objectbox/relation/ToMany.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ public class ToMany<TARGET> implements List<TARGET>, Serializable {
6868
private final Object entity;
6969
private final RelationInfo<Object, TARGET> relationInfo;
7070

71-
private ListFactory listFactory;
71+
private volatile ListFactory listFactory;
7272
private List<TARGET> entities;
7373

7474
/** Counts of all entities in the list ({@link #entities}). */
7575
private Map<TARGET, Integer> entityCounts;
7676

7777
/** Entities added since last put/sync. Map is used as a set (value is always Boolean.TRUE). */
78-
private Map<TARGET, Boolean> entitiesAdded;
78+
private volatile Map<TARGET, Boolean> entitiesAdded;
7979

8080
/** Entities removed since last put/sync. Map is used as a set (value is always Boolean.TRUE). */
8181
private Map<TARGET, Boolean> entitiesRemoved;
@@ -129,14 +129,16 @@ public synchronized void setRemoveFromTargetBox(boolean removeFromTargetBox) {
129129
}
130130

131131
public ListFactory getListFactory() {
132-
if (listFactory == null) {
132+
ListFactory result = listFactory;
133+
if (result == null) {
133134
synchronized (this) {
134-
if (listFactory == null) {
135-
listFactory = new CopyOnWriteArrayListFactory();
135+
result = listFactory;
136+
if (result == null) {
137+
listFactory = result = new CopyOnWriteArrayListFactory();
136138
}
137139
}
138140
}
139-
return listFactory;
141+
return result;
140142
}
141143

142144
private void ensureBoxes() {

0 commit comments

Comments
 (0)