Class: Jinx::JavaProperty
- Defined in:
- lib/jinx/metadata/java_property.rb
Overview
The attribute metadata for an introspected Java property.
Constant Summary
Constants inherited from Property
Instance Attribute Summary collapse
-
#java_accessors ⇒ Object
readonly
This property’s Java property [reader, writer] accessors, e.g.
-
#property_descriptor ⇒ Object
readonly
This property’s Java property descriptor.
Attributes inherited from Property
#accessors, #attribute, #declarer, #flags, #type
Class Method Summary collapse
-
.to_attribute_symbol(property_name) ⇒ Object
Returns a lower-case, underscore symbol for the given property_name.
Instance Method Summary collapse
-
#collection_java_class? ⇒ Boolean
private
Whether this property’s Java type is
Iterable
. -
#create_standard_attribute_symbol(pd, klass) ⇒ String
private
The lower-case, underscore symbol for the given property descriptor.
-
#generic_parameter_type ⇒ Class?
private
The domain type of this property’s property descriptor Collection generic type argument, or nil if none.
-
#infer_collection_type ⇒ Class
private
Returns the domain type for this property’s Java Collection property descriptor.
-
#infer_collection_type_from_name ⇒ Class
private
Returns the domain type for this property’s collection Java property descriptor name.
-
#infer_non_collection_type ⇒ Class
private
This property’s Ruby type.
-
#infer_type ⇒ Class
private
The type for the specified klass property descriptor pd as described in #initialize.
-
#initialize(pd, declarer, restricted_type = nil) ⇒ JavaProperty
constructor
Creates a Ruby Property symbol corresponding to the given Ruby Java class wrapper klazz and Java property_descriptor.
-
#java_reader ⇒ Symbol
The JRuby wrapper method for the Java property reader.
-
#java_to_ruby_class(jtype) ⇒ Class
private
The corresponding Ruby type.
-
#java_writer ⇒ Symbol
The JRuby wrapper method for the Java property writer.
Methods inherited from Property
#bidirectional?, #bidirectional_java_association?, #clear_inverse, #collection?, #deep_copy, #dependent?, #dependent_flag_set, #derived?, #derived_inverse, #disjoint?, #domain?, #dup_content, #flag_supported?, #independent?, #inverse, #inverse=, #inverse_property, #java_property?, #mandatory?, #many_to_many?, #nondomain?, #owner?, #owner_flag_set, #qualify, #reader, #restrict, #restrict_flags, #restriction?, #set_flag, #set_restricted_declarer, #to_s, #unidirectional?, #unidirectional_java_dependent?, #writer
Methods included from PropertyCharacteristics
#bidirectional?, #bidirectional_java_association?, #collection?, #dependent?, #derived?, #disjoint?, #domain?, #independent?, #java_property?, #mandatory?, #many_to_many?, #nondomain?, #owner?, #restriction?, #unidirectional?, #unidirectional_java_dependent?
Constructor Details
#initialize(pd, declarer, restricted_type = nil) ⇒ JavaProperty
Creates a Ruby Property symbol corresponding to the given Ruby Java class wrapper klazz and Java property_descriptor.
The property name is the lower-case, underscore property descriptor name with the alterations described in to_attribute_symbol and Class#unocclude_reserved_method.
The property type is inferred as follows:
-
If the property descriptor return type is a primitive Java type, then that type is returned.
-
If the return type is a parameterized collection, then the parameter type is returned.
-
If the return type is an unparameterized collection, then this method infers the type from the property name, e.g. StudyProtocolCollectiontype is inferred as
StudyProtocol
by stripping theCollection
suffix, capitalizing the prefix and looking for a class of that name in the Metadata#domain_module. -
Otherwise, this method returns Java::Javalang::Object.
The optional restricted_type argument restricts the property to a subclass of the declared property type.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jinx/metadata/java_property.rb', line 31 def initialize(pd, declarer, restricted_type=nil) symbol = create_standard_attribute_symbol(pd, declarer) super(symbol, declarer, restricted_type) @property_descriptor = pd # deficient Java introspector does not recognize 'is' prefix for a Boolean property rm = declarer.property_read_method(pd) raise ArgumentError.new("Property does not have a read method: #{declarer.qp}.#{pd.name}") unless rm reader = rm.name.to_sym unless declarer.method_defined?(reader) then reader = "is#{reader.to_s.capitalize_first}".to_sym unless declarer.method_defined?(reader) then raise ArgumentError.new("Reader method not found for #{declarer} property #{pd.name}") end end unless pd.write_method then raise ArgumentError.new("Property does not have a write method: #{declarer.qp}.#{pd.name}") end writer = pd.write_method.name.to_sym unless declarer.method_defined?(writer) then raise ArgumentError.new("Writer method not found for #{declarer} property #{pd.name}") end @java_accessors = [reader, writer] qualify(:collection) if collection_java_class? @type = infer_type end |
Instance Attribute Details
#java_accessors ⇒ Object (readonly)
This property’s Java property [reader, writer] accessors, e.g. [:getActivityStatus, :setActivityStatus].
12 13 14 |
# File 'lib/jinx/metadata/java_property.rb', line 12 def java_accessors @java_accessors end |
#property_descriptor ⇒ Object (readonly)
This property’s Java property descriptor.
9 10 11 |
# File 'lib/jinx/metadata/java_property.rb', line 9 def property_descriptor @property_descriptor end |
Class Method Details
.to_attribute_symbol(property_name) ⇒ Object
Returns a lower-case, underscore symbol for the given property_name. A name ending in ‘Collection’ is changed to a pluralization.
72 73 74 75 76 77 78 79 |
# File 'lib/jinx/metadata/java_property.rb', line 72 def self.to_attribute_symbol(property_name) name = if property_name =~ /(.+)Collection$/ then property_name[0...-'Collection'.length].pluralize.underscore else property_name.underscore end name.to_sym end |
Instance Method Details
#collection_java_class? ⇒ Boolean (private)
Returns whether this property’s Java type is Iterable
.
99 100 101 102 103 104 |
# File 'lib/jinx/metadata/java_property.rb', line 99 def collection_java_class? # the Java property type ptype = @property_descriptor.property_type # Test whether the corresponding JRuby wrapper class or module is an Iterable. Class.to_ruby(ptype) < Java::JavaLang::Iterable end |
#create_standard_attribute_symbol(pd, klass) ⇒ String (private)
Returns the lower-case, underscore symbol for the given property descriptor.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jinx/metadata/java_property.rb', line 86 def create_standard_attribute_symbol(pd, klass) propname = pd.name name = propname.underscore renamed = klass.unocclude_reserved_method(pd) if renamed then logger.debug { "Renamed #{klass.qp} reserved Ruby method #{name} to #{renamed}." } renamed else JavaProperty.to_attribute_symbol(propname) end end |
#generic_parameter_type ⇒ Class? (private)
Returns the domain type of this property’s property descriptor Collection generic type argument, or nil if none.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/jinx/metadata/java_property.rb', line 128 def generic_parameter_type method = @property_descriptor.readMethod || return gtype = method.genericReturnType return unless Java::JavaLangReflect::ParameterizedType === gtype atypes = gtype.actualTypeArguments return unless atypes.size == 1 atype = atypes[0] klass = java_to_ruby_class(atype) logger.debug { "Inferred #{declarer.qp} #{self} domain type #{klass.qp} from generic parameter #{atype.name}." } if klass klass end |
#infer_collection_type ⇒ Class (private)
Returns the domain type for this property’s Java Collection property descriptor. If the property type is parameterized by a single domain class, then that generic type argument is the domain type. Otherwise, the type is inferred from the property name as described in #infer_collection_type_from_name.
116 117 118 |
# File 'lib/jinx/metadata/java_property.rb', line 116 def infer_collection_type generic_parameter_type or infer_collection_type_from_name or Java::JavaLang::Object end |
#infer_collection_type_from_name ⇒ Class (private)
Returns the domain type for this property’s collection Java property descriptor name. By convention, Jinx domain collection propertys often begin with a domain type name and end in ‘Collection’. This method strips the Collection suffix and checks whether the prefix is a domain class.
For example, the type of the property named distributionProtocolCollection
is inferred as DistributionProtocol
by stripping the Collection
suffix, capitalizing the prefix and looking for a class of that name in this classifier’s domain_module.
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/jinx/metadata/java_property.rb', line 158 def infer_collection_type_from_name # the property name pname = @property_descriptor.name # The potential class name is the capitalized property name without a 'Collection' suffix. cname = pname.capitalize_first.sub(/Collection$/, '') jname = [@declarer.parent_module, cname].join('::') klass = eval jname rescue nil if klass then logger.debug { "Inferred #{declarer.qp} #{self} collection domain type #{klass.qp} from the property name." } end klass end |
#infer_non_collection_type ⇒ Class (private)
Returns this property’s Ruby type.
121 122 123 124 |
# File 'lib/jinx/metadata/java_property.rb', line 121 def infer_non_collection_type jtype = @property_descriptor.property_type Class.to_ruby(jtype) end |
#infer_type ⇒ Class (private)
Returns the type for the specified klass property descriptor pd as described in #initialize.
107 108 109 |
# File 'lib/jinx/metadata/java_property.rb', line 107 def infer_type collection? ? infer_collection_type : infer_non_collection_type end |
#java_reader ⇒ Symbol
Returns the JRuby wrapper method for the Java property reader.
58 59 60 |
# File 'lib/jinx/metadata/java_property.rb', line 58 def java_reader java_accessors.first end |
#java_to_ruby_class(jtype) ⇒ Class (private)
Returns the corresponding Ruby type.
142 143 144 145 |
# File 'lib/jinx/metadata/java_property.rb', line 142 def java_to_ruby_class(jtype) name = String === jtype ? jtype : jtype.name Class.to_ruby(name) end |
#java_writer ⇒ Symbol
Returns the JRuby wrapper method for the Java property writer.
63 64 65 |
# File 'lib/jinx/metadata/java_property.rb', line 63 def java_writer java_accessors.last end |