class JavaHead::Package
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
Constants
- FORMAT
The required format for all package names
Attributes
getter methods for name, superpackage, path
getter methods for name, superpackage, path
getter methods for name, superpackage, path
Public Class Methods
Get the package that corresponds to name
@param [String] name the name of the package @return [JavaHead::Package] the package that corresponds to name
# 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
Private Class Methods
Construct a package This method is private
@param [String] name The Java name of the package
# 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
Public Instance Methods
return a class within the current package
@param [String] name the name of the class within the package @return [JavaHead::Class] the child class
# 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
get all classes in the current package
@return [Array<JavaHead::Class>] all classes in the current package
# 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
compile all classes in the package
@return [JavaHead::Package] this package
# File lib/java_head/package.rb, line 96 def compile classes.each { |c| c.compile } self end
Check if all the classes in this package are compiled
@return [Boolean] Whether or not all classes are compiled
# File lib/java_head/package.rb, line 104 def compiled? classes.each do |jclass| return false unless jclass.compiled? end true end
recursively compute fullname using superpackage fullname
@return [String] The package's full name, e.g. com.example.packagename
# File lib/java_head/package.rb, line 46 def fullname return @name unless @superpackage "#{@superpackage.fullname}.#{@name}" end
print useful fully-qualified name and path of class
@return [String] A string that outlines the basic attributes of the object
# File lib/java_head/package.rb, line 57 def inspect "[Java Package, name: #{fullname}, path: #{path}]" end
returns class(name) or subpackage(name) depending on the format of name
@param [String] name the name of the member element @return [JavaHead::Package,JavaHead::Class] The child package or class
# 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
call remove_class on all class files of the package
@return [JavaHead::Package] the current value of this
# File lib/java_head/package.rb, line 114 def remove_class classes.each { |c| c.remove_class } self end
return a subpackage of the current package
@param [String] name the name of the child package @return [JavaHead::Package] the child package
# File lib/java_head/package.rb, line 65 def subpackage(name) Dir.chdir @path do self.class.get("#{fullname}.#{name}") end end