Skip to content

How to handle display of java-wrapper objects with empty j contents? #1

@apjanke

Description

@apjanke

I'm building most of my classes in MSch as Matlab classdef wrappers around the corresponding Java objects. The pattern looks like this:

classdef Identity < handle
  
  properties (SetAccess = private, Hidden = true)
    % The underlying com.jcraft.jsch.Identity Java object
    j
  end
  properties (Dependent)
    name
    algorithmName
    isEncrypted
  end
  
  methods
    
    function this = Identity(jIdentity)
      if nargin == 0
        return
      end
      mustBeA(jIdentity, 'com.jcraft.jsch.Identity');
      this.j = jIdentity;
    end
    
    function out = get.name(this)
      out = string(this.j.getName);
    end
    
    function out = get.algorithmName(this)
      out = string(this.j.getAlgName);
    end
    
    function out = get.isEncrypted(this)
      out = this.j.isEncrypted;
    end

  end

end

That j property is intended to hold the wrapped Java object.

But it's necessary to allow for Matlab objects that have an empty j value: it's not possible in all cases to construct appropriate default underlying Java object values efficiently or at all. And we need to be able to construct "null" or "uninitialized" Matlab objects, for the cases when we need to pre-allocate an array (e.g. idents = repmat(msch.Identity, [1 n]);) or construct a default object instance to conform to declarative type constraint requirements on properties in other objects.

For these "uninitialized" objects, j is [], so the get.XXX(this) methods will fail, because they try to do a method call on [], which is a double that doesn't support those methods. And the default object disp()/display() for scalar objects will call those methods, because it tries to call those methods. What happens is that those properties appear to not exist in the object display (it doesn't throw an error).

Should I do something to handle this more gracefully?

  • Custom disp() that displays " (uninitialized)" for uninitialized objects?
  • Alter the get.* methods to return default values when j is empty?

I think this is just a cosmetic issue, since display doesn't actually raise an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions