Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ji] support converting Ruby File -> Java File; no need to set meta-c…
Browse files Browse the repository at this point in the history
…lass
kares committed Aug 27, 2017
1 parent 538b5e0 commit 0ac10ac
Showing 3 changed files with 21 additions and 9 deletions.
17 changes: 11 additions & 6 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
@@ -237,14 +237,10 @@ public static RubyClass createFileClass(Ruby runtime) {
return fileClass;
}

private static ObjectAllocator FILE_ALLOCATOR = new ObjectAllocator() {
private static final ObjectAllocator FILE_ALLOCATOR = new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
RubyFile instance = new RubyFile(runtime, klass);

instance.setMetaClass(klass);

return instance;
return new RubyFile(runtime, klass);
}
};

@@ -2013,6 +2009,15 @@ private static RubyString checkHome(ThreadContext context) {
return (RubyString) home;
}

@Override
public Object toJava(Class target) {
if (target == java.io.File.class) {
final String path = getPath();
return path == null ? null : new java.io.File(path);
}
return super.toJava(target);
}

private static RubyString doJoin(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
final Ruby runtime = context.runtime;
final String separator = runtime.getClass("File").getConstant("SEPARATOR").toString();
4 changes: 1 addition & 3 deletions core/src/main/java/org/jruby/ext/tempfile/Tempfile.java
Original file line number Diff line number Diff line change
@@ -72,9 +72,7 @@ public class Tempfile extends RubyFile implements Finalizable {
private static ObjectAllocator TEMPFILE_ALLOCATOR = new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
RubyFile instance = new Tempfile(runtime, klass);

return instance;
return new Tempfile(runtime, klass);
}
};

9 changes: 9 additions & 0 deletions spec/java_integration/addons/io_spec.rb
Original file line number Diff line number Diff line change
@@ -99,4 +99,13 @@
str = file.read(10)
expect(str).to eq(String.from_java_bytes(bytes.array))
end

it "is coercible to java.io.Files" do
file = Tempfile.new("io_spec").to_java 'java.io.File'
expect(java.io.File).to be === file
file = File.open(__FILE__).to_java java.io.File
expect(java.io.File).to be === file
expect(file.getPath).to eql __FILE__
end

end

0 comments on commit 0ac10ac

Please sign in to comment.