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:fr.certu.chouette.command.Command.java

private void executeInfoValidationParameters() throws Exception {
    try {/*from   ww  w  . j  a  v a2  s.co m*/
        Class<?> c = validationParameters.getClass();
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals("test3_2_Polygon"))
                continue;
            int m = field.getModifiers();
            if (Modifier.isPrivate(m) && !Modifier.isStatic(m)) {
                printField(c, field, "");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

}

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

/**
 * Determine whether the field is declared public static
 * @param f/*  w  w  w . ja v  a2 s. c om*/
 * @return True if the field is declared public static
 */
public static boolean isPublicStatic(Field f) {
    final int modifiers = f.getModifiers();
    return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers);
}

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

/**
 * Gets the bean size intern.// www.ja  v a  2  s . c om
 *
 * @param bean the bean
 * @param clazz the clazz
 * @param m the m
 * @param classNameMatcher the class name matcher
 * @param fieldNameMatcher the field name matcher
 * @return the bean size intern
 */
public static int getBeanSizeIntern(Object bean, Class<?> clazz, IdentityHashMap<Object, Object> m,
        Matcher<String> classNameMatcher, Matcher<String> fieldNameMatcher) {
    if (classNameMatcher.match(clazz.getName()) == false) {
        return 0;
    }
    if (clazz.isArray() == true) {
        if (clazz == boolean[].class) {
            return (((boolean[]) bean).length * 4);
        } else if (clazz == char[].class) {
            return (((char[]) bean).length * 2);
        } else if (clazz == byte[].class) {
            return (((byte[]) bean).length * 1);
        } else if (clazz == short[].class) {
            return (((short[]) bean).length * 2);
        } else if (clazz == int[].class) {
            return (((int[]) bean).length * 4);
        } else if (clazz == long[].class) {
            return (((long[]) bean).length * 4);
        } else if (clazz == float[].class) {
            return (((float[]) bean).length * 4);
        } else if (clazz == double[].class) {
            return (((double[]) bean).length * 8);
        } else {
            int length = Array.getLength(bean);
            int ret = (length * 4);
            for (int i = 0; i < length; ++i) {
                ret += getBeanSize(Array.get(bean, i), m, classNameMatcher, fieldNameMatcher);
            }
            return ret;
        }
    }
    int ret = 0;
    try {
        for (Field f : clazz.getDeclaredFields()) {
            int mod = f.getModifiers();
            if (Modifier.isStatic(mod) == true) {
                continue;
            }
            if (fieldNameMatcher.match(clazz.getName() + "." + f.getName()) == false) {
                continue;
            }
            if (f.getType() == Boolean.TYPE) {
                ret += 4;
            } else if (f.getType() == Character.TYPE) {
                ret += 2;
            } else if (f.getType() == Byte.TYPE) {
                ret += 1;
            } else if (f.getType() == Short.TYPE) {
                ret += 2;
            } else if (f.getType() == Integer.TYPE) {
                ret += 4;
            } else if (f.getType() == Long.TYPE) {
                ret += 8;
            } else if (f.getType() == Float.TYPE) {
                ret += 4;
            } else if (f.getType() == Double.TYPE) {
                ret += 8;
            } else {

                ret += 4;
                Object o = null;
                try {
                    o = readField(bean, f);
                    if (o == null) {
                        continue;
                    }
                } catch (NoClassDefFoundError ex) {
                    // nothing
                    continue;
                }
                int nestedsize = getBeanSize(o, o.getClass(), m, classNameMatcher, fieldNameMatcher);
                ret += nestedsize;
            }
        }
    } catch (NoClassDefFoundError ex) {
        // ignore here.
    }
    if (clazz == Object.class || clazz.getSuperclass() == null) {
        return ret;
    }
    ret += getBeanSizeIntern(bean, clazz.getSuperclass(), m, classNameMatcher, fieldNameMatcher);
    return ret;
}

From source file:com.sm.query.utils.QueryUtils.java

/**
 * @param field/* w  w  w. j ava2s  . co  m*/
 * @return true - for write able field
 */
public static boolean isSkipField(Field field) {
    int modifier = field.getModifiers();
    if (Modifier.isFinal(modifier) || Modifier.isStatic(modifier) || Modifier.isNative(modifier)
            || Modifier.isTransient(modifier))
        return true;
    else
        return false;
}

