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:grails.plugin.springsecurity.acl.util.ProxyUtils.java

/**
 * Finds the method in the unproxied superclass if proxied.
 * @param method  the method//from w  w  w  .jav a2s. c o m
 * @return  the method in the unproxied class
 */
public static Method unproxy(final Method method) {
    Class<?> clazz = method.getDeclaringClass();

    if (!isProxy(clazz)) {
        return method;
    }

    return ReflectionUtils.findMethod(unproxy(clazz), method.getName(), method.getParameterTypes());
}

From source file:bdi4jade.util.ReflectionUtils.java

private static String methodToPropertyName(String prefix, Method method) {
    String property = method.getName().substring(prefix.length());
    return property.substring(0, 1).toLowerCase() + property.substring(1);
}

From source file:com.appleframework.jmx.connector.framework.ConnectorRegistry.java

@SuppressWarnings("deprecation")
private static ConnectorRegistry create(ApplicationConfig config) throws Exception {

    Map<String, String> paramValues = config.getParamValues();
    String appId = config.getApplicationId();
    String connectorId = (String) paramValues.get("connectorId");

    File file1 = new File(CoreUtils.getConnectorDir() + File.separatorChar + connectorId);

    URL[] urls = new URL[] { file1.toURL() };
    ClassLoader cl = ClassLoaderRepository.getClassLoader(urls, true);

    //URLClassLoader cl = new URLClassLoader(urls,
    //        ConnectorMBeanRegistry.class.getClassLoader());

    Registry.MODELER_MANIFEST = MBEANS_DESCRIPTOR;
    URL res = cl.getResource(MBEANS_DESCRIPTOR);

    logger.info("Application ID   : " + appId);
    logger.info("Connector Archive: " + file1.getAbsoluteFile());
    logger.info("MBean Descriptor : " + res.toString());

    //Thread.currentThread().setContextClassLoader(cl);
    //Registry.setUseContextClassLoader(true);

    ConnectorRegistry registry = new ConnectorRegistry();
    registry.loadMetadata(cl);//from w  w w .  j a va  2 s  .c  o m

    String[] mbeans = registry.findManagedBeans();

    MBeanServer server = MBeanServerFactory.newMBeanServer(DOMAIN_CONNECTOR);

    for (int i = 0; i < mbeans.length; i++) {
        ManagedBean managed = registry.findManagedBean(mbeans[i]);
        String clsName = managed.getType();
        String domain = managed.getDomain();
        if (domain == null) {
            domain = DOMAIN_CONNECTOR;
        }

        Class<?> cls = Class.forName(clsName, true, cl);
        Object objMBean = null;

        // Use the factory method when it is defined.
        Method[] methods = cls.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(FACTORY_METHOD) && Modifier.isStatic(method.getModifiers())) {
                objMBean = method.invoke(null);
                logger.info("Create MBean using factory method.");
                break;
            }
        }

        if (objMBean == null) {
            objMBean = cls.newInstance();
        }

        // Call the initialize method if the MBean extends ConnectorSupport class
        if (objMBean instanceof ConnectorSupport) {
            Method method = cls.getMethod("initialize", new Class[] { Map.class });
            Map<String, String> props = config.getParamValues();
            method.invoke(objMBean, new Object[] { props });
        }
        ModelMBean mm = managed.createMBean(objMBean);

        String beanObjName = domain + ":name=" + mbeans[i];
        server.registerMBean(mm, new ObjectName(beanObjName));
    }

    registry.setMBeanServer(server);
    entries.put(appId, registry);
    return registry;
}

From source file:MethodHashing.java

public static long methodHash(Method method) throws Exception {
    Class[] parameterTypes = method.getParameterTypes();
    String methodDesc = method.getName() + "(";
    for (int j = 0; j < parameterTypes.length; j++) {
        methodDesc += getTypeString(parameterTypes[j]);
    }/*from  w ww. jav  a 2  s. c om*/
    methodDesc += ")" + getTypeString(method.getReturnType());

    long hash = 0;
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(512);
    MessageDigest messagedigest = MessageDigest.getInstance("SHA");
    DataOutputStream dataoutputstream = new DataOutputStream(
            new DigestOutputStream(bytearrayoutputstream, messagedigest));
    dataoutputstream.writeUTF(methodDesc);
    dataoutputstream.flush();
    byte abyte0[] = messagedigest.digest();
    for (int j = 0; j < Math.min(8, abyte0.length); j++)
        hash += (long) (abyte0[j] & 0xff) << j * 8;
    return hash;
}

From source file:com.cloudmine.api.CMApiCredentials.java

/**
 * If you are using CloudMine on Android, this or {@link #initialize(String, String, Object)} is the initialize method you should be calling. Works just
 * like {@link #initialize(String, String, String)}, but sets some important android only information
 * @param id//from  ww  w.  jav  a  2 s  .  co  m
 * @param apiKey
 * @param baseUrl the base CloudMine URL to use, if running on a non default CloudMine install
 * @param context either null if not running on android, or the value of getApplicationContext from your main activity. It isn't typed here so the Java sdk does not have any android dependencies
 * @return
 * @throws CreationException in addition to the reasons defined in {@link #initialize(String, String)}, also if you do not provide the application context and you're running on android
 */
