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:edu.cmu.tetradapp.util.TetradSerializableUtils.java

/**
 * Returns a reference to the public static serializableInstance() method of
 * clazz, if there is one; otherwise, returns null.
 *//*from  w  ww .  ja  v a 2  s .  c  o m*/
public Method serializableInstanceMethod(Class clazz) {
    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
        if ("serializableInstance".equals(method.getName())) {
            Class[] parameterTypes = method.getParameterTypes();

            if (!(parameterTypes.length == 0)) {
                continue;
            }

            if (!(Modifier.isStatic(method.getModifiers()))) {
                continue;
            }

            if (Modifier.isAbstract(method.getModifiers())) {
                continue;
            }

            return method;
        }
    }

    return null;
}

From source file:adalid.core.XS1.java

static Field getField(boolean log, String role, String name, Class<?> type, Class<?> top,
        Class<?>... validTypes) {
    String string;//from   ww  w.j a  va  2  s .co  m
    if (StringUtils.isBlank(role)) {
        string = "field role is missing or invalid";
        if (log) {
            logFieldErrorMessage(role, name, type, null, string);
        }
        return null;
    }
    if (StringUtils.isBlank(name)) {
        string = "field name is missing or invalid";
        if (log) {
            logFieldErrorMessage(role, name, type, null, string);
        }
        return null;
    }
    if (type == null) {
        string = "class is missing or invalid";
        if (log) {
            logFieldErrorMessage(role, name, type, null, string);
        }
        return null;
    }
    Field field = getField(name, type, top);
    if (field == null) {
        string = "field " + name + " not in class";
        if (log) {
            logFieldErrorMessage(role, name, type, field, string);
        }
        return null;
    }
    int modifiers = field.getModifiers();
    if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
        string = "field " + name + " has static and/or final modifier";
        if (log) {
            logFieldErrorMessage(role, name, type, field, string);
        }
        return null;
    }
    int length = validTypes == null ? 0 : validTypes.length;
    if (length < 1) {
        return field;
    }
    Class<?> ft = getTrueType(field.getType());
    String[] strings = new String[length];
    int i = 0;
    for (Class<?> vt : validTypes) {
        if (vt.isAssignableFrom(ft)) {
            return field;
        }
        strings[i++] = vt.getSimpleName();
    }
    string = "type of " + name + " is not " + StringUtils.join(strings, " or ");
    if (log) {
        logFieldErrorMessage(role, name, type, field, string);
    }
    return null;
}

From source file:com.github.helenusdriver.driver.impl.ClassInfoImpl.java

/**
 * Finds all final fields in this class and all super classes up to and
 * excluding the first class that is not annotated with the corresponding
 * entity annotation./*w  ww  .  j  a va  2 s .  c o  m*/
 *
 * @author paouelle
 *
 * @return a non-<code>null</code> map of all final fields and their default
 *         values
 */
private Map<Field, Object> findFinalFields() {
    final Map<Field, Object> ffields = new HashMap<>(8);
    MutableObject<Object> obj = null; // lazy evaluated

    // go up the hierarchy until we hit the class for which we have a default
    // serialization constructor as that class will have its final fields
    // properly initialized by the ctor whereas all others will have their final
    // fields initialized with 0, false, null, ...
    for (Class<? super T> clazz = this.clazz; clazz != constructor.getDeclaringClass(); clazz = clazz
            .getSuperclass()) {
        for (final Field field : clazz.getDeclaredFields()) {
            final int mods = field.getModifiers();

            if (Modifier.isFinal(mods) && !Modifier.isStatic(mods)) {
                field.setAccessible(true); // so we can access its value directly
                if (obj == null) {
                    // instantiates a dummy version and access its value
                    try {
                        // find default ctor even if private
                        final Constructor<T> ctor = this.clazz.getDeclaredConstructor();

                        ctor.setAccessible(true); // in case it was private
                        final T t = ctor.newInstance();

                        obj = new MutableObject<>(t);
                    } catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
                        throw new IllegalArgumentException(
                                "unable to instantiate object: " + this.clazz.getName(), e);
                    } catch (InvocationTargetException e) {
                        final Throwable t = e.getTargetException();

                        if (t instanceof Error) {
                            throw (Error) t;
                        } else if (t instanceof RuntimeException) {
                            throw (RuntimeException) t;
                        } else { // we don't expect any of those
                            throw new IllegalArgumentException(
                                    "unable to instantiate object: " + this.clazz.getName(), t);
                        }
                    }
                }
                try {
                    ffields.put(field, field.get(obj.getValue()));
                } catch (IllegalAccessException e) {
                    throw new IllegalArgumentException("unable to access final value for field: "
                            + field.getDeclaringClass().getName() + "." + field.getName(), e);
                }
            }
        }
    }
    return ffields;
}

