Class: JavaClass::Classpath::FolderClasspath

Inherits:
FileClasspath show all
Defined in:
lib/javaclass/classpath/folder_classpath.rb

Overview

Abstraction of a folder on the CLASSPATH. This is a leaf in the classpath tree.

Author

Peter Kofler

Direct Known Subclasses

ConventionClasspath

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileClasspath

#==, #additional_classpath, #elements, #jar?, #to_key, #to_s

Constructor Details

#initialize(folder) ⇒ FolderClasspath

Create a classpath with this folder .



18
19
20
21
22
23
24
25
# File 'lib/javaclass/classpath/folder_classpath.rb', line 18

def initialize(folder)
  super(folder)
  unless FolderClasspath::valid_location?(folder)
    raise IOError, "folder #{folder} not found/no folder"
  end
  @folder = folder
  init_classes
end

Class Method Details

.valid_location?(file) ⇒ Boolean

Check if the file is a valid location for a folder classpath.

Returns:

  • (Boolean)


13
14
15
# File 'lib/javaclass/classpath/folder_classpath.rb', line 13

def self.valid_location?(file)
  FileTest.exist?(file) && FileTest.directory?(file) 
end

Instance Method Details

#countObject

Return the number of classes in this folder.



51
52
53
# File 'lib/javaclass/classpath/folder_classpath.rb', line 51

def count
  @class_names.size
end

#includes?(classname) ⇒ Boolean

Return if classname is included in this folder.

Returns:

  • (Boolean)


37
38
39
# File 'lib/javaclass/classpath/folder_classpath.rb', line 37

def includes?(classname)
  @class_lookup[to_key(classname).file_name] 
end

#load_binary(classname) ⇒ Object

Load the binary data of the file name or class name classname from this folder.



42
43
44
45
46
47
48
# File 'lib/javaclass/classpath/folder_classpath.rb', line 42

def load_binary(classname)
  key = to_key(classname)
  unless includes?(key)
    raise ClassNotFoundError.new(key, @folder)
  end
  File.open(File.join(@folder, key), 'rb') { |io| io.read.freeze }
end

#names(&filter) ⇒ Object

Return the list of class names found in this folder. An additional block is used as filter on class names.



28
29
30
31
32
33
34
# File 'lib/javaclass/classpath/folder_classpath.rb', line 28

def names(&filter)
  if block_given?
    @class_names.find_all { |n| filter.call(n) }
  else
    @class_names.dup
  end
end