Class: JavaClass::Classpath::JarClasspath
- Inherits:
-
FileClasspath
- Object
- FileClasspath
- JavaClass::Classpath::JarClasspath
- Defined in:
- lib/javaclass/classpath/jar_classpath.rb
Overview
Abstraction of a ZIP or JAR on the CLASSPATH. May return additional classpath elements for referenced libs. This is a leaf in the classpath tree.
- Author
-
Peter Kofler
Direct Known Subclasses
Class Method Summary collapse
-
.valid_location?(file) ⇒ Boolean
Check if the file is a valid location for a jar classpath.
Instance Method Summary collapse
-
#additional_classpath ⇒ Object
Return list of additional classpath elements defined in the manifest of this jarfile.
-
#count ⇒ Object
Return the number of classes in this jar.
-
#includes?(classname) ⇒ Boolean
Return if classname is included in this jar.
-
#initialize(jarfile) ⇒ JarClasspath
constructor
Create a classpath with this jarfile .
-
#jar? ⇒ Boolean
Return
true
as this classpath element is a jar. -
#load_binary(classname) ⇒ Object
Load the binary data of the file name or class name classname from this jar.
-
#names(&filter) ⇒ Object
Return the list of class names found in this jar.
Methods inherited from FileClasspath
#==, #elements, #to_key, #to_s
Constructor Details
#initialize(jarfile) ⇒ JarClasspath
Create a classpath with this jarfile .
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 33 def initialize(jarfile) super(jarfile) unless JarClasspath::valid_location?(jarfile) raise IOError, "jarfile #{jarfile} not found/no file" end @jarfile = jarfile init_classes @manifest = JavaClass::Gems::ZipFile.new(@jarfile).read('META-INF/MANIFEST.MF') setup_cache if JavaClass.unpack_jars? end |
Class Method Details
.valid_location?(file) ⇒ Boolean
Check if the file is a valid location for a jar classpath.
28 29 30 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 28 def self.valid_location?(file) FileTest.exist?(file) && FileTest.file?(file) && FileTest.size(file) > 0 && file =~ /\.jar$|\.zip$/ end |
Instance Method Details
#additional_classpath ⇒ Object
Return list of additional classpath elements defined in the manifest of this jarfile.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 50 def additional_classpath if @manifest cp = @manifest.gsub(/\s{4,}/, ' ').scan(/^(.*): (.*)\s*$/).find { |p| p[0] == 'Class-Path' } if cp cp[1].strip.split.collect { |jar| File.join(File.dirname(@jarfile), jar) } else [] end else [] end end |
#count ⇒ Object
Return the number of classes in this jar.
91 92 93 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 91 def count @class_names.size end |
#includes?(classname) ⇒ Boolean
Return if classname is included in this jar.
73 74 75 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 73 def includes?(classname) @class_lookup[to_key(classname).file_name] end |
#jar? ⇒ Boolean
Return true
as this classpath element is a jar. Zip files return false
as well.
45 46 47 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 45 def jar? @manifest != nil end |
#load_binary(classname) ⇒ Object
Load the binary data of the file name or class name classname from this jar.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 78 def load_binary(classname) key = to_key(classname) if JavaClass.unpack_jars? @delegate.load_binary(key) else unless includes?(key) raise ClassNotFoundError.new(key, @jarfile) end JavaClass::Gems::ZipFile.new(@jarfile).read(key).freeze end end |
#names(&filter) ⇒ Object
Return the list of class names found in this jar. An additional block is used as filter on class names.
64 65 66 67 68 69 70 |
# File 'lib/javaclass/classpath/jar_classpath.rb', line 64 def names(&filter) if block_given? @class_names.find_all { |n| filter.call(n) } else @class_names.dup end end |