Class: JavaClass::Classpath::JarClasspath

Inherits:
Object
  • Object
show all
Defined in:
lib/javaclass/classpath/jar_classpath.rb

Overview

Abstraction of a ZIP or JAR on the CLASSPATH.

Author

Peter Kofler

Direct Known Subclasses

JavaHomeClasspath

Instance Method Summary collapse

Constructor Details

#initialize(jarfile) ⇒ JarClasspath

Return the list of classnames found in this jarfile .

Raises:

  • (IOError)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/javaclass/classpath/jar_classpath.rb', line 12

def initialize(jarfile)
  @jarfile = jarfile
  raise IOError, "jarfile #{@jarfile} not found" if !FileTest.exist? @jarfile
  raise "#{@jarfile} is no file" if !FileTest.file? @jarfile
  @classes = list_classes.collect { |cl| cl.to_javaname }
  @manifest =
  begin
    Zip::ZipFile.open(@jarfile) { |zipfile| zipfile.file.read("META-INF/MANIFEST.MF") }
  rescue
    nil
  end
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
# File 'lib/javaclass/classpath/jar_classpath.rb', line 71

def ==(other)
  other.class == JarClasspath && other.to_s == self.to_s
end

#additional_classpathObject

Return list of additional classpath elements defined in the manifest of this jarfile.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/javaclass/classpath/jar_classpath.rb', line 31

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

#countObject

Return the number of classes in this jar.



63
64
65
# File 'lib/javaclass/classpath/jar_classpath.rb', line 63

def count
  @classes.size
end

#includes?(classname) ⇒ Boolean

Return if classname is included in this jar.

Returns:

  • (Boolean)


50
51
52
# File 'lib/javaclass/classpath/jar_classpath.rb', line 50

def includes?(classname)
  @classes.include?(normalize(classname))
end

#jar?Boolean

Return if the given classpath element is a jar.

Returns:

  • (Boolean)


26
27
28
# File 'lib/javaclass/classpath/jar_classpath.rb', line 26

def jar?
  @manifest != nil
end

#load_binary(classname) ⇒ Object

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



55
56
57
58
59
60
# File 'lib/javaclass/classpath/jar_classpath.rb', line 55

def load_binary(classname)
  raise "class #{classname} not found in #{@jarfile}" unless includes?(classname)
  Zip::ZipFile.open(@jarfile) do |zipfile|
    zipfile.file.read(normalize(classname))
  end
end

#namesObject

Return the list of class names found in this jar.



45
46
47
# File 'lib/javaclass/classpath/jar_classpath.rb', line 45

def names
  @classes.dup
end

#to_sObject



67
68
69
# File 'lib/javaclass/classpath/jar_classpath.rb', line 67

def to_s
  @jarfile
end