Skip to content

Commit

Permalink
Merge branch 'master' into sym_enc_enebo
Browse files Browse the repository at this point in the history
headius committed Jun 13, 2017
2 parents 040b3f9 + c8561db commit 831b60e
Showing 8 changed files with 243 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ os:

env:
global:
- JAVA_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Xmn36M -Xmx512M"
- JAVA_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Xmn36M -Xmx512M -Djava.security.egd=file:/dev/./urandom"
- MALLOC_ARENA_MAX=2
matrix:
- PHASE='-Ptest'
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/RubyClass.java
Original file line number Diff line number Diff line change
@@ -197,8 +197,9 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
* constructor (Ruby, RubyClass) on the given class via a static
* __allocate__ method intermediate.
*
* @param clazz The class from which to grab a standard Ruby __allocate__
* method.
* @param clazz The class from which to grab a standard Ruby __allocate__ method.
*
* @note Used with `jrubyc --java` generated (interoperability) class files.
*/
public void setRubyStaticAllocator(final Class<?> clazz) {
try {
Original file line number Diff line number Diff line change
@@ -79,7 +79,9 @@ public class UnmarshalStream extends InputStream {
public UnmarshalStream(Ruby runtime, InputStream in, IRubyObject proc, boolean taint) throws IOException {
assert runtime != null;
assert in != null;
assert proc != null;

// Older native java ext expects proc can be null (spymemcached.jruby at least).
if (proc == null) proc = runtime.getNil();

this.runtime = runtime;
this.cache = new UnmarshalCache(runtime);
6 changes: 1 addition & 5 deletions lib/ruby/stdlib/jruby/compiler.rb
Original file line number Diff line number Diff line change
@@ -56,10 +56,6 @@ def compile_argv(argv)
options[:javac_options] << o
end

#opts.on("-5"," --jdk5", "Generate JDK 5 classes (version 49)") do |x|
# options[:jdk5] = true
#end

opts.on("--java", "Generate Java classes (.java) for a script containing Ruby class definitions") do
options[:java] = true
end
@@ -146,7 +142,7 @@ def compile_files_with_options(filenames, options = default_options)

FileUtils.mkdir_p java_dir

java_src = File.join(java_dir, cls.name + ".java")
java_src = File.join(java_dir, "#{cls.name}.java")
puts "Generating Java class #{cls.name} to #{java_src}" if options[:verbose]

files << java_src
238 changes: 143 additions & 95 deletions lib/ruby/stdlib/jruby/compiler/java_class.rb

Large diffs are not rendered by default.

30 changes: 14 additions & 16 deletions spec/jrubyc/java/constructor_spec.rb
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ def generate(script)
describe "with no initialize method" do
it "generates a default constructor" do
cls = generate("class Foo; end").classes[0]
cls.constructor?.should be false
expect( cls.has_constructor? ).to be false

java = cls.to_s
java.should match EMPTY_INITIALIZE_PATTERN
@@ -28,23 +28,21 @@ def generate(script)
describe "and a constructor java_signature on a another method" do
it "generates a default constructor" do
cls = generate("class Foo; def initialize(a); end; java_signature 'Foo()'; def default_cnstr(); end; end").classes[0]
cls.constructor?.should be true
expect( cls.has_constructor? ).to be true

init = cls.methods[0]
init.should_not be nil
init.name.should == "initialize"
init.constructor?.should == true
init.java_signature.to_s.should == "Object initialize(Object a)"
init.args.length.should == 1
expect( init = cls.methods[0] ).to_not be nil
expect( init.name ).to eql "initialize"
expect( init.constructor? ).to be true
expect( init.java_signature.to_s ).to eql "Object initialize(Object a)"
expect( init.args.length ).to eql 1

java = init.to_s
java.should match OBJECT_INITIALIZE_PATTERN

def_cnstr = cls.methods[1]
def_cnstr.should_not be nil
def_cnstr.constructor?.should == true
def_cnstr.java_signature.to_s.should == "Foo()"
def_cnstr.args.length.should == 0
expect( def_cnstr = cls.methods[1] ).to_not be nil
expect( def_cnstr.constructor? ).to be true
expect( def_cnstr.java_signature.to_s ).to eql "Foo()"
expect( def_cnstr.args.length ).to eql 0

java = def_cnstr.to_s
java.should match EMPTY_INITIALIZE_PATTERN
@@ -54,7 +52,7 @@ def generate(script)
describe "with no arguments" do
it "generates a default constructor" do
cls = generate("class Foo; def initialize; end; end").classes[0]
expect( cls.constructor? ).to be true
expect( cls.has_constructor? ).to be true

expect( init = cls.methods[0] ).to_not be nil
expect( init.name ).to eql 'initialize'
@@ -70,7 +68,7 @@ def generate(script)
describe "with one argument and no java_signature" do
it "generates an (Object) constructor" do
cls = generate("class Foo; def initialize(a); end; end").classes[0]
expect( cls.constructor? ).to be true
expect( cls.has_constructor? ).to be true

init = cls.methods[0]
expect( init.name ).to eql 'initialize'
@@ -86,7 +84,7 @@ def generate(script)
describe "with one argument and a java_signature" do
it "generates a type-appropriate constructor" do
cls = generate("class Foo; java_signature 'Foo(String)'; def initialize(a); end; end").classes[0]
expect( cls.constructor? ).to be true
expect( cls.has_constructor? ).to be true

init = cls.methods[0]
expect( init.name ).to eql 'initialize'
31 changes: 31 additions & 0 deletions spec/jrubyc/java/files/method_visibility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class MethodVisibility

private

def priv_method; false end

def publ_method; true end
public :publ_method

def prot_method; end
protected :prot_method

public

def hello(arg = there)
"Hello #{arg}"
end

def there
@there ||= 'World!'
end
private :there

protected

java_signature 'public int protMethodWithJSign(String)'
def prot_method_with_java_signature(str_arg)
return str_arg.size
end

end
47 changes: 47 additions & 0 deletions spec/jrubyc/java/method_spec.rb
Original file line number Diff line number Diff line change
@@ -411,6 +411,53 @@ def bar_int(a); end
end
end

describe 'with private (Ruby) :method' do

it 'does not generate the given method' do
cls = generate("class Foo; def abar; end; private :abar; def afoo; true; end; end").classes[0]

java_source = cls.to_s

expect( java_source ).to_not include 'Object abar' + '()'
expect( java_source ).to include 'public Object afoo' + '()'
end

end

describe 'with (Ruby) visibility' do

METHOD_VISIBILITY_SOURCE = File.expand_path('files/method_visibility.rb', File.dirname(__FILE__))

it 'does map public and protected methods to Java with same modifiers' do
cls = generate(File.read(METHOD_VISIBILITY_SOURCE)).classes[0]

java_source = cls.to_s

expect( java_source ).to include 'public Object publ_method' + '()'
expect( java_source ).to include 'public Object hello' + '(Object arg)'

expect( java_source ).to include 'protected Object prot_method' + '()'
end

it 'does not generate private methods (by default)' do
cls = generate(File.read(METHOD_VISIBILITY_SOURCE)).classes[0]

java_source = cls.to_s

expect( java_source ).to_not include 'Object there' + '()'
expect( java_source ).to_not include 'Object priv_method' + '()'
end

it 'explicit java_signature with modifier overrides Ruby visibility' do
cls = generate(File.read(METHOD_VISIBILITY_SOURCE)).classes[0]

java_source = cls.to_s

expect( java_source ).to include 'public int protMethodWithJSign' + '(String str_arg)'
end

end

describe "when no class definitions are present in the target script" do
before do
@source = Tempfile.new('jrubyc_method_spec')

0 comments on commit 831b60e

Please sign in to comment.