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() {};
}
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 enum
s 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.