9 Inheritance of Annotations, Settings, and Fields
When a class extends an event, it inherits the event's annotations, settings, and fields. However, a class doesn't inherit private fields or annotations that lack the @java.lang.Inherited meta-annotation.
The example InheritanceSample.java
demonstrates
this. It defines three events: FileAction
,
FileUpload
, and ImageUpload
.
import jdk.jfr.Category;
import jdk.jfr.Description;
import jdk.jfr.Event;
import jdk.jfr.Label;
import jdk.jfr.Name;
import jdk.jfr.StackTrace;
public class InheritanceSample {
@Category("Files")
@StackTrace(false)
abstract static class FileAction extends Event {
@Label("In Progress")
boolean inProgress;
}
@Name("com.oracle.FileUpload")
@Description("Uploaded file that might be a text file")
@Label("File Upload")
static class FileUpload extends FileAction {
@Label("Text file")
private boolean isText;
}
@Name("com.oracle.ImageUpload")
@Label("Image Upload")
static class ImageUpload extends FileUpload {
}
public static void main(String... args) {
FileUpload fu = new FileUpload();
fu.inProgress = true;
fu.isText = false;
fu.commit();
ImageUpload iu = new ImageUpload();
iu.inProgress = false;
iu.commit();
}
}
Run InheritanceSample
with the following
commands:
java -XX:StartFlightRecording:filename=i.jfr InheritanceSample.java
jfr print --events FileUpload,ImageUpload i.jfr
The last command prints output similar to the following:
com.oracle.FileUpload {
startTime = 15:22:28.794
isText = false
inProgress = true
...
}
com.oracle.ImageUpload {
startTime = 15:22:28.822
inProgress = false
...
}
Abstract event classes, such as FileAction
are not
registered, so their metadata is never available for inspection.
Classes don't inherit annotations that lack the @java.lang.Inherited annotation, such as
@Name
and @Description
.
Because the field isText
is private,
ImageUpload
doesn't inherit it.