Class: JavaHead::Class

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Class

Construct a new Class object

Parameters:

  • name (String)

    the full name of the class

Raises:



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

#nameObject (readonly)

name, package, and path are publicly visible



19
20
21
# File 'lib/java_head/class.rb', line 19

def name
  @name
end

#packageObject (readonly)

name, package, and path are publicly visible



19
20
21
# File 'lib/java_head/class.rb', line 19

def package
  @package
end

#pathObject (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

Returns:

Raises:



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?

Returns:

  • (Boolean)

    whether or not the class 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

Parameters:

  • args (Array)

    the command-line arguments to be passed to the Java program

Returns:

  • (String)

    the output of the program execution

Raises:



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

#fullnameString Also known as: to_s

Get the fully qualified name of the class

Returns:

  • (String)

    the full name of the class, e.g. com.example.projects.Circle



23
24
25
# File 'lib/java_head/class.rb', line 23

def fullname
  "#{@package.fullname}.#{@name}"
end

#inspectString

Inspect incorporates meaningful data like name, location and whether class is compiled

Returns:

  • (String)

    useful data about the current object



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_classJavaHead::Class, Boolean

Remove the existing compiled class

Returns:

  • (JavaHead::Class, Boolean)

    this class object or false if not successful



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

Parameters:

  • args (Array)

    the arguments to be passed to the #exec method

Returns:

  • (String)

    the output created by the Java program



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

Parameters:

  • args (Array)

    the arguments to be passed to the #compile method

Returns:

  • (JavaHead::Class, NilClass)

    this class object or nil if the compilation failed



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