class JavaClass::Classpath::CompositeClasspath
List of class path elements constructed from a full CLASSPATH variable.
- Author
-
Peter Kofler
Public Class Methods
Create an empty classpath composite root. The optional file is for identifying subclasses.
# File lib/javaclass/classpath/composite_classpath.rb, line 20 def initialize(root='.') super(root) @elements = [] end
Public Instance Methods
Wrap the elem classpath with a new TrackingClasspath
and add it to the list of elements.
Was the classname accessed then return the count? If classname is nil
then check if any class was accessed. (See TrackingClasspath
)
# File lib/javaclass/classpath/tracking_classpath.rb, line 125 def accessed(classname=nil) if classname key = to_key(classname) found = find_element_for(key) if found then found.accessed(key) else 0 end else @elements.inject(0) do |s,e| accessed = e.accessed if accessed then s + accessed else s end end end end
Add the elem classpath element to the list.
# File lib/javaclass/classpath/composite_classpath.rb, line 43 def add_element(elem) if elem.count > 0 && !@elements.find { |cpe| cpe == elem } @elements << elem end elem.additional_classpath.each do |acpe| # referred classpath elements may be missing if JarClasspath.valid_location?(acpe) add_element(JarClasspath.new(acpe)) end end end
Add the name class path which may be a file or a folder to this classpath.
# File lib/javaclass/classpath/composite_classpath.rb, line 32 def add_file_name(name) if FolderClasspath.valid_location?(name) add_element(FolderClasspath.new(name)) elsif JarClasspath.valid_location?(name) add_element(JarClasspath.new(name)) else warn("tried to add an invalid classpath location #{name}") end end
Return the classnames of all accessed classes in child elements. (See TrackingClasspath
)
# File lib/javaclass/classpath/tracking_classpath.rb, line 139 def all_accessed @elements.map { |cp| cp.all_accessed }.flatten.sort end
Return the number of classes in this classpath.
# File lib/javaclass/classpath/composite_classpath.rb, line 78 def count @elements.inject(0) { |s,e| s + e.count } end
Return all the classpath elements (the children) of this path and all child paths.
# File lib/javaclass/classpath/composite_classpath.rb, line 26 def elements # [self] + */ @elements.map { |cp| cp.elements }.flatten end
Return if classname is included in this classpath. If yes, return the count (usually 1).
# File lib/javaclass/classpath/composite_classpath.rb, line 61 def includes?(classname) key = to_key(classname) found = find_element_for(key) if found then 1 else nil end end
Load the binary data of the file name or class name classname from this classpath.
# File lib/javaclass/classpath/composite_classpath.rb, line 68 def load_binary(classname) key = to_key(classname) found = find_element_for(key) unless found raise ClassNotFoundError.new(key, to_s) end found.load_binary(key) end
Mark the classname as accessed. Return the number of accesses so far. (See TrackingClasspath
)
# File lib/javaclass/classpath/tracking_classpath.rb, line 113 def mark_accessed(classname) key = to_key(classname) found = find_element_for(key) if found found.mark_accessed(key) else nil end end
Return the list of class names found in this classpath. An additional block is used as filter on class names.
# File lib/javaclass/classpath/composite_classpath.rb, line 56 def names(&filter) @elements.collect { |e| e.names(&filter) }.flatten.uniq end
Reset all prior marked access in child elements. (See TrackingClasspath
)
# File lib/javaclass/classpath/tracking_classpath.rb, line 108 def reset_access @elements.each { |e| e.reset_access } end
# File lib/javaclass/classpath/composite_classpath.rb, line 82 def to_s str = super.to_s if str=='.' @elements.collect { |e| e.to_s }.join(',') else str end end
Private Instance Methods
Return the matching classpath element for the given key
# File lib/javaclass/classpath/composite_classpath.rb, line 94 def find_element_for(key) @elements.find { |e| e.includes?(key) } end