From source file:com.glaf.core.util.ReflectUtils.java

public static boolean isBeanPropertyReadMethod(Method method) {
    return method != null && Modifier.isPublic(method.getModifiers())
            && !Modifier.isStatic(method.getModifiers()) && method.getReturnType() != void.class
            && method.getDeclaringClass() != Object.class && method.getParameterTypes().length == 0
            && ((method.getName().startsWith("get") && method.getName().length() > 3)
                    || (method.getName().startsWith("is") && method.getName().length() > 2));
}

From source file:com.glaf.core.util.ReflectUtils.java

public static boolean isBeanPropertyWriteMethod(Method method) {
    return method != null && Modifier.isPublic(method.getModifiers())
            && !Modifier.isStatic(method.getModifiers()) && method.getDeclaringClass() != Object.class
            && method.getParameterTypes().length == 1 && method.getName().startsWith("set")
            && method.getName().length() > 3;
}

From source file:org.apache.hadoop.yarn.api.TestPBImplRecords.java

/**
 * this method generate record instance by calling newIntance
 * using reflection, add register the generated value to typeValueCache
 *///w  ww .j ava2 s  .c  om
@SuppressWarnings("rawtypes")
private static Object generateByNewInstance(Class clazz) throws Exception {
    Object ret = typeValueCache.get(clazz);
    if (ret != null) {
        return ret;
    }
    Method newInstance = null;
    Type[] paramTypes = new Type[0];
    // get newInstance method with most parameters
    for (Method m : clazz.getMethods()) {
        int mod = m.getModifiers();
        if (m.getDeclaringClass().equals(clazz) && Modifier.isPublic(mod) && Modifier.isStatic(mod)
                && m.getName().equals("newInstance")) {
            Type[] pts = m.getGenericParameterTypes();
            if (newInstance == null || (pts.length > paramTypes.length)) {
                newInstance = m;
                paramTypes = pts;
            }
        }
    }
    if (newInstance == null) {
        throw new IllegalArgumentException("type " + clazz.getName() + " does not have newInstance method");
    }
    Object[] args = new Object[paramTypes.length];
    for (int i = 0; i < args.length; i++) {
        args[i] = genTypeValue(paramTypes[i]);
    }
    ret = newInstance.invoke(null, args);
    typeValueCache.put(clazz, ret);
    return ret;
}

From source file:com.hiperf.common.ui.server.storage.impl.PersistenceHelper.java

