Example usage for java.lang.reflect Method getModifiers

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

Introduction

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

Prototype

@Override
public int getModifiers() 

Source Link

Usage

From source file:jenkins.plugins.git.MethodUtils.java

/**
 * Checks if the  method defined on the base type with the given arguments
 * are overridden in the given derived type.
 *//*from  ww w . j av a  2 s .co  m*/
// TODO replace with core utility method once JENKINS-30002 is available in base version of Jenkins
static boolean isOverridden(@Nonnull Class base, @Nonnull Class derived, @Nonnull String methodName,
        @Nonnull Class... types) {
    Method baseMethod = getMethodImpl(base, methodName, types);
    Method derivedMethod = getMethodImpl(derived, methodName, types);
    return baseMethod == null ? derivedMethod != null && !Modifier.isAbstract(derivedMethod.getModifiers())
            : !baseMethod.equals(derivedMethod);
}

From source file:Main.java

/**
 * Returns a clone of the specified object, if it can be cloned, otherwise
 * throws a CloneNotSupportedException.//from   w  ww.j  a  v  a 2 s  . c  o m
 * 
 * @param object
 *          the object to clone (<code>null</code> not permitted).
 * @return A clone of the specified object.
 * @throws CloneNotSupportedException
 *           if the object cannot be cloned.
 */
public static Object clone(final Object object) throws CloneNotSupportedException {
    if (object == null) {
        throw new IllegalArgumentException("Null 'object' argument.");
    }

    try {
        final Method method = object.getClass().getMethod("clone", (Class[]) null);
        if (Modifier.isPublic(method.getModifiers())) {
            return method.invoke(object, (Object[]) null);
        }
    } catch (NoSuchMethodException e) {
        System.out.println("Object without clone() method is impossible.");
    } catch (IllegalAccessException e) {
        System.out.println("Object.clone(): unable to call method.");
    } catch (InvocationTargetException e) {
        System.out.println("Object without clone() method is impossible.");
    }

    throw new CloneNotSupportedException("Failed to clone.");
}

From source file:com.iorga.iraj.json.MethodTemplate.java

public static boolean isMethodTemplate(final Method targetMethod) {
    return PropertyTemplate.isPropertyTemplate(targetMethod)
            && (targetMethod.getModifiers() & PUBLIC_STATIC) == PUBLIC_STATIC;
}

From source file:org.braiden.fpm2.util.PropertyUtils.java

public static Map<String, Object> describe(Object bean)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Validate.notNull(bean);/*from  ww  w.  j  a va 2 s.  com*/

    Map<String, Object> result = new HashMap<String, Object>();

    for (Method method : bean.getClass().getMethods()) {
        boolean isAccepted = Modifier.isPublic(method.getModifiers())
                && !Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length == 0
                && !METHOD_GET_CLASS.equals(method.getName());

        boolean isObjGetter = isAccepted && !Void.class.isAssignableFrom(method.getReturnType())
                && method.getName().startsWith(GETTER_PREFIX);
        boolean isBooleanGetter = isAccepted
                && (Boolean.class.isAssignableFrom(method.getReturnType())
                        || boolean.class.isAssignableFrom(method.getReturnType()))
                && method.getName().startsWith(GETTER_BOOLEAN_PREFIX);

        if (isObjGetter || isBooleanGetter) {
            String propName = StringUtils
                    .uncapitalize(isObjGetter ? method.getName().substring(GETTER_PREFIX.length())
                            : method.getName().substring(GETTER_BOOLEAN_PREFIX.length()));
            result.put(propName, method.invoke(bean));
        }
    }

    return result;
}

From source file:com.jaspersoft.jasperserver.util.QueryUtil.java

/**
 * Execute DataBaseMetaData methods using reflection
 * @param dmd/*from  w  ww .  ja v  a  2s .  co  m*/
 * @param methodName
 * @param parameters
 * @return
 * @throws ClassNotFoundException
 */
