Example usage for java.lang.reflect Modifier isPublic

List of usage examples for java.lang.reflect Modifier isPublic

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isPublic.

Prototype

public static boolean isPublic(int mod) 

Source Link

Document

Return true if the integer argument includes the public modifier, false otherwise.

Usage

From source file:com.klwork.common.utils.ReflectionUtils.java

/**
 * Determine whether the given field is a "public static final" constant.
 * @param field the field to check/*  w  ww  .j  av  a 2 s  .  c o  m*/
 */
public static boolean isPublicStatic(Field field) {
    int modifiers = field.getModifiers();
    return (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers));
}

From source file:org.eclipse.buildship.docs.source.SourceMetaDataVisitor.java

private void maybeAddPropertyFromField(GroovySourceAST t) {
    GroovySourceAST parentNode = getParentNode();
    boolean isField = parentNode != null && parentNode.getType() == OBJBLOCK;
    if (!isField) {
        return;/*from www  .ja  v a2 s. c  om*/
    }

    int modifiers = extractModifiers(t);
    boolean isConst = getCurrentClass().isInterface()
            || (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers));
    if (isConst) {
        visitConst(t);
        return;
    }

    boolean isProp = groovy && !Modifier.isStatic(modifiers) && !Modifier.isPublic(modifiers)
            && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers);
    if (!isProp) {
        return;
    }

    ASTIterator children = new ASTIterator(t);
    children.skip(MODIFIERS);

    String propertyName = extractIdent(t);
    TypeMetaData propertyType = extractTypeName(children.current);
    ClassMetaData currentClass = getCurrentClass();

    MethodMetaData getterMethod = currentClass
            .addMethod(String.format("get%s", StringUtils.capitalize(propertyName)), propertyType, "");
    PropertyMetaData property = currentClass.addReadableProperty(propertyName, propertyType,
            getJavaDocCommentsBeforeNode(t), getterMethod);
    findAnnotations(t, property);
    if (!Modifier.isFinal(modifiers)) {
        MethodMetaData setterMethod = currentClass
                .addMethod(String.format("set%s", StringUtils.capitalize(propertyName)), TypeMetaData.VOID, "");
        setterMethod.addParameter(propertyName, propertyType);
        currentClass.addWriteableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t),
                setterMethod);
    }
}

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

/**
 * @param superClass//from  w w w .  j  a  v a2 s. com
 */
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);
        }

    }
}

From source file:hu.bme.mit.sette.common.model.snippet.SnippetContainer.java

/**
 * Validates methods of the class./*  ww  w .java2 s  .  c om*/
 *
 * @param validator
 *            a validator
 * @return a map containing the snippet methods by their name
 */
private Map<String, Method> validateMethods(final AbstractValidator<?> validator) {
    // check: only "[public|private] static" or synthetic methods
    Map<String, Method> snippetMethods = new HashMap<String, Method>();

    for (Method method : javaClass.getDeclaredMethods()) {
        if (method.isSynthetic()) {
            // skip synthetic methods
            continue;
        }

        MethodValidator v = new MethodValidator(method);

        if (snippetMethods.get(method.getName()) != null) {
            v.addException("The method must have a unique name");
        }

        int methodModifiers = method.getModifiers();

        if (!Modifier.isPublic(methodModifiers) && !Modifier.isPrivate(methodModifiers)) {
            v.addException("The method must be public or private");
        }

        v.withModifiers(Modifier.STATIC);
        v.withoutModifiers(Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.SYNCHRONIZED);

        AnnotationMap methodAnns = SetteAnnotationUtils.getSetteAnnotations(method);

        if (Modifier.isPublic(methodModifiers)) {
            if (methodAnns.get(SetteNotSnippet.class) == null) {
                // should be snippet, validated by Snippet class and added
                // later
                snippetMethods.put(method.getName(), method);
            } else {
                // not snippet
                snippetMethods.put(method.getName(), null);

                if (methodAnns.size() != 1) {
                    v.addException("The method must not have " + "any other SETTE annotations "
                            + "if it is not a snippet.");
                }
            }
        } else {
            // method is private
            if (methodAnns.size() != 0) {
                v.addException("The method must not have " + "any SETTE annotations");
            }
        }

        validator.addChildIfInvalid(v);
    }

    return snippetMethods;
}

