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:org.acoveo.tools.Reflection.java

/**
 * Invoke the collection add method on the given object
 *//*from   w  w w  .ja  va 2 s .c o  m*/
public static void addToCollection(Object target, Method collectionAdder, Object added) {
    if (target == null || collectionAdder == null)
        return;
    makeAccessible(collectionAdder, collectionAdder.getModifiers());
    try {
        collectionAdder.invoke(target, new Object[] { added });
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Invoke the collection remove method on the given object
 *//* www.j a v a 2s  .  com*/
public static void removeFromCollection(Object target, Method collectionRemover, Object removed) {
    if (target == null || collectionRemover == null)
        return;
    makeAccessible(collectionRemover, collectionRemover.getModifiers());
    try {
        collectionRemover.invoke(target, new Object[] { removed });
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:org.jgentleframework.utils.Utils.java

/**
 * Returns the specified default <code>setter</code> of the specified field.
 * //from   w  w w  .  j a v a 2s  .c om
 * @param declaringClass
 *            the object class declared specified field.
 * @param fieldName
 *            name of field
 * @return returns the <code>setter</code> method if it exists, if not,
 *         return <code>null</code>.
 */
public static Method getDefaultSetter(Class<?> declaringClass, String fieldName) {

    Method setter = null;
    Field field = null;
    String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    try {
        field = ReflectUtils.getSupportedField(declaringClass, fieldName);
        setter = ReflectUtils.getSupportedMethod(declaringClass, methodName,
                new Class<?>[] { field.getType() });
    } catch (NoSuchFieldException e1) {
        return null;
    } catch (NoSuchMethodException e) {
        return null;
    }
    try {
        if (setter != null && !Modifier.isPublic(setter.getModifiers()))
            throw new IllegalAccessException();
    } catch (IllegalAccessException e) {
        if (log.isErrorEnabled()) {
            log.error("The setter method [" + methodName + "] declared in [" + declaringClass
                    + "] must be public", e);
        }
    }
    return setter;
}

From source file:fr.univlorraine.mondossierweb.config.DebugConfig.java

/**
 * Branche customizableTraceInterceptor sur les mthodes public des classes du package controllers
 * @return/*w  w  w .  j a  v a2s.com*/
 */
@Bean
public Advisor controllersAdvisor() {
    return new StaticMethodMatcherPointcutAdvisor(customizableTraceInterceptor()) {
        private static final long serialVersionUID = 5897279987213542868L;

        @Override
        public boolean matches(Method method, Class<?> clazz) {
            return Modifier.isPublic(method.getModifiers()) && clazz.getPackage() != null
                    && clazz.getPackage().getName().startsWith(UserController.class.getPackage().getName());
        }
    };
}

From source file:org.netxilia.spi.impl.formula.function.FunctionRegistry.java

/**
 * register all the public methods of the given instance
 * /*w  w  w.  j a  v a 2 s . c  om*/
 * @param instance
 */
public void registerMethods(Object instance) {
    for (Method m : instance.getClass().getDeclaredMethods())
        if ((m.getModifiers() & Modifier.PUBLIC) != 0
                && AnnotationUtils.findAnnotation(m, SkipFunction.class) == null)
            registerFunction(new MethodWrapper(instance, m));
}

From source file:org.seasar.dao.spring.autoregister.AbstractBeanAutoRegister.java

private boolean isApplicableAspect(final Method method) {
    final int mod = method.getModifiers();
    return !Modifier.isFinal(mod) && !Modifier.isStatic(mod);
}

From source file:org.apache.hadoop.fs.TestFilterFs.java

public void testFilterFileSystem() throws Exception {
    for (Method m : AbstractFileSystem.class.getDeclaredMethods()) {
        if (Modifier.isStatic(m.getModifiers()))
            continue;
        if (Modifier.isPrivate(m.getModifiers()))
            continue;
        if (Modifier.isFinal(m.getModifiers()))
            continue;

        try {/*from  w  ww. j  av  a 2 s  . c  o m*/
            DontCheck.class.getMethod(m.getName(), m.getParameterTypes());
            LOG.info("Skipping " + m);
        } catch (NoSuchMethodException exc) {
            LOG.info("Testing " + m);
            try {
                FilterFs.class.getDeclaredMethod(m.getName(), m.getParameterTypes());
            } catch (NoSuchMethodException exc2) {
                LOG.error("FilterFileSystem doesn't implement " + m);
                throw exc2;
            }
        }
    }
}

From source file:io.stallion.reflection.PropertyUtils.java

public static Boolean isReadable(Object obj, String name) {
    String cacheKey = obj.getClass().getCanonicalName() + "|" + name;
    if (lookupCache.containsKey(cacheKey)) {
        return (Boolean) lookupCache.get(cacheKey);
    }//from  w  w w . j  a  va 2  s  . c  om

    String postFix = name.toUpperCase();
    if (name.length() > 1) {
        postFix = name.substring(0, 1).toUpperCase() + name.substring(1);
    }
    Method method = null;
    try {
        method = obj.getClass().getMethod("get" + postFix, (Class<?>[]) null);
    } catch (NoSuchMethodException e) {

    }
    if (method == null) {
        try {
            method = obj.getClass().getMethod("is" + postFix, (Class<?>[]) null);
        } catch (NoSuchMethodException e) {

        }
    }
    if (method == null) {
        lookupCache.put(cacheKey, false);
        return false;
    }
    if (method.getModifiers() == Modifier.PUBLIC) {
        lookupCache.put(cacheKey, true);
        return true;
    }
    lookupCache.put(cacheKey, false);
    return false;
}

From source file:de.codesourcery.eve.apiclient.parsers.HttpAPIClientTest.java

private static Method findParserMethod(String name, Class<?>... args) throws Exception {
    try {/*  w w  w .  java  2  s  .  c o  m*/
        return AbstractResponseParser.class.getDeclaredMethod(name, args);
    } catch (Exception e) {
        try {
            return AbstractResponseParser.class.getMethod(name, args);
        } catch (Exception e2) {

            for (Method m : AbstractResponseParser.class.getMethods()) {

                if (!m.getName().equals(name)) {
                    System.out.println("# Name mismatch: " + m);
                    continue;
                }

                if (!ObjectUtils.equals(m.getParameterTypes(), args)) {
                    System.out.println("# Param mismatch: " + m);
                    continue;
                }

                final int modifiers = m.getModifiers();

                if (Modifier.isStatic(modifiers) || Modifier.isPrivate(modifiers)
                        || Modifier.isFinal(modifiers)) {
                    System.out.println("# Modifier mismatch: " + m);
                    continue;
                }

                return m;
            }
            throw e2;
        }
    }
}

From source file:io.stallion.reflection.PropertyUtils.java

/**
 * Build a map of direct javabeans properties of the target object. Only read/write properties (ie: those who have
 * both a getter and a setter) are returned.
 * @param target the target object from which to get properties names.
 * @return a Map of String with properties names as key and their values
 * @throws PropertyException if an error happened while trying to get a property.
 *///from   w  ww .  jav a  2  s  .  c o m
public static Map<String, Object> getProperties(Object target,
        Class<? extends Annotation>... excludeAnnotations) throws PropertyException {
    Map<String, Object> properties = new HashMap<String, Object>();
    Class clazz = target.getClass();
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String name = method.getName();
        Boolean hasExcludeAnno = false;
        if (excludeAnnotations.length > 0) {
            for (Class<? extends Annotation> anno : excludeAnnotations) {
                if (method.isAnnotationPresent(anno)) {
                    hasExcludeAnno = true;
                }
            }
        }
        if (hasExcludeAnno) {
            continue;
        }
        if (method.getModifiers() == Modifier.PUBLIC && method.getParameterTypes().length == 0
                && (name.startsWith("get") || name.startsWith("is"))
                && containsSetterForGetter(clazz, method)) {
            String propertyName;
            if (name.startsWith("get"))
                propertyName = Character.toLowerCase(name.charAt(3)) + name.substring(4);
            else if (name.startsWith("is"))
                propertyName = Character.toLowerCase(name.charAt(2)) + name.substring(3);
            else
                throw new PropertyException(
                        "method '" + name + "' is not a getter, thereof no setter can be found");

            try {
                Object propertyValue = method.invoke(target, (Object[]) null); // casting to (Object[]) b/c of javac 1.5 warning
                if (propertyValue != null && propertyValue instanceof Properties) {
                    Map propertiesContent = getNestedProperties(propertyName, (Properties) propertyValue);
                    properties.putAll(propertiesContent);
                } else {
                    properties.put(propertyName, propertyValue);
                }
            } catch (IllegalAccessException ex) {
                throw new PropertyException("cannot set property '" + propertyName + "' - '" + name
                        + "' is null and cannot be auto-filled", ex);
            } catch (InvocationTargetException ex) {
                throw new PropertyException("cannot set property '" + propertyName + "' - '" + name
                        + "' is null and cannot be auto-filled", ex);
            }

        } // if
    } // for

    return properties;
}