3

I am using a java public interface in a matlab scripted (but object oriented) software.

We frequently have to call java methods and this work flawlessly. If I have the following java class:

package com.toto

public class Foo {
    public static void printHello() {
        System.out.println("Hello World"); 
    }
}

Then in Matlab I just call:

com.toto.Foo.printHello

To get the print displayed in my console command.

Now what I would like to do is something similar to:

package com.toto

public class Foo {
    public static <E> void printClass(Class<E> type) {
        System.out.println("My type: " + type); 
    }
}

public class Goo {
....
}  

And in Matlab:

com.toto.Foo.printClass(com.toto.Goo.class)

Which is actually not working.

Any solution for this ?

Edit: here is a working java example, the code in main should be executed under matlab:

public class Test
{
  public static void main(String[] args)
  {
    Foo.printClass(Goo.class);
  }
}

public class Foo 
{
    public static <E> void printClass(Class<E> type) 
    {
        System.out.println("My type: " + type); 
    }
}

 public class Goo {
    public Goo() {};
 }  

1 Answer 1

1

The problem here is that the .class syntax is not valid in Matlab:

com.toto.Goo.class

What you can do is to make an instance of Goo and then use the getClass method on that object:

goo = com.toto.Goo();
com.toto.Foo.printClass(goo.getClass());

Or if you want to use only the name of the Java class (or for example in case of Java enums where the instantiation is not possible) you can use javaclass from undocumentedmatlab.com.

The main part of this function is

jclass = java.lang.Class.forName('com.toto.Goo', ...
    true, ...
    java.lang.Thread.currentThread().getContextClassLoader());

which uses the forName method of Class:

Returns the Class object associated with the class or interface with the given string name, using the given class loader.

This second approach can be used as an equivalent of the Java .class syntax.

Sign up to request clarification or add additional context in comments.

3 Comments

That's actually what I am doing. I have a static getDefault() method because Goo is actually an Enum, so I can't have a public constructor in it. However, of course I am on java 1.6 so I cannot enforce the getDefault() method in a interface, nor an abstract class. So I basically rely on the developer good will to implement this method. This is why I dislike this solution (and that I really do not like java, on the side).
Updated the answer for you. I hope it satisfies your needs.
That's great thanks, Mathworks discourage the use of forName (they indicate to use javaObjectEDT), but in that case it's a really nice use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.