Issue Details (XML | Word | Printable)

Key: JRUBY-45
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: David Kellum
Votes: 0
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
JRuby

Java class with initialize() method can't be constructed from jruby

Created: 25/Jul/06 08:51 PM   Updated: 22/Dec/07 06:28 AM
Component/s: Java Integration
Affects Version/s: JRuby 0.9.0, JRuby 0.9.1, JRuby 0.9.2, JRuby 0.9.8, JRuby 0.9.9, JRuby 1.0.0RC1, JRuby 1.0.0RC2, JRuby 1.0.0RC3
Fix Version/s: JRuby 1.0.1, JRuby 1.1b1

Time Tracking:
Not Specified

File Attachments: 1. Java Source File TestClassWithInitialize.java (0.1 kB)
2. File testClassWithInitialize.rb (0.2 kB)

Environment: SVN Trunk of jRuby on Linux, Sun JDK 1.5.0.07
Issue Links:
Related
 
dependent
 

Testcase included: yes


 Description  « Hide
Attached test case testClassWithInitialize.rb with attached java class org.jruby.javasupport.test.TestClassWithInitialize fails with the following:

test/testClassWithInitialize.rb:8:in `invoke': invokee not a java object (TypeError)
from ruby/testClassWithInitialize.rb:8:in `initialize'
from ruby/testClassWithInitialize.rb:8:in `new'
from ruby/testClassWithInitialize.rb:8

Name collision with ruby initialize()?



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Thomas E Enebo added a comment - 26/Jul/06 07:38 PM
Yep. It is a collision with the initialize used by the Ruby class we use to create a proxy to the included Java class. We actually knew about this, but for some reason did not have a bug open on it.

Charles Oliver Nutter added a comment - 17/Apr/07 01:29 PM
This may be fixed along with Bill's fixes for JRUBY-778.

Charles Oliver Nutter added a comment - 20/Apr/07 12:22 AM
This is a tricky one. I'm not sure what the right way to fix this is. Probably the simplest way is to provide a special name for Java methods like initialize that can't be overridden. I know that makes it a bit more cumbersome to invoke them, but it would be much more painful internally to try to allow an "initialize" method to come through.

So suggestions?

_initialize?
_initialize_?
__initialize__?
j_initialize?
send(:initialize)? (maybe with a custom impl of send that is aware of such things?)

Charles Oliver Nutter added a comment - 20/Apr/07 12:24 AM
The patch for JRUBY-814 may make this easier to fix, but doesn't currently resolve this issue.

Ola Bini added a comment - 20/Apr/07 01:07 AM
I propose either
"initialize!"

or following the Java convention, and taking the class name.


Charles Oliver Nutter added a comment - 22/Apr/07 10:25 PM
Not going to get fixed in time for 099. Needs more debate and discussion.

Charles Oliver Nutter added a comment - 24/Apr/07 03:32 PM
I'm starting to think we should leave the built-in initialize alone. It's generally been the case that Java stuff entering Ruby is a second-class citizen when it comes to conflicting with critical method names, and initialize certainly falls into that category. So I think providing something like initialize! which is impossible to define in Java code would be a pretty good choice. Plus, it seems like it almost fits what an exclamation point means, since initialize! could reasonably be expected to initialize the target Java object (service, cache, whatever it is).

Bill Dortch added a comment - 24/Apr/07 04:07 PM
I was also thinking about using the exclamation point for overriding protected methods (and fields), should we decide to support that capability. If we want to make them protected methods in Ruby (thus preserving at least some semblance of their "protectedness"), we'll need to name them differently from public methods, as there are many instances in the java.* packages of public and protected methods overloading the same name (see java.awt.Component, for example).

I don't think that precludes using ! for other purposes.


Charles Oliver Nutter added a comment - 05/May/07 07:04 PM
I like initialize! myself, but this isn't going to get done by tomorrow. We really oughta have this for for 1.0, so I'll increase to critical.

Charles Oliver Nutter added a comment - 16/May/07 02:18 PM
I think this may need to get punted for 1.0. Perhaps there's a workaround we can add to this bug? I'm not going to have time to work on it...but if someone else has a patch, it could be accepted today.

Bill Dortch added a comment - 16/May/07 03:01 PM
This issue isn't quite what it was when it was opened. A Java class with an initialize method can be instantiated, and that class can call it's initialize method. We just ignore the method in JRuby, so you can't call it or override it:
public class TestInit {

    public TestInit() {
        initialize();
    }
    public void initialize() {
        System.out.println("initialize called");
    }
}
irb(main):001:0> include Java
=> Object
irb(main):002:0> T = org.jruby.javasupport.TestInit
=> Java::OrgJrubyJavasupport::TestInit
irb(main):003:0> t = T.new
initialize called
=> #<Java::OrgJrubyJavasupport::TestInit:0x1e1ec86 @java_object=org.jruby.javasupport.TestInit@10418
76>
irb(main):004:0> class T2 < T
irb(main):005:1> def initialize(*args)
irb(main):006:2> super
irb(main):007:2> puts "T2 init"
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> t2 = T2.new
initialize called
T2 init
=> #<T2:0x148e798 @java_object=org.jruby.javasupport.TestInit$Proxy0@135605a>
irb(main):011:0> t.class
=> Java::OrgJrubyJavasupport::TestInit
irb(main):012:0>

So I think this is a (comparatively) minor issue right now...


Charles Oliver Nutter added a comment - 16/May/07 04:09 PM
Do we have a good way to do a "java_send" on java objects? That woudl be a sufficient workaround for now.
public class TestInit {

    public TestInit() {
        initialize();
    }
    public void initialize() {
        System.out.println("initialize called");
    }
}
t =TestInit.new
t.java_send :initialize

etc...


Charles Oliver Nutter added a comment - 16/May/07 04:16 PM
Either this has a nice workaround like java_send or it's going to remain "broken" for 1.0...not enough time for a more major addition to Java support.

Bill Dortch added a comment - 16/May/07 04:43 PM
There's not an easy workaround now. I could hack something together, but then we'd have to live with it. I'd rather wait and do this post-1.0 as part of the work to be done to support protected methods/fields (which I thought I was going to get done as part of JRUBY-814, but just ran into too many gotchas).

The post-1.0 work will involve (at least) a reworking of the JavaClass code, which will be a good thing (there's still room for improvement in performance, etc.), and we might want to stop and look at the whole Java support infrastructure – there's a lot I'd do differently in a perfect world...

Meanwhile, as I noted, I don't think this issue is quite as serious now as when the problem was initially reported – the submitted test case now works.

-Bill


Charles Oliver Nutter added a comment - 30/Sep/07 11:31 AM
Since the original case works, I'm marking this as fixed. There's a large overhaul of java integration coming, so the remaining discussions should be addressed as part of that.