Class: JavaHead::Class
- Inherits:
-
Object
- Object
- JavaHead::Class
- Includes:
- Exceptions
- Defined in:
- lib/java_head/class.rb
Overview
Class to represent Java Classes
Constant Summary collapse
- ARGFORMAT =
The format for command-line arguments
/^[\-a-zA-Z@][a-zA-Z0-9\-:="'@]*$/.freeze
- FORMAT =
The format for classnames, e.g. com.example.projects.Shape
/^([a-z_][a-z0-9_]*\.)*[A-Z]\w*$/.freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
name, package, and path are publicly visible.
-
#package ⇒ Object
readonly
name, package, and path are publicly visible.
-
#path ⇒ Object
readonly
name, package, and path are publicly visible.
Instance Method Summary collapse
-
#compile(*args) ⇒ JavaHead::Class
Compile the program Raises a CompilerException if there was a problem compiling.
-
#compiled? ⇒ Boolean
Check if the class is compiled?.
-
#exec(*args) ⇒ String
Take given command line arguments, check them for validity, add them to a java command and run the command to execute the class.
-
#fullname ⇒ String
(also: #to_s)
Get the fully qualified name of the class.
-
#initialize(name) ⇒ Class
constructor
Construct a new Class object.
-
#inspect ⇒ String
Inspect incorporates meaningful data like name, location and whether class is compiled.
-
#remove_class ⇒ JavaHead::Class, Boolean
Remove the existing compiled class.
-
#run(*args) ⇒ String
Integrated compile, run, remove_class This method assumes to some extent that compilation will succeed, so although this may fail, its arguments are passed to the exec method.
-
#test(*args) ⇒ JavaHead::Class, NilClass
Test to see if compilation works, args are passed to the compile method.
Constructor Details
#initialize(name) ⇒ Class
Construct a new Class object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/java_head/class.rb', line 8 def initialize(name) raise ClassException, "Invalid class name #{name}" unless name.match FORMAT names = name.split('.') @name = names.pop.freeze @package = JavaHead::Package.get(names.join('.')) @path = @package.path.join("#{@name}.java") raise ClassException, "Location not found for class #{name}" unless @path.exist? and @path.file? end |
Instance Attribute Details
#name ⇒ Object (readonly)
name, package, and path are publicly visible
19 20 21 |
# File 'lib/java_head/class.rb', line 19 def name @name end |
#package ⇒ Object (readonly)
name, package, and path are publicly visible
19 20 21 |
# File 'lib/java_head/class.rb', line 19 def package @package end |
#path ⇒ Object (readonly)
name, package, and path are publicly visible
19 20 21 |
# File 'lib/java_head/class.rb', line 19 def path @path end |
Instance Method Details
#compile(*args) ⇒ JavaHead::Class
Compile the program Raises a CompilerException if there was a problem compiling
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/java_head/class.rb', line 36 def compile(*args) remove_class if compiled? command = 'javac ' args.each do |arg| arg = arg.to_s raise CompilerException, "Invalid compiling argument #{arg}" unless arg.match ARGFORMAT end command << args.join(' ') command << ' ' command << @path.to_s output = `#{command}` raise CompilerException, "Class #{fullname} could not compile" unless compiled? self end |
#compiled? ⇒ Boolean
Check if the class is compiled?
98 99 100 |
# File 'lib/java_head/class.rb', line 98 def compiled? @path.dirname.join("#{@name}.class").exist? end |
#exec(*args) ⇒ String
Take given command line arguments, check them for validity, add them to a java command and run the command to execute the class
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/java_head/class.rb', line 106 def exec(*args) raise RunnerException, "Class #{fullname} cannot be run because it is not compiled" unless compiled? command = "java #{fullname}" args.each do |arg| arg = arg.to_s raise RunnerException, "Invalid command-line argument: #{arg}" unless arg.match ARGFORMAT command << ' ' command << arg end `#{command}` end |
#fullname ⇒ String Also known as: to_s
Get the fully qualified name of the class
23 24 25 |
# File 'lib/java_head/class.rb', line 23 def fullname "#{@package.fullname}.#{@name}" end |
#inspect ⇒ String
Inspect incorporates meaningful data like name, location and whether class is compiled
120 121 122 |
# File 'lib/java_head/class.rb', line 120 def inspect "[Java Class, name: #{fullname}, path: #{@path}, #{ compiled? ? 'Compiled' : 'Not Compiled'}]" end |
#remove_class ⇒ JavaHead::Class, Boolean
Remove the existing compiled class
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/java_head/class.rb', line 54 def remove_class Dir.chdir(@package.path) do Pathname.glob("#{@name}$*.class") do |pathname| pathname.unlink end Pathname.new("#{@name}.class").unlink end self # the file doesn't exist or there was a problem loading it rescue Errno::ENOENT return false end |
#run(*args) ⇒ String
Integrated compile, run, remove_class This method assumes to some extent that compilation will succeed, so although this may fail, its arguments are passed to the exec method
88 89 90 91 92 93 |
# File 'lib/java_head/class.rb', line 88 def run(*args) compile # this is a simple list of things for the interpreter to do output = exec *args remove_class output # return output end |
#test(*args) ⇒ JavaHead::Class, NilClass
Test to see if compilation works, args are passed to the compile method
72 73 74 75 76 77 78 79 |
# File 'lib/java_head/class.rb', line 72 def test(*args) compile(*args) remove_class self rescue Exception => e puts "Exception of type #{e.class} while compiling #{fullname}: #{e}" nil end |