the art of - siliconindia...the art of metaprogramming in java falguni vyas dec 08, 2012 metadata...

38
The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012

Upload: others

Post on 28-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

The Art of

Metaprogrammingin Java

Falguni Vyas

Dec 08, 2012

Page 2: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Metadata

Page 3: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Data that describes other data

Defined as data providing information about one or more aspects of the data, such as:

• Means of creation of the data

• Purpose of the data

• Time and date of creation

• Creator or author of data

What is Metadata?

© Copyright 2012 GlobalLogic 3

• Creator or author of data

• Location on a computer network where the data was created

• Standards used

For example,

• A digital image

• A text document

• SVN Checkout Metadata

Page 4: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Implicit

• Adhering to a certain convention

Explicit

• External to the code

How is Metadata expressed?

© Copyright 2012 GlobalLogic 4

• External to the code

• DSL, XML, RDF

• Internal to the code

• As comments on code, Javadoc

• As the code itself like HTML Meta tags, Java Annotations

Page 5: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Schema

• Data dictionary in RDBMS

• Check constraints (JSR 303)

Semantics

• WSDL : Web Services Description Language

How is Metadata being used?

© Copyright 2012 GlobalLogic 5

• WSDL : Web Services Description Language

• RDF : Resource Description Framework

Data Management

• .svn

Configuration

Page 6: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Metaprogramming

Page 7: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

© Copyright 2012 GlobalLogic 7

Page 8: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Write code that writes code

Writing programs that write or manipulate other programs or

themselves based on some metadata

Supported in many languages and across several platforms

What is Metaprogramming?

© Copyright 2012 GlobalLogic 8

Supported in many languages and across several platforms

An underused feature

Back bone of many of the most successful frameworks

Page 9: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Expose the internals of the run-time engine to the

programming code through APIs.

Dynamic execution of expressions that contain programming

commands

How does Metaprogramming work?

© Copyright 2012 GlobalLogic 9

Program transformation system

Page 10: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Keeps code small, simple, DRYer, lighter, more intuitive and

more scalable

Rapid prototyping

Minimize the LOC to express a solution

Metaprogramming : Benefits

© Copyright 2012 GlobalLogic 10

Minimize the LOC to express a solution

Makes good programmers really productive

Page 11: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

No well defined best practices

Many techniques focused on individual aspects of

metaprogramming are often Language specific

Metaprogramming : Techniques?

© Copyright 2012 GlobalLogic 11

Page 12: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

History

• JDK 1.0 (1996) - no metaprogramming features

• JDK 1.1 (1998) - java.lang.reflect (but only introspection)

• JDK 1.3 (2001) - dynamic proxies classes

• JDK 1.4 (2003) - generics

© Copyright 2012 GlobalLogic 12

• JDK 1.5 (2004) - annotations + annotation processing +

instrumentation

• JDK 1.6 (2006) - annotation processing part of javac

• JDK 1.7 (2011) - invokedynamic

• JDK 1.8 (2012) - lambda expressions

Page 13: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Advanced Java Metaprogramming

Page 14: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Annotation Processing

Class Loading / instrumentation

Dynamic Proxies

Advanced Java Metaprogramming

© Copyright 2012 GlobalLogic 14

Page 15: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

JSR 175 was released as a part of J2SE 5.0 (Tiger)

java.lang.annotation package

Annotation does not effect the program semantics

JSR 175 Metadata facility for Java

© Copyright 2012 GlobalLogic 15

Benefits :

• Allows us to extend the language with new metadata.

• Helps us “color” the code

• Great expressive power

• Can be retained until runtime

Page 16: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Annotations applied to java code

• @Deprecated

• @Override

• @SupressWarning

Built-In Annotations

© Copyright 2012 GlobalLogic 16

Annotations applied to other annotations

• @Documented

• @Inherited

• @Target

• @Retention

Page 17: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

JSR 250 (Common Annotations for the Java Platform)

Common Annotations

© Copyright 2012 GlobalLogic 17

Page 18: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

@Target.ElementType

@Target

© Copyright 2012 GlobalLogic 18

Page 19: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Says whether VM retains the annotation for reflective access

at runtime

Retention Policy

• Source : discarded at compile time

@Retention

© Copyright 2012 GlobalLogic 19

• Class : Kept in bytecode but not loaded into VM this is

default

• Runtime : available for reflective access at runtime

Page 20: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Lets create a custom annotation and use it

Custom Annotation : Create

© Copyright 2012 GlobalLogic 20

Page 21: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Custom Annotation: Annotate

© Copyright 2012 GlobalLogic 21

Page 22: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Custom Annotation : Annotation Parser

© Copyright 2012 GlobalLogic 22

Page 23: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Custom Annotation: In Action

© Copyright 2012 GlobalLogic 23

Page 24: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

APT : Annotation Processing Tool

Compile time

• Annotation Processing at build time JSR 269 :

Pluggable Annotation Processing API

Load-time

Annotation Processing

© Copyright 2012 GlobalLogic 24

Load-time

• Java Profiling with the java.lang.instrument Package

Runtime

• Java Reflection API

Page 25: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

JSR 269 Exploiting : Take1

© Copyright 2012 GlobalLogic 25

Page 26: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

JSR 269 Exploiting : Take2

© Copyright 2012 GlobalLogic 26

Page 27: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

JSR 269 Exploiting : In Action

© Copyright 2012 GlobalLogic 27

Page 28: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Modification of java byte code for the purpose of gathering

data

AOP frameworks like AspectJ use the instrumentation

framework to implement load time weaving of their advices

to the point-cuts

Instrumentation

© Copyright 2012 GlobalLogic 28

to the point-cuts

Page 29: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Definition

• Ability to introspect metalevel information about the

program structure itself at runtime.

• Mechanisms to change the program interpretation or

meaning at the runtime (intercession)

Reflection

© Copyright 2012 GlobalLogic 29

meaning at the runtime (intercession)

Usually this metalevel information is modeled using the

general abstraction mechanisms available in the language.

Page 30: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Dynamic mechanism to create an implementation to some

particular interface at runtime

Basic Idea:

• Associate an invocation handler to the set of interfaces

Reflection : Dynamic Proxy Classes

© Copyright 2012 GlobalLogic 30

• Every method call to the interface is dispatched to the

invocation handler

Page 31: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Metaprogramming in the wild

Page 32: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Metaprogramming in the wild

© Copyright 2012 GlobalLogic 32

Page 33: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Annotation based transactions

Annotation based container configuration

SPRING : Annotations

© Copyright 2012 GlobalLogic 33

Page 34: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Groovy : MetaClass

© Copyright 2012 GlobalLogic 34

Page 35: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Metaprogramming with Groovy

© Copyright 2012 GlobalLogic 35

Page 36: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Rails : DRY Metaprogramming

© Copyright 2012 GlobalLogic 36

Page 37: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Rails : DRY Metaprogramming

© Copyright 2012 GlobalLogic 37

Page 38: The Art of - SiliconIndia...The Art of Metaprogramming in Java Falguni Vyas Dec 08, 2012 Metadata Data that describes other data Defined as data providing information about one or

Thank You.