Java Code Examples for java.lang.reflect.Modifier#isTransient()

The following examples show how to use java.lang.reflect.Modifier#isTransient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXBContextInitializer.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the field is accepted as a JAXB property.
 */
static boolean isFieldAccepted(Field field, XmlAccessType accessType) {
    // We only accept non static fields which are not marked @XmlTransient or has transient modifier
    if (field.isAnnotationPresent(XmlTransient.class)
        || Modifier.isTransient(field.getModifiers())) {
        return false;
    }
    if (Modifier.isStatic(field.getModifiers())) {
        return field.isAnnotationPresent(XmlAttribute.class);
    }
    if (accessType == XmlAccessType.PUBLIC_MEMBER
        && !Modifier.isPublic(field.getModifiers())) {
        return false;
    }
    if (accessType == XmlAccessType.NONE
        || accessType == XmlAccessType.PROPERTY) {
        return checkJaxbAnnotation(field.getAnnotations());
    }
    return true;
}
 
Example 2
Source File: ReflectUtils.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public static Map<String, Field> getBeanPropertyFields(Class cl) {
    Map<String, Field> properties = new HashMap<String, Field>();
    for(; cl != null; cl = cl.getSuperclass()) {
        Field[] fields = cl.getDeclaredFields();
        for(Field field : fields) {
            if (Modifier.isTransient(field.getModifiers())
                || Modifier.isStatic(field.getModifiers())) {
                continue;
            }

            field.setAccessible(true);

            properties.put(field.getName(), field);
        }
    }

    return properties;
}
 
Example 3
Source File: ClassUtils.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
private static void declaredOneField(Field field, Map<Integer, List<Field>> orderFiledMap,
    Map<Integer, Field> indexFiledMap, Map<String, Field> ignoreMap, ExcelIgnoreUnannotated excelIgnoreUnannotated,
    Boolean convertAllFiled) {
    ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class);
    if (excelIgnore != null) {
        ignoreMap.put(field.getName(), field);
        return;
    }
    ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
    boolean noExcelProperty = excelProperty == null
        && ((convertAllFiled != null && !convertAllFiled) || excelIgnoreUnannotated != null);
    if (noExcelProperty) {
        ignoreMap.put(field.getName(), field);
        return;
    }
    boolean isStaticFinalOrTransient =
        (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()))
            || Modifier.isTransient(field.getModifiers());
    if (excelProperty == null && isStaticFinalOrTransient) {
        ignoreMap.put(field.getName(), field);
        return;
    }
    if (excelProperty != null && excelProperty.index() >= 0) {
        if (indexFiledMap.containsKey(excelProperty.index())) {
            throw new ExcelCommonException("The index of '" + indexFiledMap.get(excelProperty.index()).getName()
                + "' and '" + field.getName() + "' must be inconsistent");
        }
        indexFiledMap.put(excelProperty.index(), field);
        return;
    }

    int order = Integer.MAX_VALUE;
    if (excelProperty != null) {
        order = excelProperty.order();
    }
    List<Field> orderFiledList = orderFiledMap.get(order);
    if (orderFiledList == null) {
        orderFiledList = new ArrayList<Field>();
        orderFiledMap.put(order, orderFiledList);
    }
    orderFiledList.add(field);
}
 
Example 4
Source File: ReflectionToStringBuilder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns whether or not to append the given <code>Field</code>.
 * <ul>
 * <li>Transient fields are appended only if {@link #isAppendTransients()} returns <code>true</code>.
 * <li>Static fields are appended only if {@link #isAppendStatics()} returns <code>true</code>.
 * <li>Inner class fields are not appened.</li>
 * </ul>
 * 
 * @param field
 *            The Field to test.
 * @return Whether or not to append the given <code>Field</code>.
 */
protected boolean accept(Field field) {
    if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
        // Reject field from inner class.
        return false;
    }
    if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) {
        // Reject transient fields.
        return false;
    }
    if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) {
        // Reject static fields.
        return false;
    }
    if (this.getExcludeFieldNames() != null
        && Arrays.binarySearch(this.getExcludeFieldNames(), field.getName()) >= 0) {
        // Reject fields from the getExcludeFieldNames list.
        return false;
    }
    return true;
}
 
