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.gradle.api.internal.AbstractClassGenerator.java

private void inspectType(Class<?> type, ClassMetaData classMetaData) {
    ClassDetails classDetails = ClassInspector.inspect(type);
    for (Method method : classDetails.getAllMethods()) {
        if (method.getAnnotation(Inject.class) != null) {
            if (!Modifier.isPublic(method.getModifiers()) && !Modifier.isProtected(method.getModifiers())) {
                throw new UnsupportedOperationException(String.format(
                        "Cannot attach @Inject to method %s.%s() as it is not public or protected.",
                        method.getDeclaringClass().getSimpleName(), method.getName()));
            }/* w  w w. ja v a2s. com*/
            if (Modifier.isStatic(method.getModifiers())) {
                throw new UnsupportedOperationException(
                        String.format("Cannot attach @Inject to method %s.%s() as it is static.",
                                method.getDeclaringClass().getSimpleName(), method.getName()));
            }
        }
    }
    for (PropertyDetails property : classDetails.getProperties()) {
        PropertyMetaData propertyMetaData = classMetaData.property(property.getName());
        for (Method method : property.getGetters()) {
            propertyMetaData.addGetter(method);
        }
        for (Method method : property.getSetters()) {
            propertyMetaData.addSetter(method);
        }
    }
    for (Method method : classDetails.getInstanceMethods()) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 1) {
            classMetaData.addCandidateSetMethod(method);
        }
        if (parameterTypes.length > 0 && parameterTypes[parameterTypes.length - 1].equals(Action.class)) {
            classMetaData.addActionMethod(method);
        } else if (parameterTypes.length > 0
                && parameterTypes[parameterTypes.length - 1].equals(Closure.class)) {
            classMetaData.addClosureMethod(method);
        }
    }
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void restOperationsAreAvailable() throws Exception {
    RestTemplate delegate = mock(RestTemplate.class);
    given(delegate.getUriTemplateHandler()).willReturn(new DefaultUriBuilderFactory());
    final TestRestTemplate restTemplate = new TestRestTemplate(delegate);
    ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {

        @Override//ww  w.  ja  v a  2  s.c  o m
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Method equivalent = ReflectionUtils.findMethod(TestRestTemplate.class, method.getName(),
                    method.getParameterTypes());
            assertThat(equivalent).as("Method %s not found", method).isNotNull();
            assertThat(Modifier.isPublic(equivalent.getModifiers()))
                    .as("Method %s should have been public", equivalent).isTrue();
            try {
                equivalent.invoke(restTemplate, mockArguments(method.getParameterTypes()));
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }

        private Object[] mockArguments(Class<?>[] parameterTypes) throws Exception {
            Object[] arguments = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                arguments[i] = mockArgument(parameterTypes[i]);
            }
            return arguments;
        }

        @SuppressWarnings("rawtypes")
        private Object mockArgument(Class<?> type) throws Exception {
            if (String.class.equals(type)) {
                return "String";
            }
            if (Object[].class.equals(type)) {
                return new Object[0];
            }
            if (URI.class.equals(type)) {
                return new URI("http://localhost");
            }
            if (HttpMethod.class.equals(type)) {
                return HttpMethod.GET;
            }
            if (Class.class.equals(type)) {
                return Object.class;
            }
            if (RequestEntity.class.equals(type)) {
                return new RequestEntity(HttpMethod.GET, new URI("http://localhost"));
            }
            return mock(type);
        }

    }, (method) -> Modifier.isPublic(method.getModifiers()));

}

From source file:org.codehaus.groovy.grails.commons.ClassPropertyFetcher.java

private void init() {
    FieldCallback fieldCallback = new ReflectionUtils.FieldCallback() {
        public void doWith(Field field) {
            if (field.isSynthetic()) {
                return;
            }//w ww  . ja  v a 2 s . co  m
            final int modifiers = field.getModifiers();
            if (!Modifier.isPublic(modifiers)) {
                return;
            }

            final String name = field.getName();
            if (name.indexOf('$') == -1) {
                boolean staticField = Modifier.isStatic(modifiers);
                if (staticField) {
                    staticFetchers.put(name, new FieldReaderFetcher(field, staticField));
                } else {
                    instanceFetchers.put(name, new FieldReaderFetcher(field, staticField));
                }
            }
        }
    };

    MethodCallback methodCallback = new ReflectionUtils.MethodCallback() {
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (method.isSynthetic()) {
                return;
            }
            if (!Modifier.isPublic(method.getModifiers())) {
                return;
            }
            if (Modifier.isStatic(method.getModifiers()) && method.getReturnType() != Void.class) {
                if (method.getParameterTypes().length == 0) {
                    String name = method.getName();
                    if (name.indexOf('$') == -1) {
                        if (name.length() > 3 && name.startsWith("get")
                                && Character.isUpperCase(name.charAt(3))) {
                            name = name.substring(3);
                        } else if (name.length() > 2 && name.startsWith("is")
                                && Character.isUpperCase(name.charAt(2))
                                && (method.getReturnType() == Boolean.class
                                        || method.getReturnType() == boolean.class)) {
                            name = name.substring(2);
                        }
                        PropertyFetcher fetcher = new GetterPropertyFetcher(method, true);
                        staticFetchers.put(name, fetcher);
                        staticFetchers.put(StringUtils.uncapitalize(name), fetcher);
                    }
                }
            }
        }
    };

    List<Class<?>> allClasses = resolveAllClasses(clazz);
    for (Class<?> c : allClasses) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            try {
                fieldCallback.doWith(field);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException(
                        "Shouldn't be illegal to access field '" + field.getName() + "': " + ex);
            }
        }
        Method[] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            try {
                methodCallback.doWith(method);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException(
                        "Shouldn't be illegal to access method '" + method.getName() + "': " + ex);
            }
        }
    }

    propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz);
    for (PropertyDescriptor desc : propertyDescriptors) {
        Method readMethod = desc.getReadMethod();
        if (readMethod != null) {
            boolean staticReadMethod = Modifier.isStatic(readMethod.getModifiers());
            if (staticReadMethod) {
                staticFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod));
            } else {
                instanceFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod));
            }
        }
    }
}

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

private void findTraceMethods(Method[] methods) {
    for (Method method : methods) {
        String methodName = method.getName();
        if (!methodName.equals(ON_TRACE_METHOD_NAME) && methodName.startsWith(ON_TRACE_METHOD_NAME)
                || (methodName.startsWith(ON_TRACE_METHOD_PREFIX)
                        && methodName.endsWith(ON_TRACE_METHOD_POSTFIX))) {
            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  ww .ja va 2 s.c  o  m*/
                }
            }
        }
    }
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

public static boolean isPublic(Method method) {
    return Modifier.isPublic(method.getModifiers());
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

public static boolean isPrivate(Method method) {
    return Modifier.isPrivate(method.getModifiers());
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

public static boolean isAbstract(Method method) {
    return Modifier.isAbstract(method.getModifiers());
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

public static boolean isProtected(Method method) {
    return Modifier.isProtected(method.getModifiers());
}

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

private void findExposureMethods(Method[] methods) {
    for (Method method : methods) {
        String methodName = method.getName();
        if (!methodName.equals(ON_EXPOSURE_METHOD_NAME) && methodName.startsWith(ON_EXPOSURE_METHOD_NAME)
                || (methodName.startsWith(ON_EXPOSURE_METHOD_PREFIX)
                        && methodName.endsWith(ON_EXPOSURE_METHOD_POSTFIX))) {
            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  w w  w.  j av a  2s.  co m*/
                }
            }
        }
    }
}

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

/**
 * @param superClass//from   w ww .j a  v a 2 s.  co  m
 */
private void checkPropertiesForClass(Class<?> superClass) {
    getLog().debug("Check properties for class " + superClass.getName());
    // get all property descriptors
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(superClass);
    // for all properties, add it to component. If property have not abstract getter/setter ,
    // add it with exist = true . If property in list of hidden names, set hidden = true.
    PropertyBean property;
    for (int i = 0; i < properties.length; i++) {
        PropertyDescriptor descriptor = properties[i];
        if (!containProperty(descriptor.getName())) {
            if (isIgnorableProperty(superClass, descriptor.getName())) {
                continue;
            }
            Class<?> type = descriptor.getPropertyType();
            getLog().debug("Register property  " + descriptor.getName() + " with type name "
                    + type.getCanonicalName());
            property = new PropertyBean();
            property.setName(descriptor.getName());
            property.setDescription(descriptor.getShortDescription());
            property.setDisplayname(descriptor.getDisplayName());
            property.setClassname(descriptor.getPropertyType().getCanonicalName());
            property.setExist(true);
            addProperty(property);
        } else {
            // Load and check property.
            getLog().debug("Check  property  " + descriptor.getName());
            property = (PropertyBean) this.properties.get(descriptor.getName());
            if (property.getClassname() == null) {
                property.setClassname(descriptor.getPropertyType().getCanonicalName());
            } else {
                if (!property.getClassname().equals(descriptor.getPropertyType().getCanonicalName())) {
                    String message = "Class " + property.getClassname() + " for property " + property.getName()
                            + " not equals with real bean property type: "
                            + descriptor.getPropertyType().getCanonicalName();
                    getLog().error(message);
                    //throw new IllegalArgumentException(message);
                }
            }
            if (property.getDescription() == null) {
                property.setDescription(descriptor.getShortDescription());
            }
            if (property.getDisplayname() == null) {
                property.setDisplayname(descriptor.getDisplayName());
            }
            property.setExist(true);
        }
        Method getter = descriptor.getReadMethod();
        Method setter = descriptor.getWriteMethod();
        // Abstract methods
        if (null != setter && null != getter) {
            if ((Modifier.isAbstract(getter.getModifiers()) && Modifier.isAbstract(setter.getModifiers()))
                    || superClass.isInterface()) {
                getLog().debug("Detect as abstract property  " + descriptor.getName());
                property.setExist(false);
            }
        }

        if (null == setter || (!Modifier.isPublic(setter.getModifiers()))) {
            getLog().debug("Detect as hidden property  " + descriptor.getName());
            property.setHidden(true);
        }
        if (isAttachedProperty(property)) {
            property.setAttachedstate(true);
        }
        if (property.isInstanceof("javax.faces.el.MethodBinding")
                || property.isInstanceof("javax.faces.el.ValueBinding")) {
            property.setElonly(true);
        }

    }
}