advertisement

Kotlin gets Reflection

50 %
50 %
advertisement
Information about Kotlin gets Reflection
Technology

Published on July 31, 2013

Author: abreslav

Source: slideshare.net

Description

Presented at JVM Language Summit, 2013
advertisement

KOTLIN GETS REFLECTION Andrey.Breslav@JetBrains.com

Introspection is examination of one's own conscious thoughts and feelings. Schultz, D. P.; Schultz, S. E. (2012). A history of modern psychology (10th ed.)

LEGAL Takeiteasy

DON’T JUDGE STRICTLY

OUTLINE • Intro • Ways of Introspection • Reflection API • Reflection Literals • Expression Trees • Conclusion

USE CASES Dependency Injection Data binding Convention over configuration Hacks Workarounds Black Magic

INTROSPECTION Instances - what is your class? Classes & Types - what are your members? supertypes? create an instance Methods/Fields - what are your parameters/types/etc? run with these args. Expressions - ???

JAVA.LANG.REFLECT? Top-level functions Default arguments Properties Nullable types Special types (Nothing, (Mutable)List, etc) … + modules (classes in a package)

METADATA *.kt *.class (kotlin) *.jar (kotlin) kotlin symbols kotlin symbols kotlin symbols java symbols kotlinc

HOW TO STORE METADATA? *.class Custom attributes Annotations Annotate each symbol One big annotation

ONE BIG ANNOTATION *.class @KotlinClass(“data”) val/var types defaults … Java definitions erased generic annotations …

RE-USE j.l.Class *.kt *.class AST @KotlinClass Symbols

DISCREPANCY 1 java.lang.annotation.Annotation vs org.jetbrains.kotlin.internal….AnnotationDescriptor

PURE JAVA CLASSES? j.l.Class *.kt *.class AST @KotlinClass Symbols *.java java2k

DISCREPANCY 2 // Java class Foo { @NotNull String getFirstName() { … } String getMiddleName() { … } } // Corresponding Kotlin class Foo { fun getFirstName(): String fun getMiddleName(): String? }

DISCREPANCY 2 j.l.Class *.class Symbols *.java java2k annotations.xml

SUMMARY 1 Kotlin-specific reflection API - works for Java as well Metadata representation - one big annotation - re-use code from the compiler Problems - representing annotations - nullable/mutable types

ENTRY POINTS

SYNTAX (TENTATIVE) Foo::class expr::class List<String>::member org.sample.bar::member expr::member expr::type (maybe)

USE CASES: PASSING CODE AROUND list.filter(Item::isValid) * Why not a lambda? foo {a, b, c -> bar(a, b, c) }

USE CASES: CONSTRUCTORS fun <T: Tag> tag( create: () -> T, init: T.() -> Unit ) { … } tag(::DIV) { // kara.tags::DIV … }

USE CASES: DATA BINDING class Model { var userName: String by observable() } bind(view.textField, model::userName)

GENERICS? fun foo(s: String): Foo ::foo : (String) -> Foo fun <T> foo(t: T): T { … } ::foo : ∀T.(T) -> T

RANK-2 POLYMORPHISM interface Function1<P1, R> { R invoke(P1 p1); } vs interface GenericFunction1_1<P1, R> { <T> R<T> invoke(P1<T> p1); }

ENCODING FOR FUNCTIONS f: (Foo) -> Bar is Function1<Foo, Bar> interface Function1<P1, R> { R invoke(P1 p1); }

GENERIC FUNCTIONS f: <T>(List<T>) -> T • GenericFunction<List<T>, T> ??? • GenericFunction<T, List<T>, T> ??? interface GenericFunction1_1<P1, R> { <T> R<T> invoke(P1<T> p1); } + Kinds

GENERICS class Foo<T> { fun <R> bar(t: T): R { … } } Foo<T’>::bar<R’> : (T’) -> R’

EXPR::TYPE val x: Any = listOf(1, 2, 3) x::class -> java.util.ArrayList x::type -> java.util.ArrayList<Unknown> vs val x = listOf(1, 2, 3) // x: List<Int> x::class -> java.util.ArrayList x::type -> java.util.ArrayList<Int>

DELEGATED PROPERTIES val foos: List<Foo> by Lazy { foos.find(…) } class Lazy<T>(compute: () -> T) { private var value: T? = null fun get(me: Any, p: PropertyMetadata): T { if (value == null) value = compute() return value } }

DELEGATED PROPERTIES val foos: List<Foo> by Lazy { foos.find(…) } ::foos::delegate : Lazy<Foo> ::foos : Property<List<Foo>> or ::foos.delegate : Lazy<Foo> ::foos : DelegatedProperty< List<Foo>, Lazy<Foo> >

SUMMARY 2 Reflection literals - Static name lookup & typing - Generics are hard, as usual

CODE INTROSPECTION I told you!

USE CASES: LINQ db .selectFrom(::User) .where { lastName.startsWith(“A”) } .orderBy { lastName + firstName }

server client server USE CASES: WEB html { body { onLoad { ... } ... } }

EXPRESSION TREES fun onLoad(ast: Expression<() -> Unit>) { ... } onLoad { ... // compiled to factory calls } At run time the tree can be translated to JS

SUMMARY Kotlin’s introspection facilities - Reflection API - Java interop is the hardest issue - Reflection literals - Issues with generics - Expression trees - Compiler as a service

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

KOTLIN GETS REFLECTION - Oracle | Integrated Cloud ...

OUTLINE • Intro • Ways of Introspection • Reflection API • Reflection Literals • Expression Trees • Conclusion
Read more

Reflection - Kotlin Programming Language

Reflection. Reflection is a set ... To get the Kotlin class corresponding to a Java class, use the .kotlin extension property: fun getKClass (o: Any ...
Read more

reflection - Get function by name dynamically in Kotlin ...

Get function by name dynamically in Kotlin. ... kotlin reflection get list of fields. 3 Kotlin Reflection Issue. 3 ProGuard rules for Kotlin reflection.
Read more

reflection - How can I get the name of a Kotlin property ...

I have the following function to access a property's delegate. It uses Kotlin reflection to get a property's name and Java reflection to get the field.
Read more

M8 is out! | Kotlin Blog

M8 is out! Posted on July 2 ... Let’s get started. JVM Reflection for properties. ... Another side of Kotlin’s reflection is its interoperability with ...
Read more

Kotlin M11 is Out! | Kotlin Blog

Kotlin M11 is Out! Posted on March 19 ... is a multi-declaration, i.e. a gets the first component of each Pair ... Dex method count for kotlin reflection ...
Read more

Retrieving Class Objects (The Java™ Tutorials > The ...

This reflection Java tutorial describes using reflection for accessing and manipulating classes, fields, methods, and constructors
Read more

Calling Java from Kotlin - Kotlin Programming Language

Kotlin is a statically typed programming language for the JVM, Android and the browser, 100% interoperable with Java
Read more