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:ome.util.ReflectionUtils.java

static List checkGettersAndSetters(Method[] methods) {
    List goodMethods = new ArrayList();

    if (null == methods) {
        return goodMethods;
    }//from w  w  w.j a  va2s. co  m

    for (int i = 0; i < methods.length; i++) {
        boolean ok = true;
        Method method = methods[i];
        int mod = method.getModifiers();
        if (!Modifier.isPublic(mod) || Modifier.isStatic(mod)) {
            ok = false;
        }

        if (method.getName().startsWith("get")) {
            if (0 != method.getParameterTypes().length) {
                ok = false;
            }
        } else if (method.getName().startsWith("set")) {
            // No constaints yet on setters.
        } else {
            ok = false;
        }

        if (ok) {
            goodMethods.add(method);
        }
    }
    return goodMethods;
}

From source file:com.yahoo.egads.data.JsonEncoder.java

public static void fromJson(Object object, JSONObject json_obj) throws Exception {
    // for each json key-value, that has a corresponding variable in object ...
    for (Iterator k = json_obj.keys(); k.hasNext();) {
        String key = (String) k.next();
        Object value = json_obj.get(key);

        // try to access object variable
        Field field = null;/*  w w w .  jav a2 s  . com*/
        try {
            field = object.getClass().getField(key);
        } catch (Exception e) {
            continue;
        }
        if (Modifier.isStatic(field.getModifiers())) {
            continue;
        }
        if (Modifier.isPrivate(field.getModifiers())) {
            continue;
        }
        Object member = field.get(object);

        if (json_obj.isNull(key)) {
            field.set(object, null);
            continue;
            // if variable is container... recurse
        } else if (member instanceof JsonAble) {
            ((JsonAble) member).fromJson((JSONObject) value);
            // if variable is an array... recurse on sub-objects
        } else if (member instanceof ArrayList) {
            // Depends on existance of ArrayList<T> template parameter, and T constructor with no arguments.
            // May be better to use custom fromJson() in member class.
            ArrayList memberArray = (ArrayList) member;
            JSONArray jsonArray = (JSONArray) value;

            // find array element constructor
            ParameterizedType arrayType = null;
            if (field.getGenericType() instanceof ParameterizedType) {
                arrayType = (ParameterizedType) field.getGenericType();
            }
            for (Class c = member.getClass(); arrayType == null && c != null; c = c.getSuperclass()) {
                if (c.getGenericSuperclass() instanceof ParameterizedType) {
                    arrayType = (ParameterizedType) c.getGenericSuperclass();
                }
            }
            if (arrayType == null) {
                throw new Exception("could not find ArrayList element type for field 'key'");
            }
            Class elementClass = (Class) (arrayType.getActualTypeArguments()[0]);
            Constructor elementConstructor = elementClass.getConstructor();

            // for each element in JSON array ... append element to member array, recursively decode element
            for (int i = 0; i < jsonArray.length(); ++i) {
                Object element = elementConstructor.newInstance();
                fromJson(element, jsonArray.getJSONObject(i));
                memberArray.add(element);
            }
            // if variable is simple value... set
        } else if (field.getType() == float.class) {
            field.set(object, (float) json_obj.getDouble(key));
        } else {
            field.set(object, value);
        }
    }
}

From source file:com.sap.csc.poc.ems.persistence.initial.entitlement.EntitlementItemAttributeDataInitializer.java

