ECS 160 Reflection + Annotation

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

11 Terms

1
New cards

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

2
New cards

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

3
New cards

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

4
New cards

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

5
New cards

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

6
New cards

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

7
New cards

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

8
New cards

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

9
New cards

What are annotations for compiler checks?

These will be checked at compile time

@Override

@Deprecated

@SupressedWarnings

10
New cards

How do you create a new annotation?

You have to use @interface

Each field must have a constructor, and they can have default values

11
New cards

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