From source file:com.ms.commons.test.BaseTestCase.java

/**
 * ()/*  w ww .j  av  a  2 s .c o  m*/
 */
@SuppressWarnings("unchecked")
protected <T> T getObject(Object object, String fieldName) {
    Class<?> clazz = (object.getClass() == Class.class) ? (Class<?>) object : object.getClass();
    try {
        Field field = ReflectUtil.getDeclaredField(clazz, fieldName);
        field.setAccessible(true);
        if (Modifier.isStatic(field.getModifiers())) {
            return (T) field.get(null);
        } else {
            return (T) field.get(object);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

private static Object readPseudoPropertyValueNamed0(String propertyName, Object bean) {
    Object value = null;/*from ww  w .j a  va2 s .  c  o m*/
    try {
        PropertyDescriptor pd = findPropertyDescriptorFor(bean.getClass(), propertyName);
        // ???????
        if (pd != null) {
            Method reader = pd.getReadMethod();
            if (reader != null) {
                reader.setAccessible(true);
                value = reader.invoke(bean);
            } else {
                if (L.isDebugEnabled()) {
                    Map<String, Object> replace = new HashMap<String, Object>(1);
                    replace.put("property", propertyName);
                    L.debug(Strings.substitute(R.getString("E-UTIL#0012"), replace));
                }
            }
            // ???????
        } else {
            Field f = bean.getClass().getField(propertyName);
            if (f != null && !Modifier.isStatic(f.getModifiers())) {
                f.setAccessible(true);
                value = f.get(bean);
            } else {
                if (L.isDebugEnabled()) {
                    Map<String, Object> replace = new HashMap<String, Object>(1);
                    replace.put("property", propertyName);
                    L.debug(Strings.substitute(R.getString("D-UTIL#0019"), replace));
                }
            }
        }
    } catch (SecurityException e) {
        throw new InternalException(Beans.class, "E-UTIL#0010", e);
    } catch (NoSuchFieldException e) {
        Map<String, String> replace = new HashMap<String, String>(1);
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0011", replace, e);
    } catch (IllegalArgumentException e) {
        Map<String, String> replace = new HashMap<String, String>(1);
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0012", replace, e);
    } catch (IllegalAccessException e) {
        throw new InternalException(Beans.class, "E-UTIL#0013", e);
    } catch (InvocationTargetException e) {
        Map<String, String> replace = new HashMap<String, String>(2);
        replace.put("class", bean.getClass().getName());
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0014", replace, e);
    }
    return value;
}

From source file:com.ms.commons.test.BaseTestCase.java

/**
 * ()//from   w w w  .j  av a2  s.  c o  m
 */
protected void setObject(Object object, String fieldName, Object value) {
    Class<?> clazz = (object.getClass() == Class.class) ? (Class<?>) object : object.getClass();
    try {
        Field field = ReflectUtil.getDeclaredField(clazz, fieldName);
        field.setAccessible(true);
        if (Modifier.isStatic(field.getModifiers())) {
            field.set(null, value);
        } else {
            field.set(object, value);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.dpbymqn.fsm.manager.FsmManager.java

private TransitionCallback invokeTransitor(final Method m, StateListener sl) {
    final WeakReference<StateListener> slRef = new WeakReference<StateListener>(sl);
    final TransitionCallback cbk = new TransitionCallback() {
        @Override//  ww  w . ja va  2  s . c  o  m
        public void onTransition(StatefulObject st, String fromState, String toState) {
            if (m.getParameterTypes() != null) {
                // many different types and possibilities
                switch (m.getParameterTypes().length) {
                case 0:
                    invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get());
                    break;
                case 1:
                    if (String.class.equals(m.getParameterTypes()[0])) {
                        if (fromState != null) {
                            invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), toState);
                        } else {
                            invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), fromState);
                        }
                    } else if (StatefulObject.class.isAssignableFrom(m.getParameterTypes()[0])) {
                        invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), st);
                    } else {
                        //                                invoke(m, Modifier.isStatic(m.getModifiers()) ? null : sl, st);
                        throw new UnsupportedOperationException("Single parameter, but not recognized:"
                                + m.getParameterTypes()[0].getSimpleName());
                    }
                    break;
                case 2:
                    if (String.class.equals(m.getParameterTypes()[0])) {
                        invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), fromState, toState);
                    } else if (StatefulObject.class.isAssignableFrom(m.getParameterTypes()[0])) {
                        if (fromState != null) {
                            invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), st, toState);
                        } else {
                            invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), st, fromState);
                        }
                    }
                    break;
                case 3:
                    // format: void method( Stated st, String fromState, String toState )
                    invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), st, fromState, toState);
                    break;
                }
            }
        }
    };
    return cbk;
}

