Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Java reflection to find a method

Subject: Java reflection to find a method

From: Lori

Date: 21 Oct, 2009 20:24:20

Message: 1 of 5

Using Java reflection to find a method in Matlab created jar file does not seem to work. You can find the class using Class.forName() but not a method in the class. When you print out the methods in the class you can get a list that includes (for example):
   public java.lang.Object[] MyClass.myMethod(int,java.lang.Object[]) throws com.mathworks.toolbox.javabuilder.MWException
   public void MyClass.myMethod(java.lang.Object[],java.lang.Object[]) throws com.mathworks.toolbox.javabuilder.MWException
   public void MyClass.myMethod(java.util.List,java.util.List) throws com.mathworks.toolbox.javabuilder.MWException

The method is really called in the code with an integer and 4 strings with an expected return. So if you try to get that method (the first one in the above list) you would do the following:
  Class[] list = { Integer.class, java.lang.Object[].class};
  Method method = Class.forName("MyClass").getMethod("myMethod", Class[] list);

It throws an NoSuchMethodException.

Help!?! Anyone got a clue what's wrong.

Subject: Java reflection to find a method

From: Lori

Date: 21 Oct, 2009 22:40:20

Message: 2 of 5

"Lori " <lori.cook@hp.com> wrote in message <hbnqlk$p3v$1@fred.mathworks.com>...
> Using Java reflection to find a method in Matlab created jar file does not seem to work. You can find the class using Class.forName() but not a method in the class. When you print out the methods in the class you can get a list that includes (for example):
> public java.lang.Object[] MyClass.myMethod(int,java.lang.Object[]) throws com.mathworks.toolbox.javabuilder.MWException
> public void MyClass.myMethod(java.lang.Object[],java.lang.Object[]) throws com.mathworks.toolbox.javabuilder.MWException
> public void MyClass.myMethod(java.util.List,java.util.List) throws com.mathworks.toolbox.javabuilder.MWException
>
> The method is really called in the code with an integer and 4 strings with an expected return. So if you try to get that method (the first one in the above list) you would do the following:
> Class[] list = { Integer.class, java.lang.Object[].class};
> Method method = Class.forName("MyClass").getMethod("myMethod", Class[] list);
>
> It throws an NoSuchMethodException.
>
> Help!?! Anyone got a clue what's wrong.

BTW - the method really takes an integer (to indicate it returns a value) followed by 4 strings. The list use to be a Class[] with Integer.class and 4 String.class objects. This worked with 2006b. Not working with 2009a.

Subject: Java reflection to find a method

From: Yair Altman

Date: 22 Oct, 2009 07:47:03

Message: 3 of 5

"Lori " <lori.cook@hp.com> wrote in message <hbo2kk$19f$1@fred.mathworks.com>...
> "Lori " <lori.cook@hp.com> wrote in message <hbnqlk$p3v$1@fred.mathworks.com>...
> > Using Java reflection to find a method in Matlab created jar file does not seem to work. You can find the class using Class.forName() but not a method in the class. When you print out the methods in the class you can get a list that includes (for example):
> > public java.lang.Object[] MyClass.myMethod(int,java.lang.Object[]) throws com.mathworks.toolbox.javabuilder.MWException
> > public void MyClass.myMethod(java.lang.Object[],java.lang.Object[]) throws com.mathworks.toolbox.javabuilder.MWException
> > public void MyClass.myMethod(java.util.List,java.util.List) throws com.mathworks.toolbox.javabuilder.MWException
> >
> > The method is really called in the code with an integer and 4 strings with an expected return. So if you try to get that method (the first one in the above list) you would do the following:
> > Class[] list = { Integer.class, java.lang.Object[].class};
> > Method method = Class.forName("MyClass").getMethod("myMethod", Class[] list);
> >
> > It throws an NoSuchMethodException.
> >
> > Help!?! Anyone got a clue what's wrong.
>
> BTW - the method really takes an integer (to indicate it returns a value) followed by 4 strings. The list use to be a Class[] with Integer.class and 4 String.class objects. This worked with 2006b. Not working with 2009a.


