Coder Social home page Coder Social 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

selfish icon selfish

Class-free, pure prototypal inheritance

shoes4 icon shoes4

Shoes 4 : the next version of Shoes

spec icon spec

The Ruby Spec Suite aka ruby/spec

strategic icon strategic

Strategic - Painless Strategy Pattern in Ruby and Rails

super_module icon super_module

SuperModule allows defining class methods and method invocations the same way a super class does without using def included(base). This also succeeds ActiveSupport::Concern by offering lighter syntax

thegist icon thegist

Gist application built with Glimmer/SWT

thoughtbarf icon thoughtbarf

Auto-moderated online social community for safe free speech, blogging, and discussion

tk icon tk

Tk interface module using tcltklib

to_collection icon to_collection

Treat an array of objects and a singular object uniformly as a collection of objects. Especially useful in processing REST Web Service API JSON responses in a functional approach.

ultra_light_wizard icon ultra_light_wizard

No time to manage a wizard state machine, session variables, or complicated controllers? Use ultra light wizard!! A RESTful session-less validation-friendly simple multi-step form approach in Rails.

wheeze icon wheeze

A free and open-source paint program.

wisper icon wisper

Wisper is a Ruby library for decoupling and managing the dependencies of your domain models.

yasl icon yasl

Yet Another Serialization Library - A pure Ruby auto-serialization library that works across different Ruby implementations like Opal and JRuby as an automatic alternative to YAML/Marshal. Unlike Marshal, it does not raise errors for unserializable objects, thus provides a highly productive friction-free auto-serialization experience.

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.