Class: JavaHead::Class
- Inherits:
-
Object
- Object
- JavaHead::Class
- Defined in:
- lib/java_head.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][a-z0-9_]*$/.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
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/java_head.rb', line 171 def initialize(name) raise ClassException, "Invalid class name #{name}" unless name.match FORMAT names = name.split('.') @name = names.pop.freeze @package = 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
182 183 184 |
# File 'lib/java_head.rb', line 182 def name @name end |
#package ⇒ Object (readonly)
name, package, and path are publicly visible
182 183 184 |
# File 'lib/java_head.rb', line 182 def package @package end |
#path ⇒ Object (readonly)
name, package, and path are publicly visible
182 183 184 |
# File 'lib/java_head.rb', line 182 def path @path end |
Instance Method Details
#compile(*args) ⇒ JavaHead::Class
Compile the program Raises a CompilerException if there was a problem compiling
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/java_head.rb', line 199 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?
261 262 263 |
# File 'lib/java_head.rb', line 261 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
269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/java_head.rb', line 269 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
186 187 188 |
# File 'lib/java_head.rb', line 186 def fullname "#{@package.fullname}.#{@name}" end |
#inspect ⇒ String
Inspect incorporates meaningful data like name, location and whether class is compiled
283 284 285 |
# File 'lib/java_head.rb', line 283 def inspect "[Java Class, name: #{fullname}, path: #{@path}, #{ compiled? ? 'Compiled' : 'Not Compiled'}]" end |
#remove_class ⇒ JavaHead::Class, Boolean
Remove the existing compiled class
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/java_head.rb', line 217 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
251 252 253 254 255 256 |
# File 'lib/java_head.rb', line 251 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
235 236 237 238 239 240 241 242 |
# File 'lib/java_head.rb', line 235 def test(*args) compile(*args) remove_class self rescue Exception => e puts "Exception of type #{e.class} while compiling #{fullname}: #{e}" nil end |