$ jruby ns.rb
ns.rb:You can simple create two java files and compile them using javac.
require 'java'
require 'pp'
class Rule
end
module MyCompany
module JavaModel
include_package 'com.model'
end
end
pp MyCompany::JavaModel.const_get('Foo')
pp MyCompany::JavaModel.const_get('Rule')
com/model/Foo.java:
package com.model;
public class Foo {
}
com/model/Rule.java:
package com.model;
public class Rule {
}
$ javac com/model/Foo.javaHere is the output:
$ javac com/model/Rule.java
Java::ComModel::FooWhy the second line is not Java::ComModel::Rule? include_package just overrides const_missing instead of defining constants in module MyCompany::JavaModel. When you call MyCompany::JavaModel.const_get("Rule"), the constant "Rule" is defined, const_missing is skipped.
Rule
Using java_import will work correctly although you have to list each class.
Note: if you use java_import outside the module, you will overwrite the constant Rule.
module MyCompany
module JavaModel
java_import 'com.model.Foo'
java_import 'com.model.Rule'
end
end
No comments:
Post a Comment