Transcript Reflection

Reflection
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Contents
1. What? Why? Where?
2. Reflection API

Reflecting Classes

Reflecting Fields

Reflecting Constructors

Reflecting Methods

Reflecting Annotations
2
Questions
sli.do
#4517
3
4
What is Metaprogramming?
Writing programs that treat
other programs as their data
5
What is Reflection?
The ability of a programming
language to be its own metalanguage
6
Why Reflection?
Code is easier to
maintain and extend
7
Where is Reflection used?
Writing tools for programmers
8
9
Obtaining a Class Object
Compile time:
Class sbClass = StringBuilder.class;
Run time:
… = Class.forName("java.lang.StringBuilder");
10
Name of a Class
getName()
vs
getSimpleName()
11
Base Class and Interfaces
Class baseClass =
testClass.getSuperclass();
Class[] interfaces =
testClass.getInterfaces();
12
New Instance
StringBuilder builder = (StringBuilder)
Class.forName("java.lang.StringBuilder")
.newInstance();
13
Demo
14
Obtaining Field Objects
Field[] publicFields =
sbClass.getFields();
Field[] allFields =
sbClass.getDeclaredFields();
15
Field Type and Name
Field field = testClass.getDeclaredField("testInt");
String fieldName = field.getName();
Class fieldType = field.getType();
Useful when creating
columns of the type
in Database tables
16
Field Altering
Test testObj = (Test) testClass.newInstance();
Field field = testClass.getDeclaredField("testInt");
field.set(testObj, 5);
int fieldValue = (int) field.get(testObj);
WARNING: use with
extreme caution as
you could alter an
objects internal state!
17
Modifiers
Field field = testClass.getDeclaredField("testInt");
int mod = field.getModifiers();
String modString = Modifier.toString(mod);
Modifiers are retrieved
as an integer with a
single bit set to 1
18
Exercises in Class
Obtaining Constructor Objects
Constructor[] publicCtors =
testClass.getConstructors();
Constructor[] allCtors =
testClass.getDeclaredConstructors();
20
Obtaining a Certain Constructor
Constructor constructor =
StringBuilder.class.getConstructor(String.class);
21
Instantiating Objects
StringBuilder builder =
(StringBuilder) constructor.newInstance("hi!");
Supply a parameter for
each argument in the
constructor you are
invoking
22
Constructor Parameters
Class[] parameterTypes =
constructor.getParameterTypes();
23
Obtaining Method Objects
Method[] publicMethods =
testClass.getMethods();
Method[] allMethods =
testClass.getDeclaredMethods();
24
Obtaining а certain Method Object
Method append =
sbClass.getMethod("append", String.class);
25
Invoking Methods
append.invoke(builder, "hi!");
Target object
instance for which to
invoke the method
Parameters for the
method
26
Method Parameters and Return Types
Class[] appendParams = append.getParameterTypes();
Class returnType = append.getReturnType();
27
Exercises in Class
Obtaining Class Annotations
Annotation[] annots = testClass.getAnnotations();
for (Annotation annotation : annots) {
Alias alias = (Alias) annotation;
// do smth with alias
}
29
Obtaining Method/Field Annotations
annots = method/field.getDeclaredAnnotations();
for (Annotation annotation : annots) {
Alias alias = (Alias) annotation;
// do smth with alias
}
30
Exercises in Class
Summary
What are the:

Benefits

Drawbacks
Of using reflection?
32
Reflection
?
https://softuni.bg/java-advanced-oop
License
 This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons AttributionNonCommercial-ShareAlike 4.0 International" license
 Attribution: this work may contain portions from

"Fundamentals of Computer Programming with Java" book by Svetlin Nakov & Co. under CC-BY-SA license

"OOP" course by Telerik Academy under CC-BY-NC-SA license
34
Free Trainings @ Software University
 Software University Foundation – softuni.org
 Software University – High-Quality Education,
Profession and Job for Software Developers

softuni.bg
 Software University @ Facebook

facebook.com/SoftwareUniversity
 Software University @ YouTube

youtube.com/SoftwareUniversity
 Software University Forums – forum.softuni.bg