java_override 0.1.3-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ *.swp
2
+ .rvmrc
3
+ .vimrc
4
+ Gemfile.lock
5
+ pkg/
6
+ vendor/cache/*.gem
7
+ *.gem
8
+ *.rbc
9
+ .bundle
10
+ .config
11
+ coverage
12
+ InstalledFiles
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+
21
+ # YARD artifacts
22
+ .yardoc
23
+ _yardoc
24
+ doc/
25
+ *.class
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2012-07-29
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Szymon Wrozynski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,91 @@
1
+ Java::Override JRuby Module
2
+ ===========================
3
+
4
+ * [Homepage](https://rubygems.org/gems/java_override)
5
+ * [Documentation](http://rubydoc.info/gems/java_override/frames)
6
+
7
+ Description
8
+ -----------
9
+
10
+ Java::Override is a JRuby module enabling overriding native Java methods with
11
+ Ruby naming conventions. JRuby allows you to call Java methods (of Java classes/interfaces)
12
+ with Ruby conventions. You can use so called *snake_case*, Java Beans accessors are
13
+ *translated* to Ruby ones (e.g. <code>getFooBar</code> can be accessed via <code>foo\_bar</code>,
14
+ <code>isFooBar</code> via <code>foo\_bar?</code>, or <code>setFooBar</code> via <code>foo\_bar=</code>).
15
+ Those methods are added to Java classes as JRuby aliases. However, if you want to override
16
+ a Java method you cannot override such alias because Java will not see it. Java will do polymorphic
17
+ call of its own, native methods. Therefore you have to override native Java methods and it
18
+ doesn't look pretty in JRuby code.
19
+
20
+ The Java::Override module abolishes this inconvenience. Once included in the subclass it
21
+ creates aliases to native java methods and supports inheritance and polymorphism that way.
22
+ Moreover, it adds proper aliases for Java interfaces included as modules.
23
+
24
+ Examples
25
+ --------
26
+
27
+ Look at the following implementation of javax.swing.table.AbstractTableModel class.
28
+
29
+ require 'java/override'
30
+
31
+ java_import javax::swing::table::AbstractTableModel
32
+
33
+ class MyTableModel < AbstractTableModel
34
+ include Java::Override
35
+
36
+ def initialize
37
+ super
38
+ @column_names = ['First Name', 'Last Name']
39
+ @data = [
40
+ ['John', 'Doe'],
41
+ ['Jack', 'FooBar']
42
+ ]
43
+ end
44
+
45
+ def column_count
46
+ @column_names.size
47
+ end
48
+
49
+ def row_count
50
+ @data.size
51
+ end
52
+
53
+ def column_name(col)
54
+ @column_names[col]
55
+ end
56
+
57
+ def get_value_at(row, col)
58
+ @data[row][col]
59
+ end
60
+
61
+ def column_class(col)
62
+ get_value_at(0, c).java_class
63
+ end
64
+
65
+ def cell_editable?(row, col)
66
+ col.zero?
67
+ end
68
+
69
+ def set_value_at(value, row, col)
70
+ @data[row][col] = value
71
+ fire_table_cell_updated(row, col)
72
+ end
73
+ end
74
+
75
+ Requirements
76
+ ------------
77
+
78
+ Java::Override requires JRuby in 1.9 mode and a decent JVM. It has been tested
79
+ under JRuby 1.6.2 and Oracle Java 7 JDK.
80
+
81
+ Install
82
+ -------
83
+
84
+ $ gem install java_override
85
+
86
+ Copyright
87
+ ---------
88
+
89
+ Copyright (c) 2012 Szymon Wrozynski
90
+
91
+ See LICENSE.txt for details.
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler'
7
+ rescue LoadError => e
8
+ warn e.message
9
+ warn "Run `gem install bundler` to install Bundler."
10
+ exit -1
11
+ end
12
+
13
+ begin
14
+ Bundler.setup(:development)
15
+ rescue Bundler::BundlerError => e
16
+ warn e.message
17
+ warn "Run `bundle install` to install missing gems."
18
+ exit e.status_code
19
+ end
20
+
21
+ require 'rake'
22
+
23
+ require 'rubygems/tasks'
24
+ Gem::Tasks.new
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new do |test|
28
+ test.libs << 'test'
29
+ test.pattern = 'test/**/test_*.rb'
30
+ test.verbose = true
31
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require_relative 'lib/java/override/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "java_override"
7
+ gem.version = Java::Override::VERSION
8
+ gem.summary = %q{Support for Java class and interface inheritance in JRuby}
9
+ gem.description = %q{Support for Java class and interface inheritance in JRuby. Enable overriding native Java methods with JRuby ones follwing Ruby naming conventions.}
10
+ gem.license = "MIT"
11
+ gem.authors = ["Szymon Wrozynski"]
12
+ gem.email = "szymon@wrozynski.com"
13
+ gem.homepage = "https://rubygems.org/gems/java_override"
14
+
15
+ gem.required_ruby_version = '>= 1.9.2'
16
+ gem.platform = 'java'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ['lib']
22
+
23
+ gem.add_development_dependency 'bundler', '~> 1.0'
24
+ gem.add_development_dependency 'rake', '~> 0.8'
25
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
26
+ gem.add_development_dependency 'shoulda'
27
+ end
@@ -0,0 +1,56 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Java::Override JRuby Module
4
+ # Copyright (c) 2012 Szymon Wrozynski
5
+ # Licensed under the MIT License conditions.
6
+ #
7
+ # Support for Java class inheritance in JRuby.
8
+ # Enable overriding plain Java methods with Ruby naming conventions.
9
+
10
+ require 'java'
11
+ require 'set'
12
+ require 'java/override/version'
13
+
14
+ module Java
15
+ module Override
16
+
17
+ private
18
+
19
+ def method_added(m)
20
+ return if @_java_override_internal_call
21
+ @_java_override_internal_call = true
22
+
23
+ unless private_instance_methods(true).include?(m)
24
+ super_methods = Set.new(superclass.instance_methods.map(&:to_s))
25
+
26
+ included_modules.each do |i|
27
+ if i.respond_to?(:java_class)
28
+ super_methods.merge(i.java_class.java_instance_methods.map(&:name).uniq)
29
+ end
30
+ end
31
+
32
+ if m.to_s.end_with?('?')
33
+ prefix = 'is'
34
+ elsif m.to_s.end_with?('=')
35
+ prefix = 'set'
36
+ else
37
+ prefix = 'get'
38
+ end
39
+
40
+ base = m.to_s.gsub(/[^a-z0-9]/i, '')
41
+
42
+ find_java_m = ->(n) { super_methods.find { |m| m.to_s.casecmp(n).zero? } }
43
+ java_m = find_java_m.call("#{prefix}#{base}") || find_java_m.call(base)
44
+
45
+ alias_method(java_m, m) if java_m && java_m != m.to_s
46
+ end
47
+
48
+ remove_instance_variable(:@_java_override_internal_call)
49
+ end
50
+
51
+ def self.included(klass)
52
+ klass.extend(self)
53
+ klass.send(:undef_method, :method_added)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ module Java
2
+ module Override
3
+ # Java::Override version
4
+ VERSION = "0.1.3"
5
+ end
6
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ require 'bundler'
5
+ rescue LoadError => e
6
+ warn e.message
7
+ warn "Run `gem install bundler` to install Bundler."
8
+ exit e.status_code
9
+ end
10
+
11
+ begin
12
+ Bundler.setup(:default, :development, :test)
13
+ rescue Bundler::BundlerError => e
14
+ warn e.message
15
+ warn "Run `bundle install` to install missing gems."
16
+ exit e.status_code
17
+ end
18
+
19
+ require 'test/unit'
20
+
21
+ class Test::Unit::TestCase
22
+ end
@@ -0,0 +1,4 @@
1
+ public interface TestInterface {
2
+ String method();
3
+ String myLongMethodNameWithABBRV();
4
+ }
@@ -0,0 +1,39 @@
1
+ public class TestSuperclass {
2
+ protected String foo;
3
+
4
+ public String method() {
5
+ return "TestSuperclass#method";
6
+ }
7
+
8
+ public String myLongMethodName() {
9
+ return "TestSuperclass#myLongMethodName";
10
+ }
11
+
12
+ public String myLongMethodNameWithABBRV() {
13
+ return "TestSuperclass#myLongMethodNameWithABBRV";
14
+ }
15
+
16
+ public boolean isFooBar() {
17
+ return true;
18
+ }
19
+
20
+ public String getFoo() {
21
+ return this.foo;
22
+ }
23
+
24
+ public void setFoo(String foo) {
25
+ this.foo = foo;
26
+ }
27
+
28
+ public String testInterface(TestInterface ti) {
29
+ return ti.method() + ti.myLongMethodNameWithABBRV();
30
+ }
31
+
32
+ protected String protectedMethod() {
33
+ return "TestSuperclass#protectedMethod";
34
+ }
35
+
36
+ public String testProtectedMethod() {
37
+ return protectedMethod();
38
+ }
39
+ }
@@ -0,0 +1,135 @@
1
+ require 'helper'
2
+ require 'java'
3
+ require 'java/override'
4
+ require 'shoulda'
5
+ require 'fileutils'
6
+
7
+ `javac -d . #{File.join('test', 'java', 'TestInterface.java')} #{File.join('test', 'java', 'TestSuperclass.java')}`
8
+
9
+ java_import "TestInterface"
10
+ java_import "TestSuperclass"
11
+
12
+ FileUtils.rm 'TestInterface.class'
13
+ FileUtils.rm 'TestSuperclass.class'
14
+
15
+ class TestJavaOverride < Test::Unit::TestCase
16
+ def setup
17
+ @superclass = TestSuperclass.new
18
+ @my_class = MyClass.new
19
+ end
20
+
21
+ should "have version" do
22
+ version = Java::Override.const_get('VERSION')
23
+
24
+ assert !version.empty?, 'should have a VERSION constant'
25
+ end
26
+
27
+ should "override simple methods" do
28
+ assert_equal "MyClass: TestSuperclass#myLongMethodName", @my_class.my_long_method_name
29
+ assert_equal "MyClass: TestSuperclass#myLongMethodName", @my_class.myLongMethodName
30
+ end
31
+
32
+ should "override javabean accessors" do
33
+ @my_class.foo = 'FOO'
34
+
35
+ assert_equal "MyClassGetter: MyClassSetter: FOO", @my_class.foo
36
+ assert_equal "MyClassGetter: MyClassSetter: FOO", @my_class.getFoo
37
+
38
+ assert TestSuperclass.new.foo_bar?
39
+ assert TestSuperclass.new.isFooBar
40
+
41
+ refute @my_class.foo_bar?
42
+ refute @my_class.isFooBar
43
+ end
44
+
45
+ should "not add aliases for private methods" do
46
+ assert MyClass.private_instance_methods(true).include?(:my_private_method)
47
+ refute MyClass.private_instance_methods(true).include?(:myPrivateMethod)
48
+ refute MyClass.instance_methods(false).include?(:myPrivateMethod)
49
+ end
50
+
51
+ should "add method_added only to the singleton class even if included" do
52
+ refute_respond_to @my_class, :method_added
53
+ refute MyClass.instance_methods.include?(:method_added)
54
+ end
55
+
56
+ should "add method_added as a private singleton method" do
57
+ assert MyClass.private_methods.include?(:method_added)
58
+ assert MyClass.class.private_instance_methods(true).include?(:method_added)
59
+ end
60
+
61
+ should "handle names with abbreviations written in upper case" do
62
+ assert_equal "MyClass: TestSuperclass#myLongMethodNameWithABBRV", @my_class.my_long_method_name_with_abbrv
63
+ assert_equal "MyClass: TestSuperclass#myLongMethodNameWithABBRV", @my_class.myLongMethodNameWithABBRV
64
+ end
65
+
66
+ should "handle plain method names" do
67
+ assert_equal "MyClass: TestSuperclass#method", @my_class.method
68
+ end
69
+
70
+ should "handle protected methods" do
71
+ assert_equal "MyClass: TestSuperclass#protectedMethod", @my_class.test_protected_method
72
+ assert_equal "MyClass: TestSuperclass#protectedMethod", @my_class.testProtectedMethod
73
+ end
74
+
75
+ should "handle interface methods" do
76
+ assert_equal(
77
+ "MyInterface#methodMyInterface#my_long_method_name_with_abbrv",
78
+ @my_class.testInterface(MyInterfaceImpl.new)
79
+ )
80
+ end
81
+ end
82
+
83
+
84
+ class MyClass < TestSuperclass
85
+ include Java::Override
86
+
87
+ def method
88
+ "MyClass: #{super}"
89
+ end
90
+
91
+ def my_long_method_name
92
+ "MyClass: #{super}"
93
+ end
94
+
95
+ def my_long_method_name_with_abbrv
96
+ "MyClass: #{super}"
97
+ end
98
+
99
+ def foo_bar?
100
+ !super
101
+ end
102
+
103
+ def foo
104
+ "MyClassGetter: #{super}"
105
+ end
106
+
107
+ def foo=(foo)
108
+ super("MyClassSetter: #{foo}")
109
+ end
110
+
111
+ protected
112
+
113
+ def protected_method
114
+ "MyClass: #{super}"
115
+ end
116
+
117
+ private
118
+
119
+ def my_private_method
120
+
121
+ end
122
+ end
123
+
124
+ class MyInterfaceImpl
125
+ include Java::Override
126
+ include TestInterface
127
+
128
+ def method
129
+ "MyInterface#method"
130
+ end
131
+
132
+ def my_long_method_name_with_abbrv
133
+ "MyInterface#my_long_method_name_with_abbrv"
134
+ end
135
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: java_override
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.3
6
+ platform: java
7
+ authors:
8
+ - Szymon Wrozynski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ none: false
28
+ prerelease: false
29
+ type: :development
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: '0.8'
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: '0.8'
43
+ none: false
44
+ prerelease: false
45
+ type: :development
46
+ - !ruby/object:Gem::Dependency
47
+ name: rubygems-tasks
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: '0.2'
53
+ none: false
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ version: '0.2'
59
+ none: false
60
+ prerelease: false
61
+ type: :development
62
+ - !ruby/object:Gem::Dependency
63
+ name: shoulda
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ none: false
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ none: false
76
+ prerelease: false
77
+ type: :development
78
+ description: Support for Java class and interface inheritance in JRuby. Enable overriding native Java methods with JRuby ones follwing Ruby naming conventions.
79
+ email: szymon@wrozynski.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - .gitignore
85
+ - ChangeLog.md
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - java_override.gemspec
91
+ - lib/java/override.rb
92
+ - lib/java/override/version.rb
93
+ - test/helper.rb
94
+ - test/java/TestInterface.java
95
+ - test/java/TestSuperclass.java
96
+ - test/test_java_override.rb
97
+ homepage: https://rubygems.org/gems/java_override
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: 1.9.2
109
+ none: false
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ none: false
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Support for Java class and interface inheritance in JRuby
122
+ test_files:
123
+ - test/helper.rb
124
+ - test/java/TestInterface.java
125
+ - test/java/TestSuperclass.java
126
+ - test/test_java_override.rb
127
+ ...