Backendless.UserService does not support dynamically invoke

I’m not sure if this is backendless issue or I am missing something. When I dynamically invoke BackendlessUser’s methods such as “setPassword”, “setProperty”, it works:
backendlessUserClass = (Class<?>) dexClassLoader.loadClass(“com.backendless.BackendlessUser”);
instance = backendlessUserClass.newInstance();

final Method setPasswordMethod = backendlessUserClass.getMethod(“setPassword”, String.class);
setPasswordMethod.invoke(instance, “mypassword”);

final Method setUserMethod = backendlessUserClass.getMethod(“setProperty”, String.class, Object.class);
setUserMethod.invoke(instance, “name”, “John”);
However, when i invoke Backendless.UserService’s “login”, the application fails:

userServiceClass = dexClassLoader.loadClass(“com.backendless.UserService”);
//final Method getInstanceMethod = userServiceClass.getDeclaredMethod(“getInstance”, null);instance = userServiceClass.newInstance(); //failed here//I looked at the UserService and there is no public constructor in UserServicefinal Method loginMethod = userServiceClass.getDeclaredMethod(“login”, String.class, String.class, boolean.class);getInstanceMethod.invoke(instance, “John”, “mypassword”, true);Am I missing something or UserService has issue?Thank you

Hi Key,

Is there a particular reason you need to invoke Backendless API through reflection?

Mark

Thank you Mark. I plan to use Reflection for several APIs so I can target devices running different versions of Android with a simple application package. Also this is a convenient way to execute code not installed as part of an application package to leverage new APIs not only with Backendless.

Key

Thanks, Key. Rather than calling “newInstance()” on com.backendless.UserService, try loading the “com.backendless.Backendless” class first, and from there getting the “UserService” field. The reason is you do not want to skip the static initializer block in the Backendless class.

Thank you Mark, I will try that.

Mark,

I try to use com.backendless.Persistence to save() and find() with the similar approach that you suggested. The “save” method works great without invoke the “of” but the “find” method I got exception method not found whether I invoke the “of” or not. Any suggestions, please see my snippet code for both below. Thank you.

//save works great
… (some other code here to load)
Field persistenceField = backendlessClass.getDeclaredField(“Persistence”);
persistenceField.setAccessible(true);
persistenceObject = persistenceField.get(backendlessClass);
persistenceClass = persistenceObject.getClass();
Method saveMethod = persistenceClass.getDeclaredMethod(“save”, Object.class);
saveMethod.invoke(persistenceObject, backendlessUserObject);

//find throws exception
… (some other code here to load)
Field persistenceField = backendlessClass.getDeclaredField(“Persistence”);
persistenceField.setAccessible(true);
persistenceObject = persistenceField.get(backendlessClass);
persistenceClass = persistenceObject.getClass();
Class<?> dataQueryClass = classLoader.loadClass(“com.backendless.persistence.BackendlessDataQuery”);
Object dataQueryObject = dataQueryClass.newInstance();
Method whereMethod = dataQueryClass.getMethod(“setWhereClause”, String.class);
String whereClause = “objectId = '” + id + “’” ;
setWhereClauseMethod.invoke(belDataQueryObject, whereClause);

Method ofMethod = persistenceClass.getDeclaredMethod(“of”, Person.class.getClass());
Object personClassObject = ofMethod.invoke(persistenceObject, Person.class);
Class<?> dataStoreFactoryClass = personClassObject.getClass();

Method findMethod = dataStoreFactoryClass .getDeclaredMethod(“find”, Object.class); //exception method find not found here
Object[] backendlessCollectionObject = (Object[]) findMethod.invoke(dataQueryObject);

Hi Key,

The reason you get that exception is because there is no find method with the signature you’re looking up:

find( Object obj )

Take a look at the list of available methods here:
https://github.com/Backendless/Android-SDK/blob/master/src/com/backendless/IDataStore.java

I think the one you want is this:
https://github.com/Backendless/Android-SDK/blob/master/src/com/backendless/IDataStore.java#L63

This means the second argument in the getDeclaredMethod must change.

Regards,
Mark

Thanks Mark.

sva