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:org.openinfinity.core.aspect.ArgumentBuilder.java

private void doRecursiveFieldLookUpAndCallFieldCallback(
        final ArgumentGatheringFieldCallback<Field, Object> argumentGatheringCallback, final Object[] objects) {
    for (final Object object : objects) {
        try {/*from  w  w  w . j a v a2  s  .  c  om*/
            if (object != null) {
                ReflectionUtils.doWithFields(object.getClass(), new FieldCallback() {
                    public void doWith(Field field) {
                        try {
                            if (!field.isAccessible()) {
                                field.setAccessible(Boolean.TRUE);
                            }
                            if (!(Modifier.isStatic(field.getModifiers())
                                    || Modifier.isFinal(field.getModifiers()))) {
                                argumentGatheringCallback.onField(field, object);
                                LOGGER.debug("Accessing field: " + field.getName());
                            }
                        } catch (Throwable e) {
                            LOGGER.error("Failure occurred while accessing object field.", e);
                        }
                    }
                });
            }
        } catch (Throwable throwable) {
            throw new SystemException(throwable);
        }
    }
}

From source file:org.codehaus.griffon.commons.ClassPropertyFetcher.java

private void init() {
    FieldCallback fieldCallback = new ReflectionUtils.FieldCallback() {
        public void doWith(Field field) {
            if (field.isSynthetic())
                return;
            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));
                }/*www  .ja va  2s. c om*/
            }
        }
    };

    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:org.alex73.skarynka.scan.Book2.java

private void set(Object obj, String fieldName, String value, String debug) {
    try {//from ww  w.  ja v a2s  .c o m
        Field f = obj.getClass().getField(fieldName);
        if (!Modifier.isPublic(f.getModifiers()) || Modifier.isStatic(f.getModifiers())
                || Modifier.isTransient(f.getModifiers())) {
            errors.add("Field is not public for '" + debug + "'");
            return;
        }
        if (f.getType() == int.class) {
            f.setInt(obj, Integer.parseInt(value));
        } else if (f.getType() == boolean.class) {
            f.setBoolean(obj, Boolean.parseBoolean(value));
        } else if (f.getType() == String.class) {
            f.set(obj, value);
        } else if (Set.class.isAssignableFrom(f.getType())) {
            TreeSet<String> v = new TreeSet<>(Arrays.asList(value.split(";")));
            f.set(obj, v);
        } else {
            errors.add("Unknown field class for set '" + debug + "'");
            return;
        }
    } catch (NoSuchFieldException ex) {
        errors.add("Unknown field for set '" + debug + "'");
    } catch (IllegalAccessException ex) {
        errors.add("Wrong field for set '" + debug + "'");
    } catch (Exception ex) {
        errors.add("Error set value to field for '" + debug + "'");
    }
}

From source file:Mopex.java

/**
 * Returns an array of the methods that are not static.
 * /* ww w.  j a v a 2 s .  c  o m*/
 * @return java.lang.reflect.Method[]
 * @param cls
 *            java.lang.Class
 */
//start extract getInstanceMethods
public static Method[] getInstanceMethods(Class cls) {
    List instanceMethods = new ArrayList();
    for (Class c = cls; c != null; c = c.getSuperclass()) {
        Method[] methods = c.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++)
            if (!Modifier.isStatic(methods[i].getModifiers()))
                instanceMethods.add(methods[i]);
    }
    Method[] ims = new Method[instanceMethods.size()];
    for (int j = 0; j < instanceMethods.size(); j++)
        ims[j] = (Method) instanceMethods.get(j);
    return ims;
}

From source file:cop.raml.mocks.MockUtils.java

private static TypeElementMock createClassElement(@NotNull Class<?> cls) throws ClassNotFoundException {
    TypeElementMock element = new TypeElementMock(cls.getName(), ElementKind.CLASS);
    element.setType(new TypeMirrorMock(element, TypeMirrorMock.getTypeKind(cls)));

    if (cls.getName().startsWith("cop.") || cls.getName().startsWith("spring.")) {
        VariableElementMock var;

        for (Field field : cls.getDeclaredFields())
            if ((var = createVariable(field.getName(), field.getType(),
                    Modifier.isStatic(field.getModifiers()))) != null)
                element.addEnclosedElement(setAnnotations(var, field));

        ExecutableElementMock exe;/*from  www . j  a v a 2s  .c  om*/

        for (Method method : cls.getDeclaredMethods())
            if ((exe = createExecutable(method)) != null)
                element.addEnclosedElement(setAnnotations(exe, method));
    }

    return setAnnotations(element, cls);
}