private static final Set<PropertyDescriptor> initClassMapping(String className)
        throws ClassNotFoundException, IntrospectionException, PersistenceException {
    Set<PropertyDescriptor> ids = new HashSet<PropertyDescriptor>();
    Set<PropertyDescriptor> collections = new HashSet<PropertyDescriptor>();
    Set<PropertyDescriptor> lazys = new HashSet<PropertyDescriptor>();
    Set<PropertyDescriptor> eagers = new HashSet<PropertyDescriptor>();
    Set<LinkFileInfo> linkedFiles = new HashSet<LinkFileInfo>();
    idsByClassName.put(className, ids);//from  w w w. j a  v  a2 s.com
    collectionsByClassName.put(className, collections);
    lazysByClassName.put(className, lazys);
    eagerObjectsByClassName.put(className, eagers);
    linkedFilesByClassName.put(className, linkedFiles);
    List<String> idsAttributes = new ArrayList<String>();
    Class<?> c = Class.forName(className);
    Table tableAnn = c.getAnnotation(Table.class);
    if (tableAnn != null) {
        tableByClassName.put(className, tableAnn.name());
    } else {
        Entity entityAnn = c.getAnnotation(Entity.class);
        if (entityAnn.name() != null) {
            tableByClassName.put(className, entityAnn.name());
        } else {
            tableByClassName.put(className, className.substring(className.lastIndexOf(".") + 1));
        }
    }
    BeanInfo beanInfo = Introspector.getBeanInfo(c);
    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
    propertyDescriptorsByClassName.put(className, pds);
    IdClass idClass = c.getAnnotation(IdClass.class);

    for (Field f : c.getDeclaredFields()) {
        Id id = f.getAnnotation(Id.class);
        if (id != null) {
            idsAttributes.add(f.getName());
            if (f.isAnnotationPresent(GeneratedValue.class)) {
                generatedIdClasses.add(className);
            }
        }
    }
    if (!idsAttributes.isEmpty()) {
        for (Field f : c.getDeclaredFields()) {
            if (!Modifier.isStatic(f.getModifiers())) {
                PropertyDescriptor pd = getPropertyDescriptor(pds, f);
                processField(className, pd, ids, collections, lazys, eagers, f);
            }
        }
        if (idClass != null) {
            Class clazz = idClass.value();
            for (Field f : clazz.getDeclaredFields()) {
                if (!Modifier.isStatic(f.getModifiers())) {
                    PropertyDescriptor pd = getPropertyDescriptor(pds, f);
                    processField(clazz.getName(), pd, ids, collections, lazys, eagers, f);
                }
            }
        }
        /*for(PropertyDescriptor pd : pds) {
           processLinkedFiles(pds, linkedFiles, pd);
        }*/
    } else {
        for (PropertyDescriptor pd : pds) {
            processMethod(className, pds, ids, collections, lazys, eagers, linkedFiles, pd);
        }
        if (idClass != null) {
            Class clazz = idClass.value();
            for (PropertyDescriptor pd : Introspector.getBeanInfo(clazz).getPropertyDescriptors()) {
                processMethod(clazz.getName(), pds, ids, collections, lazys, eagers, linkedFiles, pd);
            }
        }
    }
    return ids;

}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Copy all instance fiels from source to target. static fields are not copied.
 * /*from  w w w .  j  a  v  a2  s. c o  m*/
 * @param <T> the generic type
 * @param targetClass the target class
 * @param source the source
 * @param target the target
 */
public static <T> void copyInstanceProperties(Class<?> targetClass, T source, T target) {
    if (targetClass == null) {
        return;
    }

    for (Field f : targetClass.getDeclaredFields()) {
        int mod = f.getModifiers();
        if (Modifier.isStatic(mod) == true) {
            continue;
        }
        Object value = readField(source, f.getName());
        writeField(target, f, value);
    }
    copyInstanceProperties(targetClass.getSuperclass(), source, target);
}

From source file:com.p5solutions.core.utils.ReflectionUtility.java

/**
 * Find get methods with no params. All getters that aren't Native, Static,
 * Abstract, Synthetic, or Bridge are returned.
 * //from www.j  a v  a  2s.  co m
 * @param clazz
 *          the clazz
 * @return the list
 */
public synchronized static List<Method> findGetMethodsWithNoParams(Class<?> clazz) {

    /* if a cache already exists simply return it */
    List<Method> returnMethods = cachedMethods.get(clazz);

    if (returnMethods == null) {
        /* create a new cache and return it */
        returnMethods = new ArrayList<Method>();

        for (Method method : findAllMethods(clazz)) {

            // check for compiler introduced methods, as well as native methods.
            // simply ignore these methods, as they are probably not what we are
            // looking for.

            int modifiers = method.getModifiers();

            if (Modifier.isNative(modifiers) //
                    || Modifier.isStatic(modifiers) //
                    || Modifier.isAbstract(modifiers) //
                    || method.isSynthetic() //
                    || method.isBridge()) {
                continue;
            }

            // if its a getter then add it to the list
            if (isIs(method) || isGetter(method)) {
                if (!doesMethodHaveParams(method)) {
                    returnMethods.add(method);
                }
            }
        }

        cachedMethods.put(clazz, returnMethods);
    }

    return returnMethods;
}