require 'java' module Luruju JUDEAPI_PHEAD = 'com.change_vision.jude.api.inf' include_class "#{JUDEAPI_PHEAD}.project.ProjectAccessorFactory" class JudeApiLoader def self.load jude_file project_accessor = ProjectAccessorFactory.getProjectAccessor project_accessor.open jude_file getter = project_accessor.getClass.getDeclaredMethod "getProject", nil new.build project_accessor, getter ensure project_accessor.close rescue Exception end def initialize @already_builded = {} end def build java_parent, java_getter return nil if java_getter.getGenericParameterTypes.length > 0 begin java_obj = java_parent.send java_getter.getName rescue NativeException # p "Warning(#{__FILE__},#{__LINE__}) " + java_getter.getName end return nil if java_obj == nil java_return_class = java_getter.getReturnType if is_jude_class? java_return_class return build_judeapi(java_obj) elsif java_return_class.isArray return build_array(java_obj, java_getter) else return java_obj end end def build_array java_obj, java_getter comp_type = java_getter.getReturnType.getComponentType if is_jude_class? comp_type jude_objects = Array.new java_obj.each {|i| jude_objects << build_judeapi(i) } return jude_objects else return java_obj.to_a end end def build_judeapi java_obj if is_jude_named_element?(java_obj) and @already_builded.key?(java_obj.getId) return @already_builded[java_obj.getId] end java_if = java_obj.getClass.getInterfaces[0] jude_class = JudeApi.module_eval java_if.getSimpleName.jude_class jude_object = jude_class.new @already_builded[java_obj.getId] = jude_object if jude_object.kind_of_named_element? java_if.getMethods.each do |java_method| next if java_method.getGenericParameterTypes.length > 0 setter_name = java_method.getName.sub(/^(get)/,'').underscore << "=" jude_object.send setter_name, build(java_obj, java_method) end return jude_object end def is_jude_class? java_class java_class.getName.index(JUDEAPI_PHEAD) == 0 end def self.define_jude_class class_id judeapi_class_name = class_id.to_s.jude_class JudeApi.module_eval "class #{judeapi_class_name} < JudeBase ; end" judeapi_class = JudeApi.module_eval judeapi_class_name judeapi_module_name = class_id.to_s.jude_module judeapi_module = JudeApi.module_eval judeapi_module_name judeapi_class.send :include, judeapi_module judeapi_class end def self.define_jude_module module_id java_if = java.lang.Class.forName "#{JUDEAPI_PHEAD}.model.#{module_id.to_s.java_if}" judeapi_module_name = java_if.getSimpleName.jude_module JudeApi.module_eval "module #{judeapi_module_name} ; end" judeapi_module = JudeApi.module_eval judeapi_module_name java_if.getGenericInterfaces().each do |generic_if| JudeApi.module_eval generic_if.getSimpleName.jude_class judeapi_module.send :include, JudeApi.module_eval(generic_if.getSimpleName.jude_module) end java_if.getDeclaredMethods.each do |method| next if method.getGenericParameterTypes.length > 0 java_method_name = method.getName attr_name = java_method_name.sub(/^(get)/,'').underscore judeapi_module.module_eval "attr_accessor :#{attr_name}" if /^is_/ =~ attr_name judeapi_module.send :alias_method, "#{attr_name}?".to_sym, attr_name.to_sym end end java_if.getDeclaredFields.each do |f| judeapi_module.module_eval "#{f.getName} = #{f.get(nil)}" end judeapi_module end def is_jude_named_element? jude_obj jude_obj.getClass.getMethod "getId", nil return true rescue java.lang.NoSuchMethodException return false end end end class String def is_jude_class? /^Jude/ =~ self and not /Methods$/ =~ self end def is_jude_module? /^Jude/ =~ self and /Methods$/ =~ self end def jude_class 'Jude' + sub(/^(I|Jude)/,'').sub(/ER/, "Er").sub(/Methods$/, '') end def jude_module 'Jude' + sub(/^(I|Jude)/,'').sub(/ER/, "Er") + "Methods" end def java_if sub(/^(I|Jude)/,'I').sub(/Er/, "ER").sub(/Methods$/, '') end end class Module alias :const_missing_except_luruju :const_missing def const_missing class_id return const_missing_except_luruju(class_id) unless self == Luruju::JudeApi return Luruju::JudeApiLoader::define_jude_class(class_id) if class_id.to_s.is_jude_class? return Luruju::JudeApiLoader::define_jude_module(class_id) if class_id.to_s.is_jude_module? raise NameError end end