Class: JavaHead::Package

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/java_head/package.rb

Overview

The class to represent Java packages. Packages are immutable and duplicate package names are not allowed. To this end, the ::new method is private and packages are accessed using the ::get method which checks the class’s internal cache prior to creating a new object

Constant Summary collapse

FORMAT =

The required format for all package names

/^([a-z][a-z0-9]*\.)*[a-z_][a-z0-9_]*$/.freeze
@@stored =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Package

Construct a package This method is private

Parameters:

  • name (String)

    The Java name of the package

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/java_head/package.rb', line 16

def initialize(name)
  raise PackageException, "Package #{name} already exists" if @@stored[name.intern]
  
  # Test name
  raise PackageException, "Invalid package name #{name}" unless name.match FORMAT

  
  names = name.split('.') # An array of the package names, we will be using this a lot
  JavaHead::CLASSPATH.each do |base|
    absolute = base.join(*names)
    @path = absolute.realpath if absolute.exist? and absolute.directory?
  end
  raise PackageException, "Could not find directory for package #{name}" unless @path
  
  
  # Set superpackage
  @name = names.pop.freeze
  if names.empty?
    @superpackage = nil
  else
    @superpackage = self.class.get(names.join('.'))
  end
end

Instance Attribute Details

#nameObject (readonly)

getter methods for name, superpackage, path



41
42
43
# File 'lib/java_head/package.rb', line 41

def name
  @name
end

#pathObject (readonly)

getter methods for name, superpackage, path



41
42
43
# File 'lib/java_head/package.rb', line 41

def path
  @path
end

#superpackageObject (readonly)

getter methods for name, superpackage, path



41
42
43
# File 'lib/java_head/package.rb', line 41

def superpackage
  @superpackage
end

Class Method Details

.get(name) ⇒ JavaHead::Package

Get the package that corresponds to name

Parameters:

  • name (String)

    the name of the package

Returns:



151
152
153
154
155
156
157
# File 'lib/java_head/package.rb', line 151

def get(name)
  sym = name.intern
  return @@stored[sym] if @@stored[sym]
  package = new(name)
  @@stored[sym] = package
  package
end

Instance Method Details

#class(name = nil) ⇒ JavaHead::Class

return a class within the current package

Parameters:

  • name (String) (defaults to: nil)

    the name of the class within the package

Returns:



75
76
77
78
79
80
# File 'lib/java_head/package.rb', line 75

def class(name=nil)
  return super() if name.eql? nil
  Dir.chdir @path do
    JavaHead::Class.new("#{fullname}.#{name}")
  end
end

#classesArray<JavaHead::Class>

get all classes in the current package

Returns:



85
86
87
88
89
90
91
# File 'lib/java_head/package.rb', line 85

def classes
  Dir.chdir(@path) do
    Dir.glob('*.java').map! do |filename|
      self.class( filename.match(/^([A-Z][A-Za-z0-9]*)\.java$/)[1] )
    end
  end
end

#compileJavaHead::Package

compile all classes in the package

Returns:



96
97
98
99
# File 'lib/java_head/package.rb', line 96

def compile
  classes.each { |c| c.compile }
  self
end

#compiled?Boolean

Check if all the classes in this package are compiled

Returns:

  • (Boolean)

    Whether or not all classes are compiled



104
105
106
107
108
109
# File 'lib/java_head/package.rb', line 104

def compiled?
  classes.each do |jclass|
    return false unless jclass.compiled?
  end
  true
end

#fullnameString Also known as: to_s

recursively compute fullname using superpackage fullname

Returns:

  • (String)

    The package’s full name, e.g. com.example.packagename



46
47
48
49
# File 'lib/java_head/package.rb', line 46

def fullname
  return @name unless @superpackage
  "#{@superpackage.fullname}.#{@name}"
end

#inspectString

print useful fully-qualified name and path of class

Returns:

  • (String)

    A string that outlines the basic attributes of the object



57
58
59
# File 'lib/java_head/package.rb', line 57

def inspect
  "[Java Package, name: #{fullname}, path: #{path}]"
end

#member(name) ⇒ JavaHead::Package, JavaHead::Class Also known as: >

returns #class(name) or #subpackage(name) depending on the format of name

Parameters:

  • name (String)

    the name of the member element

Returns:



125
126
127
128
129
130
131
132
# File 'lib/java_head/package.rb', line 125

def member(name)
  if name.match JavaHead::Class::FORMAT
    self.class(name)
  else
    subpackage(name)
  end
  
end

#remove_classJavaHead::Package

call #remove_class on all class files of the package

Returns:



114
115
116
117
# File 'lib/java_head/package.rb', line 114

def remove_class
  classes.each { |c| c.remove_class }
  self
end

#subpackage(name) ⇒ JavaHead::Package

return a subpackage of the current package

Parameters:

  • name (String)

    the name of the child package

Returns:



65
66
67
68
69
# File 'lib/java_head/package.rb', line 65

def subpackage(name)
  Dir.chdir @path do
    self.class.get("#{fullname}.#{name}")
  end
end