Java Code Examples for java.lang.reflect.Modifier#TRANSIENT

The following examples show how to use java.lang.reflect.Modifier#TRANSIENT . 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: AvroReflection.java    From swim with Apache License 2.0 6 votes vote down vote up
static <T> AvroRecordType<T, T> reflectField(AvroRecordType<T, T> recordType, Field field) {
  final int modifiers = field.getModifiers();
  if ((modifiers & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
    if ((modifiers & (Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED)) != 0 || modifiers == 0) {
      field.setAccessible(true);
    }

    String name;
    final AvroMember member = field.getAnnotation(AvroMember.class);
    name = member != null ? member.value() : null;
    if (name == null || name.length() == 0) {
      name = field.getName();
    }
    recordType = recordType.field(field(field, reflectGenericType(field.getGenericType())));
  }
  return recordType;
}
 
Example 2
Source File: JenkinsMappedClass.java    From DotCi with MIT License 6 votes vote down vote up
@Override
protected void discover() {
    for (final Field field : ReflectionUtils.getDeclaredAndInheritedFields(getClazz(), true)) {
        field.setAccessible(true);
        final int fieldMods = field.getModifiers();
        if (field.isAnnotationPresent(Transient.class)
            || field.isSynthetic() && (fieldMods & Modifier.TRANSIENT) == Modifier.TRANSIENT
            || getMapper().getOptions().isActLikeSerializer() && ((fieldMods & Modifier.TRANSIENT) == Modifier.TRANSIENT)
            || getMapper().getOptions().isIgnoreFinals() && ((fieldMods & Modifier.FINAL) == Modifier.FINAL)) {
            // ignore these
        } else {
            getPersistenceFields().add(mapField(field));
        }

    }
}
 
Example 3
Source File: ObjectStreamClass.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Returns array of ObjectStreamFields corresponding to all non-static
 * non-transient fields declared by given class.  Each ObjectStreamField
 * contains a Field object for the field it represents.  If no default
 * serializable fields exist, NO_FIELDS is returned.
 */
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
    Field[] clFields = cl.getDeclaredFields();
    ArrayList<ObjectStreamField> list = new ArrayList<>();
    int mask = Modifier.STATIC | Modifier.TRANSIENT;

    for (int i = 0; i < clFields.length; i++) {
        if ((clFields[i].getModifiers() & mask) == 0) {
            list.add(new ObjectStreamField(clFields[i], false, true));
        }
    }
    int size = list.size();
    return (size == 0) ? NO_FIELDS :
        list.toArray(new ObjectStreamField[size]);
}
 
Example 4
Source File: CustomCodeView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Writes edited code back to the CustomCodeData structure.
 */
CustomCodeData retreiveCodeData() {
    retreiveCodeData(CodeCategory.CREATE_AND_INIT);
    retreiveCodeData(CodeCategory.DECLARATION);

    VariableDeclaration decl = codeData.getDeclarationData();
    boolean local = variableValues[variableCombo.getSelectedIndex()];
    int modifiers;
    if (local != decl.local) {
        modifiers = local ? lastLocalModifiers : lastFieldModifiers;
        if (finalCheckBox.isSelected()) // only final makes sense for both local and field scope
            modifiers |= Modifier.FINAL;
        else
            modifiers &= ~Modifier.FINAL;
    }
    else {
        modifiers = accessValues[accessCombo.getSelectedIndex()];
        if (staticCheckBox.isSelected())
            modifiers |= Modifier.STATIC;
        if (finalCheckBox.isSelected())
            modifiers |= Modifier.FINAL;
        if (transientCheckBox.isSelected())
            modifiers |= Modifier.TRANSIENT;
        if (volatileCheckBox.isSelected())
            modifiers |= Modifier.VOLATILE;
        if (local)
            modifiers &= ~(Modifier.STATIC | Modifier.TRANSIENT | Modifier.VOLATILE);
    }
    decl.local = local;
    decl.modifiers = modifiers;

    return codeData;
}
 
Example 5
Source File: ObjectStreamClass.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns array of ObjectStreamFields corresponding to all non-static
 * non-transient fields declared by given class.  Each ObjectStreamField
 * contains a Field object for the field it represents.  If no default
 * serializable fields exist, NO_FIELDS is returned.
 */
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
    Field[] clFields = cl.getDeclaredFields();
    ArrayList<ObjectStreamField> list = new ArrayList<>();
    int mask = Modifier.STATIC | Modifier.TRANSIENT;

    for (int i = 0; i < clFields.length; i++) {
        if ((clFields[i].getModifiers() & mask) == 0) {
            list.add(new ObjectStreamField(clFields[i], false, true));
        }
    }
    int size = list.size();
    return (size == 0) ? NO_FIELDS :
        list.toArray(new ObjectStreamField[size]);
}
 
