java reflection @konatechadda

19
Java Reflection API @KonaTechAdda Md Imran Hasan Hira Software Engineer Kona Software Lab Ltd. http:// bd.linkedin.com/in/imranhasanhira https ://www.facebook.com/KonaSoftware https:// www.linkedin.com/company/kona-software-lab-ltd-

Upload: md-imran-hasan-hira

Post on 13-Jul-2015

254 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Java Reflection @KonaTechAdda

Java Reflection API@KonaTechAdda

Md Imran Hasan Hira

Software Engineer

Kona Software Lab Ltd.

http://bd.linkedin.com/in/imranhasanhira

https://www.facebook.com/KonaSoftwarehttps://www.linkedin.com/company/kona-software-lab-ltd-

Page 2: Java Reflection @KonaTechAdda

Outlines

• Design of reflection API

• Getting class

• Encoding class names

• Examples

• Modifiers

• Drawbacks

What & Why• Logics vs configurations

• Dependency Injections

• Methods param suggestion

• Build tools

• Spring…

• Runtime Code generation

• Bytecode Manipulation

Page 3: Java Reflection @KonaTechAdda

Java Reflection

Java Ob

ject

Page 4: Java Reflection @KonaTechAdda

Basic Idea

• Accessing a field in runtime• Find a class

• Instantiate a object form the class

• Find it’s field

• Set value of the field on that object

• Calling a method in runtime• Find a class

• Instantiate a object form the class

• Find it’s method

• Invoke the method (with/without parameter)

• Get the returned value

Page 5: Java Reflection @KonaTechAdda

Necessary Classes

java.lang.Class

java.lang.reflection.Method

java.lang.reflection.Field

java.lang.reflection.Constructor

java.lang.reflection.Modifier

java.lang.reflection.Parameter

Page 6: Java Reflection @KonaTechAdda

“Class” ক্লাশের public methods

Class

Including private

Including Inherited

Own + private, no inherited

Own + inherited, except private

Page 7: Java Reflection @KonaTechAdda

“Constructor” & “Method”

Constructor

Method

Page 8: Java Reflection @KonaTechAdda

“Field” & “Modifier”

Field

Modifier

Page 9: Java Reflection @KonaTechAdda

Ways to get class

1. With the static field*

2. Providing full class path

3. getClass() method

Page 10: Java Reflection @KonaTechAdda

Classpath encoding schema*

byte

char

MyClass

MyClass[]

byte[]

“B”

“C”

“Lcom.mypackage.MyClass;”

“[Lcom.myPackage.MyClass;”

“[B”

byte[][] “[[B”

Page 11: Java Reflection @KonaTechAdda

Finding fields upto superclass

new Manager();

Page 12: Java Reflection @KonaTechAdda

Data Parsing

Page 13: Java Reflection @KonaTechAdda

Data Parsing (Dynamic)

Page 14: Java Reflection @KonaTechAdda

Method Calling

Name: Default name, Age: 0

Name: Dynamic name, Age: 0

Page 15: Java Reflection @KonaTechAdda

Java Ob

ject

Page 16: Java Reflection @KonaTechAdda

Reflection Drawbacks

• Performance overhead • Dynamically type resolutions occlude JVM optimizations.

• Should be avoided in parts of code in performance sensitive applications

*Taken from a article of Dennis Sosnoski,

Page 17: Java Reflection @KonaTechAdda

Reflection Drawbacks

• Performance overhead • Dynamically type resolutions occlude JVM optimizations.

• Should be avoided in parts of code in performance sensitive applications

• Security Restrictions• Requires a runtime permission, so your code may not run in a restricted

security context.

• Exposure of Internals• Accessing private fields and methods

• Reflective code breaks abstractions*

Page 18: Java Reflection @KonaTechAdda

References

Article from Oracle -

https://docs.oracle.com/javase/tutorial/reflect/

http://docs.oracle.com/javase/8/docs/api/index.html?java/lang/Class.html

Dennis Sosnoski’s Article on Reflection -http://www.ibm.com/developerworks/java/library/j-dyn0603/index.html

Source Code Snippets -

https://github.com/imranhasanhira/java-reflection-annotation-test

Page 19: Java Reflection @KonaTechAdda

Thank You