class JavaHead::Class

Class to represent Java Classes

Constants

ARGFORMAT

The format for command-line arguments

FORMAT

The format for classnames, e.g. com.example.projects.Shape

Attributes

name[R]

name, package, and path are publicly visible

package[R]

name, package, and path are publicly visible

path[R]

name, package, and path are publicly visible

Public Class Methods

new(name) click to toggle source

Construct a new Class object

@param [String] name the full name of the class

# 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

Public Instance Methods

compile(*args) click to toggle source

Compile the program Raises a CompilerException if there was a problem compiling @return [JavaHead::Class] this class object

# 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?() click to toggle source

Check if the class is compiled?

@return [Boolean] whether or not the class compiled

# File lib/java_head/class.rb, line 98
def compiled?
  @path.dirname.join("#{@name}.class").exist?
end
exec(*args) click to toggle source

Take given command line arguments, check them for validity, add them to a java command and run the command to execute the class

@param [Array] args the command-line arguments to be passed to the Java program @return [String] the output of the program execution

# 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() click to toggle source

Get the fully qualified name of the class @return [String] the full name of the class, e.g. com.example.projects.Circle

# File lib/java_head/class.rb, line 23
def fullname
  "#{@package.fullname}.#{@name}"
end
Also aliased as: to_s
inspect() click to toggle source

Inspect incorporates meaningful data like name, location and whether class is compiled @return [String] useful data about the current object

# File lib/java_head/class.rb, line 120
def inspect
  "[Java Class, name: #{fullname}, path: #{@path}, #{ compiled? ? 'Compiled' : 'Not Compiled'}]"
end
remove_class() click to toggle source

Remove the existing compiled class

@return [JavaHead::Class, Boolean] this class object or false if not successful

# 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) click to toggle source

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

@param [Array] args the arguments to be passed to the exec method @return [String] the output created by the Java program

# 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) click to toggle source

Test to see if compilation works, args are passed to the compile method

@param [Array] args the arguments to be passed to the compile method @return [JavaHead::Class,NilClass] this class object or nil if the compilation failed

# 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
to_s()
Alias for: fullname