Example 6
Source File: ObjectStreamClass.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns array of ObjectStreamFields corresponding to all non-static
 * non-transient fields declared by given class.  Each ObjectStreamField
 * contains a Field object for the field it represents.  If no default
 * serializable fields exist, NO_FIELDS is returned.
 */
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
    Field[] clFields = cl.getDeclaredFields();
    ArrayList<ObjectStreamField> list = new ArrayList<>();
    int mask = Modifier.STATIC | Modifier.TRANSIENT;

    for (int i = 0; i < clFields.length; i++) {
        if ((clFields[i].getModifiers() & mask) == 0) {
            list.add(new ObjectStreamField(clFields[i], false, true));
        }
    }
    int size = list.size();
    return (size == 0) ? NO_FIELDS :
        list.toArray(new ObjectStreamField[size]);
}
 
Example 7
Source File: ObjectStreamClass.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Returns array of ObjectStreamFields corresponding to all non-static
 * non-transient fields declared by given class.  Each ObjectStreamField
 * contains a Field object for the field it represents.  If no default
 * serializable fields exist, NO_FIELDS is returned.
 */
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
    Field[] clFields = cl.getDeclaredFields();
    ArrayList<ObjectStreamField> list = new ArrayList<>();
    int mask = Modifier.STATIC | Modifier.TRANSIENT;

    for (int i = 0; i < clFields.length; i++) {
        if ((clFields[i].getModifiers() & mask) == 0) {
            list.add(new ObjectStreamField(clFields[i], false, true));
        }
    }
    int size = list.size();
    return (size == 0) ? NO_FIELDS :
        list.toArray(new ObjectStreamField[size]);
}
 
Example 8
Source File: ObjectStreamClass.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns array of ObjectStreamFields corresponding to all non-static
 * non-transient fields declared by given class.  Each ObjectStreamField
 * contains a Field object for the field it represents.  If no default
 * serializable fields exist, NO_FIELDS is returned.
 */
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
    Field[] clFields = cl.getDeclaredFields();
    ArrayList<ObjectStreamField> list = new ArrayList<>();
    int mask = Modifier.STATIC | Modifier.TRANSIENT;

    for (int i = 0; i < clFields.length; i++) {
        if ((clFields[i].getModifiers() & mask) == 0) {
            list.add(new ObjectStreamField(clFields[i], false, true));
        }
    }
    int size = list.size();
    return (size == 0) ? NO_FIELDS :
        list.toArray(new ObjectStreamField[size]);
}
 
Example 9
Source File: DocEnv.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert modifier bits from private coding used by
 * the compiler to that of java.lang.reflect.Modifier.
 */
static int translateModifiers(long flags) {
    int result = 0;
    if ((flags & Flags.ABSTRACT) != 0)
        result |= Modifier.ABSTRACT;
    if ((flags & Flags.FINAL) != 0)
        result |= Modifier.FINAL;
    if ((flags & Flags.INTERFACE) != 0)
        result |= Modifier.INTERFACE;
    if ((flags & Flags.NATIVE) != 0)
        result |= Modifier.NATIVE;
    if ((flags & Flags.PRIVATE) != 0)
        result |= Modifier.PRIVATE;
    if ((flags & Flags.PROTECTED) != 0)
        result |= Modifier.PROTECTED;
    if ((flags & Flags.PUBLIC) != 0)
        result |= Modifier.PUBLIC;
    if ((flags & Flags.STATIC) != 0)
        result |= Modifier.STATIC;
    if ((flags & Flags.SYNCHRONIZED) != 0)
        result |= Modifier.SYNCHRONIZED;
    if ((flags & Flags.TRANSIENT) != 0)
        result |= Modifier.TRANSIENT;
    if ((flags & Flags.VOLATILE) != 0)
        result |= Modifier.VOLATILE;
    return result;
}
 