From source file:org.apache.pig.scripting.groovy.GroovyScriptEngine.java

@Override
protected Map<String, List<PigStats>> main(PigContext context, String scriptFile) throws IOException {

    PigServer pigServer = new PigServer(context, false);

    ////from  www  . ja va2  s.c  o m
    // Register dependencies
    //

    String groovyJar = getJarPath(groovy.util.GroovyScriptEngine.class);

    if (null != groovyJar) {
        pigServer.registerJar(groovyJar);
    }

    //
    // Register UDFs
    //

    registerFunctions(scriptFile, null, context);

    try {

        //
        // Load the script
        //

        Class c = gse.loadScriptByName(new File(scriptFile).toURI().toString());

        //
        // Extract the main method
        //

        Method main = c.getMethod("main", String[].class);

        if (null == main || !Modifier.isStatic(main.getModifiers()) || !Modifier.isPublic(main.getModifiers())
                || !Void.TYPE.equals(main.getReturnType())) {
            throw new IOException("No method 'public static void main(String[] args)' was found.");
        }

        //
        // Invoke the main method
        //

        Object[] args = new Object[1];
        String[] argv = (String[]) ObjectSerializer
                .deserialize(context.getProperties().getProperty(PigContext.PIG_CMD_ARGS_REMAINDERS));
        args[0] = argv;

        main.invoke(null, args);

    } catch (Exception e) {
        throw new IOException(e);
    }

    return getPigStatsMap();
}

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

@Override
protected Field findField(String name, Class<?> clazz, boolean mustBeStatic) {
    Field result = null;//from   w  ww  .  j av a2  s .  co  m
    Class<?> current = clazz;
    while (result == null && !Object.class.equals(current)) {
        try {
            final Field potentialField = current.getDeclaredField(name);
            if (!mustBeStatic || Modifier.isStatic(potentialField.getModifiers())) {
                if (!potentialField.isAccessible()) {
                    potentialField.setAccessible(true);
                }
                result = potentialField;
            }
        } catch (NoSuchFieldException ignored) {
        }
        current = current.getSuperclass();
    }
    return result;
}

From source file:Dumper.java

/**
 * Predicate to determine if a given field is worth being printed. This
 * method could be overridden to reflect customized policy.
 *
 * @param field the field at stake//from  w w w .ja  va2 s .c o m
 *
 * @return true if found relevant
 */
public static boolean isFieldRelevant(Field field) {
    // We don't print static field since the Dumper is meant for instances
    if (Modifier.isStatic(field.getModifiers())) {
        return false;
    }

    // We don't print non-user visible entities
    if (field.getName().indexOf('$') != -1) {
        return false;
    }

    return true;
}

From source file:com.github.jknack.handlebars.context.MemberValueResolver.java

/**
 * True if the member is static.//from w  ww .jav a  2s .c o m
 *
 * @param member The member object.
 * @return True if the member is statuc.
 */
protected boolean isStatic(final M member) {
    return Modifier.isStatic(member.getModifiers());
}

From source file:HashCodeAssist.java

/**
 * <p>/*from w w w  .  j  av  a2 s  . c  om*/
 * Appends the fields and values defined by the given object of the given
 * <code>Class</code>.
 * </p>
 * 
 * @param object
 *            the object to append details of
 * @param clazz
 *            the class to append details of
 * @param builder
 *            the builder to append to
 * @param useTransients
 *            whether to use transient fields
 * @param excludeFields
 *            Collection of String field names to exclude from use in
 *            calculation of hash code
 */
private static void reflectionAppend(Object object, Class<?> clazz, HashCodeAssist builder,
        boolean useTransients, String... excludeFields) {
    if (isRegistered(object)) {
        return;
    }
    try {
        register(object);
        Field[] fields = clazz.getDeclaredFields();
        List<String> excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields)
                : Collections.EMPTY_LIST;
        AccessibleObject.setAccessible(fields, true);
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            if (!excludedFieldList.contains(field.getName()) && (field.getName().indexOf('$') == -1)
                    && (useTransients || !Modifier.isTransient(field.getModifiers()))
                    && (!Modifier.isStatic(field.getModifiers()))) {
                try {
                    Object fieldValue = field.get(object);
                    builder.append(fieldValue);
                } catch (IllegalAccessException e) {
                    // this can't happen. Would get a Security exception
                    // instead
                    // throw a runtime exception in case the impossible
                    // happens.
                    throw new InternalError("Unexpected IllegalAccessException");
                }
            }
        }
    } finally {
        unregister(object);
    }
}