Reflection Flashcards

1
Q

What is the purpose of Java Reflection?

A

To enable programs to analyse themselves as well as other programs. Specifically, analysing fields, constructors
and methods of all accessors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the entry point for the Reflection API?

A

The entry point of reflection is an immutable instance of the objects .class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is Reflection costly?

A

Because it uses dynamic type checking as well as requires run time permissions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Reflections breaks Java conventions. What might this result in?

A

Unexpected and unreliable output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 3 ways to obtain the class entry point for reflection?

A
  • getInstace() called on the object.
  • Access from the type E.g. float.class (works on reference and primitive types).
  • forName() to get the class of the specified name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the difference between getFields() and getDeclaredFields()?

A

getFields() returns all public fields, including inherited ones.
getDeclaredFields() returns all fields (including private). Doesn’t include inherited fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the 2 main methods of creating new object instances in Reflection?

A

Constructor.newInstance() is used to call constructor of any accessor and argument type.
Class.newInstance() is used if constructor has no arguments and is public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we modify private members using Reflection?

A

We have to temporarily modify the accessor of the member via setAccessible(true), and then we can
change the value. This is done in a try catch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the 3 main uses of Reflection?

A

Force specific program states for testing.
Ensure a high level of code coverage.
Enable inspection of running programs for bugs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly