Example usage for java.lang.reflect Method invoke

List of usage examples for java.lang.reflect Method invoke

Introduction

In this page you can find the example usage for java.lang.reflect Method invoke.

Prototype

@CallerSensitive
@ForceInline 
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Source Link

Document

Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static boolean hasNavigationBar(Context activity) {
    boolean hasNavigationBar = false;
    Resources rs = activity.getResources();
    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        hasNavigationBar = rs.getBoolean(id);
    }//from  w  w  w. ja  v  a2  s .  c  om
    try {
        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
        Method m = systemPropertiesClass.getMethod("get", String.class);
        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
    } catch (Exception e) {

    }

    return hasNavigationBar;
}

From source file:Main.java

private static void setterValue(PropertyDescriptor property, Object mapValue, Object object)
        throws InvocationTargetException, IllegalAccessException, ParseException {
    Method setter = property.getWriteMethod();
    if (mapValue == null) {
        setter.invoke(object, mapValue);
        return;/* www  .j av a  2s. com*/
    }

    Class propertyType = property.getPropertyType();
    String type = propertyType.getName();
    String value = mapValue.toString();

    if (type.equals("java.lang.String")) {
        setter.invoke(object, value);
    } else if (type.equals("java.lang.Integer")) {
        setter.invoke(object, Integer.parseInt(value));
    } else if (type.equals("java.lang.Long")) {
        setter.invoke(object, Long.parseLong(value));
    } else if (type.equals("java.math.BigDecimal")) {
        setter.invoke(object, BigDecimal.valueOf(Double.parseDouble(value)));
    } else if (type.equals("java.math.BigInteger")) {
        setter.invoke(object, BigInteger.valueOf(Long.parseLong(value)));
    } else if (type.equals("java.util.Date")) {
        setter.invoke(object, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value));
    } else if (type.equals("java.lang.Boolean")) {
        setter.invoke(object, Boolean.valueOf(value));
    } else if (type.equals("java.lang.Float")) {
        setter.invoke(object, Float.parseFloat(value));
    } else if (type.equals("java.lang.Double")) {
        setter.invoke(object, Double.parseDouble(value));
    } else if (type.equals("java.lang.byte[]")) {
        setter.invoke(object, value.getBytes());
    } else {
        setter.invoke(object, value);
    }
}

From source file:Main.java

public static Object invoke(Object receiver, Object defaultValue, Method method, Object... args) {
    if (method == null)
        return defaultValue;
    try {//from   w  w w . j a  va 2s.  c  o m
        return method.invoke(receiver, args);
    } catch (Exception e) {
        Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName());
    }
    return defaultValue;
}

From source file:Main.java

public static void setPopupWindowModal(PopupWindow popupWindow, boolean modal) {
    try {// www . ja  v a2  s .  co  m
        Method method = PopupWindow.class.getDeclaredMethod("setTouchModal", boolean.class);
        method.setAccessible(true);
        method.invoke(popupWindow, modal);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static boolean setSystemUiVisibility(View view, String flagName) {
    try {//from   w  ww  . j a va2s  . c o m
        Method setUiMethod = View.class.getMethod("setSystemUiVisibility", int.class);
        Field flagField = View.class.getField(flagName);
        setUiMethod.invoke(view, flagField.get(null));
        return true;
    } catch (Exception ex) {
        return false;
    }
}

From source file:Main.java

public static Object invokeMethod(Object object, String methodName, Class param0, Object arg0) {
    if (null != object) {
        try {/*from   www  . j a va2 s. c o  m*/
            Class clz = (object instanceof Class ? (Class) object : object.getClass());
            Method method = getMethod(clz, methodName, param0);
            return method.invoke(object, arg0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

static public boolean setPin(Class<? extends BluetoothDevice> btClass, BluetoothDevice btDevice, String str)
        throws Exception {
    try {//from   ww  w. j  a  v  a2 s.  c o m
        Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
        Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] { str.getBytes() });
        Log.e("returnValue", "" + returnValue);
    } catch (SecurityException e) {
        // throw new RuntimeException(e.getMessage());  
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // throw new RuntimeException(e.getMessage());  
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block  
        e.printStackTrace();
    }
    return true;

}

From source file:Main.java

static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception {
    try {//from  w w w .j  ava  2 s  .  co  m
        Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
        Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] { str.getBytes() });
        Log.v("tag", "++++++++++++" + returnValue);
        Log.e("returnValue", "" + returnValue);
    } catch (SecurityException e) {
        // throw new RuntimeException(e.getMessage());
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // throw new RuntimeException(e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;

}

From source file:Main.java

/**
 * Synchronises the addition of an object to a many2many relationship. For example, if you have: 
 * Product.orders and Order.products, you can implement:
 * /*from   ww w.  java2s .c  om*/
 * <pre>boolean Product.addOrder( Order prod )</pre>
 * 
 * as: 
 * 
 * <pre>return addManyToMany ( this, order, "addProduct", this.orders ); // or this.getOrders()</pre>
 *  
 * where this.orders is the set internal to Product that backs the corresponding
 * relation. This call invokes this.orders.add ( order ) and, in case this operation returns true (i.e., the order
 * was not already inside the collection), it invokes order.addProduct ( this ) too. 
 * 
 * You should implement Order.product() in a dual way. The check that the element was added to the set avoids 
 * infinite loops. 
 *  
 * @param obj the object to which added is added
 * @param added the added object
 * @param inverseRelationAddMethod the name of the method to be called to add obj to added, using the inverse side of 
 * the relation obj-added relation.
 * @param internalCollection the collection for the side from obj to added to which added is actually added
 * @return true if the added was really added to obj.
 */
public static <O, A> boolean addMany2Many(O obj, A added, String inverseRelationAddMethod,
        Collection<A> internalCollection) {
    Exception theEx = null;
    try {
        if (!internalCollection.add(added))
            return false;
        Method inverseAdder = added.getClass().getMethod(inverseRelationAddMethod, obj.getClass());
        inverseAdder.invoke(added, obj);
    } catch (NoSuchMethodException ex) {
        theEx = ex;
    } catch (SecurityException ex) {
        theEx = ex;
    } catch (IllegalAccessException ex) {
        theEx = ex;
    } catch (IllegalArgumentException ex) {
        theEx = ex;
    } catch (InvocationTargetException ex) {
        theEx = ex;
    } finally {
        if (theEx != null)
            throw new IllegalArgumentException("Internal error: " + theEx.getMessage(), theEx);
    }
    return true;
}

From source file:Main.java

public static Bitmap createVideoThumbnail(String filePath) {
    // MediaMetadataRetriever is available on API Level 8
    // but is hidden until API Level 10
    Class<?> clazz = null;/*  ww w.  j  a  v a 2  s. co  m*/
    Object instance = null;
    try {
        clazz = Class.forName("android.media.MediaMetadataRetriever");
        instance = clazz.newInstance();

        Method method = clazz.getMethod("setDataSource", String.class);
        method.invoke(instance, filePath);

        // The method name changes between API Level 9 and 10.
        if (Build.VERSION.SDK_INT <= 9) {
            return (Bitmap) clazz.getMethod("captureFrame").invoke(instance);
        } else {
            byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance);
            if (data != null) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                if (bitmap != null)
                    return bitmap;
            }
            return (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance);
        }
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } catch (InstantiationException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (InvocationTargetException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (ClassNotFoundException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (NoSuchMethodException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (IllegalAccessException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } finally {
        try {
            if (instance != null) {
                clazz.getMethod("release").invoke(instance);
            }
        } catch (Exception ignored) {
        }
    }
    return null;
}