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

/**
 * check for getter method and return the value.{@link #getGetter(Object, String)}.
 *//*ww w .  j av a 2  s  .c o m*/
public static Object getProperty(Object bean, String property) {
    if (bean == null || TextUtils.isEmpty(property)) {
        return null;
    }
    try {
        Method getter = getGetter(bean, property);
        if (getter != null) {
            if (!getter.isAccessible()) {
                getter.setAccessible(true);
            }
            return getter.invoke(bean, new Object[0]);
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.weibo.api.motan.protocol.grpc.GrpcUtil.java

public static MethodDescriptor.Marshaller getJsonMarshaller(Class clazz) {
    try {//from  ww  w  . j a v  a 2 s .  c  o m
        if (MessageLite.class.isAssignableFrom(clazz)) {
            Method method = clazz.getDeclaredMethod("getDefaultInstance", null);
            Message message = (Message) method.invoke(null, null);
            return jsonMarshaller(message);
        }
    } catch (Exception ignore) {
    }
    return null;
}

From source file:de.beyondjava.angularFaces.components.puiSync.JSONUtilities.java

public static String writeObjectToJSONString(Object bean) {
    if (null != jackson) {
        try {/*from   w w  w.  j  a v  a2  s . c  om*/
            Method method = jackson.getClass().getMethod("writeValueAsString", Object.class);
            if (null != method) {
                return (String) method.invoke(jackson, bean);
            }
        } catch (ReflectiveOperationException e) {
        }

    }
    if (null != gson) {
        try {
            Method method = gson.getClass().getMethod("toJson", Object.class);
            if (null != method) {
                return (String) method.invoke(gson, bean);
            }
        } catch (ReflectiveOperationException e) {
        }
    }
    return null;
}

From source file:Main.java

/**
 * Convert a translucent themed Activity
 * {@link android.R.attr#windowIsTranslucent} back from opaque to
 * translucent following a call to//from   w ww  .j  av a 2  s  . com
 * {@link #convertActivityFromTranslucent(Activity)} .
 * <p/>
 * Calling this allows the Activity behind this one to be seen again. Once
 * all such Activities have been redrawn
 * <p/>
 * This call has no effect on non-translucent activities or on activities
 * with the {@link android.R.attr#windowIsFloating} attribute.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("rawtypes")
public static void convertActivityToTranslucent(Activity activity) {

    try {
        Class[] t = Activity.class.getDeclaredClasses();
        Class<?> translucentConversionListenerClazz = null;
        Class[] method = t;
        int len$ = t.length;

        for (int i$ = 0; i$ < len$; ++i$) {
            Class<?> clazz = method[i$];
            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                translucentConversionListenerClazz = clazz;
                break;
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Method var8 = Activity.class.getDeclaredMethod("convertToTranslucent",
                    translucentConversionListenerClazz, ActivityOptions.class);
            var8.setAccessible(true);
            var8.invoke(activity, new Object[] { null, null });
        } else {
            Method var8 = Activity.class.getDeclaredMethod("convertToTranslucent",
                    translucentConversionListenerClazz);
            var8.setAccessible(true);
            var8.invoke(activity, new Object[] { null });
        }
    } catch (Throwable e) {
    }
}

From source file:Main.java

public static BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
    UUID uuid = UUID.fromString(SERIAL_SERVICE_UUID);
    BluetoothSocket socket = null;/*w  w  w.j  a v a  2  s . c o m*/
    if (Build.VERSION.SDK_INT >= 10) {
        try {
            final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord",
                    new Class[] { UUID.class });
            System.out.println("Performing invoke");
            socket = (BluetoothSocket) m.invoke(device, uuid);
            System.out.println("Invoke done");
            System.out.println(socket);
        } catch (Exception e) {
            Log.e("error", "Could not create Insecure RFComm Connection", e);
        }
    } else {
        //TODO, support older versions with secure connection
    }
    return socket;
}

From source file:com.knowbout.hibernate.HibernateUtil.java

public static synchronized void addDeleteHandler(DeleteHandler<?> deleteHandler) {
    if (deleteHandlers == null) {
        deleteHandlers = new LinkedList<DeleteHandler<?>>();
        EventListeners els = config.getEventListeners();
        DeleteEventListener[] dels = els.getDeleteEventListeners();
        DeleteEventListener[] newDels = new DeleteEventListener[dels.length + 1];
        System.arraycopy(dels, 0, newDels, 1, dels.length);
        newDels[0] = new DeleteEventListener() {

            private static final long serialVersionUID = 0L;

            public void onDelete(DeleteEvent event) throws HibernateException {
                Object deleted = event.getObject();
                for (DeleteHandler<?> dh : deleteHandlers) {
                    Class<?> handledType = (Class) ((ParameterizedType) dh.getClass().getGenericSuperclass())
                            .getActualTypeArguments()[0];
                    if (handledType.isInstance(deleted)) {
                        /* The following should work but won't compile, so I'll invoke through reflection
                        dh.onDelete(handledType.cast(deleted));
                        *///  ww  w  . java 2s. co  m
                        Class clazz = dh.getClass();
                        try {
                            Method method = clazz.getMethod("onDelete", new Class[] { handledType });
                            method.invoke(dh, new Object[] { deleted });
                        } catch (Exception e) {
                            throw new Error("Bug in HibernateUtil.addDeleteHandler()");
                        }
                    }
                }
            }

        };
    }
    deleteHandlers.add(0, deleteHandler);
}

From source file:com.wxxr.nirvana.ContainerAccess.java

/**
 * Returns an attribute from a context./*from  www  .  java  2 s.  c o  m*/
 *
 * @param context
 *            The context object to use.
 * @param attributeName
 *            The name of the attribute to search for.
 * @return The object, that is the value of the specified attribute.
 */
private static Object getAttribute(Object context, String attributeName) {
    try {
        Class<?> contextClass = context.getClass();
        Method attrMethod = contextClass.getMethod("getAttribute", String.class);
        return attrMethod.invoke(context, attributeName);
    } catch (Exception e) {
        LOG.warn("Unable to retrieve container from specified context: '" + context + "'", e);
        return null;
    }
}

From source file:gov.nih.nci.cabig.caaers.web.search.CommandToSQL.java

public static void invokeMethod(Object query, String joinMethodName, Class[] par, Object[] obj)
        throws Exception {
    Method mthd = query.getClass().getMethod(joinMethodName, par);
    mthd.invoke(query, obj);
}

From source file:net.sourceforge.atunes.utils.ReflectionUtils.java

/**
 * Invokes a method of an object with given arguments
 * //ww  w  .ja v a  2  s  . c  o m
 * @param instance
 * @param method
 * @param arguments
 * @return result of invoke
 */
public static Object invoke(final Object instance, final Method method, final Object... arguments) {
    if (method != null) {
        try {
            return method.invoke(instance, arguments);
        } catch (IllegalArgumentException e) {
            Logger.error(e);
        } catch (IllegalAccessException e) {
            Logger.error(e);
        } catch (InvocationTargetException e) {
            Logger.error(e);
        }
    }
    return null;
}

From source file:com.googlecode.xtecuannet.framework.catalina.manager.tomcat.constants.Constants.java

public static String invokeStringFunction(Method m, String[] params) {

    String out = null;/*from w ww  .j av a2  s . c  o m*/

    try {

        out = (String) m.invoke(null, (Object[]) params);

    } catch (Exception e) {
        logger.error("Error invoking function: " + m.getName() + " with params: " + Arrays.asList(params), e);
    }
    return out;
}