@Override
public Collection<EntitlementItemAttribute> create() {
    ArrayList<EntitlementItemAttribute> attributes = new ArrayList<>();
    // Software License
    EntitlementType sl = entitlementTypeDataInitializer.getByName("SL");
    // Software License - Item Attributes
    Field[] itemFields = FieldUtils.getAllFields(SoftwareLicenseEntitlementItem.class);
    sl.setItemAttributes(new ArrayList<>(itemFields.length));
    for (Field itemField : itemFields) {
        if (!Modifier.isStatic(itemField.getModifiers()) &&
        // Exclusion from JPA annotations
                CollectionUtils.isEmpty(CollectionUtils.intersection(
                        // Annotations
                        Arrays.asList(itemField.getAnnotations()).stream()
                                .map(annotation -> annotation.annotationType()).collect(Collectors.toList()),
                        // Exclusions
                        EXCLUSION_PROPERTY_ATTRIBUTES)))
            sl.getItemAttributes()/*  w  ww  . ja  v a  2 s  .  c o  m*/
                    .add(generateEntitlementAttributes(itemField, new EntitlementItemAttribute()));
    }
    for (EntitlementItemAttribute itemAttribute : sl.getItemAttributes()) {
        itemAttribute.setEntitlementType(sl);
        attributes.add(itemAttribute);
    }

    return attributes;
}

From source file:com.sap.csc.poc.ems.persistence.initial.entitlement.EntitlementHeaderAttributeDataInitializer.java

@Override
public Collection<EntitlementHeaderAttribute> create() {
    ArrayList<EntitlementHeaderAttribute> attributes = new ArrayList<>();
    // Software License
    EntitlementType sl = entitlementTypeDataInitializer.getByName("SL");
    // Software License - Header Attributes
    Field[] headerFields = FieldUtils.getAllFields(SoftwareLicenseEntitlementHeader.class);
    sl.setHeaderAttributes(new ArrayList<>(headerFields.length));
    for (Field headerField : headerFields) {
        if (!Modifier.isStatic(headerField.getModifiers()) &&
        // Exclusion from JPA annotations
                CollectionUtils.isEmpty(CollectionUtils.intersection(
                        // Annotations
                        Arrays.asList(headerField.getAnnotations()).stream()
                                .map(annotation -> annotation.annotationType()).collect(Collectors.toList()),
                        // Exclusions
                        EXCLUSION_PROPERTY_ATTRIBUTES)))
            sl.getHeaderAttributes()/*w  w  w . ja v  a 2 s.c o m*/
                    .add(generateEntitlementAttributes(headerField, new EntitlementHeaderAttribute()));
    }
    for (EntitlementHeaderAttribute headerAttribute : sl.getHeaderAttributes()) {
        headerAttribute.setEntitlementType(sl);
        attributes.add(headerAttribute);
    }

    return attributes;
}

From source file:net.mindengine.blogix.utils.BlogixUtils.java

private static Method findMethodInClass(Class<?> controllerClass, String methodName) {
    Method[] methods = controllerClass.getMethods();
    for (Method method : methods) {
        if (Modifier.isStatic(method.getModifiers()) && method.getName().equals(methodName)) {
            return method;
        }/*w w w  .  j av  a 2  s.  c om*/
    }
    return null;
}

From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java

public SubResource scanMethods(Class<?> clazz, Method method) {

    int modifier = method.getModifiers();

    if (Modifier.isStatic(modifier) || !Modifier.isPublic(modifier)) {
        return null;
    }/*  ww  w.  j av a  2s.  co  m*/

    SubResource subResource = new SubResource();
    subResource.setReflectionMethod(method);

    if (!scanHttpMethod(subResource, method)) {
        return null;
    }

    scanPath(subResource, method);
    scanConsume(subResource, method);
    scanProduce(subResource, method);
    scanDetail(subResource, method);
    scanName(subResource, method);
    scanHttpCodes(subResource, method);
    scanResponseType(subResource, method);
    subResource.setClazz(clazz);

    return subResource;
}

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

@Override
protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
    Method result = null;/*  w  w w.  j a v  a  2  s  . c  om*/
    final PropertyDescriptor propertyDescriptor = findPropertyDescriptorFor(clazz, propertyName);
    if (propertyDescriptor != null) {
        result = propertyDescriptor.getReadMethod();
    }
    if (result == null) {
        Class<?> current = clazz;
        final String getterName = "get" + StringUtils.capitalize(propertyName);
        while (result == null && !Object.class.equals(current)) {
            try {
                final Method potentialMethod = current.getDeclaredMethod(getterName);
                if (!mustBeStatic || Modifier.isStatic(potentialMethod.getModifiers())) {
                    if (!potentialMethod.isAccessible()) {
                        potentialMethod.setAccessible(true);
                    }
                    result = potentialMethod;
                }
            } catch (NoSuchMethodException ignored) {
            }
            current = current.getSuperclass();
        }
    }
    return result;
}

