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.freetmp.common.util.ClassUtils.java

public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
    if (method != null && isOverridable(method, targetClass) && targetClass != null
            && !targetClass.equals(method.getDeclaringClass())) {
        try {/*from   w  w w  .  j  av a  2s.  c o  m*/
            if (Modifier.isPublic(method.getModifiers())) {
                try {
                    return targetClass.getMethod(method.getName(), method.getParameterTypes());
                } catch (NoSuchMethodException ex) {
                    return method;
                }
            } else {
                Method specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(),
                        method.getParameterTypes());
                return (specificMethod != null ? specificMethod : method);
            }
        } catch (SecurityException ex) {
            // Security settings are disallowing reflective access; fall back to 'method' below.
        }
    }
    return method;
}

From source file:com.evolveum.midpoint.repo.sql.query2.definition.ClassDefinitionParser.java

private JpaEntityDefinition parseClass(Class jpaClass) {

    Class jaxbClass = getJaxbClassForEntity(jpaClass);
    JpaEntityDefinition entity = new JpaEntityDefinition(jpaClass, jaxbClass);
    LOGGER.trace("### {}", entity);

    addVirtualDefinitions(jpaClass, entity);
    Method[] methods = jpaClass.getMethods();

    for (Method method : methods) {
        String methodName = method.getName();
        if (Modifier.isStatic(method.getModifiers()) || "getClass".equals(methodName)
                || (!methodName.startsWith("is") && !methodName.startsWith("get"))
                || method.getAnnotation(NotQueryable.class) != null) {
            //it's not getter for queryable property
            continue;
        }//from  www. j  a  va  2s  .c o m

        if (method.isAnnotationPresent(Transient.class)) {
            continue;
        }

        LOGGER.trace("# {}", method);

        JpaLinkDefinition linkDefinition;
        OwnerGetter ownerGetter = method.getAnnotation(OwnerGetter.class);
        if (ownerGetter != null) {
            String jpaName = getJpaName(method);
            JpaDataNodeDefinition nodeDefinition = new JpaEntityPointerDefinition(ownerGetter.ownerClass());
            // Owner is considered as not embedded, so we generate left outer join to access it
            // (instead of implicit inner join that would be used if we would do x.owner.y = '...')
            linkDefinition = new JpaLinkDefinition(new ParentPathSegment(), jpaName, null, false,
                    nodeDefinition);
        } else {
            linkDefinition = parseMethod(method);
        }
        entity.addDefinition(linkDefinition);
    }
    return entity;
}

From source file:org.ajax4jsf.builder.config.ComponentBaseBean.java

public boolean isSuperSaveStateMethodExists() {
    try {/*  www . j  a  v a 2 s. co m*/
        Class superClass = getLoader().loadClass(getSuperclass());
        Class[] signature = { FacesContext.class };
        try {
            Method m = superClass.getMethod("saveState", signature);
            return !Modifier.isAbstract(m.getModifiers());
        } catch (NoSuchMethodException e) {
            return false;
        }
    } catch (ClassNotFoundException e) {
        getLog().error("superclass not found for tag " + getTag().getName(), e);
        return false;
    }
}

From source file:com.taobao.adfs.util.Utilities.java

/**
 * not support more override method, ASM is a better solution.
 * refer: http://stackoverflow.com/questions/4024587/get-callers-method-not-name
 *//*from www . ja va 2 s. c  om*/
public static Method getCaller(boolean staticMethod) throws IOException {
    StackTraceElement caller = Thread.currentThread().getStackTrace()[3];
    Class<?> clazz = ClassCache.getWithIOException(caller.getClassName());
    for (Method method : clazz.getDeclaredMethods()) {
        if (!method.getName().equals(caller.getMethodName()))
            continue;
        if (Modifier.isStatic(method.getModifiers()) != staticMethod)
            continue;
        return method;
    }
    throw new IOException("fail to get caller");
}

From source file:com.datos.vfs.util.DelegatingFileSystemOptionsBuilder.java

/**
 * create the list of all set*() methods for the given scheme
 *//*from   w  w w.  ja  v  a  2  s  .co  m*/
private Map<String, List<Method>> createSchemeMethods(final String scheme) throws FileSystemException {
    final FileSystemConfigBuilder fscb = getManager().getFileSystemConfigBuilder(scheme);
    if (fscb == null) {
        throw new FileSystemException("vfs.provider/no-config-builder.error", scheme);
    }

    final Map<String, List<Method>> schemeMethods = new TreeMap<>();

    final Method[] methods = fscb.getClass().getMethods();
    for (final Method method : methods) {
        if (!Modifier.isPublic(method.getModifiers())) {
            continue;
        }

        final String methodName = method.getName();
        if (!methodName.startsWith("set")) {
            // not a setter
            continue;
        }

        final String key = methodName.substring(3).toLowerCase();

        List<Method> configSetter = schemeMethods.get(key);
        if (configSetter == null) {
            configSetter = new ArrayList<>(2);
            schemeMethods.put(key, configSetter);
        }
        configSetter.add(method);
    }

    return schemeMethods;
}