From source file:com.nridge.core.base.field.data.DataBeanBag.java

/**
 * Accepts a POJO containing one or more public annotated get
 * methods and creates a DataBag from them.  The DataBeanObject2
 * test class provides a reference example.
 *
 * @param anObject POJO instance.//  w ww  . j  a va  2s  . co m
 *
 * @return Data bag instance populated with field information.
 *
 * @throws NSException Thrown if object is null.
 * @throws IllegalAccessException Thrown if access is illegal.
 * @throws InvocationTargetException Thrown if target execution fails.
 */
public static DataBag fromMethodsToBag(Object anObject)
        throws NSException, IllegalAccessException, InvocationTargetException {
    DataField dataField;
    BeanField beanField;
    boolean isPublicAccess, isAnnotationPresent;

    if (anObject == null)
        throw new NSException("Object is null");

    DataBag dataBag = new DataBag(anObject.toString());
    Class<?> objClass = anObject.getClass();
    Method[] methodArray = objClass.getDeclaredMethods();
    for (Method objMethod : methodArray) {
        isPublicAccess = Modifier.isPublic(objMethod.getModifiers());
        isAnnotationPresent = objMethod.isAnnotationPresent(BeanField.class);
        if ((isAnnotationPresent) && (isPublicAccess)) {
            beanField = objMethod.getAnnotation(BeanField.class);
            dataField = reflectMethod(anObject, beanField, objMethod);
            dataBag.add(dataField);
        }
    }

    return dataBag;
}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Make the given member accessible if it isn't already.
 *//*from w  w w . j a  v  a2  s. c o m*/
private static void makeAccessible(AccessibleObject ao, int mods) {
    try {
        if (!Modifier.isPublic(mods) && !ao.isAccessible())
            AccessController.doPrivileged(J2DoPrivHelper.setAccessibleAction(ao, true));
    } catch (SecurityException se) {
        throw new UserException(_loc.get("reflect-security", ao)).setFatal(true);
    }
}

From source file:ca.uhn.fhir.jaxrs.server.AbstractJaxRsConformanceProvider.java

/**
 * This method will add a provider to the conformance. This method is almost an exact copy of {@link ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods }
 * /*  ww  w  . j  a  v a 2  s  .  com*/
 * @param theProvider
 *           an instance of the provider interface
 * @param theProviderInterface
 *           the class describing the providers interface
 * @return the numbers of basemethodbindings added
 * @see ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods
 */
