Skip to content

Commit 92dc95f

Browse files
committed
Various SpotBugs fixes
1 parent f35e273 commit 92dc95f

File tree

60 files changed

+276
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+276
-284
lines changed

mx.truffleruby/spotbugs-filters.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<Bug pattern="EI_EXPOSE_REP2" />
2222
<Bug pattern="FE_FLOATING_POINT_EQUALITY" />
2323
<Bug pattern="NM_CLASS_NOT_EXCEPTION" />
24+
<Bug pattern="RC_REF_COMPARISON_BAD_PRACTICE_BOOLEAN" />
25+
<Bug pattern="BX_UNBOXING_IMMEDIATELY_REBOXED" /> <!-- used as a way to assert the type -->
26+
<Bug pattern="PZLA_PREFER_ZERO_LENGTH_ARRAYS" />
2427
</Or>
2528
</Not>
2629
</Match>

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,16 @@ public class RubyContext {
144144

145145
private static boolean preInitializeContexts = TruffleRuby.PRE_INITIALIZE_CONTEXTS;
146146

147+
private static boolean isPreInitializingContext() {
148+
boolean isPreInitializingContext = preInitializeContexts;
149+
preInitializeContexts = false; // Only the first context is pre-initialized
150+
return isPreInitializingContext;
151+
}
152+
147153
public RubyContext(RubyLanguage language, TruffleLanguage.Env env) {
148154
Metrics.printTime("before-context-constructor");
149155

150-
this.preInitializing = preInitializeContexts;
151-
RubyContext.preInitializeContexts = false; // Only the first context is pre-initialized
156+
this.preInitializing = isPreInitializingContext();
152157
this.preInitialized = preInitializing;
153158

154159
preInitializationManager = preInitializing ? new PreInitializationManager(this) : null;

src/main/java/org/truffleruby/RubyFileTypeDetector.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.nio.charset.Charset;
1515
import java.nio.charset.StandardCharsets;
1616
import java.util.Locale;
17-
import java.util.function.BiConsumer;
1817
import java.util.regex.Pattern;
1918

2019
import org.jcodings.Encoding;
@@ -80,14 +79,11 @@ public Charset findEncoding(TruffleFile file) throws IOException {
8079
if (encodingCommentLine != null) {
8180
Rope encodingCommentRope = StringOperations.encodeRope(encodingCommentLine, UTF8Encoding.INSTANCE);
8281
Charset[] encodingHolder = new Charset[1];
83-
RubyLexer.parseMagicComment(encodingCommentRope, new BiConsumer<String, Rope>() {
84-
@Override
85-
public void accept(String name, Rope value) {
86-
if (RubyLexer.isMagicEncodingComment(name)) {
87-
Encoding encoding = EncodingManager.getEncoding(value);
88-
if (encoding != null) {
89-
encodingHolder[0] = encoding.getCharset();
90-
}
82+
RubyLexer.parseMagicComment(encodingCommentRope, (name, value) -> {
83+
if (RubyLexer.isMagicEncodingComment(name)) {
84+
Encoding encoding = EncodingManager.getEncoding(value);
85+
if (encoding != null) {
86+
encodingHolder[0] = encoding.getCharset();
9187
}
9288
}
9389
});

src/main/java/org/truffleruby/aot/TruffleRubySupport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.regex.Matcher;
2727
import java.util.regex.Pattern;
2828

29+
import com.oracle.truffle.api.CompilerDirectives;
2930
import org.jcodings.Encoding;
3031
import org.jcodings.EncodingDB;
3132
import org.jcodings.util.ArrayReader;
@@ -73,12 +74,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
7374
try (InputStream is = ArrayReader.class.getResourceAsStream(entry)) {
7475
if (is != null) {
7576
byte[] buf = new byte[is.available()];
76-
new DataInputStream(is).readFully(buf);
77+
try (DataInputStream dataInputStream = new DataInputStream(is)) {
78+
dataInputStream.readFully(buf);
79+
}
7780
jcodingsTables.put(name, buf);
7881
} else {
79-
throw new Error("Unable to load Jcodings table " + name);
82+
throw CompilerDirectives.shouldNotReachHere("Unable to load Jcodings table " + name);
8083
}
8184
} catch (IOException e) {
85+
throw CompilerDirectives.shouldNotReachHere(e);
8286
}
8387
}
8488
return jcodingsTables;

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
import org.truffleruby.RubyContext;
1919
import org.truffleruby.RubyLanguage;
20+
import org.truffleruby.SuppressFBWarnings;
2021
import org.truffleruby.cext.ValueWrapperManagerFactory.AllocateHandleNodeGen;
2122
import org.truffleruby.cext.ValueWrapperManagerFactory.GetHandleBlockHolderNodeGen;
23+
import org.truffleruby.extra.ffi.Pointer;
2224
import org.truffleruby.language.NotProvided;
2325
import org.truffleruby.language.RubyBaseNode;
2426

@@ -32,6 +34,7 @@
3234
import com.oracle.truffle.api.library.ExportLibrary;
3335
import com.oracle.truffle.api.library.ExportMessage;
3436

37+
@SuppressFBWarnings("VO")
3538
public class ValueWrapperManager {
3639

3740
static final long UNSET_HANDLE = -2L;
@@ -232,7 +235,7 @@ public boolean isFull() {
232235
}
233236

234237
public long setHandleOnWrapper(ValueWrapper wrapper) {
235-
long handle = getBase() + count * 8;
238+
long handle = getBase() + count * Pointer.SIZE;
236239
wrapper.setHandle(handle, this);
237240
wrappers[count] = wrapper;
238241
count++;

src/main/java/org/truffleruby/collections/IntHashMap.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,4 @@ public String toString() {
331331
}
332332
}
333333

334-
@Override
335-
public Object clone() {
336-
IntHashMap<V> newMap = new IntHashMap<>(table.length, loadFactor);
337-
for (int i = 0; i < table.length; i++) {
338-
Entry<V> entry = table[i];
339-
while (entry != null) {
340-
newMap.put(entry.getKey(), entry.getValue());
341-
entry = entry.next;
342-
}
343-
}
344-
return newMap;
345-
}
346-
347334
}

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ public CoreLibrary(RubyContext context) {
700700
patchFiles = initializePatching(context);
701701
}
702702

703+
@SuppressFBWarnings("SIC")
703704
private ConcurrentMap<String, Boolean> initializePatching(RubyContext context) {
704705
defineModule(truffleModule, "Patching");
705706
final ConcurrentMap<String, Boolean> patchFiles = new ConcurrentHashMap<>();
@@ -711,7 +712,7 @@ private ConcurrentMap<String, Boolean> initializePatching(RubyContext context) {
711712
patchesDirectory,
712713
new SimpleFileVisitor<Path>() {
713714
@Override
714-
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
715+
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
715716
String relativePath = patchesDirectory.relativize(path).toString();
716717
if (relativePath.endsWith(".rb")) {
717718
patchFiles.put(relativePath.substring(0, relativePath.length() - 3), false);

src/main/java/org/truffleruby/core/GCNodes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.lang.management.GarbageCollectorMXBean;
1313
import java.lang.management.ManagementFactory;
1414

15+
import org.truffleruby.SuppressFBWarnings;
1516
import org.truffleruby.builtins.CoreMethod;
1617
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1718
import org.truffleruby.builtins.CoreModule;
@@ -50,6 +51,7 @@ protected Object vmGCStart() {
5051
@Primitive(name = "gc_force")
5152
public static abstract class GCForce extends PrimitiveArrayArgumentsNode {
5253

54+
@SuppressFBWarnings("DLS")
5355
@TruffleBoundary
5456
@Specialization
5557
protected Object force() {

src/main/java/org/truffleruby/core/MarkingService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public void runAllMarkers() {
9090
ExtensionCallStack stack = markingService.getThreadLocalData().getExtensionCallStack();
9191
stack.push(stack.getBlock());
9292
try {
93+
// TODO (eregon, 15 Sept 2020): there seems to be no synchronization here while walking the list of
94+
// markingService, and concurrent mutations seem to be possible.
9395
MarkerReference currentMarker = markingService.getFirst();
9496
MarkerReference nextMarker;
9597
while (currentMarker != null) {

src/main/java/org/truffleruby/core/ReferenceProcessingService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.oracle.truffle.api.CompilerDirectives;
1818
import org.truffleruby.RubyContext;
19+
import org.truffleruby.SuppressFBWarnings;
1920
import org.truffleruby.core.thread.RubyThread;
2021
import org.truffleruby.core.thread.ThreadManager;
2122
import org.truffleruby.language.control.RaiseException;
@@ -164,15 +165,17 @@ protected void processReferenceQueue(Class<?> owner) {
164165

165166
protected void createProcessingThread(Class<?> owner) {
166167
final ThreadManager threadManager = context.getThreadManager();
168+
RubyThread newThread;
167169
synchronized (this) {
168170
if (processingThread != null) {
169171
return;
170172
}
171-
processingThread = threadManager.createBootThread(threadName());
173+
newThread = threadManager.createBootThread(threadName());
174+
processingThread = newThread;
172175
}
173176
final String sharingReason = "creating " + threadName() + " thread for " + owner.getSimpleName();
174177

175-
threadManager.initialize(processingThread, null, threadName(), sharingReason, () -> {
178+
threadManager.initialize(newThread, null, threadName(), sharingReason, () -> {
176179
while (true) {
177180
final ProcessingReference<?> reference = (ProcessingReference<?>) threadManager
178181
.runUntilResult(null, processingQueue::remove);
@@ -271,6 +274,7 @@ protected synchronized void add(R newRef) {
271274
first = newRef;
272275
}
273276

277+
@SuppressFBWarnings("IS")
274278
protected R getFirst() {
275279
return first;
276280
}

0 commit comments

Comments
 (0)