Skip to content

Commit

Permalink
[Truffle] Clear all major findbugs bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Jan 6, 2015
1 parent 2201765 commit 8c588f4
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 33 deletions.
Expand Up @@ -18,7 +18,6 @@
import org.jruby.Ruby;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.encoding.EncodingService;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.*;
import org.jruby.util.ByteList;
Expand Down Expand Up @@ -62,8 +61,8 @@ public RubyArray convpath(RubyEncodingConverter converter) {

v = new RubyArray(getContext().getCoreLibrary().getArrayClass(),
new Object[]{
RubyEncoding.getEncoding(getContext(), source),
RubyEncoding.getEncoding(getContext(), destination)
RubyEncoding.getEncoding(source),
RubyEncoding.getEncoding(destination)
}, 2);
}
result[r++] = v;
Expand Down Expand Up @@ -156,8 +155,8 @@ public void call(byte[] source, byte[] destination, int depth) {

v = new RubyArray(getContext().getCoreLibrary().getArrayClass(),
new Object[]{
RubyEncoding.getEncoding(getContext(), destinationEncoding),
RubyEncoding.getEncoding(getContext(), sourceEncoding)
RubyEncoding.getEncoding(destinationEncoding),
RubyEncoding.getEncoding(sourceEncoding)
}, 2);
}

Expand Down
Expand Up @@ -49,7 +49,7 @@ public RubyEncoding defaultExternal() {
encoding = UTF8Encoding.INSTANCE;
}

return RubyEncoding.getEncoding(getContext(), encoding);
return RubyEncoding.getEncoding(encoding);
}

}
Expand All @@ -75,7 +75,7 @@ public RubyEncoding defaultInternal() {
encoding = UTF8Encoding.INSTANCE;
}