public static Method findMethod(DatabaseMetaData dmd, String methodName, Object[] parameters)
        throws ClassNotFoundException {
    long startTime = System.currentTimeMillis();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Enter findMethod .. Start Time" + System.currentTimeMillis());
        }
        Class cl = Class.forName("java.sql.DatabaseMetaData");
        Method[] methods = cl.getDeclaredMethods();
        // Trying to avoid collision of methods with varying parameters and avoid having to do parameter class types

        int paramCount = 0;
        if (null != parameters) {
            paramCount = parameters.length;
        }

        for (Method m : methods) {
            if (m.getName().equals(methodName)) {
                if (Modifier.isPublic(m.getModifiers()) && m.getParameterTypes().length == paramCount) {
                    return m;
                }
            }
        } //for
        return null;
    } finally {
        if (logger.isDebugEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            logger.debug("Exit findMethod .. Total Time Spent: " + elapsedTime);
        }
    }

}

From source file:org.blocks4j.reconf.client.elements.ConfigurationItemElement.java

public static List<ConfigurationItemElement> from(ConfigurationRepositoryElement repository) {
    List<ConfigurationItemElement> result = new ArrayList<ConfigurationItemElement>();

    for (Method method : repository.getInterfaceClass().getMethods()) {
        if (Modifier.isAbstract(method.getModifiers())) {
            checkAnnotations(method);/*from   www  .  j a  va  2  s . c  o  m*/

            ConfigurationItem ann = method.getAnnotation(ConfigurationItem.class);

            if (ann != null) {
                result.add(createConfigurationItemElement(repository, method, ann));
            }
        }
    }

    return result;
}

From source file:Main.java

/**
 * find all getter and is method and return the value by map.
 *///ww w  .  j a  va 2s. co  m
public static Map<String, Object> getProperties(Object bean) {
    Map<String, Object> map = new HashMap<>();
    for (Method method : bean.getClass().getMethods()) {
        String name = method.getName();
        if (((name.length() > 3 && name.startsWith("get")) || (name.length() > 2 && name.startsWith("is")))
                && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0
                && method.getDeclaringClass() != Object.class) {
            int i = name.startsWith("get") ? 3 : 2;
            String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1);
            try {
                map.put(key, method.invoke(bean, new Object[0]));
            } catch (Exception e) {
            }
        }
    }
    return map;
}

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

/**
 * java bean??Map/*from  www  . j  a  v  a2 s . com*/
 * @param bean
 * @param map
 * @author 
 * @since 
 */
public static void beanToMap(Object bean, Map<String, Object> map) {
    if (map == null || bean == null)
        return;
    Method[] methods = bean.getClass().getDeclaredMethods();
    for (Method m : methods) {
        String name = m.getName();
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        if (!name.startsWith("get") && !name.startsWith("is"))
            continue;
        int position = name.startsWith("get") ? 3 : 2;
        String n = StringHelpers.lowerFirst(name.substring(position));
        try {
            Object val = m.invoke(bean);
            map.put(n, val);
        } catch (Exception e) {
        }
    }
}

From source file:org.grails.orm.hibernate.cfg.AbstractIdentityEnumType.java

@SuppressWarnings("unchecked")
public static boolean supports(@SuppressWarnings("rawtypes") Class enumClass) {
    if (enumClass.isEnum()) {
        try {//  ww  w .j  a  v  a  2 s .  co  m
            Method idAccessor = enumClass.getMethod(ENUM_ID_ACCESSOR);
            int mods = idAccessor.getModifiers();
            if (Modifier.isPublic(mods) && !Modifier.isStatic(mods)) {
                Class<?> returnType = idAccessor.getReturnType();
                return returnType != null
                        && typeResolver.basic(returnType.getName()) instanceof AbstractStandardBasicType;
            }
        } catch (NoSuchMethodException e) {
            // ignore
        }
    }
    return false;
}

From source file:net.sf.companymanager.qbe.JpaUtil.java

private static boolean isPrimaryKey(final Method method) {
    return isPublic(method.getModifiers())
            && (method.getAnnotation(Id.class) != null || method.getAnnotation(EmbeddedId.class) != null);
}