From source file:com.android.camera2.its.ItsSerializer.java

@SuppressWarnings("unchecked")
public static CaptureRequest.Builder deserialize(CaptureRequest.Builder mdDefault, JSONObject jsonReq)
        throws ItsException {
    try {/* w  ww. ja va2 s  .  c o m*/
        Logt.i(TAG, "Parsing JSON capture request ...");

        // Iterate over the CaptureRequest reflected fields.
        CaptureRequest.Builder md = mdDefault;
        Field[] allFields = CaptureRequest.class.getDeclaredFields();
        for (Field field : allFields) {
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())
                    && field.getType() == CaptureRequest.Key.class
                    && field.getGenericType() instanceof ParameterizedType) {
                ParameterizedType paramType = (ParameterizedType) field.getGenericType();
                Type[] argTypes = paramType.getActualTypeArguments();
                if (argTypes.length > 0) {
                    CaptureRequest.Key key = (CaptureRequest.Key) field.get(md);
                    String keyName = key.getName();
                    Type keyType = argTypes[0];

                    // For each reflected CaptureRequest entry, look inside the JSON object
                    // to see if it is being set. If it is found, remove the key from the
                    // JSON object. After this process, there should be no keys left in the
                    // JSON (otherwise an invalid key was specified).

                    if (jsonReq.has(keyName) && !jsonReq.isNull(keyName)) {
                        if (keyType instanceof GenericArrayType) {
                            Type elmtType = ((GenericArrayType) keyType).getGenericComponentType();
                            JSONArray ja = jsonReq.getJSONArray(keyName);
                            Object val[] = new Object[ja.length()];
                            for (int i = 0; i < ja.length(); i++) {
                                if (elmtType == int.class) {
                                    Array.set(val, i, ja.getInt(i));
                                } else if (elmtType == byte.class) {
                                    Array.set(val, i, (byte) ja.getInt(i));
                                } else if (elmtType == float.class) {
                                    Array.set(val, i, (float) ja.getDouble(i));
                                } else if (elmtType == long.class) {
                                    Array.set(val, i, ja.getLong(i));
                                } else if (elmtType == double.class) {
                                    Array.set(val, i, ja.getDouble(i));
                                } else if (elmtType == boolean.class) {
                                    Array.set(val, i, ja.getBoolean(i));
                                } else if (elmtType == String.class) {
                                    Array.set(val, i, ja.getString(i));
                                } else if (elmtType == Size.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i, new Size(obj.getInt("width"), obj.getInt("height")));
                                } else if (elmtType == Rect.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i, new Rect(obj.getInt("left"), obj.getInt("top"),
                                            obj.getInt("bottom"), obj.getInt("right")));
                                } else if (elmtType == Rational.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i,
                                            new Rational(obj.getInt("numerator"), obj.getInt("denominator")));
                                } else if (elmtType == RggbChannelVector.class) {
                                    JSONArray arr = ja.getJSONArray(i);
                                    Array.set(val, i,
                                            new RggbChannelVector((float) arr.getDouble(0),
                                                    (float) arr.getDouble(1), (float) arr.getDouble(2),
                                                    (float) arr.getDouble(3)));
                                } else if (elmtType == ColorSpaceTransform.class) {
                                    JSONArray arr = ja.getJSONArray(i);
                                    Rational xform[] = new Rational[9];
                                    for (int j = 0; j < 9; j++) {
                                        xform[j] = new Rational(arr.getJSONObject(j).getInt("numerator"),
                                                arr.getJSONObject(j).getInt("denominator"));
                                    }
                                    Array.set(val, i, new ColorSpaceTransform(xform));
                                } else if (elmtType == MeteringRectangle.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i, new MeteringRectangle(obj.getInt("x"), obj.getInt("y"),
                                            obj.getInt("width"), obj.getInt("height"), obj.getInt("weight")));
                                } else {
                                    throw new ItsException("Failed to parse key from JSON: " + keyName);
                                }
                            }
                            if (val != null) {
                                Logt.i(TAG, "Set: " + keyName + " -> " + Arrays.toString(val));
                                md.set(key, val);
                                jsonReq.remove(keyName);
                            }
                        } else {
                            Object val = null;
                            if (keyType == Integer.class) {
                                val = jsonReq.getInt(keyName);
                            } else if (keyType == Byte.class) {
                                val = (byte) jsonReq.getInt(keyName);
                            } else if (keyType == Double.class) {
                                val = jsonReq.getDouble(keyName);
                            } else if (keyType == Long.class) {
                                val = jsonReq.getLong(keyName);
                            } else if (keyType == Float.class) {
                                val = (float) jsonReq.getDouble(keyName);
                            } else if (keyType == Boolean.class) {
                                val = jsonReq.getBoolean(keyName);
                            } else if (keyType == String.class) {
                                val = jsonReq.getString(keyName);
                            } else if (keyType == Size.class) {
                                JSONObject obj = jsonReq.getJSONObject(keyName);
                                val = new Size(obj.getInt("width"), obj.getInt("height"));
                            } else if (keyType == Rect.class) {
                                JSONObject obj = jsonReq.getJSONObject(keyName);
                                val = new Rect(obj.getInt("left"), obj.getInt("top"), obj.getInt("right"),
                                        obj.getInt("bottom"));
                            } else if (keyType == Rational.class) {
                                JSONObject obj = jsonReq.getJSONObject(keyName);
                                val = new Rational(obj.getInt("numerator"), obj.getInt("denominator"));
                            } else if (keyType == RggbChannelVector.class) {
                                JSONObject obj = jsonReq.optJSONObject(keyName);
                                JSONArray arr = jsonReq.optJSONArray(keyName);
                                if (arr != null) {
                                    val = new RggbChannelVector((float) arr.getDouble(0),
                                            (float) arr.getDouble(1), (float) arr.getDouble(2),
                                            (float) arr.getDouble(3));
                                } else if (obj != null) {
                                    val = new RggbChannelVector((float) obj.getDouble("red"),
                                            (float) obj.getDouble("greenEven"),
                                            (float) obj.getDouble("greenOdd"), (float) obj.getDouble("blue"));
                                } else {
                                    throw new ItsException("Invalid RggbChannelVector object");
                                }
                            } else if (keyType == ColorSpaceTransform.class) {
                                JSONArray arr = jsonReq.getJSONArray(keyName);
                                Rational a[] = new Rational[9];
                                for (int i = 0; i < 9; i++) {
                                    a[i] = new Rational(arr.getJSONObject(i).getInt("numerator"),
                                            arr.getJSONObject(i).getInt("denominator"));
                                }
                                val = new ColorSpaceTransform(a);
                            } else if (keyType instanceof ParameterizedType
                                    && ((ParameterizedType) keyType).getRawType() == Range.class
                                    && ((ParameterizedType) keyType).getActualTypeArguments().length == 1
                                    && ((ParameterizedType) keyType)
                                            .getActualTypeArguments()[0] == Integer.class) {
                                JSONArray arr = jsonReq.getJSONArray(keyName);
                                val = new Range<Integer>(arr.getInt(0), arr.getInt(1));
                            } else {
                                throw new ItsException(
                                        "Failed to parse key from JSON: " + keyName + ", " + keyType);
                            }
                            if (val != null) {
                                Logt.i(TAG, "Set: " + keyName + " -> " + val);
                                md.set(key, val);
                                jsonReq.remove(keyName);
                            }
                        }
                    }
                }
            }
        }

        // Ensure that there were no invalid keys in the JSON request object.
        if (jsonReq.length() != 0) {
            throw new ItsException("Invalid JSON key(s): " + jsonReq.toString());
        }

        Logt.i(TAG, "Parsing JSON capture request completed");
        return md;
    } catch (java.lang.IllegalAccessException e) {
        throw new ItsException("Access error: ", e);
    } catch (org.json.JSONException e) {
        throw new ItsException("JSON error: ", e);
    }
}