return RubyEncoding.getEncoding(getContext(), encoding);
return RubyEncoding.getEncoding(encoding);
}

}
Expand Down Expand Up @@ -139,7 +139,7 @@ public FindNode(FindNode prev) {
public RubyEncoding find(RubyString name) {
notDesignedForCompilation();

return RubyEncoding.getEncoding(getContext(), name.toString());
return RubyEncoding.getEncoding(name.toString());
}

}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/truffle/nodes/core/IONodes.java
Expand Up @@ -22,6 +22,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -75,7 +76,7 @@ public RubyArray readLines(RubyString file) {

final List<Object> lines = new ArrayList<>();

try(final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.toString())))) {
try(final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.toString()), StandardCharsets.UTF_8))) {

while (true) {
final String line = reader.readLine();
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -47,6 +47,7 @@

import java.io.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.*;

@CoreClass(name = "Kernel")
Expand Down Expand Up @@ -90,7 +91,7 @@ public RubyString backtick(RubyString command) {
}

final InputStream stdout = process.getInputStream();
final InputStreamReader reader = new InputStreamReader(stdout);
final InputStreamReader reader = new InputStreamReader(stdout, StandardCharsets.UTF_8);

final StringBuilder resultBuilder = new StringBuilder();

Expand Down Expand Up @@ -1983,7 +1984,14 @@ public RubyString sprintf(Object[] args) {
notDesignedForCompilation();

final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(outputStream);

final PrintStream printStream;

try {
printStream = new PrintStream(outputStream, true, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

if (args.length > 0) {
final String format = args[0].toString();
Expand Down
Expand Up @@ -579,7 +579,7 @@ public EncodingNode(EncodingNode prev) {
public RubyEncoding encoding(RubyString string) {
notDesignedForCompilation();

return RubyEncoding.getEncoding(getContext(), string.getBytes().getEncoding());
return RubyEncoding.getEncoding(string.getBytes().getEncoding());
}
}

Expand Down Expand Up @@ -616,7 +616,7 @@ public ForceEncodingNode(ForceEncodingNode prev) {
@Specialization
public RubyString forceEncoding(RubyString string, RubyString encodingName) {
notDesignedForCompilation();
final RubyEncoding encoding = RubyEncoding.getEncoding(getContext(), encodingName.toString());
final RubyEncoding encoding = RubyEncoding.getEncoding(encodingName.toString());
return forceEncoding(string, encoding);
}

Expand Down
Expand Up @@ -35,6 +35,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -381,7 +382,7 @@ public void loadRubyCore(String fileName) {
throw new RuntimeException("couldn't load Truffle core library " + fileName);
}

source = Source.fromReader(new InputStreamReader(resource.getInputStream()), "core:/" + fileName);
source = Source.fromReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8), "core:/" + fileName);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -861,7 +862,7 @@ public RubyHash getENV() {
return envHash;
}

public RubyEncoding getDefaultEncoding() { return RubyEncoding.getEncoding(context, "US-ASCII"); }
public RubyEncoding getDefaultEncoding() { return RubyEncoding.getEncoding("US-ASCII"); }

private RubyHash getSystemEnv() {
final List<KeyValue> entries = new ArrayList<>();
Expand Down
Expand Up @@ -16,6 +16,7 @@
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.util.ByteList;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -31,11 +32,11 @@ public class RubyEncoding extends RubyBasicObject {
private final ByteList name;
private final boolean dummy;

public static synchronized RubyEncoding getEncoding(RubyContext context, Encoding encoding) {
return lookup.get(new String(encoding.getName()).toLowerCase());
public static synchronized RubyEncoding getEncoding(Encoding encoding) {
return lookup.get(new String(encoding.getName(), StandardCharsets.UTF_8).toLowerCase());
}

public static RubyEncoding getEncoding(RubyContext context, String name) {
public static RubyEncoding getEncoding(String name) {
return lookup.get(name.toLowerCase());
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.jruby.truffle.runtime.RubyContext;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
* Represents the Ruby {@code File} class.
Expand Down Expand Up @@ -76,7 +77,7 @@ public static RubyFile open(RubyContext context, String fileName, String mode) {

if (mode.equals("rb")) {
try {
reader = new InputStreamReader(new FileInputStream(fileName));
reader = new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
Expand All @@ -86,15 +87,15 @@ public static RubyFile open(RubyContext context, String fileName, String mode) {
reader = null;

try {
writer = new OutputStreamWriter(new FileOutputStream(fileName));
writer = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else if (mode.equals("a") || mode.equals("ab")) {
reader = null;

try {
writer = new OutputStreamWriter(new FileOutputStream(fileName, true));
writer = new OutputStreamWriter(new FileOutputStream(fileName, true), StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -100,7 +100,7 @@ public Object matchCommon(ByteList bytes, boolean operator, boolean setNamedCapt
if (setNamedCaptures && regex.numberOfNames() > 0) {
for (Iterator<NameEntry> i = regex.namedBackrefIterator(); i.hasNext();) {
final NameEntry e = i.next();
final String name = new String(e.name, e.nameP, e.nameEnd - e.nameP).intern();
final String name = new String(e.name, e.nameP, e.nameEnd - e.nameP, StandardCharsets.UTF_8).intern();
setFrame(frame, name, getContext().getCoreLibrary().getNilObject());
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ public Object matchCommon(ByteList bytes, boolean operator, boolean setNamedCapt
if (setNamedCaptures && regex.numberOfNames() > 0) {
for (Iterator<NameEntry> i = regex.namedBackrefIterator(); i.hasNext();) {
final NameEntry e = i.next();
final String name = new String(e.name, e.nameP, e.nameEnd - e.nameP).intern();
final String name = new String(e.name, e.nameP, e.nameEnd - e.nameP, StandardCharsets.UTF_8).intern();
int nth = regex.nameToBackrefNumber(e.name, e.nameP, e.nameEnd, region);

final Object value;
Expand Down Expand Up @@ -413,7 +413,7 @@ public void forceEncoding(RubyEncoding encoding) {

public RubyEncoding getEncoding() {
if (encoding == null) {
encoding = RubyEncoding.getEncoding(getContext(), regex.getEncoding());
encoding = RubyEncoding.getEncoding(regex.getEncoding());
}

return encoding;
Expand Down
Expand Up @@ -15,18 +15,31 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class StringFormatter {

@CompilerDirectives.TruffleBoundary
public static String format(RubyContext context, String format, List<Object> values) {
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArray);

final PrintStream printStream;

try {
printStream = new PrintStream(byteArray, false, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

format(context, printStream, format, values);

return byteArray.toString();
try {
return byteArray.toString(StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

@CompilerDirectives.TruffleBoundary
Expand Down
Expand Up @@ -56,6 +56,7 @@
import org.jruby.util.KeyValuePair;
import org.jruby.util.cli.Options;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
Expand Down Expand Up @@ -1027,7 +1028,7 @@ public RubyNode visitDotNode(org.jruby.ast.DotNode node) {
@Override
public RubyNode visitEncodingNode(org.jruby.ast.EncodingNode node) {
SourceSection sourceSection = translate(node.getPosition());
return new ObjectLiteralNode(context, sourceSection, RubyEncoding.getEncoding(context, node.getEncoding()));
return new ObjectLiteralNode(context, sourceSection, RubyEncoding.getEncoding(node.getEncoding()));
}

@Override
Expand Down Expand Up @@ -1583,7 +1584,7 @@ public RubyNode visitMatch2Node(org.jruby.ast.Match2Node node) {
if (regex.numberOfNames() > 0) {
for (Iterator<NameEntry> i = regex.namedBackrefIterator(); i.hasNext(); ) {
final NameEntry e = i.next();
final String name = new String(e.name, e.nameP, e.nameEnd - e.nameP).intern();
final String name = new String(e.name, e.nameP, e.nameEnd - e.nameP, StandardCharsets.UTF_8).intern();

if (environment.hasOwnScopeForAssignments()) {
environment.declareVar(name);
Expand Down Expand Up @@ -2208,7 +2209,7 @@ public RubyNode visitRegexpNode(org.jruby.ast.RegexpNode node) {
} else if (node.getOptions().getKCode().getKCode().equals("UTF8")) {
regexp.forceEncoding((RubyEncoding) context.getCoreLibrary().getEncodingClass().getConstants().get("UTF_8").getValue());
} else {
regexp.forceEncoding(RubyEncoding.getEncoding(context, node.getEncoding()));
regexp.forceEncoding(RubyEncoding.getEncoding(node.getEncoding()));
}

final ObjectLiteralNode literalNode = new ObjectLiteralNode(context, translate(node.getPosition()), regexp);
Expand All @@ -2227,7 +2228,7 @@ public static boolean all7Bit(byte[] bytes) {
}

if (bytes[n] == '\\' && n + 1 < bytes.length && bytes[n + 1] == 'x') {
int b = Integer.parseInt(new String(Arrays.copyOfRange(bytes, n + 2, n + 4)), 16);
int b = Integer.parseInt(new String(Arrays.copyOfRange(bytes, n + 2, n + 4), StandardCharsets.UTF_8), 16);

if (b > 0x7F) {
return false;
Expand Down
Expand Up @@ -37,6 +37,7 @@

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class TranslatorDriver {

Expand Down Expand Up @@ -108,7 +109,7 @@ public RubyRootNode parse(RubyContext context, Source source, Encoding defaultEn
org.jruby.ast.RootNode node;

try {
node = (org.jruby.ast.RootNode) parser.parse(source.getName(), source.getCode().getBytes(), new ManyVarsDynamicScope(staticScope), parserConfiguration);
node = (org.jruby.ast.RootNode) parser.parse(source.getName(), source.getCode().getBytes(StandardCharsets.UTF_8), new ManyVarsDynamicScope(staticScope), parserConfiguration);
} catch (Exception e) {
String message = e.getMessage();

Expand Down Expand Up @@ -203,7 +204,7 @@ private Object getData(RubyContext context) {
}

final org.jruby.RubyFile jrubyFile = (org.jruby.RubyFile) jrubyData;
final RubyFile truffleFile = new RubyFile(context.getCoreLibrary().getFileClass(), new InputStreamReader(jrubyFile.getInStream()), null);
final RubyFile truffleFile = new RubyFile(context.getCoreLibrary().getFileClass(), new InputStreamReader(jrubyFile.getInStream(), StandardCharsets.UTF_8), null);

return truffleFile;
}
Expand Down
2 changes: 1 addition & 1 deletion tool/jt.rb
Expand Up @@ -79,7 +79,7 @@ def findbugs
end

def findbugs_report
sh 'tool/truffle-findbugs.sh --report'
sh 'tool/truffle-findbugs.sh --report' rescue nil
sh 'open truffle-findbugs-report.html' rescue nil
end

Expand Down
4 changes: 4 additions & 0 deletions tool/truffle-findbugs-exclude.xml
Expand Up @@ -3,4 +3,8 @@
<Match>
<Class name="~.*NodeFactory.*" />
</Match>
<Match>
<Class name="org.jruby.truffle.nodes.core.GCNodes$GarbageCollectNode" />
<Bug pattern="DM_GC" />
</Match>
</FindBugsFilter>

0 comments on commit 8c588f4

Please sign in to comment.