1/10
Looks like no tags are added yet.
What is persistence?
persistence is used to mark what data we want to save, our goal is to try and reuse persistence code which involved reflection + annotation
Java Object Class
I java, all classes inherit the object class implicitly, which means we do not need to extend to the object class to know we belong to it, the methods call the argument it is being applied to,
Student s = new Student()
s.getName();
calls on s which is a student
What is Reflection?
Ability of a program to inspect and manipulate its own structure and behavior at runtime within the same language environment, Can (dynamically) instantiate classes given a class name and invoke methods and constructors on it
allows for the access to methods, fields, and constructors dynamically, this requires a no argument constructor and public methods we can access
What is the benefit of reflection allowing for dynamic calls?
this allows for code reuse and flexibility at runtime, instead of needing to know what type of object we are at compile time, we get to know it at runtime which means we don’t need to type check earlier, like is the object a teacher or is it a student
it also allows for extensibility as we can now work with any types of objects and decouples from the application
What types of reflection are they and how do they differ?
Compile time reflection: compiler analyzes the program's types and generates new code before the application executes, resulting in zero performance loss at runtime, C++
Runtime reflection: compiler analyzes the program's types and generates new code before the application executes, resulting in zero performance loss at runtime due to overhead which is usually negligible since we have dynamic usage, Java and Javascript
What are the disadvantage of reflection at runtime?
it is slower
direct access: 1.5 – 3 ns
cached reflection: 6 ns
uncached reflection: 200+ ns
we cannot create new methods or fields
there is a full loss of type safety, so we should’t use it with singleton as it can bypass the private constructor and create multiple instances
What are Key classes?
Every class we have would have a Class object that our compiler automatically makes, this is represented with java.lang.Class
What are Annotations?
they are metadata added to Java code
they have no direct impact on code execution at runtime
they can target, methods, fields, classes
What are annotations for compiler checks?
These will be checked at compile time
@Override
@Deprecated
@SupressedWarnings
How do you create a new annotation?
You have to use @interface
Each field must have a constructor, and they can have default values
What are the different Retention Policies and when do you use them?
RetentionPolicy.SOUCE - only present in source file, this is for compiler warnings
RetentionPolicy.CLASS - this is the default retention policy, only present in the class field
RetentionPolicy.RUNTIME - present at runtime, reflection can only access annotations with this runtime, IsAnnotationPresent, @Test, @beforeEach