GithubHelp home page GithubHelp logo

nested_inherited_jruby_include_package

JRuby's include_package (mentioned in https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby) includes Java packages in JRuby on const_missing. However, it does not trickle down to subclasses/submodules nor nested-classes/nested-modules.

nested_inherited_jruby_include_package patches up JRuby to add that capability.

Example (nesting):

require 'nested_inherited_jruby_include_package'

class OuterClass
  include_package java.util
  class InnerClass
    def initialize
      p Arrays.asList(1, 2, 3)
    end
  end
end

OuterClass::InnerClass.new
# prints #<Java::JavaUtil::Arrays::ArrayList:0x233fe9b6>

Example (inheritance):

require 'nested_inherited_jruby_include_package'

class SuperClass
  include_package java.util
end

class SubClass < SuperClass
  def initialize
    p Arrays.asList(1, 2, 3)
  end
end

SubClass.new
# prints #<Java::JavaUtil::Arrays::ArrayList:0x7ce3cb8e>

Example (java inheritance):

module GUI
  include_package 'javax.swing'
  include_package 'java.awt.image' # BufferedImage
  # bellow won't work without accessing BufferedImage before :
  class ShowImage < JFrame
    p BufferedImage.new(10, 20, BufferedImage::TYPE_BYTE_BINARY)
  end
end
# prints #<Java::JavaAwtImage::BufferedImage:0x2f112965>

More examples can be found in:

spec/lib/core/src/main/ruby/jruby/java/core_ext/module_spec.rb

Setup

Add the following to Gemfile:

gem 'nested_inherited_jruby_include_package', '~> 0.3.0'

And, then run:

jruby -S bundle install

If you are not using Bundler, run:

jruby -S gem install nested_inherited_jruby_include_package -v 0.3.0

Then add this line to your code:

require 'nested_inherited_jruby_include_package'

Caveats

This gem relies on Module#const_missing after it aliases it to Module#const_missing_without_nested_inherited_jruby_include_package

As such, it works optimally if your project and loaded gems do not override Module#const_missing

Implementation Note

To avoid method and constant pollution in Module, the implementation intentionally trades off code clarity for lack of pollution by not relying on methods, yet local lambdas, in modularizing logic.

Issue Reporting

This is an early alpha. It has only been used in a couple of projects. As such, there are no guarantees for its functionality. Please report any issues you might discover when using on your own projects.

Change Log

CHANGELOG.md

License

The MIT License

LICENSE.txt

Copyright

Copyright (c) 2020 Andy Maleh

Andy Maleh's Projects

Andy Maleh doesnโ€™t have any public repositories yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.