java_head 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +10 -0
- data/java_head.gemspec +25 -0
- data/lib/java_head/version.rb +3 -0
- data/lib/java_head.rb +377 -0
- data/test/src/com/example/projects/broken/Broken.java +5 -0
- data/test/src/com/example/projects/safe/Safe.java +19 -0
- data/test/test_class.rb +40 -0
- data/test/test_package.rb +33 -0
- data/test/test_regex.rb +45 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ec3c3534f665067559a3fdecdcf3d8433c55da9
|
4
|
+
data.tar.gz: e82c020ce91bdbb202f1be04bcd0dd28f5c4db79
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6203d12e340b405add5f0c93cfa45f95b243aa46433139d9b3c4fd491b44ccb0d9d64a060c4833ed67c6aefaef95fd791cb4e7bef694c380833752671dd1a15b
|
7
|
+
data.tar.gz: 6f287491d1ef91290e85118558b47cbf9bc67e3407c393d85e47c269d16e3fa4749f2d07d91251311143f0851023687e7930401e628ce65dbc12c53c554a91d6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 AndrewTLee
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# JavaHead
|
2
|
+
|
3
|
+
JavaHead is designed to run Java classes easily with familiar Ruby syntax.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'java_head'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install java_head
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Use the two primary JavaHead classes, Package and Class, to represent Java packages and classes, respectively. Here is some basic usage:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'java_head'
|
27
|
+
|
28
|
+
# JavaHead::CLASSPATH is an array of Pathnames that represent where JavaHead will search for your classes. Its initial value is created based on the CLASSPATH environment variable
|
29
|
+
# You can also change your CLASSPATH like so:
|
30
|
+
JavaHead::CLASSPATH.push(Pathname.new('/my/java/classpath'))
|
31
|
+
|
32
|
+
# Get a package, this will load the package corresponding to /my/java/classpath/com/example/foo
|
33
|
+
package = JavaHead > 'com.example.foo'
|
34
|
+
|
35
|
+
|
36
|
+
subpackage = package > 'bar' # Pull up a subpackage
|
37
|
+
subpackage.compile # invoke javac to compile all files in the ppackage
|
38
|
+
subpackage.remove_class # remove .class files
|
39
|
+
|
40
|
+
jclass = subpackage > 'MyClass' # Also JavaHead > 'com.example.foo.bar.MyClass' or 'com.example.foo.bar.MyClass'.java
|
41
|
+
jclass.package == subpackage # => true
|
42
|
+
jclass.compile # Compile the class, this returns the JavaHead::Class object
|
43
|
+
jclass.exec # Execute the compiled class, this returns the output of the execution
|
44
|
+
jclass.remove_class # Remove the .class file
|
45
|
+
jclass.run # Do the same thing with only one method, this returns the same as #exec()
|
46
|
+
jclass.run('Hello','World') # You can also pass command-line arguments to your Java programs
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it ( https://github.com/AndrewTLee/java_head/fork )
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/java_head.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'java_head/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "java_head"
|
8
|
+
spec.version = JavaHead::VERSION
|
9
|
+
spec.authors = ["AndrewTLee"]
|
10
|
+
spec.email = ["andytaelee@gmail.com"]
|
11
|
+
spec.summary = %q{Represent, compile, and run Java code in Ruby.}
|
12
|
+
spec.description = %q{JavaHead contains classes to reprsent Java packages and classes and execute them in Ruby. Use this in scripts to run Java programs from Ruby, or in IRB to develop Java in a sensible environment}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "colorize", "~> 0.7.3"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.4.3"
|
25
|
+
end
|
data/lib/java_head.rb
ADDED
@@ -0,0 +1,377 @@
|
|
1
|
+
|
2
|
+
# Version info
|
3
|
+
require "java_head/version"
|
4
|
+
|
5
|
+
# A file to represent java Packages and Classes
|
6
|
+
# requires pathnames
|
7
|
+
require 'pathname'
|
8
|
+
|
9
|
+
# The namespace for the classes
|
10
|
+
module JavaHead
|
11
|
+
|
12
|
+
# The class to represent Java packages.
|
13
|
+
# Packages are immutable and duplicate package names are not allowed.
|
14
|
+
# To this end, the ::new method is private and packages are accessed using
|
15
|
+
# the ::get method which checks the class's internal cache prior to creating a new object
|
16
|
+
class Package
|
17
|
+
|
18
|
+
# Construct a package
|
19
|
+
# This method is private
|
20
|
+
#
|
21
|
+
# @param [String] name The Java name of the package
|
22
|
+
def initialize(name)
|
23
|
+
raise PackageException, "Package #{name} already exists" if @@stored[name.intern]
|
24
|
+
|
25
|
+
# Test name
|
26
|
+
raise PackageException, "Invalid package name #{name}" unless name.match FORMAT
|
27
|
+
|
28
|
+
|
29
|
+
names = name.split('.') # An array of the package names, we will be using this a lot
|
30
|
+
|
31
|
+
CLASSPATH.each do |base|
|
32
|
+
absolute = base.join(*names)
|
33
|
+
@path = absolute.realpath if absolute.exist? and absolute.directory?
|
34
|
+
end
|
35
|
+
raise PackageException, "Could not find directory for package #{name}" unless @path
|
36
|
+
|
37
|
+
|
38
|
+
# Set superpackage
|
39
|
+
@name = names.pop.freeze
|
40
|
+
if names.empty?
|
41
|
+
@superpackage = nil
|
42
|
+
else
|
43
|
+
@superpackage = self.class.get(names.join('.'))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# getter methods for name, superpackage, path
|
48
|
+
attr_reader :name,:superpackage,:path
|
49
|
+
|
50
|
+
# recursively compute fullname using superpackage fullname
|
51
|
+
#
|
52
|
+
# @return [String] The package's full name, e.g. com.example.packagename
|
53
|
+
def fullname
|
54
|
+
return @name unless @superpackage
|
55
|
+
"#{@superpackage.fullname}.#{@name}"
|
56
|
+
end
|
57
|
+
|
58
|
+
# to_s returns fullname
|
59
|
+
alias to_s fullname
|
60
|
+
|
61
|
+
# print useful fully-qualified name and path of class
|
62
|
+
#
|
63
|
+
# @return [String] A string that outlines the basic attributes of the object
|
64
|
+
def inspect
|
65
|
+
"[Java Package, name: #{fullname}, path: #{path}]"
|
66
|
+
end
|
67
|
+
|
68
|
+
# return a subpackage of the current package
|
69
|
+
#
|
70
|
+
# @param [String] name the name of the child package
|
71
|
+
# @return [JavaHead::Package] the child package
|
72
|
+
def subpackage(name)
|
73
|
+
self.class.get("#{fullname}.#{name}")
|
74
|
+
end
|
75
|
+
|
76
|
+
# return a class within the current package
|
77
|
+
#
|
78
|
+
# @param [String] name the name of the class within the package
|
79
|
+
# @return [JavaHead::Class] the child class
|
80
|
+
def class(name=nil)
|
81
|
+
return super() if name.eql? nil
|
82
|
+
Class.new("#{fullname}.#{name}")
|
83
|
+
end
|
84
|
+
|
85
|
+
# get all classes in the current package
|
86
|
+
#
|
87
|
+
# @return [Array<JavaHead::Class>] all classes in the current package
|
88
|
+
def classes
|
89
|
+
Dir.chdir(@path) do
|
90
|
+
Dir.glob('*.java').map! do |filename|
|
91
|
+
self.class( filename.match(/^([A-Z][A-Za-z0-9]*)\.java$/)[1] )
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# compile all classes in the package
|
97
|
+
#
|
98
|
+
# @return [JavaHead::Package] this package
|
99
|
+
def compile
|
100
|
+
classes.each { |c| c.compile }
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
# Check if all the classes in this package are compiled
|
105
|
+
#
|
106
|
+
# @return [Boolean] Whether or not all classes are compiled
|
107
|
+
def compiled?
|
108
|
+
classes.each do |jclass|
|
109
|
+
return false unless jclass.compiled?
|
110
|
+
end
|
111
|
+
true
|
112
|
+
end
|
113
|
+
|
114
|
+
# call #remove_class on all class files of the package
|
115
|
+
#
|
116
|
+
# @return [JavaHead::Package] the current value of this
|
117
|
+
def remove_class
|
118
|
+
classes.each { |c| c.remove_class }
|
119
|
+
self
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
# returns #class(name) or #subpackage(name) depending on the format of name
|
125
|
+
#
|
126
|
+
# @param [String] name the name of the member element
|
127
|
+
# @return [JavaHead::Package,JavaHead::Class] The child package or class
|
128
|
+
def member(name)
|
129
|
+
if name.match Class::FORMAT
|
130
|
+
self.class(name)
|
131
|
+
else
|
132
|
+
subpackage(name)
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
# > is a handy operator alias for member
|
138
|
+
alias > member
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
# The required format for all package names
|
143
|
+
FORMAT = /^([a-z][a-z0-9]*\.)*[a-z_][a-z0-9_]*$/.freeze
|
144
|
+
@@stored = Hash.new
|
145
|
+
|
146
|
+
|
147
|
+
class << self
|
148
|
+
private :new
|
149
|
+
|
150
|
+
# Get the package that corresponds to name
|
151
|
+
#
|
152
|
+
# @param [String] name the name of the package
|
153
|
+
# @return [JavaHead::Package] the package that corresponds to name
|
154
|
+
def get(name)
|
155
|
+
sym = name.intern
|
156
|
+
return @@stored[sym] if @@stored[sym]
|
157
|
+
package = new(name)
|
158
|
+
@@stored[sym] = package
|
159
|
+
package
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
# Class to represent Java Classes
|
167
|
+
class Class
|
168
|
+
# Construct a new Class object
|
169
|
+
#
|
170
|
+
# @param [String] name the full name of the class
|
171
|
+
def initialize(name)
|
172
|
+
raise ClassException, "Invalid class name #{name}" unless name.match FORMAT
|
173
|
+
|
174
|
+
names = name.split('.')
|
175
|
+
@name = names.pop.freeze
|
176
|
+
@package = Package.get(names.join('.'))
|
177
|
+
@path = @package.path.join("#{@name}.java")
|
178
|
+
|
179
|
+
raise ClassException, "Location not found for class #{name}" unless @path.exist? and @path.file?
|
180
|
+
end
|
181
|
+
# name, package, and path are publicly visible
|
182
|
+
attr_reader :name, :package, :path
|
183
|
+
|
184
|
+
# Get the fully qualified name of the class
|
185
|
+
# @return [String] the full name of the class, e.g. com.example.projects.Circle
|
186
|
+
def fullname
|
187
|
+
"#{@package.fullname}.#{@name}"
|
188
|
+
end
|
189
|
+
|
190
|
+
# #to_s is #fullname
|
191
|
+
alias to_s fullname
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
# Compile the program
|
197
|
+
# Raises a CompilerException if there was a problem compiling
|
198
|
+
# @return [JavaHead::Class] this class object
|
199
|
+
def compile(*args)
|
200
|
+
remove_class if compiled?
|
201
|
+
command = 'javac '
|
202
|
+
args.each do |arg|
|
203
|
+
arg = arg.to_s
|
204
|
+
raise CompilerException, "Invalid compiling argument #{arg}" unless arg.match ARGFORMAT
|
205
|
+
end
|
206
|
+
command << args.join(' ')
|
207
|
+
command << ' '
|
208
|
+
command << @path.to_s
|
209
|
+
output = `#{command}`
|
210
|
+
raise CompilerException, "Class #{fullname} could not compile" unless compiled?
|
211
|
+
self
|
212
|
+
end
|
213
|
+
|
214
|
+
# Remove the existing compiled class
|
215
|
+
#
|
216
|
+
# @return [JavaHead::Class, Boolean] this class object or false if not successful
|
217
|
+
def remove_class
|
218
|
+
Dir.chdir(@package.path) do
|
219
|
+
Pathname.glob("#{@name}$*.class") do |pathname|
|
220
|
+
pathname.unlink
|
221
|
+
end
|
222
|
+
Pathname.new("#{@name}.class").unlink
|
223
|
+
end
|
224
|
+
self
|
225
|
+
|
226
|
+
# the file doesn't exist or there was a problem loading it
|
227
|
+
rescue Errno::ENOENT
|
228
|
+
return false
|
229
|
+
end
|
230
|
+
|
231
|
+
# Test to see if compilation works, args are passed to the compile method
|
232
|
+
#
|
233
|
+
# @param [Array] args the arguments to be passed to the #compile method
|
234
|
+
# @return [JavaHead::Class,NilClass] this class object or nil if the compilation failed
|
235
|
+
def test(*args)
|
236
|
+
compile(*args)
|
237
|
+
remove_class
|
238
|
+
self
|
239
|
+
rescue Exception => e
|
240
|
+
puts "Exception of type #{e.class} while compiling #{fullname}: #{e}"
|
241
|
+
nil
|
242
|
+
end
|
243
|
+
|
244
|
+
# Integrated compile, run, remove_class
|
245
|
+
# This method assumes to some extent that
|
246
|
+
# compilation will succeed, so although this may fail,
|
247
|
+
# its arguments are passed to the exec method
|
248
|
+
#
|
249
|
+
# @param [Array] args the arguments to be passed to the #exec method
|
250
|
+
# @return [String] the output created by the Java program
|
251
|
+
def run(*args)
|
252
|
+
compile # this is a simple list of things for the interpreter to do
|
253
|
+
output = exec *args
|
254
|
+
remove_class
|
255
|
+
output # return output
|
256
|
+
end
|
257
|
+
|
258
|
+
# Check if the class is compiled?
|
259
|
+
#
|
260
|
+
# @return [Boolean] whether or not the class compiled
|
261
|
+
def compiled?
|
262
|
+
@path.dirname.join("#{@name}.class").exist?
|
263
|
+
end
|
264
|
+
|
265
|
+
# Take given command line arguments, check them for validity, add them to a java command and run the command to execute the class
|
266
|
+
#
|
267
|
+
# @param [Array] args the command-line arguments to be passed to the Java program
|
268
|
+
# @return [String] the output of the program execution
|
269
|
+
def exec(*args)
|
270
|
+
raise RunnerException, "Class #{fullname} cannot be run because it is not compiled" unless compiled?
|
271
|
+
command = "java #{fullname}"
|
272
|
+
args.each do |arg|
|
273
|
+
arg = arg.to_s
|
274
|
+
raise RunnerException, "Invalid command-line argument: #{arg}" unless arg.match ARGFORMAT
|
275
|
+
command << ' '
|
276
|
+
command << arg
|
277
|
+
end
|
278
|
+
`#{command}`
|
279
|
+
end
|
280
|
+
|
281
|
+
# Inspect incorporates meaningful data like name, location and whether class is compiled
|
282
|
+
# @return [String] useful data about the current object
|
283
|
+
def inspect
|
284
|
+
"[Java Class, name: #{fullname}, path: #{@path}, #{ compiled? ? 'Compiled' : 'Not Compiled'}]"
|
285
|
+
end
|
286
|
+
|
287
|
+
# The format for command-line arguments
|
288
|
+
ARGFORMAT = /^[\-a-zA-Z@][a-zA-Z0-9\-:="'@]*$/.freeze
|
289
|
+
# The format for classnames, e.g. com.example.projects.Shape
|
290
|
+
FORMAT = /^([a-z_][a-z0-9_]*\.)*[A-Z][a-z0-9_]*$/.freeze
|
291
|
+
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
# Methods in the eigenclass of Java
|
296
|
+
class << self
|
297
|
+
# Find a package using Package.get
|
298
|
+
#
|
299
|
+
# @param [String] name the name of the package to be found
|
300
|
+
# @return [JavaHead::Package] the package corresponding to name
|
301
|
+
def package(name)
|
302
|
+
Package.get(name)
|
303
|
+
end
|
304
|
+
|
305
|
+
# Returns the class with no arguments
|
306
|
+
# Returns a new class with the given name if an argument is passed
|
307
|
+
#
|
308
|
+
# @param [String] name the name of the class to initialize
|
309
|
+
# @return [JavaHead::Class] the resulting class
|
310
|
+
def class(name = nil)
|
311
|
+
return super() if name.eql? nil
|
312
|
+
Class.new(name)
|
313
|
+
end
|
314
|
+
|
315
|
+
# Creates either a class or a package
|
316
|
+
# depending on the format of the given string
|
317
|
+
#
|
318
|
+
# @param [String] name the name of the child element
|
319
|
+
# @return [JavaHead::Package, JavaHead::Class] the resulting package or class object
|
320
|
+
def member(name)
|
321
|
+
if name.match Class::FORMAT
|
322
|
+
self.class(name)
|
323
|
+
else
|
324
|
+
package(name)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
# > is an alias for member
|
329
|
+
alias > member
|
330
|
+
end
|
331
|
+
|
332
|
+
# An array of Pathnames representing the CLASSPATH environment variable
|
333
|
+
# Defaults to the current values of the $CLASSPATH environment variable
|
334
|
+
CLASSPATH = [Pathname.new('.')]
|
335
|
+
|
336
|
+
if ENV['CLASSPATH'] # if there is a CLASSPATH environment variable, let's use it.
|
337
|
+
ENV['CLASSPATH'].split(':').each do |string| # Add all class path env variables to the CLASSPATH as Pathnames
|
338
|
+
CLASSPATH.push( Pathname.new(string) )
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
CLASSPATH.uniq!
|
343
|
+
|
344
|
+
# General Java::Class exception
|
345
|
+
class ClassException < StandardError
|
346
|
+
end
|
347
|
+
# General Java::Package exception
|
348
|
+
class PackageException < StandardError
|
349
|
+
end
|
350
|
+
|
351
|
+
# Represents exceptions while compiling
|
352
|
+
class CompilerException < StandardError
|
353
|
+
end
|
354
|
+
|
355
|
+
|
356
|
+
# Represents exceptions while running
|
357
|
+
class RunnerException < StandardError
|
358
|
+
end
|
359
|
+
|
360
|
+
end
|
361
|
+
|
362
|
+
class String
|
363
|
+
# This method allows more convenience in initializing JavaHead objects.
|
364
|
+
# For instance:
|
365
|
+
# Java::Package.get('com.example.shapes')
|
366
|
+
# to be written:
|
367
|
+
# 'com.example.shapes'.java
|
368
|
+
# and
|
369
|
+
# Java::Class.new('com.example.shapes.Circle')
|
370
|
+
# to be written as
|
371
|
+
# 'com.example.shapes.Circle'.java
|
372
|
+
#
|
373
|
+
# @return [JavaHead::Package, JavaHead::Class] JavaHead.member(self)
|
374
|
+
def java
|
375
|
+
JavaHead.member self
|
376
|
+
end
|
377
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
package com.example.projects.safe;
|
2
|
+
|
3
|
+
public class Safe {
|
4
|
+
private int a;
|
5
|
+
|
6
|
+
public Safe(int a) {
|
7
|
+
this.a = a;
|
8
|
+
}
|
9
|
+
|
10
|
+
public int getA() {
|
11
|
+
return a;
|
12
|
+
}
|
13
|
+
|
14
|
+
// This class is executable and outputs the first argument it is passed
|
15
|
+
public static void main(String[] args) {
|
16
|
+
String arg = args[0];
|
17
|
+
System.out.println(arg);
|
18
|
+
}
|
19
|
+
}
|
data/test/test_class.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'colorize'
|
3
|
+
require 'java_head'
|
4
|
+
|
5
|
+
|
6
|
+
class TestClass < MiniTest::Test
|
7
|
+
|
8
|
+
def setup
|
9
|
+
JavaHead::CLASSPATH.push( Pathname.new("#{__dir__}/src") )
|
10
|
+
@safe = JavaHead::Class.new('com.example.projects.safe.Safe')
|
11
|
+
@broken = JavaHead::Class.new('com.example.projects.broken.Broken')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_safe_class_should_compile
|
15
|
+
@safe.compile
|
16
|
+
assert @safe.compiled?
|
17
|
+
@safe.remove_class
|
18
|
+
refute @safe.compiled?
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_safe_class_should_run
|
22
|
+
Dir.chdir "#{__dir__}/src" do
|
23
|
+
output = @safe.run('Input')
|
24
|
+
assert_equal output.chomp, 'Input'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_broken_class_should_not_compile
|
29
|
+
assert_raises JavaHead::CompilerException do
|
30
|
+
puts
|
31
|
+
puts 'THE FOLLOWING JAVA ERROR IS EXPECTED, WE ARE TESTING THAT BROKEN CLASSES SHOULD NOT COMPILE'
|
32
|
+
@broken.compile()
|
33
|
+
end
|
34
|
+
@broken.remove_class
|
35
|
+
refute @broken.compiled?
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'java_head'
|
3
|
+
|
4
|
+
class TestPackage < MiniTest::Test
|
5
|
+
def setup
|
6
|
+
JavaHead::CLASSPATH.push( Pathname.new("#{__dir__}/src") )
|
7
|
+
@package = JavaHead::Package.get 'com.example'
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_package_should_match_directory
|
11
|
+
assert_equal @package.path, Pathname.new("#{__dir__}/src/com/example")
|
12
|
+
assert_equal @package.superpackage, JavaHead::Package.get('com')
|
13
|
+
assert_equal @package.subpackage('projects'), JavaHead::Package.get('com.example.projects')
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_safe_package_should_compile
|
17
|
+
pkg = @package > 'projects.safe'
|
18
|
+
pkg.compile
|
19
|
+
assert pkg.compiled?
|
20
|
+
pkg.remove_class
|
21
|
+
refute pkg.compiled?
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_broken_package_should_not_compile
|
25
|
+
pkg = @package > 'projects.broken'
|
26
|
+
assert_raises JavaHead::CompilerException do
|
27
|
+
pkg.compile
|
28
|
+
end
|
29
|
+
refute pkg.compiled?
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
data/test/test_regex.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'java_head'
|
3
|
+
|
4
|
+
class TestRegex < MiniTest::Test
|
5
|
+
|
6
|
+
def test_package_format
|
7
|
+
good = ['com.example.asdf','_name','hello.asdf']
|
8
|
+
bad = ['com.example.','Name','arg-','-hello.world']
|
9
|
+
|
10
|
+
good.each do |name|
|
11
|
+
assert name.match(JavaHead::Package::FORMAT)
|
12
|
+
end
|
13
|
+
|
14
|
+
bad.each do |name|
|
15
|
+
refute name.match(JavaHead::Package::FORMAT)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_class_format
|
20
|
+
good = ['com.example.Name','A','Map','a.b.c.d.e.f.Name']
|
21
|
+
bad = ['3Package','com.example.-asdf','hello','A-name']
|
22
|
+
|
23
|
+
good.each do |name|
|
24
|
+
assert name.match(JavaHead::Class::FORMAT)
|
25
|
+
end
|
26
|
+
|
27
|
+
bad.each do |name|
|
28
|
+
refute name.match(JavaHead::Class::FORMAT)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_argument_format
|
33
|
+
good = ['arg1','hello','-g:none','a=1','asdf="d"']
|
34
|
+
bad = [';asdf','%%%']
|
35
|
+
|
36
|
+
good.each do |arg|
|
37
|
+
assert arg.match(JavaHead::Class::ARGFORMAT)
|
38
|
+
end
|
39
|
+
bad.each do |arg|
|
40
|
+
refute arg.match(JavaHead::Class::ARGFORMAT)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: java_head
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AndrewTLee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-26 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: colorize
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.7.3
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.7.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 5.4.3
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 5.4.3
|
69
|
+
description: JavaHead contains classes to reprsent Java packages and classes and execute
|
70
|
+
them in Ruby. Use this in scripts to run Java programs from Ruby, or in IRB to develop
|
71
|
+
Java in a sensible environment
|
72
|
+
email:
|
73
|
+
- andytaelee@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- java_head.gemspec
|
84
|
+
- lib/java_head.rb
|
85
|
+
- lib/java_head/version.rb
|
86
|
+
- test/src/com/example/projects/broken/Broken.java
|
87
|
+
- test/src/com/example/projects/safe/Safe.java
|
88
|
+
- test/test_class.rb
|
89
|
+
- test/test_package.rb
|
90
|
+
- test/test_regex.rb
|
91
|
+
homepage: ''
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.4.3
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Represent, compile, and run Java code in Ruby.
|
115
|
+
test_files:
|
116
|
+
- test/src/com/example/projects/broken/Broken.java
|
117
|
+
- test/src/com/example/projects/safe/Safe.java
|
118
|
+
- test/test_class.rb
|
119
|
+
- test/test_package.rb
|
120
|
+
- test/test_regex.rb
|