Class JavaClass::Classpath::TrackingClasspath
In: lib/javaclass/classpath/tracking_classpath.rb
Parent: SimpleDelegator

A delegator classpath that tracks which classes have been accessed. For an example see how to find (un)referenced JARs.

Author:Peter Kofler

Methods

Public Class methods

Create a tracked instance of the classpath .

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 13
      def initialize(classpath)
        unless classpath.respond_to?(:load_binary) || classpath.respond_to?(:load)  
          raise ArgumentError, "wrong type of delegatee #{classpath.class}"
        end
        @classpath = classpath
        reset_access
        super(classpath)
      end

Public Instance methods

Was the classname accessed then return the count? If classname is nil then check if any class was accessed.

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 69
      def accessed(classname=nil)
        if classname
          key = to_key(classname)
          @accessed[key] 
        else
          @accessed.values.inject(0) {|s,e| s + e }
        end
      end

Return the classnames of all accessed classes. This is a list of frozen JavaClassFileName.

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 79
      def all_accessed
        @accessed.keys.sort
      end

Returns the wrapped classpath element (self) of this decorated classpath.

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 23
      def elements
        if [FolderClasspath, JarClasspath].include?(@classpath.class)
          # TODO check for any subclass of FileClasspath instead
          [self]
        else
          @classpath.elements
        end
      end

Read and disassemble the given class classname and mark as accessed.

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 45
      def load(classname)
        key = to_key(classname)
        mark_accessed(key)
        @classpath.load(key)
      end

Load the binary and mark the classname as accessed.

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 38
      def load_binary(classname)
        key = to_key(classname)
        mark_accessed(key)
        @classpath.load_binary(key)
      end

Mark the classname as accessed. Return the number of accesses so far.

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 52
      def mark_accessed(classname)
        key = to_key(classname)
        
        if @classpath.includes?(key)
          
          # hash keys need to be frozen to keep state
          if !@accessed.include?(key)
            key = key.freeze 
          end
    
          @accessed[key] += 1
        else
          nil
        end
      end

Reset all prior marked access.

[Source]

# File lib/javaclass/classpath/tracking_classpath.rb, line 33
      def reset_access
        @accessed = Hash.new(0) # class_file (JavaClassFileName) => cnt
      end

[Validate]