Example 5
Source File: Serializer.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the field should be serialized or not
 * 
 * @param keyName
 *            key name
 * @param field
 *            The field to be serialized
 * @param getter
 *            Getter method for field
 * @return true if the field should be serialized, otherwise false
 */
public static boolean serializeField(String keyName, Field field, Method getter) {
    log.trace("serializeField - keyName: {} field: {} method: {}", new Object[] { keyName, field, getter });
    // if "field" is a class or is transient, skip it
    if ("class".equals(keyName)) {
        return false;
    }
    if (field != null) {
        if (Modifier.isTransient(field.getModifiers())) {
            log.trace("Skipping {} because its transient", keyName);
            return false;
        } else if (field.isAnnotationPresent(DontSerialize.class)) {
            log.trace("Skipping {} because its marked with @DontSerialize", keyName);
            return false;
        }
    }
    if (getter != null && getter.isAnnotationPresent(DontSerialize.class)) {
        log.trace("Skipping {} because its marked with @DontSerialize", keyName);
        return false;
    }
    log.trace("Serialize field: {}", field);
    return true;
}
 
Example 6
Source File: HessianSerializerInput.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a map of the classes fields.
 */
protected HashMap getFieldMap(Class cl)
{
    HashMap fieldMap = new HashMap();

    for (; cl != null; cl = cl.getSuperclass()) {
        Field[] fields = cl.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];

            if (Modifier.isTransient(field.getModifiers()) ||
                Modifier.isStatic(field.getModifiers()))
                continue;

            // XXX: could parameterize the handler to only deal with public
            field.setAccessible(true);

            fieldMap.put(field.getName(), field);
        }
    }

    return fieldMap;
}
 
Example 7
Source File: ReflectionToStringBuilder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns whether or not to append the given <code>Field</code>.
 * <ul>
 * <li>Transient fields are appended only if {@link #isAppendTransients()} returns <code>true</code>.
 * <li>Static fields are appended only if {@link #isAppendStatics()} returns <code>true</code>.
 * <li>Inner class fields are not appened.</li>
 * </ul>
 * 
 * @param field
 *            The Field to test.
 * @return Whether or not to append the given <code>Field</code>.
 */
protected boolean accept(Field field) {
    if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
        // Reject field from inner class.
        return false;
    }
    if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) {
        // Reject transient fields.
        return false;
    }
    if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) {
        // Reject static fields.
        return false;
    }
    if (this.excludeFieldNames != null
        && Arrays.binarySearch(this.excludeFieldNames, field.getName()) >= 0) {
        // Reject fields from the getExcludeFieldNames list.
        return false;
    }
    return true;
}
 
Example 8
Source File: BeanBinding.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void collectFieldAccessors(@Nonnull Class<?> aClass, @Nonnull List<MutableAccessor> accessors) {
  Class<?> currentClass = aClass;
  do {
    for (Field field : currentClass.getDeclaredFields()) {
      int modifiers = field.getModifiers();
      //noinspection deprecation
      if (!Modifier.isStatic(modifiers) &&
          (field.getAnnotation(OptionTag.class) != null ||
           field.getAnnotation(Tag.class) != null ||
           field.getAnnotation(Attribute.class) != null ||
           field.getAnnotation(Property.class) != null ||
           field.getAnnotation(Text.class) != null ||
           field.getAnnotation(CollectionBean.class) != null ||
           (Modifier.isPublic(modifiers) &&
            // we don't want to allow final fields of all types, but only supported
            (!Modifier.isFinal(modifiers) || Collection.class.isAssignableFrom(field.getType())) &&
            !Modifier.isTransient(modifiers) &&
            field.getAnnotation(Transient.class) == null))) {
        accessors.add(new FieldAccessor(field));
      }
    }
  }
  while ((currentClass = currentClass.getSuperclass()) != null && currentClass.getAnnotation(Transient.class) == null);
}
 
Example 9
Source File: XmlDump.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param field
 * @return true, if the given field should be compared.
 */
protected boolean accept(final Field field)
{
  if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
    // Reject field from inner class.
    return false;
  }
  if (field.getName().equals("handler") == true) {
    // Handler of Javassist proxy should be ignored.
    return false;
  }
  if (Modifier.isTransient(field.getModifiers()) == true) {
    // transients.
    return false;
  }
  if (Modifier.isStatic(field.getModifiers()) == true) {
    // transients.
    return false;
  }
  return true;
}
 
