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

public static void enterA2Mode() {
    System.err.println("Orion::enterA2Mode");
    try {/* w  ww . j  a  v a2  s.  c o m*/

        Constructor RegionParamsConstructor = epdControllerRegionParamsClass.getConstructor(new Class[] {
                Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, epdControllerWaveClass, Integer.TYPE });

        Object localRegionParams = RegionParamsConstructor
                .newInstance(new Object[] { 0, 0, 600, 800, waveEnums[2], 16 }); // Wave = DU

        Method epdControllerSetRegionMethod = epdControllerClass.getMethod("setRegion",
                new Class[] { String.class, epdControllerRegionClass, epdControllerRegionParamsClass });
        epdControllerSetRegionMethod.invoke(null, new Object[] { "Orion", regionEnums[2], localRegionParams });

        Thread.sleep(100L);
        localRegionParams = RegionParamsConstructor
                .newInstance(new Object[] { 0, 0, 600, 800, waveEnums[3], 14 }); // Wave = A2
        epdControllerSetRegionMethod.invoke(null, new Object[] { "Orion", regionEnums[2], localRegionParams });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

private static void setBitmap(BitmapDrawable drawable, Bitmap bitmap) {
    try {// ww w .j  ava  2s  . c om
        Method method = getMethod(BitmapDrawable.class, "setBitmap", new Class[] { Bitmap.class });
        if (method != null) {
            method.setAccessible(true);
            method.invoke(drawable, bitmap);
        }
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setDUMode() {
    System.err.println("Orion::setDUMode");
    try {//from  w  w  w  .j av  a2  s.  co  m
        if (successful) {
            Constructor RegionParamsConstructor = epdControllerRegionParamsClass
                    .getConstructor(new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE,
                            epdControllerWaveClass, Integer.TYPE });

            Object localRegionParams = RegionParamsConstructor
                    .newInstance(new Object[] { 0, 0, 600, 800, waveEnums[2], 14 });

            Method epdControllerSetRegionMethod = epdControllerClass.getMethod("setRegion",
                    new Class[] { String.class, epdControllerRegionClass, epdControllerRegionParamsClass });
            epdControllerSetRegionMethod.invoke(null,
                    new Object[] { "Orion", regionEnums[2], localRegionParams });
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:grails.plugin.cache.util.ClassUtils.java

/**
 * This method will try to retrieve the value of the named property from the
 * object using a corresponding getter method.  If no getter method is found
 * then this method will look for the corresponding field and return its value.
 *
 * @param object object to inspect// ww  w  .  j  ava  2s  . com
 * @param propertyOrFieldName the name of the field or property to retrieve
 * @return the value of the field or property, null if neither is found
 */
public static Object getPropertyOrFieldValue(Object object, String propertyOrFieldName) {
    final String getterName = GrailsNameUtils.getGetterName(propertyOrFieldName);
    final Class<? extends Object> objectClass = object.getClass();
    try {
        final Method method = objectClass.getMethod(getterName, new Class[0]);
        if (method != null) {
            ReflectionUtils.makeAccessible(method);
            return method.invoke(object, new Object[0]);
        }
    } catch (Exception e) {
    }
    try {
        final Field field = ReflectionUtils.findField(objectClass, propertyOrFieldName);
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            return field.get(object);
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:jp.rikinet.util.dto.DtoFactory.java

/**
 * ?? DTO ??/*  ww w.  j  a  v a 2  s.c o  m*/
 * @param dto ??? DTO
 * @param name ??birthDate ? setter ? setBirthDate()
 * @param value ?
 */
private static void setProperty(Root dto, String name, Object value) {
    Class<?> cl = dto.getClass();
    String mName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
    try {
        Method m = cl.getMethod(mName, value.getClass());
        m.invoke(dto, value);
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException("setter not found", e);
    } catch (InvocationTargetException | IllegalAccessException e) {
        throw new IllegalArgumentException("setter call failed", e);
    }
}

From source file:Main.java

public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception {
    Field[] fields = cls.getDeclaredFields();
    Object obj = cls.newInstance();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        Method methodSet = cls.getDeclaredMethod(strSet, clsType);
        if (map.containsKey(name)) {
            Object objValue = typeConversion(clsType, map.get(name));
            methodSet.invoke(obj, objValue);
        }//from w w  w.  ja v  a 2s .  c o m
    }
    return obj;
}

From source file:net.daboross.bukkitdev.skywars.util.CrossVersion.java

/**
 * Supports Bukkit earlier than 1.6. TODO: removable?
 *//*from  ww w  . ja  v a 2 s . c  o  m*/
public static void setHealth(Damageable d, double health) {
    Validate.notNull(d, "Damageable cannot be null");
    try {
        d.setHealth(health);
    } catch (NoSuchMethodError ignored) {
        Class<? extends Damageable> dClass = d.getClass();
        try {
            Method healthMethod = dClass.getMethod("setHealth", Integer.TYPE);
            healthMethod.invoke(d, (int) health);
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException ex) {
            SkyStatic.getLogger().log(Level.WARNING, "Couldn't find / use .setHealth method of LivingEntity!",
                    ex);
        }
    }
}

From source file:Main.java

public static Object setObjectFileValue(Object obj, Map<String, String> data) throws Exception {
    Class<?> cls = obj.getClass();
    Field[] fields = cls.getDeclaredFields();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        Method methodSet = cls.getDeclaredMethod(strSet, clsType);
        if (data.containsKey(name)) {
            Object objValue = typeConversion(clsType, data.get(name));
            methodSet.invoke(obj, objValue);
        }/*from   ww  w  .  j  a  v a  2 s .  c o  m*/
    }
    return obj;
}

From source file:Main.java

private static Object invoke(Class<?> cls, Object receiver, String methodname, Object... args)
        throws Exception {
    Method method = null;
    if (args == null || args.length == 0) {
        method = cls.getMethod(methodname, new Class[0]);
        method.setAccessible(true);/*from ww w.  j av  a2 s .  c  o  m*/
        return method.invoke(receiver, new Object[0]);
    }
    method = cls.getMethod(methodname, getParameterTypes(args));
    method.setAccessible(true);
    return method.invoke(receiver, args);
}

From source file:Main.java

/**
 * Creates a {@link JCheckBoxMenuItem} for a menu or {@link JToggleButton}
 * for a tool bar/*from   www.j a  v  a2s .co m*/
 * 
 * @param menuOrToolBar
 * @param action 
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static void addBooleanActionTo(Container menuOrToolBar, Action action) {
    Method addButton;
    try {
        addButton = Container.class.getMethod("add", Component.class);
        if (menuOrToolBar instanceof JMenu) {
            addButton.invoke(menuOrToolBar, new JCheckBoxMenuItem(action));
        } else {
            final JToggleButton jToggleButton = new JToggleButton(action);
            jToggleButton.setText(null);
            addButton.invoke(menuOrToolBar, jToggleButton);
        }
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
}