public int addProvider(IResourceProvider theProvider, Class<? extends IResourceProvider> theProviderInterface)
        throws ConfigurationException {
    int count = 0;

    for (Method m : ReflectionUtil.getDeclaredMethods(theProviderInterface)) {
        BaseMethodBinding<?> foundMethodBinding = BaseMethodBinding.bindMethod(m, getFhirContext(),
                theProvider);
        if (foundMethodBinding == null) {
            continue;
        }

        count++;

        // if (foundMethodBinding instanceof ConformanceMethodBinding) {
        // myServerConformanceMethod = foundMethodBinding;
        // continue;
        // }

        if (!Modifier.isPublic(m.getModifiers())) {
            throw new ConfigurationException(
                    "Method '" + m.getName() + "' is not public, FHIR RESTful methods must be public");
        } else {
            if (Modifier.isStatic(m.getModifiers())) {
                throw new ConfigurationException(
                        "Method '" + m.getName() + "' is static, FHIR RESTful methods must not be static");
            } else {
                ourLog.debug("Scanning public method: {}#{}", theProvider.getClass(), m.getName());

                String resourceName = foundMethodBinding.getResourceName();
                ResourceBinding resourceBinding;
                if (resourceName == null) {
                    resourceBinding = myServerBinding;
                } else {
                    RuntimeResourceDefinition definition = getFhirContext().getResourceDefinition(resourceName);
                    if (myResourceNameToBinding.containsKey(definition.getName())) {
                        resourceBinding = myResourceNameToBinding.get(definition.getName());
                    } else {
                        resourceBinding = new ResourceBinding();
                        resourceBinding.setResourceName(resourceName);
                        myResourceNameToBinding.put(resourceName, resourceBinding);
                    }
                }

                List<Class<?>> allowableParams = foundMethodBinding.getAllowableParamAnnotations();
                if (allowableParams != null) {
                    for (Annotation[] nextParamAnnotations : m.getParameterAnnotations()) {
                        for (Annotation annotation : nextParamAnnotations) {
                            Package pack = annotation.annotationType().getPackage();
                            if (pack.equals(IdParam.class.getPackage())) {
                                if (!allowableParams.contains(annotation.annotationType())) {
                                    throw new ConfigurationException("Method[" + m.toString()
                                            + "] is not allowed to have a parameter annotated with "
                                            + annotation);
                                }
                            }
                        }
                    }
                }

                resourceBinding.addMethod(foundMethodBinding);
                ourLog.debug(" * Method: {}#{} is a handler", theProvider.getClass(), m.getName());
            }
        }
    }

    return count;
}

From source file:org.batoo.common.reflect.ReflectHelper.java

/**
 * Returns the property descriptors for the class.
 * //w w  w .  j  a v  a 2s . com
 * @param clazz
 *            the class
 * @return the property descriptors
 * 
 * @since 2.0.1
 */
public static PropertyDescriptor[] getProperties(Class<?> clazz) {
    final List<PropertyDescriptor> properties = Lists.newArrayList();

    final Method[] methodList = clazz.getDeclaredMethods();

    // check each method.
    for (final Method method : methodList) {
        if (method == null) {
            continue;
        }

        // skip static and private methods.
        final int mods = method.getModifiers();
        if (Modifier.isStatic(mods) || !Modifier.isPublic(mods) || method.isBridge() || method.isSynthetic()) {
            continue;
        }

        final String name = method.getName();

        if (method.getParameterTypes().length == 0) {
            if (name.startsWith(ReflectHelper.GET_PREFIX)) {
                properties.add(new PropertyDescriptor(clazz, name.substring(3), method));
            } else if ((method.getReturnType() == boolean.class) && name.startsWith(ReflectHelper.IS_PREFIX)) {
                properties.add(new PropertyDescriptor(clazz, name.substring(2), method));
            }
        }
    }

    return properties.toArray(new PropertyDescriptor[properties.size()]);
}

From source file:com.adaptc.mws.plugins.testing.transformations.TestMixinTransformation.java

public static boolean isAddableMethod(MethodNode declaredMethod) {
    ClassNode groovyMethods = GROOVY_OBJECT_CLASS_NODE;
    String methodName = declaredMethod.getName();
    return !declaredMethod.isSynthetic() && !methodName.contains("$")
            && Modifier.isPublic(declaredMethod.getModifiers())
            && !Modifier.isAbstract(declaredMethod.getModifiers())
            && !groovyMethods.hasMethod(declaredMethod.getName(), declaredMethod.getParameters());
}

From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java

@Test
public void usingFilterTest() {
    memberCriteria.membersOfType(Method.class);
    memberCriteria.add(new ToStringPredicate());
    ClassCriteria classCriteria = new ClassCriteria();
    Iterable<Class<?>> classIterable = classCriteria.getIterable(Object.class);
    Iterable<Member> memberIterable = memberCriteria.getIterable(classIterable);
    Iterator<Member> iterator = memberIterable.iterator();
    assertTrue(iterator.hasNext());//  w  w w.j av  a2s  . c  o  m
    Member member = iterator.next();
    int modifiers = member.getModifiers();
    assertTrue(Modifier.isPublic(modifiers));
    assertFalse(iterator.hasNext());
}