Page 1 of 1

--Use Reflective

PostPosted: Wed Aug 01, 2012 4:47 pm
by Development@SIB


This article is outdated - please use our new system at

https://doc.sibvisions.com




JVx' Reflective class allows you to create class instances or call methods via reflection but without directly using java.lang.reflect.

How it works?

We have a short example for you. Our class:

Code: Select all
package com.sibvisions.app;

public class MyClient
{
   public Result doUpload(String pKey, String pValue)
   {
       ...
   }

   public Result doDownload(String pPath)
   {
       ...
   }

   public Result doDownload(File pFile)
   {
       ...
   }

}

A simple method call:

Code: Select all
Object oClient = Reflective.construct("com.sibvisions.app.MyClient");

Reflective.call(oClient, "doUpload", "Key", "Value");

Now we call doDownload:

Code: Select all
Object oClient = Reflective.construct("com.sibvisions.app.MyClient");

//no problem
Reflective.call(oClient, "doDownload", "file.txt");

//without Parameter, maybe we call the wrong method
Reflective.call(oClient, "doDownload", new Reflective.Parameter(String.class, null));

As you can see, we use the class Reflective.Parameter. It is useful if you have methods with the same name, same number of parameters but with different parameter types. Without this class, it is not guaranteed that the desired method is called.