Example usage for java.lang.reflect Modifier isStatic

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

Introduction

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

Prototype

public static boolean isStatic(int mod) 

Source Link

Document

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

Usage

From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java

/**
 *  (?),key fieldNamevalue ./* ww w . ja  v a2  s  . co m*/
 *
 * @param obj
 *            the obj
 * @param excludeFieldNames
 *            ?field names,?nullOrEmpty ?
 * @return the field value map
 * @throws ReflectException
 *             the reflect exception
 */
public static Map<String, Object> getFieldValueMap(Object obj, String[] excludeFieldNames)
        throws ReflectException {

    // (?,)
    Field[] fields = getDeclaredFields(obj);

    Map<String, Object> map = new TreeMap<String, Object>();
    if (Validator.isNotNullOrEmpty(fields)) {
        for (Field field : fields) {
            String fieldName = field.getName();

            if (Validator.isNotNullOrEmpty(excludeFieldNames)
                    && ArrayUtil.isContain(excludeFieldNames, fieldName)) {
                continue;
            }

            int modifiers = field.getModifiers();
            // ??? log
            boolean isPrivateAndStatic = Modifier.isPrivate(modifiers) && Modifier.isStatic(modifiers);
            log.debug("field name:[{}],modifiers:[{}],isPrivateAndStatic:[{}]", fieldName, modifiers,
                    isPrivateAndStatic);

            if (!isPrivateAndStatic) {
                //TODO see org.apache.commons.lang3.reflect.MemberUtils.setAccessibleWorkaround(AccessibleObject)
                field.setAccessible(true);
                try {
                    map.put(fieldName, field.get(obj));
                } catch (Exception e) {
                    log.error(e.getClass().getName(), e);
                    throw new ReflectException(e);
                }
            }
        }
    }
    return map;
}

From source file:utils.ReflectionService.java