Example 10
Source File: PolyForm.java    From swim with Apache License 2.0 5 votes vote down vote up
public <T> ClassForm<T> reflectField(ClassForm<T> classForm, Field field) {
  final int modifiers = field.getModifiers();
  if ((modifiers & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
    if ((modifiers & (Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED)) != 0 || modifiers == 0) {
      field.setAccessible(true);
    }

    // Close over the generic type of the field.
    final Type fieldType = field.getGenericType();
    addType(fieldType);

    String name;
    final FieldForm<T> fieldForm;
    final Header header = field.getAnnotation(Header.class);
    if (header != null) {
      name = header.value();
      if (name == null || name.length() == 0) {
        name = field.getName();
      }
      fieldForm = new SlotForm<T>(field, Text.from(name), formForType(fieldType));
      return classForm.putHeader(fieldForm);
    } else {
      final Member member = field.getAnnotation(Member.class);
      name = member != null ? member.value() : null;
      if (name == null || name.length() == 0) {
        name = field.getName();
      }
      fieldForm = new SlotForm<T>(field, Text.from(name), formForType(fieldType));
      return classForm.putMember(fieldForm);
    }
  }
  return classForm;
}
 
Example 11
Source File: DeclaredFieldsSerializer.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
private static void visitFields(@Nonnull final Object object, @Nonnull final FieldVisitor visitor) {
  for (final Field f : object.getClass().getDeclaredFields()) {
    if ((f.getModifiers() & (Modifier.FINAL | Modifier.NATIVE | Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
      f.setAccessible(true);
      visitor.visitField(object, f, f.getName(), f.getType());
    }
  }
}
 
Example 12
Source File: JavaObjectMapper.java    From hypergraphdb with Apache License 2.0 5 votes vote down vote up
private boolean ignoreField(Field f)
{
	int m = f.getModifiers();
	return (m & Modifier.TRANSIENT) != 0 ||
		   (m & Modifier.STATIC) != 0 ||
		   f.getAnnotation(HGIgnore.class) != null;
}
 
Example 13
Source File: ObjectStreamClass.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns array of ObjectStreamFields corresponding to all non-static
 * non-transient fields declared by given class.  Each ObjectStreamField
 * contains a Field object for the field it represents.  If no default
 * serializable fields exist, NO_FIELDS is returned.
 */
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
    Field[] clFields = cl.getDeclaredFields();
    ArrayList<ObjectStreamField> list = new ArrayList<>();
    int mask = Modifier.STATIC | Modifier.TRANSIENT;

    for (int i = 0; i < clFields.length; i++) {
        if ((clFields[i].getModifiers() & mask) == 0) {
            list.add(new ObjectStreamField(clFields[i], false, true));
        }
    }
    int size = list.size();
    return (size == 0) ? NO_FIELDS :
        list.toArray(new ObjectStreamField[size]);
}
 
Example 14
Source File: DocEnv.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert modifier bits from private coding used by
 * the compiler to that of java.lang.reflect.Modifier.
 */
static int translateModifiers(long flags) {
    int result = 0;
    if ((flags & Flags.ABSTRACT) != 0)
        result |= Modifier.ABSTRACT;
    if ((flags & Flags.FINAL) != 0)
        result |= Modifier.FINAL;
    if ((flags & Flags.INTERFACE) != 0)
        result |= Modifier.INTERFACE;
    if ((flags & Flags.NATIVE) != 0)
        result |= Modifier.NATIVE;
    if ((flags & Flags.PRIVATE) != 0)
        result |= Modifier.PRIVATE;
    if ((flags & Flags.PROTECTED) != 0)
        result |= Modifier.PROTECTED;
    if ((flags & Flags.PUBLIC) != 0)
        result |= Modifier.PUBLIC;
    if ((flags & Flags.STATIC) != 0)
        result |= Modifier.STATIC;
    if ((flags & Flags.SYNCHRONIZED) != 0)
        result |= Modifier.SYNCHRONIZED;
    if ((flags & Flags.TRANSIENT) != 0)
        result |= Modifier.TRANSIENT;
    if ((flags & Flags.VOLATILE) != 0)
        result |= Modifier.VOLATILE;
    return result;
}
 
Example 15
Source File: JsonWriter.java    From code with Apache License 2.0 5 votes vote down vote up
/**
 * Reach-ability trace to visit all objects within the graph to be written.
 * This API will handle any object, using either reflection APIs or by
 * consulting a specified FIELD_SPECIFIERS map if provided.
 * @param stack Deque used to manage descent into graph (rather than using Java stack.) This allows for
 * much larger graph processing.
 * @param obj Object root of graph
 * @param fieldSpecifiers Map of optional field specifiers, which are used to override the field list returned by
 * the JDK reflection operations.  This allows a subset of the actual fields on an object to be serialized.
 */
protected void traceFields(final Deque<Object> stack, final Object obj, final Map<Class, List<Field>> fieldSpecifiers)
{
    // If caller has special Field specifier for a given class
    // then use it, otherwise use reflection.
    Collection<Field> fields = getFieldsUsingSpecifier(obj.getClass(), fieldSpecifiers);
    Collection<Field> fieldsBySpec = fields;
    if (fields == null)
    {   // Trace fields using reflection
        fields = MetaUtils.getDeepDeclaredFields(obj.getClass()).values();
    }
    for (final Field field : fields)
    {
        if ((field.getModifiers() & Modifier.TRANSIENT) != 0)
        {
            if (fieldsBySpec == null || !fieldsBySpec.contains(field))
            {   // Skip tracing transient fields EXCEPT when the field is listed explicitly by using the fieldSpecifiers Map.
                // In that case, the field must be traced, even though it is transient.
                continue;
            }
        }
        try
        {
            final Object o = field.get(obj);
            if (o != null && !MetaUtils.isLogicalPrimitive(o.getClass()))
            {   // Trace through objects that can reference other objects
                stack.addFirst(o);
            }
        }
        catch (Exception ignored) { }
    }
}
 
Example 16
Source File: ObjectStreamClass.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns array of ObjectStreamFields corresponding to all non-static
 * non-transient fields declared by given class.  Each ObjectStreamField
 * contains a Field object for the field it represents.  If no default
 * serializable fields exist, NO_FIELDS is returned.
 */
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
    Field[] clFields = cl.getDeclaredFields();
    ArrayList<ObjectStreamField> list = new ArrayList<>();
    int mask = Modifier.STATIC | Modifier.TRANSIENT;

    for (int i = 0; i < clFields.length; i++) {
        if ((clFields[i].getModifiers() & mask) == 0) {
            list.add(new ObjectStreamField(clFields[i], false, true));
        }
    }
    int size = list.size();
    return (size == 0) ? NO_FIELDS :
        list.toArray(new ObjectStreamField[size]);
}
 
Example 17
Source File: DocEnv.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert modifier bits from private coding used by
 * the compiler to that of java.lang.reflect.Modifier.
 */
static int translateModifiers(long flags) {
    int result = 0;
    if ((flags & Flags.ABSTRACT) != 0)
        result |= Modifier.ABSTRACT;
    if ((flags & Flags.FINAL) != 0)
        result |= Modifier.FINAL;
    if ((flags & Flags.INTERFACE) != 0)
        result |= Modifier.INTERFACE;
    if ((flags & Flags.NATIVE) != 0)
        result |= Modifier.NATIVE;
    if ((flags & Flags.PRIVATE) != 0)
        result |= Modifier.PRIVATE;
    if ((flags & Flags.PROTECTED) != 0)
        result |= Modifier.PROTECTED;
    if ((flags & Flags.PUBLIC) != 0)
        result |= Modifier.PUBLIC;
    if ((flags & Flags.STATIC) != 0)
        result |= Modifier.STATIC;
    if ((flags & Flags.SYNCHRONIZED) != 0)
        result |= Modifier.SYNCHRONIZED;
    if ((flags & Flags.TRANSIENT) != 0)
        result |= Modifier.TRANSIENT;
    if ((flags & Flags.VOLATILE) != 0)
        result |= Modifier.VOLATILE;
    return result;
}
 
Example 18
Source File: Objects.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a string reporting the value of each declared field, via reflection.
 * Static and transient fields are automatically skipped. Produces output like
 * "SimpleClassName[integer=1234,string="hello",character='c',intArray=[1,2,3]]".
 */
public static String toString(Object o) {
    Class<?> c = o.getClass();
    StringBuilder sb = new StringBuilder();
    sb.append(c.getSimpleName()).append('[');
    int i = 0;
    for (Field f : c.getDeclaredFields()) {
        if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) {
            continue;
        }
        f.setAccessible(true);
        try {
            Object value = f.get(o);

            if (i++ > 0) {
                sb.append(',');
            }

            sb.append(f.getName());
            sb.append('=');

            if (value.getClass().isArray()) {
                if (value.getClass() == boolean[].class) {
                    sb.append(Arrays.toString((boolean[]) value));
                } else if (value.getClass() == byte[].class) {
                    sb.append(Arrays.toString((byte[]) value));
                } else if (value.getClass() == char[].class) {
                    sb.append(Arrays.toString((char[]) value));
                } else if (value.getClass() == double[].class) {
                    sb.append(Arrays.toString((double[]) value));
                } else if (value.getClass() == float[].class) {
                    sb.append(Arrays.toString((float[]) value));
                } else if (value.getClass() == int[].class) {
                    sb.append(Arrays.toString((int[]) value));
                } else if (value.getClass() == long[].class) {
                    sb.append(Arrays.toString((long[]) value));
                } else if (value.getClass() == short[].class) {
                    sb.append(Arrays.toString((short[]) value));
                } else {
                    sb.append(Arrays.toString((Object[]) value));
                }
            } else if (value.getClass() == Character.class) {
                sb.append('\'').append(value).append('\'');
            } else if (value.getClass() == String.class) {
                sb.append('"').append(value).append('"');
            } else {
                sb.append(value);
            }
        } catch (IllegalAccessException unexpected) {
            throw new AssertionError(unexpected);
        }
    }
    sb.append("]");
    return sb.toString();
}
 
