Reflection API in Java Flashcards

1
Q

What is meant by reflection?

A

Reflection is the ability for a class or object to examine itself.

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

What is the reflection API?

A

The reflection API is an interface used to write Java code to look at the class of an object and determine its structure.

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

The ability to break open objects is typically guarded by the…

A

Security manager

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

What is the standard rule for reflection?

A

Another object shouldn’t be able to use the reflection API to do something it couldn’t do without ordinary compiled Java code.

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

What is the entry point for the reflection API?

A

The immutable java.lang.Class object generated for each class by the JVM at runtime.

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

If you don’t know the type name, how would you get the Class object of a class?

A

You can use object.getClass(), which is inherited by all classes and returns this instance’s class’s class object.

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

If you do know the type name, how would you get the Class object of a class?

A

You can simply use the .class keyword.
e.g. MainClass.class

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

If, for some reason, you have a string with a reference to the class namespace, how could you get the Class object of a class?

A

You can use Class.forName(String str), where str is your string class reference.

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

What is the difference between getFields and getField?

A

getFields eturns an array of all Field instances, detailing all public attributes associated with the class, including inherited ones.

getField returns only one Field, which is the one specified by the String argument.

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

What is the difference between getFields and getDeclaredFields?

HINT: What does Declared mean in this case?

A

getFields returns Field object(s) for attributes associated with the class, even inherited ones.

getDeclaredFields does the same, but not including inherited ones.

Declared, in this case, means Fields declared within the class.

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

What is the difference between getConstructors and getDeclaredConstructors?

HINT: Different from getFields

A

getConstructors returns public constructors associated with that class, while getDeclaredConstructors returns all constructors associated with that class.

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

What does getMethods do?

A

getMethods returns an array of Method instances, containing details of all public methods associated with this class, including inherited ones.

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

What does the parameterised definition of getMethods do?

A

When parameters are given, getMethods returns only one method, relating to the specified public method, which may be inherited, whose name matches the String name and whose arguments match the types in …argumentTypes.

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

Will getDeclaredFields return inherited Fields?

A

No.

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

Will getDeclaredConstructors return inherited Constructors?

A

No. You cannot inherit constructors, so this mode instead returns all constructors regardless of access level.

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

Will getMethods, given parameters, return inherited Fields?

A

Yes, as long as the given argument matches a method given.

17
Q

Will getMethods be able to return a protected method?

A

No. getMethods, regardless of how it is parameterised, will always return only methods with public access modifiers.

18
Q

What is the difference between Class.newInstance() and Constructor.newInstance()?

A

Class.newInstance can only invoke a constructor with no arguments, and requires that the constructor be visible.

Constructor.newInstance, however, may invoke any constructor, irrespective of the number of parameters and/or access modifiers (granted you have the Constructor object instance).

19
Q

How may you access a private variable from outside of that class using the reflection API?

A

Field, Method and Constructor all extend AccessibleObject, which contains a method called setAccessible(), which allows you to deactivate normal security when accessing that member.

So, you can find the variable using getDeclaredField or getFields, then use setAccessible to turn off security, then you may access it.

20
Q

Will the security manager always allow you to change member accessibility?

A

No. If you are running a security policy that denies this method from being invoked, you may not use this functionality. This can only be done when running no security policy, or running one that leaves this option open, for some reason.