From source file:org.ajax4jsf.builder.config.ComponentBaseBean.java

public boolean isSuperIsTransientMethodExists() {
    try {//from   w w w. j a v a 2 s .  com
        Class superClass = getLoader().loadClass(getSuperclass());
        Class[] signature = new Class[0];
        try {
            Method m = superClass.getMethod("isTransient", signature);
            return !Modifier.isAbstract(m.getModifiers());
        } catch (NoSuchMethodException e) {
            return false;
        }
    } catch (ClassNotFoundException e) {
        getLog().error("superclass not found for tag " + getTag().getName(), e);
        return false;
    }
}

From source file:org.ajax4jsf.builder.config.ComponentBaseBean.java

public boolean isSuperRestoreStateMethodExists() {
    try {/*from   w w  w  .  ja v a  2s . c  om*/
        Class superClass = getLoader().loadClass(getSuperclass());
        Class[] signature = { FacesContext.class, Object.class };
        try {
            Method m = superClass.getMethod("restoreState", signature);
            return !Modifier.isAbstract(m.getModifiers());
        } catch (NoSuchMethodException e) {
            return false;
        }
    } catch (ClassNotFoundException e) {
        getLog().error("superclass not found for tag " + getTag().getName(), e);
        return false;
    }
}

From source file:org.ajax4jsf.builder.config.ComponentBaseBean.java

public boolean isSuperSetTransientMethodExists() {
    try {//from   ww  w.  j ava 2s.  c o m
        Class superClass = getLoader().loadClass(getSuperclass());
        Class[] signature = { boolean.class };
        try {
            Method m = superClass.getMethod("setTransient", signature);
            return !Modifier.isAbstract(m.getModifiers());
        } catch (NoSuchMethodException e) {
            return false;
        }
    } catch (ClassNotFoundException e) {
        getLog().error("superclass not found for tag " + getTag().getName(), e);
        return false;
    }
}

From source file:org.crazydog.util.spring.ClassUtils.java

/**
 * Given a method, which may come from an interface, and a target class used
 * in the current reflective invocation, find the corresponding target method
 * if there is one. E.g. the method may be {@code IFoo.bar()} and the
 * target class may be {@code DefaultFoo}. In this case, the method may be
 * {@code DefaultFoo.bar()}. This enables attributes on that method to be found.
 * <p><b>NOTE:</b> In contrast to {@link org.springframework.aop.support.AopUtils#getMostSpecificMethod},
 * this method does <i>not</i> resolve Java 5 bridge methods automatically.
 * Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod}
 * if bridge method resolution is desirable (e.g. for obtaining metadata from
 * the original method definition).//from  w  w w.j ava 2  s .c o  m
 * <p><b>NOTE:</b> Since Spring 3.1.1, if Java security settings disallow reflective
 * access (e.g. calls to {@code Class#getDeclaredMethods} etc, this implementation
 * will fall back to returning the originally provided method.
 * @param method the method to be invoked, which may come from an interface
 * @param targetClass the target class for the current invocation.
 * May be {@code null} or may not even implement the method.
 * @return the specific target method, or the original method if the
 * {@code targetClass} doesn't implement it or is {@code null}
 */
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
    if (method != null && isOverridable(method, targetClass) && targetClass != null
            && !targetClass.equals(method.getDeclaringClass())) {
        try {
            if (Modifier.isPublic(method.getModifiers())) {
                try {
                    return targetClass.getMethod(method.getName(), method.getParameterTypes());
                } catch (NoSuchMethodException ex) {
                    return method;
                }
            } else {
                Method specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(),
                        method.getParameterTypes());
                return (specificMethod != null ? specificMethod : method);
            }
        } catch (SecurityException ex) {
            // Security settings are disallowing reflective access; fall back to 'method' below.
        }
    }
    return method;
}

From source file:net.sf.ehcache.config.BeanHandler.java

/**
 * Finds a setter method.//  ww w.  ja v a2s.c o m
 */
private Method findSetMethod(final Class objClass, final String prefix, final String name) throws Exception {
    final String methodName = makeMethodName(prefix, name);
    final Method[] methods = objClass.getMethods();
    Method candidate = null;
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        if (!method.getName().equals(methodName)) {
            continue;
        }
        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }
        if (method.getParameterTypes().length != 1) {
            continue;
        }
        if (!method.getReturnType().equals(Void.TYPE)) {
            continue;
        }
        if (candidate != null) {
            throw new Exception("Multiple " + methodName + "() methods in class " + objClass.getName() + ".");
        }
        candidate = method;
    }

    return candidate;
}