java 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 508bb1495b0b0285c32809ac72a966741f1ade25
4
+ data.tar.gz: a94af1d6ddcac6818daacbd08805d70ed32b3294
5
+ SHA512:
6
+ metadata.gz: e6ed25385141861cf75b0b6d99e21bd08decd76fbc1c5f424c31d723a90e686a6d7783017e9cab66620d69f8b83f11130b3ab7560d5cfccbf910b3359f63b73c
7
+ data.tar.gz: b25e6f74f44bb780ae7de2a63052accf653b2adea0611f12b2f9d8f890aa7531b9d4f40de63c6c449fd240d9728901767b2a39ee594c634f67d55b44ba296441
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in java.gemspec
4
+ gemspec
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ java (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.2.3)
10
+ rake (10.1.1)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.3)
17
+ java!
18
+ minitest
19
+ rake
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2014, Vancouver Ruby Meetup Group
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the {organization} nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Godfrey Chan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ Java™
2
+ =====
3
+
4
+ The caffeine boost you need for your late-night coding sprints.
5
+
6
+ Install
7
+ -------
8
+
9
+ `gem install java` or add `gem 'java'` to your Gemfile.
10
+
11
+ Usage
12
+ -----
13
+
14
+ Currently, the following keywords are supported: `new`, `void`, `byte`, `short`,
15
+ `int`, `long`, `float`, `double`, `bool`, `char`.
16
+
17
+ ```ruby
18
+ require 'java'
19
+
20
+ class MyClass
21
+ public def initialize(name)
22
+ @name = name
23
+ end
24
+
25
+ public void def main()
26
+ puts "This is the main method from #{@name}"
27
+ return nil
28
+ end
29
+
30
+ public int def returns_int()
31
+ 1
32
+ end
33
+
34
+ public int def not_int()
35
+ 'not int'
36
+ end
37
+
38
+ public byte def returns_byte()
39
+ 1
40
+ end
41
+
42
+ public byte def not_byte()
43
+ 128
44
+ end
45
+
46
+ private bool def this_is_private()
47
+ true
48
+ end
49
+ end
50
+
51
+ # >> obj = new MyClass("my class")
52
+ # => #<MyClass:0x000001018333d8 @name="my class">
53
+ # >> obj.main()
54
+ # This is the main method from my class
55
+ # => nil
56
+ # >> obj.returns_int()
57
+ # => 1
58
+ # >> obj.not_int()
59
+ # TypeError: Expected not_int to return int but got "not int" instead
60
+ # >> obj.returns_byte()
61
+ # => 1
62
+ # >> obj.not_byte()
63
+ # TypeError: Expected not_byte to return byte but got 128 instead
64
+ ```
65
+
66
+ Production Ready?
67
+ -----------------
68
+
69
+ It has [tests](https://github.com/vanruby/java/tree/master/test), if that's what
70
+ you are asking.
71
+
72
+ Future Work
73
+ -----------
74
+
75
+ - Bug: typed `private` and `protected` methods doesn't work
76
+ - Bug: `Kernel` defines `Array`, `Complex`, `Float`, `Hash`, `Integer`,
77
+ `Rational` and `String` which breaks `new String()` etc
78
+ - Support more keywords: `static`, `final`, etc
79
+
80
+
81
+ Credits
82
+ -------
83
+
84
+ [@tenderlove](https://github.com/tenderlove) and [@jeremy](https://github.com/jeremy)
85
+ first brought this to my attention. [@amatsuda](https://github.com/amatsuda)
86
+ also has a [similar gist](https://gist.github.com/amatsuda/6237320).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ desc 'Run tests'
5
+ test_task = Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'java/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "java"
8
+ spec.version = Java::VERSION
9
+ spec.authors = ["Godfrey Chan"]
10
+ spec.email = ["godfreykfc@gmail.com"]
11
+ spec.summary = %q{The caffeine boost you need for your late-night coding sprints.}
12
+ spec.homepage = "https://github.com/vanruby/java"
13
+ spec.license = "BSD"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ end
@@ -0,0 +1,87 @@
1
+ require "java/version"
2
+
3
+ module Java
4
+ BYTE = -1<<7...1<<7
5
+ SHORT = -1<<15...1<<15
6
+ INT = -1<<31...1<<31
7
+ LONG = -1<<63...1<<63
8
+
9
+ def self.assert_return_type(meth, type, type_klass = NilClass, rtn, &condition)
10
+ if (type == :void || rtn != nil) && (type_klass === rtn) && (!condition || condition.(rtn))
11
+ rtn
12
+ else
13
+ raise TypeError, "Expected #{meth} to return #{type} but got #{rtn.inspect} instead"
14
+ end
15
+ end
16
+ end
17
+
18
+ module Kernel
19
+ def new(klass)
20
+ if Array === klass
21
+ klass[0].send(:new, *klass[1], &klass[2])
22
+ else
23
+ klass
24
+ end
25
+ end
26
+
27
+ def method_missing(meth, *args, &block)
28
+ [Object.const_get(meth), args, block]
29
+ rescue NameError
30
+ super
31
+ end
32
+ end
33
+
34
+ class Module
35
+ private
36
+ def __java__
37
+ prepend (@__java__ = Module.new) unless @__java__
38
+ @__java__
39
+ end
40
+
41
+ def void(meth)
42
+ define_typed_method(meth, :void, NilClass)
43
+ end
44
+
45
+ def byte(meth)
46
+ define_typed_method(meth, :byte, Integer) { |rtn| ::Java::BYTE === rtn }
47
+ end
48
+
49
+ def short(meth)
50
+ define_typed_method(meth, :short, Integer) { |rtn| ::Java::SHORT === rtn }
51
+ end
52
+
53
+ def int(meth)
54
+ define_typed_method(meth, :int, Integer) { |rtn| ::Java::INT === rtn }
55
+ end
56
+
57
+ def long(meth)
58
+ define_typed_method(meth, :long, Integer) { |rtn| ::Java::LONG === rtn }
59
+ end
60
+
61
+ def float(meth)
62
+ define_typed_method(meth, :float, Float)
63
+ end
64
+
65
+ def double(meth)
66
+ define_typed_method(meth, :double, Float)
67
+ end
68
+
69
+ def bool(meth)
70
+ define_typed_method(meth, :bool, Boolean)
71
+ end
72
+
73
+ def char(meth)
74
+ define_typed_method(meth, :char, String) { |rtn| rtn.length == 1 }
75
+ end
76
+
77
+ def define_typed_method(meth, type, type_klass, &condition)
78
+ __java__.send(:define_method, meth) do |*args, &block|
79
+ ::Java.assert_return_type(meth, type, type_klass, super(*args, &block), &condition)
80
+ end
81
+ end
82
+ end
83
+
84
+ module Boolean; end
85
+
86
+ TrueClass.send(:include, Boolean)
87
+ FalseClass.send(:include, Boolean)
@@ -0,0 +1,3 @@
1
+ module Java
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,103 @@
1
+ require 'test_helper'
2
+
3
+ class JavaTest < Minitest::Test
4
+ def test_new
5
+ time = new Time()
6
+ assert_instance_of Time, time
7
+
8
+ time = new Time(2014, 3, 1)
9
+ assert_equal Time.new(2014, 3, 1), time
10
+ end
11
+
12
+ def test_void
13
+ assert_correct_type :void, nil
14
+ assert_wrong_type :void, "not void"
15
+ end
16
+
17
+ def test_byte
18
+ assert_correct_type :byte, 127
19
+ assert_correct_type :byte, -128
20
+ assert_wrong_type :byte, 128
21
+ assert_wrong_type :byte, -129
22
+ assert_wrong_type :byte, "not byte"
23
+ assert_wrong_type :byte, nil
24
+ end
25
+
26
+ def test_short
27
+ assert_correct_type :short, 32767
28
+ assert_correct_type :short, -32768
29
+ assert_wrong_type :short, 32768
30
+ assert_wrong_type :short, -32769
31
+ assert_wrong_type :short, "not short"
32
+ assert_wrong_type :short, nil
33
+ end
34
+
35
+ def test_int
36
+ assert_correct_type :int, 2147483647
37
+ assert_correct_type :int, -2147483648
38
+ assert_wrong_type :int, 2147483648
39
+ assert_wrong_type :int, -2147483649
40
+ assert_wrong_type :int, "not int"
41
+ assert_wrong_type :int, nil
42
+ end
43
+
44
+ def test_long
45
+ assert_correct_type :long, 9223372036854775807
46
+ assert_correct_type :long, -9223372036854775808
47
+ assert_wrong_type :long, 9223372036854775808
48
+ assert_wrong_type :long, -9223372036854775809
49
+ assert_wrong_type :long, "not long"
50
+ assert_wrong_type :long, nil
51
+ end
52
+
53
+ def test_float
54
+ assert_correct_type :float, 1.0
55
+ assert_correct_type :float, Math::PI, "Math::PI"
56
+ assert_wrong_type :float, 1
57
+ assert_wrong_type :float, "not float"
58
+ assert_wrong_type :float, nil
59
+ end
60
+
61
+ def test_double
62
+ assert_correct_type :double, 1.0
63
+ assert_correct_type :double, Math::PI, "Math::PI"
64
+ assert_wrong_type :double, 1
65
+ assert_wrong_type :double, "not double"
66
+ assert_wrong_type :double, nil
67
+ end
68
+
69
+ def test_bool
70
+ assert_correct_type :bool, true
71
+ assert_correct_type :bool, false
72
+ assert_wrong_type :bool, "not bool"
73
+ assert_wrong_type :bool, nil
74
+ end
75
+
76
+ def test_char
77
+ assert_correct_type :char, 'a'
78
+ assert_correct_type :char, '☃'
79
+ assert_correct_type :char, '☃', '"\u2603"'
80
+ assert_wrong_type :char, 1
81
+ assert_wrong_type :char, "not char"
82
+ assert_wrong_type :char, nil
83
+ end
84
+
85
+ private
86
+ def define_test_method(type, val, raw_val = nil)
87
+ klass = Class.new.class_eval <<-RUBY_CODE
88
+ public #{type} def call
89
+ #{raw_val ? raw_val : val.inspect}
90
+ end
91
+ RUBY_CODE
92
+
93
+ klass.new
94
+ end
95
+
96
+ def assert_correct_type(type, val, raw_val = nil)
97
+ assert_equal val, define_test_method(type, val, raw_val).call
98
+ end
99
+
100
+ def assert_wrong_type(type, val, raw_val = nil)
101
+ assert_raises(TypeError) { define_test_method(type, val, raw_val).call }
102
+ end
103
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'java'
4
+
5
+ # Ensure backward compatibility with Minitest 4
6
+ Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: java
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Godfrey Chan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - godfreykfc@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - java.gemspec
70
+ - lib/java.rb
71
+ - lib/java/version.rb
72
+ - test/java_test.rb
73
+ - test/test_helper.rb
74
+ homepage: https://github.com/vanruby/java
75
+ licenses:
76
+ - BSD
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.0
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: The caffeine boost you need for your late-night coding sprints.
98
+ test_files:
99
+ - test/java_test.rb
100
+ - test/test_helper.rb