Example 19
Source File: GeciReflectionTools.java    From javageci with Apache License 2.0 4 votes vote down vote up
/**
 * Convert a string that contains lower case letter Java modifiers comma separated into an access mask.
 *
 * @param masks  is the comma separated list of modifiers. The list can also contain the word {@code package}
 *               that will be translated to {@link GeciReflectionTools#PACKAGE} since there is no modifier {@code package}
 *               in Java.
 * @param dfault the mask to return in case the {@code includes} string is empty.
 * @return the mask converted from String
 */
public static int mask(String masks, int dfault) {
    int modMask = 0;
    if (masks == null) {
        return dfault;
    } else {
        for (var maskString : masks.split(",", -1)) {
            var maskTrimmed = maskString.trim();
            switch (maskTrimmed) {

                case "private":
                    modMask |= Modifier.PRIVATE;
                    break;
                case "public":
                    modMask |= Modifier.PUBLIC;
                    break;
                case "protected":
                    modMask |= Modifier.PROTECTED;
                    break;
                case "static":
                    modMask |= Modifier.STATIC;
                    break;
                case "package":
                    modMask |= GeciReflectionTools.PACKAGE;//reuse the bit
                    break;
                case "abstract":
                    modMask |= Modifier.ABSTRACT;
                    break;
                case "final":
                    modMask |= Modifier.FINAL;
                    break;
                case "interface":
                    modMask |= Modifier.INTERFACE;
                    break;
                case "synchronized":
                    modMask |= Modifier.SYNCHRONIZED;
                    break;
                case "native":
                    modMask |= Modifier.NATIVE;
                    break;
                case "transient":
                    modMask |= Modifier.TRANSIENT;
                    break;
                case "volatile":
                    modMask |= Modifier.VOLATILE;
                    break;
                default:
                    throw new IllegalArgumentException(maskTrimmed + " can not be used as a modifier string");
            }
        }
        return modMask;
    }
}
 