public static synchronized CMApiCredentials initialize(String id, String apiKey, String baseUrl, Object context)
        throws CreationException {
    if (Strings.isEmpty(id) || Strings.isEmpty(apiKey) || Strings.isEmpty(baseUrl)) {
        throw new CreationException(
                "Illegal null/empty argument passed to initialize. Given id=" + id + " and apiKey=" + apiKey);
    }
    try {
        Class contextClass = Class.forName("android.content.Context");
        boolean invalidContext = context == null || contextClass == null
                || !contextClass.isAssignableFrom(context.getClass());
        if (invalidContext) {
            throw new CreationException(
                    "Running on android and application context not provided, try passing getApplicationContext to this method");
        }

        for (Method method : Class.forName("com.cloudmine.api.DeviceIdentifier").getMethods()) {
            if ("initialize".equals(method.getName())) {
                method.invoke(null, context);
            }
        }
    } catch (ClassNotFoundException e) {
        LOG.info("Not running on Android", e);
    } catch (InvocationTargetException e) {
        LOG.error("Exception thrown", e);
    } catch (IllegalAccessException e) {
        LOG.error("Exception thrown", e);
    }
    credentials = new CMApiCredentials(id, apiKey, baseUrl);
    return credentials;
}

From source file:it.sample.parser.util.CommonsUtil.java

/**
 * metodo di utilita' per controllare che un metodo sia un getter
 * /*  ww  w.  j a va  2s  .c  o  m*/
 * @param method
 * @return true se il metodo e' un getter, false altrimenti
 */
private static boolean isGetter(Method method) {
    if (!method.getName().startsWith("get"))
        return false;
    if (method.getParameterTypes().length != 0)
        return false;
    if (void.class.equals(method.getReturnType()))
        return false;
    return true;
}

From source file:com.palantir.atlasdb.keyvalue.impl.partition.FailableKeyValueServices.java

public static boolean isReadMethod(Method method) {
    return READ_METHODS.contains(method.getName());
}

From source file:com.palantir.atlasdb.keyvalue.impl.partition.FailableKeyValueServices.java

public static boolean isWriteMethod(Method method) {
    return WRITE_METHODS.contains(method.getName());
}

From source file:edu.umn.msi.tropix.proteomics.parameters.ParameterUtils.java

/**
 * Uses reflection to dynamically set a parameter bean's parameters from a given map of Strings.
 * /* w w  w.j  av  a  2  s . c  o m*/
 * @param parameterMap
 *          Mapping of parameter names (attributes) to values.
 * @param parameterObject
 *          Java bean to set parameter values of of. Setter methods must conform to the Java bean naming scheme and must take in one argument of
 *          type String, Integer, Long, Float, or Double.
 * @exception IllegalArgumentException
 *              Thrown if a given setter method cannot be found or if the setter method does not conform to the rules described.
 */
@SuppressWarnings("unchecked")
public static void setParametersFromMap(final Map parameterMap, final Object parameterObject) {
    final Method[] methods = parameterObject.getClass().getMethods();
    final Map<String, Method> objectSetMethodMap = new HashMap<String, Method>(methods.length);

    for (final Method method : parameterObject.getClass().getMethods()) {
        final String methodName = method.getName();
        if (methodName.contains("set")) {
            objectSetMethodMap.put(method.getName(), method);
        }
    }

    for (final Object keyObject : parameterMap.keySet()) {
        final String key = (String) keyObject;
        final String value = (String) parameterMap.get(key);
        final String setterMethodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
        final Method setterMethod = objectSetMethodMap.get(setterMethodName);
        if (setterMethod == null) {
            continue;
        }
        final Class<?>[] parameterTypes = setterMethod.getParameterTypes();
        if (parameterTypes.length != 1) {
            throw new IllegalArgumentException(
                    "Illegal setter method found, must take in exactly one argument.");
        }
        final Class<?> argumentType = parameterTypes[0];
        try {
            if (argumentType.equals(String.class)) {
                setterMethod.invoke(parameterObject, value);
                continue;
            }
            if (value == null || value.trim().equals("")) {
                setterMethod.invoke(parameterObject, (Object) null);
                continue;
            }
            if (argumentType.equals(Double.class) || argumentType.equals(double.class)) {
                setterMethod.invoke(parameterObject, Double.parseDouble(value));
            } else if (argumentType.equals(Integer.class) || argumentType.equals(int.class)) {
                setterMethod.invoke(parameterObject, Integer.parseInt(value));
            } else if (argumentType.equals(Long.class) || argumentType.equals(long.class)) {
                setterMethod.invoke(parameterObject, Long.parseLong(value));
            } else if (argumentType.equals(Float.class) || argumentType.equals(float.class)) {
                setterMethod.invoke(parameterObject, Float.parseFloat(value));
            } else if (argumentType.equals(Boolean.class) || argumentType.equals(boolean.class)) {
                setterMethod.invoke(parameterObject, Boolean.parseBoolean(value));
            } else if (argumentType.equals(Short.class) || argumentType.equals(short.class)) {
                setterMethod.invoke(parameterObject, Short.parseShort(value));
            } else {
                throw new IllegalArgumentException("Illegal type found for argument to setter bean - type is "
                        + argumentType + " - key is " + key);
            }
        } catch (final Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}

From source file:cf.spring.servicebroker.AccessorUtils.java

/**
 * Validates that the method annotated with the specified annotation returns
 * the expected type./*ww  w. j  a v  a2 s .  co  m*/
 */
public static void validateReturnType(Method method, Class<? extends Annotation> annotationType,
        Class<?> expectedReturnType) {
    if (!method.getReturnType().equals(expectedReturnType)) {
        throw new BeanCreationException("Method " + method.getName() + " annotated with @"
                + annotationType.getName() + " must have a return type of " + expectedReturnType.getName());
    }
}