| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
See also the ClassGraph API overview.
Holds information about one field of a class. Obtained by calling ClassInfo#getFieldInfo() and similar methods.
🛑 ClassGraph makes no attempt to resolve type variables in the types of fields. If you need concrete types for a specific type context, you will need to do the type substitution yourself.
.getConstantInitializerValue() returns the constant initializer value of the field, or null if it has none. Requires ClassGraph#enableStaticFinalFieldConstantInitializerValues(), and throws IllegalStateException if that was not called before scanning.
💡 Only fields with constant initializers have a value here -- in practice, fields of primitive type and String constants. The value may be the result of basic math or string concatenation that the compiler could evaluate, but never the result of object construction, other than for String.
💡 It is up to the compiler whether a constant-valued field is assigned as a constant in the field definition itself, or assigned in a static or instance initializer block or in the constructor, so not every field that looks constant in the source has a constant initializer value in the classfile.
💡 A field initialized to a constant primitive value returns the constant wrapped in a boxed type (e.g. Integer for a primitive int-typed constant value).
💡 Every annotation getter comes in an All form and a Direct form. The All form also includes meta-annotations -- the annotations on the field's own annotations, transitively. The Direct form includes only the annotations written on the field itself.
If you call ClassGraph#ignoreFieldVisibility() before calling scan(), then non-public fields will be scanned, and FieldInfo objects will be added to the ScanResult for any non-public fields encountered while scanning classes.
Note that ClassGraph differs in its behavior from the Java reflection API. You will notice the following differences in behavior:
Java reflection:
ClassGraph:
This difference in behavior is intended to separate the concerns of whether or not to obtain field info from superclasses from whether or not to return non-public fields, making it simpler with the ClassGraph API to find non-public fields in superclasses, and/or to filter out non-public fields from a given base class, if they are not needed. With the Java reflection API, you cannot get non-public fields from superclasses using Sub.class.getFields() -- if you do want the non-public fields, you have to iterate up through the superclass hierarchy and call .getDeclaredFields() on each superclass. Conversely, with the Java reflection API you cannot request only public fields that are declared in a base class but not its superclasses (when calling .getDeclaredFields()).
With ClassGraph, you can separately decide (1) whether or not to include fields from superclasses (by calling .getDeclaredFieldInfo() if you want only the fields of a class but not its superclasses, or .getFieldInfo() if you want the fields of a class and all of its superclasses), and (2) whether or not to include non-public fields in results (by calling .ignoreFieldVisibility() to enable non-public fields).
To illustrate, given the following classes:
public class Super {
public int publicSuper;
private int privateSuper;
}
public class Sub extends Super {
public int publicSub;
private int privateSub;
}The following results are obtained:
Non-declared fields of Super:
| Mechanism | Method call | Result |
|---|---|---|
| Java reflection | Super.class.getFields() | publicSuper |
| ClassGraph | superClassInfo.getFieldInfo() | publicSuper |
| ClassGraph after .ignoreFieldVisibility() | superClassInfo.getFieldInfo() | publicSuper, privateSuper |
Non-declared fields of Sub:
| Mechanism | Method call | Result |
|---|---|---|
| Java reflection | Sub.class.getFields() | publicSuper, publicSub |
| ClassGraph | subClassInfo.getFieldInfo() | publicSuper, publicSub |
| ClassGraph after .ignoreFieldVisibility() | subClassInfo.getFieldInfo() | publicSuper, publicSub, privateSuper, privateSub |
Declared fields of Super:
| Mechanism | Method call | Result |
|---|---|---|
| Java reflection | Super.class.getDeclaredFields() | publicSuper, privateSuper |
| ClassGraph | superClassInfo.getDeclaredFieldInfo() | publicSuper |
| ClassGraph after .ignoreFieldVisibility() | superClassInfo.getDeclaredFieldInfo() | publicSuper, privateSuper |
Declared fields of Sub:
| Mechanism | Method call | Result |
|---|---|---|
| Java reflection | Sub.class.getDeclaredFields() | publicSub, privateSub |
| ClassGraph | subClassInfo.getDeclaredFieldInfo() | publicSub |
| ClassGraph after .ignoreFieldVisibility() | subClassInfo.getDeclaredFieldInfo() | publicSub, privateSub |
Extends ArrayList<FieldInfo> -- unmodifiable when it came from a scan -- with the following convenience methods:
| Back | FazBrowse Home | New Git URL |