Example 20
Source File: AnnotationMapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void processTypes(final Set<Class<?>> types) {
    while (!types.isEmpty()) {
        final Iterator<Class<?>> iter = types.iterator();
        final Class<?> type = iter.next();
        iter.remove();

        synchronized(type) {
            if (annotatedTypes.contains(type)) {
                continue;
            }
            try {
                if (type.isPrimitive()) {
                    continue;
                }

                addParametrizedTypes(type, types);

                processConverterAnnotations(type);
                processAliasAnnotation(type, types);
                processAliasTypeAnnotation(type);

                if (type.isInterface()) {
                    continue;
                }

                processImplicitCollectionAnnotation(type);

                final Field[] fields = type.getDeclaredFields();
                for (int i = 0; i < fields.length; i++ ) {
                    final Field field = fields[i];
                    if (field.isEnumConstant()
                        || (field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) > 0) {
                        continue;
                    }

                    addParametrizedTypes(field.getGenericType(), types);

                    if (field.isSynthetic()) {
                        continue;
                    }

                    processFieldAliasAnnotation(field);
                    processAsAttributeAnnotation(field);
                    processImplicitAnnotation(field);
                    processOmitFieldAnnotation(field);
                    processLocalConverterAnnotation(field);
                }
            } finally {
                annotatedTypes.add(type);
            }
        }
    }
}