Try to use the UIInspect utility from the File Exchange (http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methodspropertiescallbacks-of-an-object ) to see whether the method is correctly exposed, and also its reported (reflected) signature:

uiinspect('com.mathworks.toolbox.javabuilder.MWException'); % inspect class name
uiinspect(myClassObject); % inspect an object

Also, try to invoke the method using Matlab's awtinvoke() or javaMethod() functions (which use reflection inside, but with copious error checks);

I'd be interested to hear if you find a problem with a specific class or Matlab release.

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Java reflection to find a method

From: Lori

Date: 28 Oct, 2009 17:47:02

Message: 4 of 5

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hbp2ln$84n$1@fred.mathworks.com>...
> Try to use the UIInspect utility from the File Exchange (http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methodspropertiescallbacks-of-an-object ) to see whether the method is correctly exposed, and also its reported (reflected) signature:
>
> uiinspect('com.mathworks.toolbox.javabuilder.MWException'); % inspect class name
> uiinspect(myClassObject); % inspect an object
>
> Also, try to invoke the method using Matlab's awtinvoke() or javaMethod() functions (which use reflection inside, but with copious error checks);
>
> I'd be interested to hear if you find a problem with a specific class or Matlab release.
>
> Yair Altman
> http://UndocumentedMatlab.com
>

Yair

Thank you for the ideas. Running javaObject() and javaMethod() on the jar file produced the following results:
>>videoObj = javaObject('VideoCalibration.VideoCalibration');
??? Error using ==> javaObject
Java exception occurred:
... Matlab M-code Stack Trace ...

com.mathworks.toolbox.javabuilder.MWException: Component created using MATLAB Builder JA cannot be loaded in MATLAB

at com.mathworks.toolbox.javabuilder.internal.MWMCR.newInstance(MWMCR.java:557)

at VideoCalibration.VideoCalibrationMCRFactory.newInstance(VideoCalibrationMCRFactory.java:163)

at VideoCalibration.VideoCalibrationMCRFactory.newInstance(VideoCalibrationMCRFactory.java:174)

at VideoCalibration.VideoCalibration.<init>(VideoCalibration.java:71)

>> javaMethod('imCapture', 'VideoCalibration.VideoCalibration', 1, 'C:/Temp/testImCapture', 'adapter', 'name', 'format');
??? Error using ==> javaMethod
No static imCapture method with appropriate signature
exists in Java class VideoCalibration.VideoCalibration

So either a) I'm doing something wrong with javaObject() and/or javaMethod() or b) Matlab can't do the reflection either.

Help? Or should I be submitting a request for help to the Matlab support team?

Thanks
Lori <*>

Subject: Java reflection to find a method

From: Yair Altman

Date: 29 Oct, 2009 07:40:18

Message: 5 of 5

"Lori " <lori.cook@hp.com> wrote in message...
> Thank you for the ideas. Running javaObject() and javaMethod() on the jar file produced the following results:

> >>videoObj = javaObject('VideoCalibration.VideoCalibration');
> ??? Error using ==> javaObject
> Java exception occurred:
> ... Matlab M-code Stack Trace ...
>
> com.mathworks.toolbox.javabuilder.MWException: Component created using MATLAB Builder JA cannot be loaded in MATLAB
>
> at com.mathworks.toolbox.javabuilder.internal.MWMCR.newInstance(MWMCR.java:557)

[snip]
> >> javaMethod('imCapture', 'VideoCalibration.VideoCalibration', 1, 'C:/Temp/testImCapture', 'adapter', 'name', 'format');
> ??? Error using ==> javaMethod
> No static imCapture method with appropriate signature
> exists in Java class VideoCalibration.VideoCalibration
>
> So either a) I'm doing something wrong with javaObject() and/or javaMethod() or b) Matlab can't do the reflection either.
>
> Help? Or should I be submitting a request for help to the Matlab support team?
>
> Thanks
> Lori <*>

It seems like an internal Matlab exception prevents the creation of your VideoCalibration object - contact MathWorks support for help.

The second error is a corollary of the first: since you couldn't invoke imCapture() on the object (since you can't create an instance of it), you tried invoking it directly on the classname. However, imCapture() was not declared static in your Java code, so this cannot be done.

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us