Example 10
Source File: CommentedYaml.java    From ScoreboardStats with MIT License 6 votes vote down vote up
protected void loadConfig() {
    config = getConfigFromDisk();

    for (Field field : getClass().getDeclaredFields()) {
        if (Modifier.isTransient(field.getModifiers())
                || !field.getType().isPrimitive() && field.getType() != String.class) {
            continue;
        }

        ConfigNode node = field.getAnnotation(ConfigNode.class);
        String path = field.getName();
        if (node != null && !Strings.isNullOrEmpty(path)) {
            path = node.path();
        }

        field.setAccessible(true);
        setField(path, field);
    }
}
 
Example 11
Source File: TestFieldInformation.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean hasValidField(RecordedEvent e) throws Exception {
    RecordedObject object = e.getValue("object");
    Set<Long> visited = new HashSet<>();
    while (object != null) {
        Long address = object.getValue("address");
        if (visited.contains(address)) {
            return false;
        }
        visited.add(address);
        RecordedObject referrer = object.getValue("referrer");
        RecordedObject fieldObject = referrer != null ? referrer.getValue("field") : null;
        if (fieldObject != null) {
            String name = fieldObject.getValue("name");
            if (name != null && name.equals("testField")) {
                int modifiers = (short) fieldObject.getValue("modifiers");
                if (!Modifier.isStatic(modifiers)) {
                    throw new Exception("Field should be static");
                }
                if (!Modifier.isPublic(modifiers)) {
                    throw new Exception("Field should be private");
                }
                if (!Modifier.isFinal(modifiers)) {
                    throw new Exception("Field should be final");
                }
                if (Modifier.isTransient(modifiers)) {
                    throw new Exception("Field should not be transient");
                }
                if (Modifier.isVolatile(modifiers)) {
                    throw new Exception("Field should not be volatile");
                }
                return true;
            }
        }
        object = referrer != null ? referrer.getValue("object") : null;
    }
    return false;
}
 
Example 12
Source File: BeanWrap.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
private static boolean isTransient(AnnotatedElement annotatedElement) {
    int modifiers = ((Member) annotatedElement).getModifiers();
    if (Modifier.isTransient(modifiers)) {
        return true;
    }

    return false;
}
 
Example 13
Source File: TypeLibrary.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static ValueDescriptor createField(Field field) {
    int mod = field.getModifiers();
    if (Modifier.isTransient(mod)) {
        return null;
    }
    if (Modifier.isStatic(mod)) {
        return null;
    }
    Class<?> fieldType = field.getType();
    if (!Type.isKnownType(fieldType)) {
        return null;
    }
    boolean constantPool = Thread.class == fieldType || fieldType == Class.class;
    Type type = createType(fieldType);
    String fieldName = field.getName();
    Name name = field.getAnnotation(Name.class);
    String useName = fieldName;
    if (name != null) {
        useName = name.value();
    }
    List<jdk.jfr.AnnotationElement> ans = new ArrayList<>();
    for (Annotation a : resolveRepeatedAnnotations(field.getAnnotations())) {
        AnnotationElement ae = createAnnotation(a);
        if (ae != null) {
            ans.add(ae);
        }
    }
    return PrivateAccess.getInstance().newValueDescriptor(useName, type, ans, 0, constantPool, fieldName);
}
 
Example 14
Source File: JavaDeserializer.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
/**
  * Creates a map of the classes fields.
  */
 protected HashMap getFieldMap(Class cl)
 {
   HashMap fieldMap = new HashMap();
   
   for (; cl != null; cl = cl.getSuperclass()) {
     Field []fields = cl.getDeclaredFields();
     for (int i = 0; i < fields.length; i++) {
       Field field = fields[i];

       if (Modifier.isTransient(field.getModifiers())
    || Modifier.isStatic(field.getModifiers()))
         continue;
       else if (fieldMap.get(field.getName()) != null)
         continue;

       // XXX: could parameterize the handler to only deal with public
       try {
         field.setAccessible(true);
       } catch (Throwable e) {
         e.printStackTrace();
       }

Class type = field.getType();
FieldDeserializer deser;

if (String.class.equals(type))
  deser = new StringFieldDeserializer(field);
else if (byte.class.equals(type)) {
  deser = new ByteFieldDeserializer(field);
}
else if (short.class.equals(type)) {
  deser = new ShortFieldDeserializer(field);
}
else if (int.class.equals(type)) {
  deser = new IntFieldDeserializer(field);
}
else if (long.class.equals(type)) {
  deser = new LongFieldDeserializer(field);
}
else if (float.class.equals(type)) {
  deser = new FloatFieldDeserializer(field);
}
else if (double.class.equals(type)) {
  deser = new DoubleFieldDeserializer(field);
}
else if (boolean.class.equals(type)) {
  deser = new BooleanFieldDeserializer(field);
}
else if (java.sql.Date.class.equals(type)) {
  deser = new SqlDateFieldDeserializer(field);
 }
else if (java.sql.Timestamp.class.equals(type)) {
  deser = new SqlTimestampFieldDeserializer(field);
 }
else if (java.sql.Time.class.equals(type)) {
  deser = new SqlTimeFieldDeserializer(field);
 }
else {
  deser = new ObjectFieldDeserializer(field);
}

       fieldMap.put(field.getName(), deser);
     }
   }

   return fieldMap;
 }
 