public static JsonNode createJsonNode(Class clazz, int maxDeep) {

    if (maxDeep <= 0 || JsonNode.class.isAssignableFrom(clazz)) {
        return null;
    }//from   w  w  w  .jav  a 2  s  .com

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode objectNode = objectMapper.createObjectNode();

    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        String key = field.getName();
        try {
            // is array or collection
            if (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType())) {
                ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                Class<?> type = (Class<?>) collectionType.getActualTypeArguments()[0];
                ArrayNode arrayNode = objectMapper.createArrayNode();
                arrayNode.add(createJsonNode(type, maxDeep - 1));
                objectNode.put(key, arrayNode);
            } else {
                Class<?> type = field.getType();
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                } else if (type.isEnum()) {
                    objectNode.put(key, Enum.class.getName());
                } else if (!type.getName().startsWith("java") && !key.startsWith("_")) {
                    objectNode.put(key, createJsonNode(type, maxDeep - 1));
                } else {
                    objectNode.put(key, type.getName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return objectNode;
}

From source file:org.apache.hadoop.hive.ql.stats.TestStatsUtils.java

@Test
public void testPrimitiveSizeEstimations() throws Exception {
    HiveConf conf = new HiveConf();
    Set<String> exclusions = Sets.newHashSet();
    exclusions.add(serdeConstants.VOID_TYPE_NAME);
    exclusions.add(serdeConstants.LIST_TYPE_NAME);
    exclusions.add(serdeConstants.MAP_TYPE_NAME);
    exclusions.add(serdeConstants.STRUCT_TYPE_NAME);
    exclusions.add(serdeConstants.UNION_TYPE_NAME);
    Field[] serdeFields = serdeConstants.class.getFields();
    for (Field field : serdeFields) {
        if (!Modifier.isStatic(field.getModifiers())) {
            continue;
        }//from  w  w w.  j a va  2  s  . co  m
        if (!field.getName().endsWith("_TYPE_NAME")) {
            continue;
        }
        String typeName = (String) FieldUtils.readStaticField(field);
        if (exclusions.contains(typeName)) {
            continue;
        }
        long siz = StatsUtils.getSizeOfPrimitiveTypeArraysFromType(typeName, 3, conf);
        assertNotEquals(field.toString(), 0, siz);
    }
}

From source file:org.androidtransfuse.adapter.classes.ASTClassType.java

@Override
public boolean isStatic() {
    return Modifier.isStatic(clazz.getModifiers());
}

From source file:de.bstreit.java.oscr.initialdata.AbstractDataContainer.java

/**
 * //w w  w  .  j ava2s . com
 * @param field
 * @return true, if the field is a constant (public static final) and of the
 *         expected type provided by {@link #getType()}
 */
private boolean isConstantAndOfType(Field field) {
    final int modifiers = field.getModifiers();

    final boolean isConstant = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
            && Modifier.isPublic(modifiers);

    final boolean correctType = field.getType().equals(getType());

    return isConstant && correctType;
}

From source file:com.palantir.ptoss.util.Reflections.java

/**
 * Returns whether or not the given {@link Field} is static.
 *///  w  w w  .  j  a v  a2  s  .c  o  m
public static boolean isFieldStatic(Field field) {
    int modifiers = field.getModifiers();
    return Modifier.isStatic(modifiers);
}

From source file:com.github.braully.web.DescriptorHtmlEntity.java

private void addHtmlFilterElement(Field field) {
    if (elementsFilter == null) {
        elementsFilter = new ArrayList<>();
    }//from   w  w w  .  j av a 2s.  c  om
    final int modifiers = field.getModifiers();
    if (!Modifier.isStatic(modifiers) && !hiddenFilterProperties.contains(field.getName())) {
        elementsFilter.add(buildDescriptorHtmlEntity(field));
    }
}

From source file:org.apache.niolex.commons.reflect.MethodFilter.java

/**
 * This is the override of super method.
 * @see org.apache.niolex.commons.reflect.MethodUtil.Filter#isValid(java.lang.reflect.Method)
 *//*from w ww . j  a v  a 2s . c o  m*/
@Override
public boolean isValid(Method m) {
    if (methodName != null && !methodName.equals(m.getName())) {
        return false;
    }
    if (returnType != null && !ClassUtils.isAssignable(m.getReturnType(), returnType, true)) {
        return false;
    }
    if (parameterTypes != null && !ClassUtils.isAssignable(parameterTypes, m.getParameterTypes(), true)) {
        return false;
    }
    if (!includeStatic && Modifier.isStatic(m.getModifiers())) {
        return false;
    }
    if (!includeAbstract && Modifier.isAbstract(m.getModifiers())) {
        return false;
    }
    if (!includeSynthetic && m.isSynthetic()) {
        return false;
    }
    return true;
}

From source file:org.apromore.test.heuristic.JavaBeanHeuristic.java

/**
 * Checks that a Javabean matches a set of rules in terms of its structure and behaviour. Given a class representing a Javabean, run the following
 * checks. <ol> <li>The class has a public no-arg constructor</li> <li>For each field declared in the class, a public getter and setter
 * exists</li> <li>Set the value using the setter, then ensure that the getter returns the same object</li> </ol>
 *
 * @param clazz the class to check./*from  w  w  w . j  a  va2s.  co m*/
 * @param excludes any field names that should be excluded from the check.
 */
public void checkJavaBeanProperties(Class clazz, String... excludes) {
    Field[] fields = clazz.getDeclaredFields();
    Object bean = newInstance(clazz);
    List<String> excludeList = Arrays.asList(excludes);
    for (Field field : fields) {
        if (excludeList.contains(field.getName())) {
            continue;
        }

        int modifiers = field.getModifiers();
        if (!(Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers))) {
            processField(field, bean);
        }
    }
}

From source file:org.apache.syncope.core.persistence.jpa.entity.JPAAnyUtils.java

private static void initFieldNames(final Class<?> entityClass, final Set<String> keys) {
    List<Class<?>> classes = ClassUtils.getAllSuperclasses(entityClass);
    classes.add(entityClass);/*from   ww w . j a  v  a 2  s.  c  om*/
    for (Class<?> clazz : classes) {
        for (Field field : clazz.getDeclaredFields()) {
            if (!Modifier.isStatic(field.getModifiers()) && !field.getName().startsWith("pc")
                    && !Collection.class.isAssignableFrom(field.getType())
                    && !Map.class.isAssignableFrom(field.getType())) {

                keys.add("id".equals(field.getName()) ? "key" : field.getName());
            }
        }
    }
}