Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * @(#)Methods.java
   *
   * Copyright (c) 2011 Werner Randelshofer, Goldau, Switzerland.
   * All rights reserved.
   * 
   * You may not use, copy or modify this file, except in compliance with the
   * license agreement you entered into with Werner Randelshofer.
   * For details see accompanying license terms.
  */
 
 package org.monte.media.util;
 
Methods contains convenience methods for method invocations using java.lang.reflect.

Author(s):
Werner Randelshofer
Version:
$Id: Methods.java 299 2013-01-03 07:40:18Z werner $
 
 
 @SuppressWarnings("unchecked")
 public class Methods {
    
Prevent instance creation.
 
     private Methods() {
     }
    
    
Invokes the specified accessible parameterless method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
Returns:
NoSuchMethodException if the method does not exist or is not accessible.
 
     public static Object invoke(Object objString methodName)
     throws NoSuchMethodException {
         try {
             Method method =  obj.getClass().getMethod(methodName,  new Class[0]);
             Object result = method.invoke(objnew Object[0]);
             return result;
         } catch (IllegalAccessException e) {
             throw new NoSuchMethodException(methodName+" is not accessible");
         } catch (InvocationTargetException e) {
             // The method is not supposed to throw exceptions
             throw new InternalError(e.getMessage());
         }
     }
    
Invokes the specified accessible method with a string parameter if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
stringParameter The String parameter
Returns:
NoSuchMethodException if the method does not exist or is not accessible.
 
     public static Object invoke(Object objString methodNameString stringParameter)
     throws NoSuchMethodException {
         try {
             Method method =  obj.getClass().getMethod(methodName,  new Class[] { String.class });
             Object result = method.invoke(objnew Object[] { stringParameter });
             return result;
         } catch (IllegalAccessException e) {
             throw new NoSuchMethodException(methodName+" is not accessible");
         } catch (InvocationTargetException e) {
             // The method is not supposed to throw exceptions
             throw new InternalError(e.getMessage());
         }
     }
    
    
Invokes the specified accessible parameterless method if it exists.

Parameters:
clazz The class on which to invoke the method.
methodName The name of the method.
Returns:
NoSuchMethodException if the method does not exist or is not accessible.
 
     public static Object invokeStatic(Class clazzString methodName)
     throws NoSuchMethodException {
         try {
             Method method =  clazz.getMethod(methodName,  new Class[0]);
             Object result = method.invoke(nullnew Object[0]);
             return result;
         } catch (IllegalAccessException e) {
             throw new NoSuchMethodException(methodName+" is not accessible");
         } catch (InvocationTargetException e) {
             // The method is not supposed to throw exceptions
             throw new InternalError(e.getMessage());
         }
     }
    
Invokes the specified accessible parameterless method if it exists.

Parameters:
clazz The class on which to invoke the method.
methodName The name of the method.
Returns:
NoSuchMethodException if the method does not exist or is not accessible.
    public static Object invokeStatic(String clazzString methodName)
    throws NoSuchMethodException {
        try {
            return invokeStatic(Class.forName(clazz), methodName);
        } catch (ClassNotFoundException e) {
            throw new NoSuchMethodException("class "+clazz+" not found");
        }
    }
    
Invokes the specified parameterless method if it exists.

Parameters:
clazz The class on which to invoke the method.
methodName The name of the method.
type The parameter type.
value The parameter value.
Returns:
NoSuchMethodException if the method does not exist or is not accessible.
    public static Object invokeStatic(Class clazzString methodNameClass typeObject value)
    throws NoSuchMethodException {
        return invokeStatic(clazz,methodName,new Class[]{type},new Object[]{value});
    }
    
Invokes the specified parameterless method if it exists.

Parameters:
clazz The class on which to invoke the method.
methodName The name of the method.
types The parameter types.
values The parameter values.
Returns:
NoSuchMethodException if the method does not exist or is not accessible.
    public static Object invokeStatic(Class clazzString methodNameClass[] typesObject[] values)
    throws NoSuchMethodException {
        try {
            Method method =  clazz.getMethod(methodName,  types);
            Object result = method.invoke(nullvalues);
            return result;
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName+" is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }
    
Invokes the specified parameterless method if it exists.

Parameters:
clazz The class on which to invoke the method.
methodName The name of the method.
types The parameter types.
values The parameter values.
Returns:
NoSuchMethodException if the method does not exist or is not accessible.
    public static Object invokeStatic(String clazzString methodName,
    Class[] typesObject[] values)
    throws NoSuchMethodException {
        try {
            return invokeStatic(Class.forName(clazz), methodNametypesvalues);
        } catch (ClassNotFoundException e) {
            throw new NoSuchMethodException("class "+clazz+" not found");
        }
    }
    
Invokes the specified parameterless method if it exists.

Parameters:
clazz The class on which to invoke the method.
methodName The name of the method.
types The parameter types.
values The parameter values.
defaultValue The default value.
Returns:
The return value of the method or the default value if the method does not exist or is not accessible.
    public static Object invokeStatic(String clazzString methodName,
    Class[] typesObject[] valuesObject defaultValue) {
        try {
            return invokeStatic(Class.forName(clazz), methodNametypesvalues);
        } catch (ClassNotFoundException e) {
            return defaultValue;
        } catch (NoSuchMethodException e) {
            return defaultValue;
        }
    }
    
    
Invokes the specified getter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
defaultValue This value is returned, if the method does not exist.
Returns:
The value returned by the getter method or the default value.
    public static int invokeGetter(Object objString methodNameint defaultValue) {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[0]);
            Object result = method.invoke(objnew Object[0]);
            return ((Integerresult).intValue();
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }
    
Invokes the specified getter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
defaultValue This value is returned, if the method does not exist.
Returns:
The value returned by the getter method or the default value.
    public static long invokeGetter(Object objString methodNamelong defaultValue) {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[0]);
            Object result = method.invoke(objnew Object[0]);
            return ((Longresult).longValue();
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }
    
Invokes the specified getter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
defaultValue This value is returned, if the method does not exist.
Returns:
The value returned by the getter method or the default value.
    public static boolean invokeGetter(Object objString methodNameboolean defaultValue) {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[0]);
            Object result = method.invoke(objnew Object[0]);
            return ((Booleanresult).booleanValue();
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }
    
Invokes the specified getter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
defaultValue This value is returned, if the method does not exist.
Returns:
The value returned by the getter method or the default value.
    public static Object invokeGetter(Object objString methodNameObject defaultValue) {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[0]);
            Object result = method.invoke(objnew Object[0]);
            return result;
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }
    
Invokes the specified getter method if it exists.

Parameters:
clazz The object on which to invoke the method.
methodName The name of the method.
defaultValue This value is returned, if the method does not exist.
Returns:
The value returned by the getter method or the default value.
    public static boolean invokeStaticGetter(Class clazzString methodNameboolean defaultValue) {
        try {
            Method method =  clazz.getMethod(methodName,  new Class[0]);
            Object result = method.invoke(nullnew Object[0]);
            return ((Booleanresult).booleanValue();
        } catch (NoSuchMethodException e) {
            return defaultValue;
        } catch (IllegalAccessException e) {
            return defaultValue;
        } catch (InvocationTargetException e) {
            return defaultValue;
        }
    }
    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static Object invoke(Object objString methodNameboolean newValue)
    throws NoSuchMethodException {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[] { .} );
           return method.invoke(objnew Object[] { newValue});
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName+" is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }
    
Invokes the specified method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static Object invoke(Object objString methodNameint newValue)
    throws NoSuchMethodException {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[] { .} );
            return method.invoke(objnew Object[] { newValue});
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName+" is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }
    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static Object invoke(Object objString methodNamefloat newValue)
    throws NoSuchMethodException {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[] { .} );
            return method.invoke(objnew Object[] { new Float(newValue)});
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName+" is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }
    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static Object invoke(Object objString methodNameClass clazzObject newValue)
    throws NoSuchMethodException {
        try {
            Method method =  obj.getClass().getMethod(methodName,  new Class[] { clazz } );
            return method.invoke(objnew Object[] { newValue});
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName+" is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }
    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static Object invoke(Object objString methodNameClass[] clazzObject... newValue)
    throws NoSuchMethodException {
        try {
            Method method =  obj.getClass().getMethod(methodName,  clazz );
            return method.invoke(objnewValue);
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName+" is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            InternalError error = new InternalError(e.getMessage());
            error.initCause((e.getCause() != null) ? e.getCause() : e);
            throw error;
        }
    }
    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static void invokeIfExists(Object objString methodName) {
        try {
             invoke(objmethodName);
        } catch (NoSuchMethodException e) {
           // ignore
        }
    }
    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static void invokeIfExists(Object objString methodNamefloat newValue) {
        try {
            invoke(objmethodNamenewValue);
        } catch (NoSuchMethodException e) {
            // ignore
        }
    }
    
Invokes the specified method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static void invokeIfExists(Object objString methodNameboolean newValue) {
        try {
             invoke(objmethodNamenewValue);
        } catch (NoSuchMethodException e) {
            // ignore
        }
    }
    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static void invokeIfExists(Object objString methodNameClass clazzObject newValue) {
        try {
             invoke(objmethodNameclazznewValue);
        } catch (NoSuchMethodException e) {
           // ignore
        }
    }

    
Invokes the specified setter method if it exists.

Parameters:
obj The object on which to invoke the method.
methodName The name of the method.
    public static void invokeIfExistsWithEnum(Object objString methodNameString enumClassNameString enumValueName) {
        try {
            Class enumClass = Class.forName(enumClassName);
            Object enumValue = invokeStatic("java.lang.Enum""valueOf"new Class[] {Class.classString.class},
                    new Object[] {enumClassenumValueName}
            );
            invoke(objmethodNameenumClassenumValue);
        } catch (ClassNotFoundException e) {
            // ignore
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // ignore
            e.printStackTrace();
        }
    }
New to GrepCode? Check out our FAQ X