Method: Jinx::Importer#const_missing

Defined in:
lib/jinx/importer.rb

#const_missing(sym) ⇒ Class

Imports a Java class constant on demand. If the class does not already include this module’s mixin, then the mixin is included in the class.

Parameters:

  • the missing constant

Returns:

  • the imported class

Raises:

  • if the symbol is not an importable Java class



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jinx/importer.rb', line 48

def const_missing(sym)
  # Load the class definitions in the source directory, if necessary.
  # If a load is performed as a result of referencing the given symbol,
  # then dereference the class constant again after the load, since the class
  # might have been loaded or referenced during the load.
  unless defined? @introspected then
    configure_importer
    load_definitions
    return const_get(sym)
  end
  
  # Append the symbol to the package to make the Java class name.
  logger.debug { "Detecting whether #{sym} is a #{self} Java class..." }
  klass = @packages.detect_value do |pkg|
    begin
       java_import "#{pkg}.#{sym}"
    rescue NameError
      nil
    end
  end
  if klass then
    logger.debug { "Added #{klass} to the #{self} module." }
  else
    # Not a Java class; print a log message and pass along the error.
    logger.debug { "#{sym} is not recognized as a #{self} Java class." }
    super
  end
  
  # Introspect the Java class meta-data, if necessary.
  unless introspected?(klass) then
    (klass)
    # Print the class meta-data.
    logger.info(klass.pp_s)
  end
  
  klass
end