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.hihframework.core.utils.BeanUtils.java

/**
 * ?????//from   w  ww . j a  v a  2s  . co m
 * @param bean
 * @param name
 * @return
 * @author 
 * @since 
 */
public static String getProperty(Object bean, String name) {
    try {
        Method m = bean.getClass().getMethod("get" + StringHelpers.upperFirst(name));
        if (m != null && Modifier.isPublic(m.getModifiers())) {
            Object val = m.invoke(bean);
            return val == null ? null : val.toString();
        }
    } catch (Exception e) {
    }
    try {
        Method[] methods = bean.getClass().getDeclaredMethods();
        for (Method method : methods) {
            String n = method.getName();
            if (!n.startsWith("get"))
                continue;
            n = n.substring(3);
            if (!name.equalsIgnoreCase(n))
                continue;
            if (!Modifier.isPublic(method.getModifiers()))
                continue;
            Object val = method.invoke(bean);
            return val == null ? null : val.toString();
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:org.apache.tajo.engine.function.FunctionLoader.java

private static boolean isPublicStaticMethod(Method method) {
    return Modifier.isPublic(method.getModifiers()) && Modifier.isStatic(method.getModifiers());
}

From source file:io.neba.core.resourcemodels.factory.ModelInstantiator.java

/**
 * @return all public methods annotated with @Inject. Fails if a public method
 * annotated with @Inject does not take exactly one argument.
 *///w  w w  .ja v a 2s  .c om
@Nonnull
private static ModelServiceSetter[] resolveServiceSetters(@Nonnull Class<?> modelType) {
    List<ModelServiceSetter> serviceSetters = new ArrayList<>();
    for (Method method : modelType.getMethods()) {
        if (isStatic(method.getModifiers())) {
            continue;
        }
        if (!annotations(method).containsName(INJECT_ANNOTATION_NAME)) {
            continue;
        }

        if (method.getParameterCount() != 1) {
            throw new InvalidModelException("The method " + method
                    + " is annotated with @Inject and must thus take exactly one argument.");
        }

        Filter filter = findFilterAnnotation(method.getParameterAnnotations()[0]);
        Type serviceType = method.getGenericParameterTypes()[0];
        ServiceDependency serviceDependency = new ServiceDependency(serviceType, modelType, filter);
        serviceSetters.add(new ModelServiceSetter(serviceDependency, method));
    }
    return serviceSetters.toArray(new ModelServiceSetter[0]);
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

public static Map<String, Map<Class<?>, Method>> findUnaryMethods(Class<?> clazz) {
    Map<String, Map<Class<?>, Method>> result = new TreeMap<>();

    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
        int modified = method.getModifiers();
        if (Modifier.isPublic(modified) && method.getParameterTypes().length == 1) {
            Map<Class<?>, Method> methodMap = result.get(method.getName());
            if (methodMap == null) {
                methodMap = new HashMap<>();
                result.put(method.getName(), methodMap);
            }//from  w w  w  . j av  a  2s. c om
            methodMap.put(method.getParameterTypes()[0], method);
        }
    }

    return result;
}

From source file:org.apache.xml.security.utils.XalanXPathAPI.java

private synchronized static void fixupFunctionTable() {
    installed = false;//  www . j a v a  2 s.c o  m
    if (log.isDebugEnabled()) {
        log.debug("Registering Here function");
    }
    /**
     * Try to register our here() implementation as internal function.
     */
    try {
        Class<?>[] args = { String.class, Expression.class };
        Method installFunction = FunctionTable.class.getMethod("installFunction", args);
        if ((installFunction.getModifiers() & Modifier.STATIC) != 0) {
            Object[] params = { "here", new FuncHere() };
            installFunction.invoke(null, params);
            installed = true;
        }
    } catch (Exception ex) {
        log.debug("Error installing function using the static installFunction method", ex);
    }
    if (!installed) {
        try {
            funcTable = new FunctionTable();
            Class<?>[] args = { String.class, Class.class };
            Method installFunction = FunctionTable.class.getMethod("installFunction", args);
            Object[] params = { "here", FuncHere.class };
            installFunction.invoke(funcTable, params);
            installed = true;
        } catch (Exception ex) {
            log.debug("Error installing function using the static installFunction method", ex);
        }
    }
    if (log.isDebugEnabled()) {
        if (installed) {
            log.debug("Registered class " + FuncHere.class.getName()
                    + " for XPath function 'here()' function in internal table");
        } else {
            log.debug("Unable to register class " + FuncHere.class.getName()
                    + " for XPath function 'here()' function in internal table");
        }
    }
}

From source file:org.querybyexample.jpa.JpaUtil.java

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

From source file:com.kangyonggan.cms.util.Reflections.java

/**
 * ?private/protectedpublic?????JDKSecurityManager
 *//* w  w  w. j a  v a  2s. c  o  m*/
public static void makeAccessible(Method method) {
    boolean temp = (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible();
    if (temp) {
        method.setAccessible(true);
    }
}

From source file:Main.java

/**
 * search the method and return the defined method.
 * it will {@link Class#getMethod(String, Class[])}, if exception occurs,
 * it will search for all methods, and find the most fit method.
 *//*from  www .j a  va  2  s.com*/
public static Method searchMethod(Class<?> currentClass, String name, Class<?>[] parameterTypes, boolean boxed)
        throws NoSuchMethodException {
    if (currentClass == null) {
        throw new NoSuchMethodException("class == null");
    }
    try {
        return currentClass.getMethod(name, parameterTypes);
    } catch (NoSuchMethodException e) {
        Method likeMethod = null;
        for (Method method : currentClass.getMethods()) {
            if (method.getName().equals(name) && parameterTypes.length == method.getParameterTypes().length
                    && Modifier.isPublic(method.getModifiers())) {
                if (parameterTypes.length > 0) {
                    Class<?>[] types = method.getParameterTypes();
                    boolean eq = true;
                    boolean like = true;
                    for (int i = 0; i < parameterTypes.length; i++) {
                        Class<?> type = types[i];
                        Class<?> parameterType = parameterTypes[i];
                        if (type != null && parameterType != null && !type.equals(parameterType)) {
                            eq = false;
                            if (boxed) {
                                type = getBoxedClass(type);
                                parameterType = getBoxedClass(parameterType);
                            }
                            if (!type.isAssignableFrom(parameterType)) {
                                eq = false;
                                like = false;
                                break;
                            }
                        }
                    }
                    if (!eq) {
                        if (like && (likeMethod == null || likeMethod.getParameterTypes()[0]
                                .isAssignableFrom(method.getParameterTypes()[0]))) {
                            likeMethod = method;
                        }
                        continue;
                    }
                }
                return method;
            }
        }
        if (likeMethod != null) {
            return likeMethod;
        }
        throw e;
    }
}

From source file:com.opengamma.util.ReflectionUtils.java

/**
 * Checks if the class is closeable./*ww  w  .  ja v  a 2 s  . c o m*/
 * <p>
 * This invokes the close method if it is present.
 * 
 * @param type  the type, not null
 * @return true if closeable
 */
public static boolean isCloseable(final Class<?> type) {
    if (Closeable.class.isAssignableFrom(type)) {
        return true;
    } else if (Lifecycle.class.isAssignableFrom(type)) {
        return true;
    } else if (DisposableBean.class.isAssignableFrom(type)) {
        return true;
    }
    try {
        Method method = type.getMethod("close");
        if (Modifier.isPublic(method.getModifiers()) && method.isSynthetic() == false) {
            return true;
        }
    } catch (Exception ex) {
        try {
            Method method = type.getMethod("stop");
            if (Modifier.isPublic(method.getModifiers()) && method.isSynthetic() == false) {
                return true;
            }
        } catch (Exception ex2) {
            try {
                Method method = type.getMethod("shutdown");
                if (Modifier.isPublic(method.getModifiers()) && method.isSynthetic() == false) {
                    return true;
                }
            } catch (Exception ex3) {
                // ignored
            }
        }
    }
    return false;
}

From source file:com.xutils.view.ViewInjectorImpl.java

@SuppressWarnings("ConstantConditions")
private static void injectObject(Object handler, Class<?> handlerType, ViewFinder finder) {

    if (handlerType == null || IGNORED.contains(handlerType)) {
        return;//from   w  ww .  jav  a2s .  c  om
    }

    // inject view
    Field[] fields = handlerType.getDeclaredFields();
    if (fields != null && fields.length > 0) {
        for (Field field : fields) {

            Class<?> fieldType = field.getType();
            if (
            /* ??? */ Modifier.isStatic(field.getModifiers()) ||
            /* ?final */ Modifier.isFinal(field.getModifiers()) ||
            /* ? */ fieldType.isPrimitive() ||
            /* ? */ fieldType.isArray()) {
                continue;
            }

            ViewInject viewInject = field.getAnnotation(ViewInject.class);
            if (viewInject != null) {
                try {
                    View view = finder.findViewById(viewInject.value(), viewInject.parentId());
                    if (view != null) {
                        field.setAccessible(true);
                        field.set(handler, view);
                    } else {
                        throw new RuntimeException("Invalid id(" + viewInject.value() + ") for @ViewInject!"
                                + handlerType.getSimpleName());
                    }
                } catch (Throwable ex) {
                    LogUtil.e(ex.getMessage(), ex);
                }
            }
        }
    }

    // inject event
    Method[] methods = handlerType.getDeclaredMethods();
    if (methods != null && methods.length > 0) {
        for (Method method : methods) {

            if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPrivate(method.getModifiers())) {
                continue;
            }

            //??event
            Event event = method.getAnnotation(Event.class);
            if (event != null) {
                try {
                    // id?
                    int[] values = event.value();
                    int[] parentIds = event.parentId();
                    int parentIdsLen = parentIds == null ? 0 : parentIds.length;
                    //id?ViewInfo???
                    for (int i = 0; i < values.length; i++) {
                        int value = values[i];
                        if (value > 0) {
                            ViewInfo info = new ViewInfo();
                            info.value = value;
                            info.parentId = parentIdsLen > i ? parentIds[i] : 0;
                            method.setAccessible(true);
                            EventListenerManager.addEventMethod(finder, info, event, handler, method);
                        }
                    }
                } catch (Throwable ex) {
                    LogUtil.e(ex.getMessage(), ex);
                }
            }
        }
    }

    injectObject(handler, handlerType.getSuperclass(), finder);
}