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:com.iorga.iraj.json.MethodTemplate.java

public MethodTemplate(final Method targetMethod, final JsonWriter jsonWriter) {
    super(targetMethod, jsonWriter);

    if ((targetMethod.getModifiers() & PUBLIC_STATIC) != PUBLIC_STATIC) {
        throw new IllegalArgumentException(targetMethod
                + " must be public static in order to be called without instantiating the template");
    }/*ww w.jav  a2  s .com*/

    this.targetMethod = targetMethod;
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Determine whether the given method is overridable in the given target class.
 * @param method the method to check/*from  w  w w  .j a va 2 s  .  co  m*/
 * @param targetClass the target class to check against
 */
private static boolean isOverridable(Method method, Class<?> targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

From source file:blue.lapis.pore.impl.event.PoreEventImplTest.java

@Test
public void checkImplementedMethods() {
    Class<?> bukkitEvent = eventImpl.getSuperclass();
    for (Method method : bukkitEvent.getMethods()) {
        int modifiers = method.getModifiers();
        if (Modifier.isStatic(modifiers) || isDefault(method) || method.getDeclaringClass() == Event.class
                || method.getDeclaringClass() == Object.class || method.getName().equals("getHandlers")
                || method.getName().startsWith("_INVALID_")) {
            continue;
        }//from  w  ww  . j av a2  s .c  om

        try {
            eventImpl.getDeclaredMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException e) {
            fail(eventImpl.getSimpleName() + ": should override method " + method);
        }
    }
}

From source file:com.epam.catgenome.manager.externaldb.bindings.ExternalDBBindingTest.java

private void processFactory(Object factory)
        throws InvocationTargetException, IllegalAccessException, InstantiationException {
    List<Object> factoryObjects = new ArrayList<>();
    Map<Class, Object> objectMap = new HashMap<>();

    for (Method m : factory.getClass().getDeclaredMethods()) {
        if (Modifier.isPublic(m.getModifiers())) {
            Object o;/*from w w w.  j  av a 2 s  .c o m*/
            if (m.getParameterCount() == 0) {
                o = m.invoke(factory);
            } else {
                Object[] params = new Object[m.getParameterCount()];
                for (int i = 0; i < m.getParameterCount(); i++) {
                    params[i] = createParam(m.getParameterTypes()[i]);
                }

                o = m.invoke(factory, params);
            }

            factoryObjects.add(o);
            objectMap.put(o.getClass(), o);
        }
    }

    for (Object o : factoryObjects) {
        for (Method m : o.getClass().getDeclaredMethods()) {
            if (Modifier.isPublic(m.getModifiers())) {
                if (m.getParameterTypes().length > 0) {
                    Class type = m.getParameterTypes()[0];
                    if (objectMap.containsKey(type)) {
                        m.invoke(o, objectMap.get(type));
                    } else {
                        Object param = createParam(type);

                        m.invoke(o, param);
                    }
                } else {
                    m.invoke(o);
                }
            }
        }
    }
}

From source file:com.freetmp.common.util.ClassUtils.java

private static boolean isOverridable(Method method, Class<?> targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }/*  w w w  .j  a  v a  2 s .co  m*/
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

From source file:gobblin.runtime.cli.PublicMethodsCliObjectFactory.java

private boolean canUseMethod(Method method) {
    if (!Modifier.isPublic(method.getModifiers())) {
        return false;
    }//from  w  ww  .  j a va 2 s  .co m
    if (BLACKLISTED_FROM_CLI.contains(method.getName())) {
        return false;
    }
    if (method.isAnnotationPresent(NotOnCli.class)) {
        return false;
    }
    Class<?>[] parameters = method.getParameterTypes();
    if (parameters.length >= 2) {
        return false;
    }
    if (parameters.length == 1 && parameters[0] != String.class) {
        return false;
    }
    return true;
}

From source file:com.agileapes.couteau.context.spring.event.impl.AbstractMappedEventsTranslationScheme.java

@Override
public void fillIn(Event originalEvent, ApplicationEvent translated) throws EventTranslationException {
    for (Method method : originalEvent.getClass().getMethods()) {
        if (!method.getName().matches("set[A-Z].*") || !Modifier.isPublic(method.getModifiers())
                || !method.getReturnType().equals(void.class) || method.getParameterTypes().length != 1) {
            continue;
        }/*from   ww  w.j a va 2s  . com*/
        final String propertyName = StringUtils.uncapitalize(method.getName().substring(3));
        final Field field = ReflectionUtils.findField(translated.getClass(), propertyName);
        if (field == null || !method.getParameterTypes()[0].isAssignableFrom(field.getType())) {
            continue;
        }
        field.setAccessible(true);
        try {
            method.invoke(originalEvent, field.get(translated));
        } catch (Exception e) {
            throw new EventTranslationException("Failed to set property on original event: " + propertyName, e);
        }
    }
}

From source file:org.codehaus.groovy.grails.commons.metaclass.BaseApiProvider.java

@SuppressWarnings("unchecked")
public void addApi(final Object apiInstance) {
    if (apiInstance == null) {
        return;/*from   w w  w  .j  a  v a2s .c  om*/
    }

    Class<?> currentClass = apiInstance.getClass();
    while (currentClass != Object.class) {
        final Method[] declaredMethods = currentClass.getDeclaredMethods();

        for (final Method javaMethod : declaredMethods) {
            final int modifiers = javaMethod.getModifiers();
            if (!isNotExcluded(javaMethod, modifiers)) {
                continue;
            }

            if (Modifier.isStatic(modifiers)) {
                if (isConstructorCallMethod(javaMethod)) {
                    constructors.add(javaMethod);
                } else {
                    staticMethods.add(javaMethod);
                }
            } else {
                instanceMethods.add(new ReflectionMetaMethod(new CachedMethod(javaMethod)) {
                    @Override
                    public String getName() {

                        String methodName = super.getName();
                        if (isConstructorCallMethod(javaMethod)) {
                            return CTOR_GROOVY_METHOD;
                        }
                        return methodName;
                    }

                    @Override
                    public Object invoke(Object object, Object[] arguments) {
                        if (arguments.length == 0) {
                            return super.invoke(apiInstance, new Object[] { object });
                        }
                        return super.invoke(apiInstance,
                                ArrayUtils.add(checkForGStrings(arguments), 0, object));
                    }

                    private Object[] checkForGStrings(Object[] arguments) {
                        for (int i = 0; i < arguments.length; i++) {
                            if (arguments[i] instanceof GString) {
                                arguments[i] = arguments[i].toString();
                            }
                        }
                        return arguments;
                    }

                    @Override
                    public CachedClass[] getParameterTypes() {
                        final CachedClass[] paramTypes = method.getParameterTypes();
                        if (paramTypes.length > 0) {
                            return (CachedClass[]) ArrayUtils.subarray(paramTypes, 1, paramTypes.length);
                        }
                        return paramTypes;
                    }
                });
            }
        }
        currentClass = currentClass.getSuperclass();
    }
}

From source file:de.codesourcery.eve.skills.util.SpringBeanInjector.java

private void doInjectDependencies(Object obj)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    Class<?> currentClasz = obj.getClass();
    do {//from  w ww .  j  a  v a2 s .c  o  m
        // inject fields
        for (Field f : currentClasz.getDeclaredFields()) {

            final int m = f.getModifiers();
            if (Modifier.isStatic(m) || Modifier.isFinal(m)) {
                continue;
            }

            final Resource annot = f.getAnnotation(Resource.class);
            if (annot != null) {
                if (!f.isAccessible()) {
                    f.setAccessible(true);
                }

                if (log.isTraceEnabled()) {
                    log.trace("doInjectDependencies(): Setting field " + f.getName() + " with bean '"
                            + annot.name() + "'");
                }
                f.set(obj, getBean(annot.name()));
            }
        }

        // inject methods
        for (Method method : currentClasz.getDeclaredMethods()) {

            final int m = method.getModifiers();
            if (Modifier.isStatic(m) || Modifier.isAbstract(m)) {
                continue;
            }

            if (method.getParameterTypes().length != 1) {
                continue;
            }

            if (!method.getName().startsWith("set")) {
                continue;
            }

            final Resource annot = method.getAnnotation(Resource.class);
            if (annot != null) {
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }

                if (log.isTraceEnabled()) {
                    log.trace("doInjectDependencies(): Invoking setter method " + method.getName()
                            + " with bean '" + annot.name() + "'");
                }

                method.invoke(obj, getBean(annot.name()));
            }
        }

        currentClasz = currentClasz.getSuperclass();

    } while (currentClasz != null);
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Return a public static method of a class.
 * @param methodName the static method name
 * @param clazz the class which defines the method
 * @param args the parameter types to the method
 * @return the static method, or {@code null} if no static method was found
 * @throws IllegalArgumentException if the method name is blank or the clazz is null
 *///from  w  ww.  j  av  a  2s.c  o m
public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(methodName, "Method name must not be null");
    try {
        Method method = clazz.getMethod(methodName, args);
        return Modifier.isStatic(method.getModifiers()) ? method : null;
    } catch (NoSuchMethodException ex) {
        return null;
    }
}