Skip to content

Commit

Permalink
Implement Java.add_to_classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Jul 21, 2022
1 parent 52ceb57 commit deb052a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
New features:

* Foreign strings now have all methods of Ruby `String`. They are treated as `#frozen?` UTF-8 Ruby Strings.
* Add `Java.add_to_classpath` method to add jar paths at runtime (#2693, @bjfish).

Bug fixes:

Expand Down
1 change: 1 addition & 0 deletions spec/tags/truffle/interop/add_to_classpath_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
slow:Java.add_to_classpath loads a jar file
29 changes: 29 additions & 0 deletions spec/truffle/interop/add_to_classpath_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2022, 2022 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 2.0, or
# GNU General Public License version 2, or
# GNU Lesser General Public License version 2.1.

require_relative '../../ruby/spec_helper'

guard -> { !TruffleRuby.native? } do
describe "Java.add_to_classpath" do
before :all do
jar_dir = File.expand_path(File.dirname(__FILE__)) + '/fixtures/examplejar'
bin_dir = TruffleRuby.graalvm_home + '/bin'
Dir.chdir(jar_dir) do
system("#{bin_dir}/javac org/truffleruby/examplejar/Example.java")
system("#{bin_dir}/jar cf example.jar org/truffleruby/examplejar/Example.class")
end
@jar_file = jar_dir + '/example.jar'
end

it "loads a jar file" do
Java.add_to_classpath(@jar_file).should == true
example_object = Java.type('org.truffleruby.examplejar.Example').new
example_object.hello("Spec").should == "Hello Spec"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.truffleruby.examplejar;

public class Example {

public String hello(String name){
return "Hello " + name;
}

}
22 changes: 22 additions & 0 deletions src/main/java/org/truffleruby/interop/InteropNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.truffleruby.language.control.RaiseException;
import org.truffleruby.language.dispatch.DispatchNode;
import org.truffleruby.language.library.RubyStringLibrary;
import org.truffleruby.language.loader.FileLoader;
import org.truffleruby.language.objects.LogicalClassNode;
import org.truffleruby.shared.TruffleRuby;

Expand Down Expand Up @@ -1767,6 +1768,27 @@ private Object javaType(String name) {
return env.lookupHostSymbol(name);
}

}

@Primitive(name = "java_add_to_classpath")
public abstract static class JavaAddToClasspathNode extends PrimitiveArrayArgumentsNode {

@TruffleBoundary
@Specialization(guards = "strings.isRubyString(path)")
protected boolean javaAddToClasspath(Object path,
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings) {
TruffleLanguage.Env env = getContext().getEnv();
try {
TruffleFile file = FileLoader.getSafeTruffleFile(getLanguage(), getContext(),
strings.getJavaString(path));
env.addToHostClassPath(file);
return true;
} catch (SecurityException e) {
throw new RaiseException(getContext(),
coreExceptions().securityError("unable to add to classpath", this), e);
}
}

}
// endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Pair<Source, Rope> loadFile(String path) throws IOException {
return Pair.create(source, sourceRope);
}

static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context, String path) {
public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context, String path) {
final Env env = context.getEnv();
final TruffleFile file;
try {
Expand Down
4 changes: 4 additions & 0 deletions src/main/ruby/truffleruby/core/truffle/polyglot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,10 @@ class ForeignException < Exception # rubocop:disable Lint/InheritException
end

module Java
def self.add_to_classpath(path)
Primitive.java_add_to_classpath(path)
end

def self.type(name)
Truffle::Interop.java_type(name)
end
Expand Down

0 comments on commit deb052a

Please sign in to comment.