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.appleframework.core.utils.ClassUtility.java

/** ??method?? */
public static String getSimpleMethodSignature(Method method, boolean withModifiers, boolean withReturnType,
        boolean withClassName, boolean withExceptionType) {
    if (method == null) {
        return null;
    }/*from  w  w  w.  ja v  a2 s  .c o m*/

    StringBuilder buf = new StringBuilder();

    if (withModifiers) {
        buf.append(Modifier.toString(method.getModifiers())).append(' ');
    }

    if (withReturnType) {
        buf.append(getSimpleClassName(method.getReturnType())).append(' ');
    }

    if (withClassName) {
        buf.append(getSimpleClassName(method.getDeclaringClass())).append('.');
    }

    buf.append(method.getName()).append('(');

    Class<?>[] paramTypes = method.getParameterTypes();

    for (int i = 0; i < paramTypes.length; i++) {
        Class<?> paramType = paramTypes[i];

        buf.append(getSimpleClassName(paramType));

        if (i < paramTypes.length - 1) {
            buf.append(", ");
        }
    }

    buf.append(')');

    if (withExceptionType) {
        Class<?>[] exceptionTypes = method.getExceptionTypes();

        if (!isEmptyArray(exceptionTypes)) {
            buf.append(" throws ");

            for (int i = 0; i < exceptionTypes.length; i++) {
                Class<?> exceptionType = exceptionTypes[i];

                buf.append(getSimpleClassName(exceptionType));

                if (i < exceptionTypes.length - 1) {
                    buf.append(", ");
                }
            }
        }
    }

    return buf.toString();
}

From source file:org.araneaframework.backend.util.BeanMapper.java

/**
 * Returns <code>List&lt;String&gt;</code>- the <code>List</code> of VO
 *         field names./*ww  w  . jav a  2s .  com*/
 * @return <code>List&lt;String&gt;</code>- the <code>List</code> of VO
 *         field names.
 */
public List getBeanFields() {
    List result = new ArrayList();

    Method[] voMethods = voClass.getMethods();
    for (int i = 0; i < voMethods.length; i++) {
        Method voMethod = voMethods[i];
        //Checking that method may be a valid getter method
        if (Modifier.isPublic(voMethod.getModifiers()) && (voMethod.getParameterTypes().length == 0)
                && !(voMethod.getReturnType().isAssignableFrom(Void.class))) {
            //Checking that it's a getter method, and it has a corresponding
            // setter method.
            if (voMethod.getName().startsWith("get") && !"getClass".equals(voMethod.getName())) {
                //Adding the field...
                result.add(voMethod.getName().substring(3, 4).toLowerCase() + voMethod.getName().substring(4));
            } else if (voMethod.getName().startsWith("is") && (Boolean.class.equals(voMethod.getReturnType())
                    || boolean.class.equals(voMethod.getReturnType()))) {
                //Adding the field...
                result.add(voMethod.getName().substring(2, 3).toLowerCase() + voMethod.getName().substring(3));

            }
        }
    }

    return result;
}

From source file:org.echocat.redprecursor.annotations.utils.AccessAlsoProtectedMembersReflectivePropertyAccessor.java

@Override
protected Method findSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
    Method result = null;/*from w w  w .ja v a 2s .c  o  m*/
    final PropertyDescriptor propertyDescriptor = findPropertyDescriptorFor(clazz, propertyName);
    if (propertyDescriptor != null) {
        result = propertyDescriptor.getWriteMethod();
    }
    if (result == null) {
        Class<?> current = clazz;
        final String setterName = "set" + StringUtils.capitalize(propertyName);
        while (result == null && !Object.class.equals(current)) {
            final Method[] potentialMethods = current.getDeclaredMethods();
            for (Method potentialMethod : potentialMethods) {
                if (setterName.equals(potentialMethod.getName())) {
                    if (potentialMethod.getParameterTypes().length == 1) {
                        if (!mustBeStatic || Modifier.isStatic(potentialMethod.getModifiers())) {
                            if (!potentialMethod.isAccessible()) {
                                potentialMethod.setAccessible(true);
                            }
                            result = potentialMethod;
                        }
                    }
                }
            }
            current = current.getSuperclass();
        }
    }
    return result;
}

From source file:org.jgentleframework.integration.remoting.rmi.support.RmiBinderInterceptor.java

@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

    Remote stub = (Remote) this.binder.getStub();
    MethodInvocation invocation = new BasicMethodInvocation(obj, method, args);
    Object result = null;//from   w w w.ja va2s  . co m
    if (Modifier.isAbstract(method.getModifiers())) {
        try {
            result = this.binder.invoke(invocation, stub);
        } catch (InvocationTargetException ex) {
            Throwable targetEx = ex.getTargetException();
            if (targetEx instanceof RemoteException) {
                RemoteException rex = (RemoteException) targetEx;
                throw ExceptionUtils.convertRmiAccessException(invocation.getMethod(), rex,
                        this.binder.isConnectFailure(rex), this.binder.getServiceName());
            } else {
                Throwable exToThrow = ex.getTargetException();
                ExceptionUtils.fillInClientStackTraceIfPossible(exToThrow);
                throw exToThrow;
            }
        } catch (NoSuchMethodException ex) {
            throw new RemoteInvocationException("No matching RMI stub method found for: " + method, ex);
        } catch (RemoteException e) {
            if (this.binder.isConnectFailure(e) && this.binder.isRefreshStubOnConnectFailure()) {
                this.binder.refreshStubAndRetryInvocation(invocation);
            } else {
                throw new RemoteInvocationException("Invocation of method [" + invocation.getMethod()
                        + "] failed in RMI service [" + this.binder.getServiceName() + "]", e);
            }
        } catch (Throwable ex) {
            if (log.isErrorEnabled()) {
                log.error("Invocation of method [" + invocation.getMethod() + "] failed in RMI service ["
                        + this.binder.getServiceName() + "]", ex);
            }
            throw new RemoteInvocationException("Invocation of method [" + invocation.getMethod()
                    + "] failed in RMI service [" + this.binder.getServiceName() + "]", ex);
        }
    }
    return result;
}