Example 15
Source File: JavaDeserializer.java    From dubbox with Apache License 2.0 4 votes vote down vote up
/**
  * Creates a map of the classes fields.
  */
 protected HashMap getFieldMap(Class cl)
 {
   HashMap fieldMap = new HashMap();
   
   for (; cl != null; cl = cl.getSuperclass()) {
     Field []fields = cl.getDeclaredFields();
     for (int i = 0; i < fields.length; i++) {
       Field field = fields[i];

       if (Modifier.isTransient(field.getModifiers())
    || Modifier.isStatic(field.getModifiers()))
         continue;
       else if (fieldMap.get(field.getName()) != null)
         continue;

       // XXX: could parameterize the handler to only deal with public
       try {
         field.setAccessible(true);
       } catch (Throwable e) {
         e.printStackTrace();
       }

Class type = field.getType();
FieldDeserializer deser;

if (String.class.equals(type))
  deser = new StringFieldDeserializer(field);
else if (byte.class.equals(type)) {
  deser = new ByteFieldDeserializer(field);
}
else if (short.class.equals(type)) {
  deser = new ShortFieldDeserializer(field);
}
else if (int.class.equals(type)) {
  deser = new IntFieldDeserializer(field);
}
else if (long.class.equals(type)) {
  deser = new LongFieldDeserializer(field);
}
else if (float.class.equals(type)) {
  deser = new FloatFieldDeserializer(field);
}
else if (double.class.equals(type)) {
  deser = new DoubleFieldDeserializer(field);
}
else if (boolean.class.equals(type)) {
  deser = new BooleanFieldDeserializer(field);
}
else if (java.sql.Date.class.equals(type)) {
  deser = new SqlDateFieldDeserializer(field);
 }
else if (java.sql.Timestamp.class.equals(type)) {
  deser = new SqlTimestampFieldDeserializer(field);
 }
else if (java.sql.Time.class.equals(type)) {
  deser = new SqlTimeFieldDeserializer(field);
 }
else {
  deser = new ObjectFieldDeserializer(field);
}

       fieldMap.put(field.getName(), deser);
     }
   }

   return fieldMap;
 }
 
Example 16
Source File: BeanProxy.java    From flex-blazeds with Apache License 2.0 4 votes vote down vote up
/**
 * Do the provided modifiers indicate that this is public?
 * @param modifiers the flags to check
 * @return true if public but not final, static or transient.
 */
public static boolean isPublicField(int modifiers)
{
    return (Modifier.isPublic(modifiers) && !Modifier.isFinal(modifiers)
            && !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers));
}
 
Example 17
Source File: Element.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Returns true if the field is transient. */

  final boolean isTransient() {
    return Modifier.isTransient(getModifiers());
  }
 
Example 18
Source File: FieldDocImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return true if this field is transient
 */
public boolean isTransient() {
    return Modifier.isTransient(getModifiers());
}
 
Example 19
Source File: JFishPropertyInfoImpl.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public boolean isTransientModifier(){
	return Modifier.isTransient(readMethod.getModifiers());
}
 
Example 20
Source File: ReflectElement.java    From Diorite with MIT License 2 votes vote down vote up
/**
 * Returns true if given element is transient.
 *
 * @return true if given element is transient.
 */
default boolean isTransient()
{
    return Modifier.isTransient(this.getModifiers());
}