Class: JavaHead::Class

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

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Class

Construct a new Class object

Parameters:

  • name (String)

    the full name of the class

Raises:



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

#nameObject (readonly)

name, package, and path are publicly visible



182
183
184
# File 'lib/java_head.rb', line 182

def name
  @name
end

#packageObject (readonly)

name, package, and path are publicly visible



182
183
184
# File 'lib/java_head.rb', line 182

def package
  @package
end

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

Returns:

Raises:



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?

Returns:

  • (Boolean)

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

Parameters:

  • args (Array)

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

Returns:

  • (String)

    the output of the program execution

Raises:



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

#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



186
187
188
# File 'lib/java_head.rb', line 186

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



283
284
285
# File 'lib/java_head.rb', line 283

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



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

Parameters:

  • args (Array)

    the arguments to be passed to the #exec method

Returns:

  • (String)

    the output created by the Java program



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

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



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