Example usage for java.lang.reflect Method getName

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

Introduction

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

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

From source file:com.hihframework.core.utils.BeanUtils.java

public static void bean2Bean(Object src, Object dest, String... excludes) {
    if (src == null || dest == null || src == dest)
        return;/*from   w w w . ja v a  2 s. c o  m*/
    Method[] methods = src.getClass().getDeclaredMethods();
    for (Method m : methods) {
        String name = m.getName();
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        if (!name.startsWith("get") && !name.startsWith("is"))
            continue;
        if (Collection.class.isAssignableFrom(m.getReturnType()))
            continue;
        boolean exc = false;
        for (String exclude : excludes) {
            if (name.equalsIgnoreCase(exclude)) {
                exc = true;
                break;
            }
        }
        if (exc)
            continue;
        int position = name.startsWith("get") ? 3 : 2;
        String method = name.substring(position);
        try {
            Object val = m.invoke(src);
            if (val == null)
                continue;
            Method targetFun = dest.getClass().getMethod("set" + method, m.getReturnType());
            targetFun.invoke(dest, val);
        } catch (Exception e) {
            log.error(e);
        }

    }
}

From source file:com.ms.commons.test.common.OutputUtil.java

synchronized public static void enter(Method method) {
    inmethod = method;//  w  w w. ja v a 2  s  .  c o m
    started = System.currentTimeMillis();
    cache.add("-+-- enter method " + method.getDeclaringClass().getName() + "#" + method.getName());
}

From source file:jp.go.nict.langrid.foundation.servlet.ResponseProcessor.java

static ServiceResponse processTemplate(LangridException exception, String hostName, OutputStream out)
        throws IOException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("escapeUtils", StringEscapeUtils.class);
    map.put("exception", exception);
    map.put("hostName", hostName);
    String cn = exception.getClass().getName();
    map.put("nsSuffix", "");

    List<Attribute> properties = new ArrayList<Attribute>();
    for (Method m : exception.getClass().getDeclaredMethods()) {
        String methodName = m.getName();
        if (!methodName.startsWith("get"))
            continue;

        try {/*from w w  w  . j a  va2s. co m*/
            String name = m.getName().substring("get".length());
            name = name.substring(0, 1).toLowerCase() + name.substring(1);
            String value = (String) m.invoke(exception);
            if (value == null)
                continue;
            properties.add(new Attribute(name, value));
        } catch (ClassCastException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
    }
    map.put("properties", properties);

    if (cn.startsWith("jp.go.nict.langrid.service_1_2.")) {
        String prefix = cn.substring("jp.go.nict.langrid.service_1_2.".length());
        int i = prefix.lastIndexOf(".");
        if (i != -1) {
            map.put("nsSuffix", prefix.substring(0, i).replace(".", "/") + "/");
        }
    }
    CountingOutputStream co = new CountingOutputStream(out);
    OutputStreamWriter w = new OutputStreamWriter(co);
    exceptionTemplate.make(map).writeTo(w);
    w.flush();

    return new ServiceResponse(500, co.getCount(), "soapenv:Server.userException", exception.getDescription());
}

From source file:net.dmulloy2.autocraft.util.BlockStates.java

/**
 * Applies serialized data from {@link #serialize(BlockState)} back to a
 * BlockState./*from w ww  .j  a va 2  s. c om*/
 * 
 * @param state State to apply data to
 * @param values Data to apply
 */
public static void deserialize(BlockState state, Map<String, Object> values) {
    for (Method method : ReflectionUtil.getMethods(state.getClass())) {
        try {
            String name = method.getName();
            if (name.startsWith("set") && method.getParameterTypes().length == 1) {
                name = name.substring(3, name.length());
                if (values.containsKey(name)) {
                    method.invoke(state, values.get(name));
                }
            }
        } catch (Throwable ex) {
        }
    }
}

From source file:com.helpinput.core.MapConvertor.java

public static boolean checkHasFieldName(Object javabean, String fieldName) {
    Method[] methods = javabean.getClass().getDeclaredMethods();
    for (Method method : methods) {
        try {/*from  w  ww  .  ja  v a  2  s  . co  m*/
            if (method.getName().startsWith("set")) {
                String scanedFieldName = methodNameGetFieldName(method.getName());
                if (scanedFieldName.equals(fieldName))
                    return true;
            }
        } catch (Exception e) {
        }
    }
    return false;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.util.MethodUtils.java

public static boolean isGetterMethod(Method method) {
    return ArrayUtils.isEmpty(method.getParameterTypes()) && isGetterMethod(method.getName());
}

From source file:org.psnively.scala.beans.ScalaBeanInfo.java

private static void addScalaGetter(Map<String, PropertyDescriptor> propertyDescriptors, Method readMethod) {
    String propertyName = readMethod.getName();

    PropertyDescriptor pd = propertyDescriptors.get(propertyName);
    if (pd != null && pd.getReadMethod() == null) {
        try {/*from   www .j a v a 2  s  . c  om*/
            pd.setReadMethod(readMethod);
        } catch (IntrospectionException ex) {
            logger.debug("Could not add read method [" + readMethod + "] for " + "property [" + propertyName
                    + "]: " + ex.getMessage());
        }
    } else if (pd == null) {
        try {
            pd = new PropertyDescriptor(propertyName, readMethod, null);
            propertyDescriptors.put(propertyName, pd);
        } catch (IntrospectionException ex) {
            logger.debug("Could not create new PropertyDescriptor for " + "readMethod [" + readMethod
                    + "] property [" + propertyName + "]: " + ex.getMessage());
        }
    }
}

From source file:net.dmulloy2.autocraft.util.BlockStates.java

/**
 * Serializes a given BlockState into a map. This method ignores
 * inventories, which should be handled separately.
 *
 * @param state BlockState to serialize/*from   w  w  w.  jav a  2  s .  c o m*/
 * @return Serialized block state
 */
public static Map<String, Object> serialize(BlockState state) {
    Map<String, Object> ret = new HashMap<>();

    for (Method method : ReflectionUtil.getMethods(state.getClass())) {
        try {
            String name = method.getName();
            if (name.startsWith("get") && method.getParameterTypes().length == 0) {
                name = name.substring(3, name.length());
                Object value = method.invoke(state);
                if (!(value instanceof Inventory)) {
                    ret.put(name, value);
                }
            }
        } catch (Throwable ex) {
        }
    }

    return ret;
}

From source file:com.helpinput.core.MapConvertor.java

public static Object toJavaBeanNotClean(Object javabean, Map<?, ?> data) {
    Method[] methods = javabean.getClass().getDeclaredMethods();
    for (Method method : methods) {
        try {//w  ww.  j  a  v  a  2  s.c om
            if (method.getName().startsWith("set")) {
                String fieldName = methodNameGetFieldName(method.getName());
                Object object = data.get(fieldName);
                if (object != null)
                    method.invoke(javabean, object);
                //               String field = method.getName();
                //               field = field.substring(field.indexOf("set") + 3);
                //               field = field.toLowerCase().charAt(0) + field.substring(1);
                //               method.invoke(javabean, new Object[] { data.get(field) });
            }
        } catch (Exception e) {
        }
    }
    return javabean;
}

From source file:hudson.util.ReflectionUtils.java

/**
 * Finds a public method of the given name, regardless of its parameter definitions,
 *//*from  w ww .ja  v  a  2 s  .  co  m*/
public static Method getPublicMethodNamed(Class c, String methodName) {
    for (Method m : c.getMethods())
        if (m.getName().equals(methodName))
            return m;
    return null;
}