require 'java' require 'base64' require 'luruju/jude_api_loader_for_locked_project' module Luruju JUDEAPI_PHEAD = 'com.change_vision.jude.api.inf' include_class "#{JUDEAPI_PHEAD}.project.ProjectAccessorFactory" def self.wrap_native_exception(exception) return exception unless exception.is_a? NativeException wrapped = UnexpectedNativeException.new(exception.message) Luruju.constants.select{|x|/Exception$|Error$/ =~ x}.collect{|x|eval x}.each do |e| wrapped = e.new(exception.message) if exception.message.match(Regexp.compile(e.name.split('::').last)) end wrapped.set_backtrace(exception.backtrace) wrapped end class JudeApiLoader def initialize() @already_builded, @members = {}, [] end def self.marshal jude_file, options={} Base64.encode64(Marshal.dump(DtoBetweenCrubyAndJruby.new(self.load(jude_file, options)))) rescue return Base64.encode64(Marshal.dump(DtoBetweenCrubyAndJruby.new(nil, Luruju.wrap_native_exception($!)))) end def self.load jude_file, options={} project_accessor = ProjectAccessorFactory.getProjectAccessor project_accessor.open jude_file getter = project_accessor.getClass.getDeclaredMethod "getProject", nil new.build project_accessor, getter rescue raise Luruju.wrap_native_exception($!) ensure project_accessor.close rescue Exception 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 return build_judeapi(java_obj) if is_jude_class? java_return_class return build_array(java_obj, java_getter) if java_return_class.isArray return java_obj end def build_array java_obj, java_getter comp_type = java_getter.getReturnType.getComponentType return java_obj.to_a unless is_jude_class? comp_type java_obj.inject([]) {|jude_objects,i| jude_objects << build_judeapi(i) } 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_object = define_jude_class(java_if).new(@members) @members << jude_object @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 define_jude_class java_if jude_meta = JudeMeta.instance jude_if_name = java_if.getSimpleName.sub(/^I/,'').sub(/ER/, "Er") return jude_meta.jude_class(jude_if_name) if JudeMeta.is_defined_for_luruju?(jude_if_name) jude_if = JudeMeta.instance.create_jude_if jude_if_name java_if.getGenericInterfaces().each do |generic_if| super_if_name = generic_if.getSimpleName.sub(/^I/,'').sub(/ER/, "Er") jude_if.super_if_names << super_if_name define_jude_class generic_if unless JudeMeta.is_defined_for_luruju?(super_if_name) end java_if.getDeclaredMethods.each do |method| next if method.getGenericParameterTypes.length > 0 jude_if.attributes << method.getName.sub(/^(get)/,'').underscore end java_if.getDeclaredFields.each{|f| jude_if.constants[f.getName] = f.get(nil)} jude_if.define_for_luruju 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