From source file:com.xiongyingqi.util.MethodInvoker.java

/**
 * Invoke the specified method.//w w w .  j a  va 2s  .com
 * <p>The invoker needs to have been prepared before.
 *
 * @return the object (possibly null) returned by the method invocation,
 * or {@code null} if the method has a void return type
 * @throws java.lang.reflect.InvocationTargetException if the target method threw an exception
 * @throws IllegalAccessException                      if the target method couldn't be accessed
 * @see #prepare
 */
public Object invoke() throws InvocationTargetException, IllegalAccessException {
    // In the static case, target will simply be {@code null}.
    Object targetObject = getTargetObject();
    Method preparedMethod = getPreparedMethod();
    if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) {
        throw new IllegalArgumentException("Target method must not be non-static without a target");
    }
    ReflectionUtils.makeAccessible(preparedMethod);
    return preparedMethod.invoke(targetObject, getArguments());
}

From source file:com.googlecode.loosejar.ClassLoaderAnalyzer.java

@SuppressWarnings("unchecked")
private Enumeration<URL> findManifestResources() {
    //invoke #findResource(String) method reflectively as it is protected in java.lang.ClassLoader
    try {/*  ww w. j  a  va  2s  .  com*/
        Method method = findMethod(classLoader.getClass(), "findResources", new Class<?>[] { String.class });

        //attempt to disable security check for non-public methods.
        if (!Modifier.isPublic(method.getModifiers()) && !method.isAccessible()) {
            method.setAccessible(true);
        }

        // This will return a transitive closure of all jars on the classpath
        // in the form of
        // jar:file:/foo/bar/baz.jar!/META-INF/MANIFEST.MF
        return (Enumeration<URL>) method.invoke(classLoader, "META-INF/MANIFEST.MF");

    } catch (IllegalAccessException e) {
        log("Failed to invoke #findResources(String) method on classloader [" + classLoader + "]. "
                + "No access permissions.");
    } catch (InvocationTargetException e) {
        log("Failed to invoke #findResources(String) method on classloader [" + classLoader + "]. "
                + "The classloader is likely no longer available.");
    }
    return null;
}

From source file:net.orfjackal.retrolambda.test.LambdaTest.java

/**
 * We could make private lambda implementation methods package-private,
 * so that the lambda class may call them, but we should not make any
 * more methods non-private than is absolutely necessary.
 *//*from   w w w . j  ava  2s.c  o m*/
@Test
public void will_not_change_the_visibility_of_unrelated_methods() throws Exception {
    assertThat(unrelatedPrivateMethod(), is("foo"));

    Method method = getClass().getDeclaredMethod("unrelatedPrivateMethod");
    int modifiers = method.getModifiers();

    assertTrue("expected " + method.getName() + " to be private, but modifiers were: "
            + Modifier.toString(modifiers), Modifier.isPrivate(modifiers));
}

From source file:com.tmall.wireless.tangram3.support.ExposureSupport.java

private void findTraceMethods(Method[] methods) {
    for (Method method : methods) {
        String methodName = method.getName();
        if (isValidTraceMethodName(methodName)) {
            int modifiers = method.getModifiers();
            if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 3) {
                    Class<?> viewType = parameterTypes[0];
                    Class<?> cellType = parameterTypes[1];
                    Class<?> clickIntType = parameterTypes[2];
                    if (View.class.isAssignableFrom(viewType) && BaseCell.class.isAssignableFrom(cellType)
                            && (clickIntType.equals(int.class) || clickIntType.equals(Integer.class))) {
                        mOnTraceMethods.put(viewType, new OnTraceMethod(3, method));
                    }/*from w  w w  .  j  a v  a2  s.  com*/
                }
            }
        }
    }
}

From source file:org.gradle.api.internal.project.AnnotationProcessingTaskFactory.java

private boolean isGetter(Method method) {
    return method.getName().startsWith("get") && method.getReturnType() != Void.TYPE
            && method.getParameterTypes().length == 0 && !Modifier.isStatic(method.getModifiers());
}

From source file:com.tmall.wireless.tangram3.support.ExposureSupport.java

private void findExposureMethods(Method[] methods) {
    for (Method method : methods) {
        String methodName = method.getName();
        if (isValidExposureMethodName(methodName)) {
            int modifiers = method.getModifiers();
            if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 3) {
                    Class<?> viewType = parameterTypes[0];
                    Class<?> cellType = parameterTypes[1];
                    Class<?> clickIntType = parameterTypes[2];
                    if (View.class.isAssignableFrom(viewType) && BaseCell.class.isAssignableFrom(cellType)
                            && (clickIntType.equals(int.class) || clickIntType.equals(Integer.class))) {
                        mOnExposureMethods.put(viewType, new OnTraceMethod(3, method));
                    }//from  ww w.j a  va2 s  .  c  o  m
                }
            }
        }
    }
}