From source file:arena.utils.ReflectionUtils.java

public static String[] getAttributeNamesUsingGetter(Class<?> entityClass) {
    //        return BeanUtils.getPropertyDescriptors(entityClass);
    Class<?> clsMe = entityClass;
    List<String> voFieldNames = new ArrayList<String>();

    while (clsMe != null) {
        Field[] fields = clsMe.getDeclaredFields();

        for (int n = 0; n < fields.length; n++) {
            int modifiers = fields[n].getModifiers();
            if (!Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) {
                String fieldName = fields[n].getName();
                try {
                    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(entityClass, fieldName);
                    if (pd.getReadMethod() != null) {
                        voFieldNames.add(fieldName);
                    }/*w w  w .j  a v  a  2s  .  c  om*/
                } catch (Throwable err) {
                    // skip
                }
            }
        }

        // Loop back to parent class
        clsMe = clsMe.getSuperclass();
    }
    return voFieldNames.toArray(new String[voFieldNames.size()]);
}

From source file:com.googlecode.ddom.weaver.inject.InjectionPlugin.java

/**
 * Add the given binding. The binding will be configured with a {@link SingletonInjector} or a
 * {@link PrototypeInjector} depending on whether the injected class is a singleton as defined
 * in the Javadoc of the {@link SingletonInjector} class.
 * //from   w w  w  . java 2  s . c  om
 * @param fieldType
 *            the target field type
 * @param injectedClass
 *            the injected class or <code>null</code> to inject a <code>null</code> value
 */
public <T> void addBinding(Class<T> fieldType, Class<? extends T> injectedClass) {
    Injector injector;
    if (injectedClass == null) {
        injector = null;
    } else {
        Field instanceField;
        try {
            Field candidateField = injectedClass.getField("INSTANCE");
            int modifiers = candidateField.getModifiers();
            // Note: getField only returns public fields, so no need to check for that
            // modifier here
            if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
                if (log.isWarnEnabled()) {
                    log.warn("Ignoring public INSTANCE field in " + injectedClass.getName()
                            + " that is not static final");
                }
                instanceField = null;
            } else if (!fieldType.isAssignableFrom(candidateField.getType())) {
                if (log.isWarnEnabled()) {
                    log.warn("Ignoring INSTANCE field of incompatible type in " + injectedClass.getName());
                }
                instanceField = null;
            } else {
                instanceField = candidateField;
            }
        } catch (NoSuchFieldException ex) {
            instanceField = null;
        }
        if (instanceField != null) {
            injector = new SingletonInjector(injectedClass.getName(), instanceField.getType().getName());
        } else {
            injector = new PrototypeInjector(injectedClass.getName());
        }
    }
    addBinding(fieldType.getName(), injector);
}

From source file:org.apache.hadoop.hbase.hbql.impl.Utils.java

public static void checkForDefaultConstructors(final Class clazz) {

    if (!clazz.getName().contains("hadoop"))
        return;/*  ww w  . j  a v  a2s.  c  o  m*/

    if (Modifier.isStatic(clazz.getModifiers()))
        return;

    if (classList.contains(clazz))
        return;
    else
        classList.add(clazz);

    if (!hasDefaultConstructor(clazz))
        System.out.println(clazz.getName() + " is missing null constructor");

    Field[] fields = clazz.getDeclaredFields();

    for (final Field field : fields) {
        Class dclazz = field.getType();
        checkForDefaultConstructors(dclazz);
    }

    fields = clazz.getFields();

    for (final Field field : fields) {
        Class dclazz = field.getType();
        checkForDefaultConstructors(dclazz);
    }
}