From source file:com.alibaba.fastjson.parser.ParserConfig.java

public ObjectDeserializer createJavaBeanDeserializer(Class<?> clazz, Type type) {
    boolean asmEnable = this.asmEnable & !this.fieldBased;
    if (asmEnable) {
        JSONType jsonType = TypeUtils.getAnnotation(clazz, JSONType.class);

        if (jsonType != null) {
            Class<?> deserializerClass = jsonType.deserializer();
            if (deserializerClass != Void.class) {
                try {
                    Object deseralizer = deserializerClass.newInstance();
                    if (deseralizer instanceof ObjectDeserializer) {
                        return (ObjectDeserializer) deseralizer;
                    }/*  w  ww . jav  a2 s .  c o  m*/
                } catch (Throwable e) {
                    // skip
                }
            }

            asmEnable = jsonType.asm();
        }

        if (asmEnable) {
            Class<?> superClass = JavaBeanInfo.getBuilderClass(clazz, jsonType);
            if (superClass == null) {
                superClass = clazz;
            }

            for (;;) {
                if (!Modifier.isPublic(superClass.getModifiers())) {
                    asmEnable = false;
                    break;
                }

                superClass = superClass.getSuperclass();
                if (superClass == Object.class || superClass == null) {
                    break;
                }
            }
        }
    }

    if (clazz.getTypeParameters().length != 0) {
        asmEnable = false;
    }

    if (asmEnable && asmFactory != null && asmFactory.classLoader.isExternalClass(clazz)) {
        asmEnable = false;
    }

    if (asmEnable) {
        asmEnable = ASMUtils.checkName(clazz.getSimpleName());
    }

    if (asmEnable) {
        if (clazz.isInterface()) {
            asmEnable = false;
        }
        JavaBeanInfo beanInfo = JavaBeanInfo.build(clazz, type, propertyNamingStrategy);

        if (asmEnable && beanInfo.fields.length > 200) {
            asmEnable = false;
        }

        Constructor<?> defaultConstructor = beanInfo.defaultConstructor;
        if (asmEnable && defaultConstructor == null && !clazz.isInterface()) {
            asmEnable = false;
        }

        for (FieldInfo fieldInfo : beanInfo.fields) {
            if (fieldInfo.getOnly) {
                asmEnable = false;
                break;
            }

            Class<?> fieldClass = fieldInfo.fieldClass;
            if (!Modifier.isPublic(fieldClass.getModifiers())) {
                asmEnable = false;
                break;
            }

            if (fieldClass.isMemberClass() && !Modifier.isStatic(fieldClass.getModifiers())) {
                asmEnable = false;
                break;
            }

            if (fieldInfo.getMember() != null //
                    && !ASMUtils.checkName(fieldInfo.getMember().getName())) {
                asmEnable = false;
                break;
            }

            JSONField annotation = fieldInfo.getAnnotation();
            if (annotation != null //
                    && ((!ASMUtils.checkName(annotation.name())) //
                            || annotation.format().length() != 0 //
                            || annotation.deserializeUsing() != Void.class //
                            || annotation.unwrapped())
                    || (fieldInfo.method != null && fieldInfo.method.getParameterTypes().length > 1)) {
                asmEnable = false;
                break;
            }

            if (fieldClass.isEnum()) { // EnumDeserializer
                ObjectDeserializer fieldDeser = this.getDeserializer(fieldClass);
                if (!(fieldDeser instanceof EnumDeserializer)) {
                    asmEnable = false;
                    break;
                }
            }
        }
    }

    if (asmEnable) {
        if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) {
            asmEnable = false;
        }
    }

    if (!asmEnable) {
        return new JavaBeanDeserializer(this, clazz, type);
    }

    JavaBeanInfo beanInfo = JavaBeanInfo.build(clazz, type, propertyNamingStrategy);
    try {
        return asmFactory.createJavaBeanDeserializer(this, beanInfo);
        // } catch (VerifyError e) {
        // e.printStackTrace();
        // return new JavaBeanDeserializer(this, clazz, type);
    } catch (NoSuchMethodException ex) {
        return new JavaBeanDeserializer(this, clazz, type);
    } catch (JSONException asmError) {
        return new JavaBeanDeserializer(this, beanInfo);
    } catch (Exception e) {
        throw new JSONException("create asm deserializer error, " + clazz.getName(), e);
    }
}