Determine if a Method is a `RemotableViewMethod` - java

I am building a Widget which contains a ProgressBar. If the Widget is computing I set the visibility of that ProgressBar to VISIBLE, and to INVISIBILE if all computings stopped. There should be no Problem, because the setVisibility is documented as RemotableViewMethod. However some guys at HTC seem to forget it, (i.e on the Wildfire S), so a call to RemoteViews.setVisibility will result in a crash. For this reason I try to implement a check, if setVisibility is really callable. I have writen this Method for it:
private boolean canShowProgress(){
LogCat.d(TAG, "canShowProgress");
Class<ProgressBar> barclz = ProgressBar.class;
try {
Method method = barclz.getMethod("setVisibility", new Class[]{int.class});
Annotation[] anot = method.getDeclaredAnnotations();
return anot.length > 0;
} catch (SecurityException e) {
LogCat.stackTrace(TAG, e);
} catch (NoSuchMethodException e) {
LogCat.stackTrace(TAG, e);
}
return false;
}
This will work, but is REALLY ugly as it will return `True´ if ANY Annotiation is present. I looked, how RemoteView itself is doing the lookup and found this:
if (!method.isAnnotationPresent(RemotableViewMethod.class)) {
throw new ActionException("view: " + klass.getName()
+ " can't use method with RemoteViews: "
+ this.methodName + "(" + param.getName() + ")");
}
But i could't do the same, because the Class RemotableViewMethod is not accsesible through the sdk. How to know if it is accesible or not?

By Writing my question I had the Idea to lookup for the class by its Name, and it worked.
So I updated my Method to the following:
private boolean canShowProgress(){
LogCat.d(TAG, "canShowProgress");
Class<ProgressBar> barclz = ProgressBar.class;
try {
Method method = barclz.getMethod("setVisibility", new Class[]{int.class});
Class c = null;
try {
c = Class.forName("android.view.RemotableViewMethod");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (this.showProgress= (c != null && method.isAnnotationPresent(c)));
} catch (SecurityException e) {
LogCat.stackTrace(TAG, e);
} catch (NoSuchMethodException e) {
LogCat.stackTrace(TAG, e);
}
return false;
}
which works flawlessly

Related

Do you need a return type method to be handled in front end?

So I am trying to get more insight on Java methods as I am still new to all this. And in my method type I declared as below:
public int insert_url(long nodeid,String url,String startdt,String enddt,int enable) {
try {
// UrlLink attr = em.find(UrlLink.class,n);
String sql="INSERT INTO urllink(NODEID,URL,STARTDT,ENDDT,ENABLE) VALUES("+nodeid+",'"+url+"','"+startdt+"','"+enddt+"',"+enable+")";
em.createNativeQuery(sql).executeUpdate();
return 1;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
And in my front end, I called it simply like below:
try {
fileFacade.insert_url(nd.getNodeid(), "f0=" + nd.getNodeid() + "&ts=" + hash, currentDate, defaultDate, 1);
} catch (Exception e) {
// error should be handled
}
Initially, I was using void method rather than int. My question is if I am not using a return method,can it be handled in the front end?
In the even that the end user encounters any error, they ought to know an error occurred.

Overwrite Object properties with properties of other Object using reflection

Im trying to do something that might seem a bit unorthodox, however this is due to some limitations in a framework that im using.
Basically my case is this:
1: I have an object that has several fields, all with default values.
2: I have an other object that is initialized and has all values that I want to have, in the new object.
Im trying to do this with reflection, so looping over all public setMethods, finding all getMethods that seem to be matching from the other object, invoking them and invoke the other setMethod with the value over the invoked setMethod.
This is what I came up with so far:
java.lang.reflect.Method[] publicMethods1 = newlabels.getClass().getMethods();
for(int i=0; i<publicMethods1.length; i++){
if (publicMethods1[i].getName().startsWith("set")){
String setname = publicMethods1[i].getName();
String getname = "get"+setname.substring(3, setname.length());
try {
java.lang.reflect.Method getMethod = labels.getClass().getMethod(getname, null);
publicMethods1[i].invoke(newlabels, getMethod.invoke(labels, null));
} catch (NoSuchMethodException e1) {
//System.out.println("Couldnot find a method with name: "+getname);
} catch (SecurityException e1) {
//System.out.println("Security exception occured");
} catch (IllegalAccessException e1) {
//System.out.println("IllegalAccessException");
} catch (IllegalArgumentException e1) {
//System.out.println("IllegalArgumentException");
} catch (InvocationTargetException e1) {
//System.out.println("InvocationTargetException");
}
}
}
This unfortunately isn't working, its not giving errors either (unless a getter wasnt found, but ill take that).
I looked around on the internet and found somewhat similar in this method:
/**
* Only works for methods with equally named getters and setters
*
* 1. Get all declared methods of obj1 and find all setters
* 2. Get all declared methods of obj2 and find all getters
* 3. Find which setter belongs to which getter
* 4. Set the value of obj1.setter with obj2.getter
*
* #param obj1
* #param obj2
*/
public static void runAllSettersWithGetters(Object obj1, Object obj2) {
ArrayList<Method> setters = findSetters(obj1);
ArrayList<Method> getters = findGetters(obj2);
for(int s=0; s<setters.size(); s++){
String setmethodname = setters.get(s).getName();
String whattoset = setmethodname.substring(3);
for(int g=0; g<getters.size(); g++){
boolean isboolean = false;
boolean match = false;
if(getters.get(g).getReturnType().equals(Boolean.TYPE)){
isboolean = true;
}
String getmethodname = getters.get(g).getName();
String whattoget = getmethodname.substring(3);
if(whattoset.equalsIgnoreCase(whattoget)){
match = true;
}else{
//might start with is instead of get
whattoget = getmethodname.substring(2);
if(whattoset.equalsIgnoreCase(whattoget)){
match = true;
}
}
if(match){
try {
setters.get(s).invoke(obj1, getters.get(g).invoke(obj2));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
So I tried it, because it seems better, however in the end it gives the same not working solution.
Can anyone help me with this?

Java: check if a class exists and call a specific method if it exists

Is there a way to do the following? Check if a class exists (in the same package) and if it does exist, check if a particular method exists, and if so, calling it?
Say that I have class X. In some method of class X, I want to do the following:
if (class Y exists) { //Maybe use Class.forName("Y")?
if ( Y has method a(String, String) ) {
call Y.a("hello", "world");
}
}
Is such a thing possible? And is doing such a thing reasonable? Thanks.
Is such a thing possible? And is doing such a thing reasonable?
Thanks.
Of course it is possible.
If you develop a program or a library that has to discover dynamically some classes, it is a very reasonable thing.
If it is not the case, it could not be.
If your need makes sense, you should ask you an additional question : should you invoke a static or instance method ?
Here is a sample example with both solutions :
ReflectionClass that contains the logic using reflection :
import java.lang.reflect.Method;
public class ReflectionCalls {
public static void main(String[] args) {
new ReflectionCalls();
}
public ReflectionCalls() {
callMethod(true);
callMethod(false);
}
private void callMethod(boolean isInstanceMethod) {
String className = "DiscoveredClass";
String staticMethodName = "methodStatic";
String instanceMethodName = "methodInstance";
Class<?>[] formalParameters = { int.class, String.class };
Object[] effectiveParameters = new Object[] { 5, "hello" };
String packageName = getClass().getPackage().getName();
try {
Class<?> clazz = Class.forName(packageName + "." + className);
if (!isInstanceMethod) {
Method method = clazz.getMethod(staticMethodName, formalParameters);
method.invoke(null, effectiveParameters);
}
else {
Method method = clazz.getMethod(instanceMethodName, formalParameters);
Object newInstance = clazz.newInstance();
method.invoke(newInstance, effectiveParameters);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DiscoveredClass (the class we manipulate in the example)
package reflectionexp;
public class DiscoveredClass {
public static void methodStatic(int x, String string) {
System.out.println("static method with " + x + " and " + string);
}
public void methodInstance(int x, String string) {
System.out.println("instance method with " + x + " and " + string);
}
}
Output :
instance method with 5 and hello
static method with 5 and hello
Yes, this can be done. I've created a Test class in the same Package as the current class.
import java.lang.reflect.Method;
public class Sample {
public static void main(String[] args) {
Class<?> clazz = null;
try {
clazz = Class.forName("Test");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (clazz == null) {
System.out.println("class not found. Go eat some waffles and correct the name");
return;
}
Method m = null;
try {
m = clazz.getMethod("foo", null);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (m == null) {
System.out.println("method not found. Go eat some waffles and correct the name");
return;
}
Test t;
try {
t = (Test) clazz.newInstance();
m.invoke(t, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Test {
static {
System.out.println("test...");
}
public void foo() {
System.out.println("foo");
}
}
O/P :
test...
foo
You can use Class.forName:
try {
Class yourClass = Class.forName( "classname" );
Object o = yourClass.newInstance();
} catch( ClassNotFoundException e ) {
//Throw error or whatever
}
To check if a method exists you could use the NoSuchMethodError e in a try/catch
You can do this using reflection, however it isnt really practical unless you are trying to access classes that potentially will not be present at runtime or if you are trying to access private or hidden fields. Example below.
public static void reflectionDemo(){
//Here we attempt to get the common URI class
//If it is found, we attempt to get the create method
//We then invoke the create method and print the class name of the result.
try {
Class<?> uriClass = Class.forName("java.net.URI");
//getMethod(String name, Class<?>... args);
java.lang.reflect.Method create = uriClass.getMethod("create", String.class);
//The first parameter is null because this is a static method.
//invoke(Object target, Object... args);
System.out.println(create.invoke(null, "some/uri").getClass());
//Will print class java.net.URI
} catch (ClassNotFoundException e) {
// If class doesnt exist
e.printStackTrace();
} catch (NoSuchMethodException e) {
// If method doesnt exist
e.printStackTrace();
} catch (SecurityException e) {
// See Javadoc
e.printStackTrace();
} catch (IllegalAccessException e) {
// From invoke
e.printStackTrace();
} catch (IllegalArgumentException e) {
// From invoke
e.printStackTrace();
} catch (java.lang.reflect.InvocationTargetException e) {
// From invoke
e.printStackTrace();
}
}
To find whether a class exists, you can use the forName() method on Class.
To find whether a method exists, you can use the getMethod() method on Class.
Documentation here:
https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String)
https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethod(java.lang.String,%20java.lang.Class...)
For your class problem, you'd want to use code like:
try {
Class.forName("Y");
}
catch (ClassNotFoundException e) {
}
For your method problem, you'd want to use code like:
try {
Class.getMethod(a);
}
catch (NoSuchMethodException e) {
}
You can check if the Class exists with Class.forName("classname");
See this SO question: Check if class exists somewhere in package
If a method exists can be catched with NoSuchMethodError in your try/catch.
See this SO question: Check if method exists at Runtime in Java
try {
Object object = Class.forName("Y").newInstance();
object.a(String, String);
} catch( ClassNotFoundException | NoSuchMethodError ex) {
//do Something else
}

How to solve this chicken and egg issue?

I saw similar questions here on SO, but, I am asking this question based on this.
I have implemented the suggestion given by the accepted answer but, still I see two instances being created. The goal is that I want an instance to be created only on a particular method call. I can't use static method in an interface with Java 6.
The code I have tried
private static final Map<String, IRule> instancesMap = new Hashtable<String, IRule>();
#SuppressWarnings("unchecked")
public static <T extends IRule> T getRuleInstance(String clazz) {
try {
if (isInstanceCreated(clazz)) {
T type = (T) instancesMap.get(clazz);
if (logger.isDebugEnabled()) {
logger.debug("Found a cashed instance of " + clazz + ". Returning " + type);
}
return type;
} else {
Class<?> ruleObject = Class.forName(clazz);
Constructor<?> clazzConstructor = ruleObject.getDeclaredConstructor();
/**
* Hack encapsulation
*/
clazzConstructor.setAccessible(true);
IRule iRuleInstance = (IRule) clazzConstructor.newInstance();
if (logger.isDebugEnabled()) {
logger.debug("The instance of class " + clazz + " " + iRuleInstance + " has been created.");
}
instancesMap.put(clazz, iRuleInstance);
return (T) iRuleInstance.getInstance();
}
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException", e);
} catch (SecurityException e) {
logger.error("SecurityException", e);
} catch (NoSuchMethodException e) {
logger.error("NoSuchMethodException", e);
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException", e);
} catch (InvocationTargetException e) {
logger.error("InvocationTargetException", e);
} catch (InstantiationException e) {
logger.error("InstantiationException", e);
}
return null;
}
private static boolean isInstanceCreated(String clazz) {
return instancesMap.containsKey(clazz);
}
At the moment if there's a cache hit, you return the contents of the cache (instancesMap.get(clazz)). But if no hit, you cache one thing (instancesMap.put(clazz, iRuleInstance)) and return another (iRuleInstance.getInstance()). That doesn't make sense.
Don't call getInstance after adding to map, just return it:
instancesMap.put(clazz, iRuleInstance);
return (T) iRuleInstance;
Or, do call getInstance, but cache it:
(T) instance = iRuleInstance.getInstance();
instancesMap.put(clazz, instance);
return (T) instance;
Either way you must return what you cache so that it matches your logic for a cache hit.
Is there a particular reason why you need to have the concrete implementation of IRule?-- what methods are you going to be using from the implementation? Can't they be generalised into the interface itself? If this is the case, you could just have a map of Map<Class<? extends IRule>, IRule> to manage your instances.

Create an instance of a class and initialize all its fields

I want to create an instance of a class, but I also need to initialize also all its fields recursively.
The code you see related do the objectFactory is because some of this classes could be JAXB classes, so for every package there is an ObjectFactory with methods like createJaxbObject(....).
EDITED:
My final solutions is this one:
public Object getInstance(Class<?> instanceClass, Boolean simple,
String jaxbName) {
Object instance = null;
try {
if (instanceClass.isPrimitive())
return primitiveValues.get(instanceClass.getName());
if (List.class.isAssignableFrom(instanceClass))
return new ArrayList();
else if (instanceClass.isEnum())
return instanceClass.getEnumConstants()[0];
else if (instanceClass.isArray())
return java.lang.reflect.Array.newInstance(instanceClass, 1);
else if (BigInteger.class.isAssignableFrom(instanceClass))
return new BigInteger("0");
else if (instanceClass.equals(String.class))
return "";
else if (instanceClass.equals(Boolean.class))
return false;
else if (instanceClass.equals(EntityObjectStringType.class))
return new EntityObjectStringType();
else if (JAXBElement.class.isAssignableFrom(instanceClass)) {
try {
Method m = null;
Class<?> objFactoryClass = null;
Iterator<String> it = EditorServlet.objectFactories
.iterator();
Object of = null;
while (it.hasNext()) {
objFactoryClass = Class.forName(it.next());
of = objFactoryClass.getConstructor().newInstance();
m = getMethodFromObjectFactory(objFactoryClass,
jaxbName);
if (m != null)
if (m.getParameterTypes().length > 0)
break;
}
Object jaxbElement = getInstance(m.getParameterTypes()[0],
m.getParameterTypes()[0].getSimpleName());
return m.invoke(of, jaxbElement);
} catch (NoSuchMethodException e) {
logger.error("JAXB NoSuchMethodException");
}
} else
try {
logger.info("Costruttori per " + instanceClass.getName()
+ " " + instanceClass.getConstructors().length);
instance = instanceClass.getConstructor().newInstance();
} catch (NoSuchMethodException noSuchMethodException) {
logger.error("getConstructors NoSuchMethodException");
}
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException " + instanceClass.getName());
} catch (SecurityException e) {
logger.error("SecurityException " + instanceClass.getName());
} catch (InstantiationException e) {
logger.error("InstantiationException " + instanceClass.getName()
+ " " + instanceClass.isPrimitive());
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException " + instanceClass.getName());
} catch (InvocationTargetException e) {
logger.error("InvocationTargetException " + instanceClass.getName());
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException " + instanceClass.getName());
}
if (!simple) {
for (Field field : instanceClass.getDeclaredFields()) {
try {
Object fieldInstance = getInstance(field.getType(),
field.getName());
field.setAccessible(true);
field.set(instance, fieldInstance);
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException "
+ instanceClass.getName());
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException "
+ instanceClass.getName());
}
}
}
return instance;
}
If I could hazard a guess, you're calling your method recursively in your NoSuchMethodException catch.
Object of = getInstance(objFactoryClass);
If your recursive call keeps on not finding the method on:
Method m = getMethodFromObjectFactory(objFactoryClass, c);
... the method will call itself again, which should end with a StackOverflowError at some point.
Your recursion has no break point
Try to stop recursion where the class is primary type:
if (List.class.isAssignableFrom(c))
instance = new ArrayList();
else if (c.isEnum())
return c.getEnumConstants()[0]; //avoid stackoverflow error
else if(c.isPrimitive()) {
instance = c.getConstructor().newInstance();
// use must stop here
return instance;
} else{
instance = c.getConstructor().newInstance();
}
The isPrimitive will judge whether the class is primary type(int ,Integer,shor,Short,String...)

Categories