SlideShare una empresa de Scribd logo
1 de 63
The Art of Metaprogramming
           in Java

     Abdelmonaim Remani
        @PolymathicCoder
About Me
 Software Architect at Just.me Inc.
 Interested in technology evangelism and enterprise software development and
  architecture
 Frequent speaker (JavaOne, JAX, OSCON, OREDEV, etc…)
 Open-source advocate
 President and founder of a number of user groups
 - NorCal Java User Group
 - The Silicon Valley Spring User Group
 - The Silicon Valley Dart Meetup
 Bio:        http://about.me/PolymathicCoder
 Twitter:    @PolymathicCoder
 Email:      abdelmonaim.remani@gmail.com
About Me
 Today is my birthday!




                  01001000 01100001 01110000 01110000 01111001


                                   00100000


    01000010 01101001 01110010 01110100 01101000 01100100 01100001 01111001
License




 Creative Commons Attribution Non-Commercial 3.0 Unported
 - http://creativecommons.org/licenses/by-nc/3.0


 Disclaimer: The graphics, logos, and trademarks used this presentation belong to
  their rightful owners.
Metadata
What Is Metadata?
 The term “Metadata” was coined by Philip Bagley in 1986 in “Extension of
  Programming Language Concepts”
 Data about data or data that describes other data
 - Structural (Before there is any data)
   - Its type
   - Its valid values
   - How it relates to other data
   - Its purpose
   - Etc…
 - Descriptive (After data is there)
   - How it was created
   - The context within which it exists
   - Etc…
How Is Metadata Expressed?
 Implicit
  - Adhering to a certain convention
 Explicit
  - External to the code
    - DSL (Domain-Specific Language)
    - Markup: XML, RDF, etc…
    - Etc…
  - Internal to the code
    - As comments on code
      - Javadoc: @author, @version, @since, etc…
      - XDoclet
      - Etc…
    - As code itself
      - HTML meta tags: <meta name=“author” content=“Abdelmonaim Remani”>
      - Java Annotations: @Override, @Deprecated, etc…
      - Embedded DSL
      - Etc…
How Is Metadata Being Used?
 Schema
 - Data Dictionary in RDBMS
 - Check Constraints (JSR 303 Validation, Etc…)
 - Etc…
 Semantics
 - WSDL (Web Services Description Language)
 - RDF (Resource Description Framework)
 - Etc…
 Data Management
 - Build/deployment instructions (.svn files, .git, etc…)
 - Etc…
 Configurations
 Etc…
Metadata in Java: Annotations
Java Annotations
 JSR 175 (A Metadata Facility for the Java Programming Language)
 - Introduced in J2SE 5.0 (September 30, 2004)
 - Standardized how annotations are declared in Java code
 An alternative way to Javadoc comments, externally as XML, etc…
 - More readable
 - Closer to the code
 - Statically-typed
 - Can be retained until runtime
JDK Annotations
 Java Language Spec Annotations
 - @Override
 - @Deprecated
 - @SuppressWarning
 JSR 250 (Common Annotations for the Java Platform)
 - @PostConstruct
 - @Resource
 - @DenyAll
 - Etc…
Write Your Own


 You need to tell the compiler how the annotation is to be treated
 In java.lang.annotation package
 - @Target
   - The element type the annotation can be applied to
     - ElememtType.ANNOTATION_TYPE
     - ElememtType.CONSTRUCTOR
     - ElememtType.FIELD
     - ElememtType.LOCAL_VARIABLE
     - ElememtType.METHOD
     - ElememtType.PACKAGE
     - ElememtType.PARAMETER
     - ElememtType.TYPE
Write Your Own
 In java.lang.annotation package
 - @Retention
   - RetentionPolicy.SOURCE
     - Discarded by the compiler
   - RetentionPolicy.CLASS
     - Included in the class file but ignored the JVM. This is the default
   - RetentionPolicy.RUNTINE
     - Included in the class file and read by the JVM.
 - @Documented
   - Whether it should be shown in the javadoc or not
 - @Inherited
   - Allowed to be inherited by subclasses
Write Your Own
 Attributes
 - Carry additional metadata details
 - May only be
   - Primitives
   - Enums
   - Java.lang.String
   - Java.lang.Class
   - One-dimensional arrays of the above
 - May have a default value
 - No modifiers or parameters
Code
Notes
 Annotating a package
 - Must create a file named package-info.java in the target package
 - Note that “package-info” is not a valid Java identifier
   - Most IDEs you will prevent you from creating a class name “package-info”
   - Make sure you create it as a regular file with .java extension instead
Metaprogramming
What is Metaprogramming?
 Writing programs that write or manipulate other programs or themselves based on
  some metadata
 Metaprogramming
   -!= Generative/Automatic Programming
 Ralph Johnson
 - “It is about programming at the meta level, that is about changing your
   interpreter or changing how code is compiled”
 Black art and a Big-boy’s toy
 An underused feature
 Supported in many languages and across several platforms
 The backbone of many of the most successful frameworks
How is Metaprogramming Supported?
 Exposing the internals of the runtime/compiler as an API
 Dynamic execution of expressions containing programming commands
 - Code as Strings
 - Code as a series if method calls
 A program transformation system
 - A description gets transformed it to a target language
   - The compiler itself
   - YACC takes in grammar, and produces C/C++ code containing yyparse()
   - ANTLR (ANother Tool for Language Recognition)
Concepts
 Metalanguage
 - The language in which the metaprogram is written in
 Object Language
 - The language in which the target (produced) program is written in
 Reflection or Reflexivity
 - When metalanguage == object language
 - No translation necessary
Usage in code
 Static data that can be pre-computed or pre-generated at compile time
 Eliminate boiler-plate
 - Code that cannot be abstracted in functions for DRYness sake
   - Think Aspects in AOP
   - Stereotypes in Spring
   - Etc…
 - Code of common methods
   - Getters/setters, toString(), hashCode(), equals(), etc…
 Etc…
Benefits
 In code
 - Performance gain
 - Flexibility
 - Simplicity
 - Etc…
 Development
 - Minimize the LOC to express a solution
 - Productivity gain
 - Reduced development time/efficiency
 - Etc…
How to?
 Many techniques focused on specific aspects of metaprogramming
 No well-defined best practices


 This presentation is an attempt to bring in some structure through defining a
  process
 - Defining the metadata
 - Processing the metadata
 - Metamodel construction
 - Validating the metamodel
 - Metamodel interpretation
Metadata Processing
Metadata Processing
 Programmatically reading/accessing metadata
 - Parsing raw metadata
 - Call to an API
   - Reflection
   - Query
   - Etc…
 - Tools
Metadata Processing in Java: Annotation Processing
At Runtime: JSR 175
 JSR 175: A Metadata Facility for the Java Programming Language
 - Defined the core reflection API for reading/accessing annotations on annotated
   elements at runtime as long as their retention policy extends to the runtime
 - Reflection
   - Reading annotations is done in reference to the structure of the program
 Libraries
 - Reflections: http://code.google.com/p/reflections/
 - FEST-Reflect: http://fest.easytesting.org/
 - ReflectASM: http://code.google.com/p/reflectasm/
 - Etc…
At Runtime: JSR 175
At Build Time: JSR 269
 Mirror-Based Reflection
 - Reflective capabilities are encapsulated in intermediary objects called mirrors
 - Annotations are accessible through a processor API
 In J2SE 5.0 didn’t standardize a processing API
 - We used apt, a stand-alone tool, along with the Mirror API (com.sun.mirror)
At Build Time: JSR 269
 In Java SE 6 (December 11, 2006)
 - JSR 269: Pluggable Annotation Processing API
       -http://docs.oracle.com/javase/6/docs/technotes/guides/javac/index.htm

   - Leverages JSR 199 (Java Compiler API)
     - Javax.tools
     - Programmatically invoke javac
     - Implements ServiceLoader interface of SPI (JSR 24 - ServiceProvider API))
     - Provides the DiagnosticListener interface to allow listening for warnings and
       error by the compiler
   - Extends javac as a plug-in allowing to write custom annotation processors
   - Seamless integration with javac
     - Finds if there is an annotation registered to process a particular annotation
     - Plugs it into the compiler
At Build Time: JSR 269
 JSR 269 Defines 2 APIs
 - Writing annotation processors and interacting with the processing environment
   - Javax.annotation.processing
 - Modeling the Java Programming Language
   - Javax.lang.model
     - Type declarations and types (Accommodates Generics)
Code
Code
 Implement javax.annotation.processing.Processor or extend
  javax.annotation.processing.AbstractProcessor
 - Process method
   - Returns whether or not the annotations processed are claimed by the
     processor. This determines whether other processors can further process
     them or not
 Configure the processor using annotations
 - @SupportedAnnotationTypes
   - Register the annotations to be processed with their full-qualified name.
     Wildcats are supported as well
 - @SupportedSourceVersion
   - The Java version supported
 - @SupportedOptions
   - Register supported command-line options
Code
 Annotation processor registration
 - Javac command-line options
   - -processor <processor classes>
   - -proc:none or -proc:only. It is enabled by default
   - -processorpath <annotation path>
 - Leverage JSR 24 (ServiceProvider API) to automatically register processors
   - Compile the processor with –proc:none and package it in a jar
   - Include in META-INF/services a file named
     javax.annotation.processing.Processor containing a text entry of the full-
     qualified name of the processor class
   - Optional: Multiple processor can be registered or ordered
Metadata Processing
 Annotated elements might have nested annotated elements and so forth
 Visitor Design Pattern
 - Separation of annotation processing code from the object structure
Metamodel Construction
What is a Metamodel?
 Metadata is processed into a model that can be accessed programmatically
 - Static
 - Dynamic
 A association of the data and its metadata
Static Metamodel
 - A metamodel based on
  - One all-knowing “god object” encapsulating all possible metadata values that
    could be associated with the one annotated element
  - Advantages
    - Simple
    - Statically typed
  - Disadvantages
    - Nulls all over
Static Metamodel
Dynamic Metamodel
 Ravioli Code
 - Metamodel is structured in small and loosely-coupled components
 Decorator Design Pattern
 - Annotated elements are decorated with annotations that bring in metadata
 Advantages
 - Flexible
 Disadvantages
 - Complex
Dynamic Metamodel
Metamodel Validation
Validation?
 Ensuring the validity or correctness of semantics of the metamodel
 - Verification that a set of constraints are satisfied
 Compliance additional consistency constraints outside the Java language
  specification
 - Java only has @Target
 Example
 - Assuming that you are writing your own JSR 318 (Enterprise javabeans 3.1)
   implementation
   - You should not allow a POJO to be annotated with both @Statefull and
     @Stateless
Constraint Satisfaction
 This is anything but simple
  - A Constraint Satisfaction Problem
   - Can be resolved by multiple algorithms (Backtracking, Constrain reparation,
     and local search)
   - These algorithms are out of the scope of this presentation
Validating the Metamodel
 The imperative way
 - In Java
   - A jungle of conditional statements
Validating the Metamodel
     The logical way (Logic Programming)
       - Semantics are represented declaratively as predicates (Facts and Rules)
       - Procedurally interpreted query resolution
     - Constraint Logic Programming (An extension of Logic Programming)
         - Used for digital circuit verification
•     Prolog is King
     - tuProlog: Implementation of the Prolog interpreter in Java
       - http://alice.unibo.it/xwiki/bin/view/Tuprolog/
     - Jlog: Implementation of the Prolog interpreter in Java
       - http://jlogic.sourceforge.net/
     - JPL: Java Interface to SWI Prolog
       - http://www.swi-prolog.org/FAQ/Java.html
     Yeah you’re gonna have to learn that
Validating the Metamodel
 Clojure
 - Lisp-like language on the JVM
 - core.logic supports logic and constraint programming
 Other
 - Prova Rule Language
   - http://www.prova.ws/
 - Mercury
   - http://www.mercury.csse.unimelb.edu.au/
Validating the Metamodel
 Rules Engine
 - Drools Expert
   - A highly performance optimized rules engine
   - Rules are written in mvel (A powerful expression language) or XML
   - Integrates with Java collaborator POJOs
Interpreting the Metamodel
Transformations
 Changing the structure of existing code
 - AST (Abstract Syntax Tree) rewriting
 - Adding/removing behavior
 - Automatic generation of cookie-cutter code
 - Create complex class hierarchies
 Notable byte code manipulation libraries
 - ASM
 - GCLIB
 - Javassist
 - Etc…
Transformations
 ASM
 - Small
 - Fast
 - Low-level
 - Require a good understanding of byte-code and Java Language Spec
 - Good documentation
 - http://asm.ow2.org/
 GCLIB
 - Built on top of ASM
 - High performance
 - Issues with signed jars
 - Dropped by Hibernate in favor of Javassist for lack of support and active
 - http://cglib.sourceforge.net/
Transformations
 Javassist
 - Very easy to use
 - Works with signed jars
 - Good documentation
 - Excellent Javadoc
 - Adopted by hibernate
 - Slower than GCLIB
 - http://www.jboss.org/javassist/
Weaving in Cross-Cutting Concerns
 OOP creates a hierarchical object model by nature
 Certain things are not necessary a part of the application logic
 - Logging
 - Transactions
 - Security
 - Etc…
 AOP (Aspect Oriented Programming)
   - Code weaving
   - Dynamic Proxies
Mapping
 Map one object to another based on the metamodel
 Example
 - Dozer
   - http://dozer.sourceforge.net/
 - ORM Frameworks
Mapping



    Metadata                                   Row
                                               Class

               Metamodel


     POJO
     Class                 Mapper   Creates     Row
                                              Instance


                 POJO
                Instance
Other
 Inversion of Control
 - Dependency Injection (JSR 330)
 Instrumentation
 - Monitor and measure performance
 - Diagnostics
 - Trace info
 - Etc…
 Etc…
Metaprogramming in the Wild
Metaprogramming in the Wild
 Spring
  - Lightweight POJO-based frameworks
  - Non-invasiveness
    - Stereotyping a class in lieu of inheritance to acquire behavior
    - Arbitrary methods signature instead of overriding inherited ones
  - Declarative configuration
    - Dependency Injection
 Rails
  - Rapid Development
  - Convention over configuration
    - Active Record Pattern
 Hibernate/JPA (JSR 317)
  - ORM
 Guice
 Spring AOP
 Etc…
Model-Driven Engineering
  Philosophy
  - In an application
    - Artifacts that are good candidates for auto-generation
    - Why?
      - The architecture is defined by
       - Making abstractions guided by the separation of concerns
      - Most deal with the same concerns
       - Avoid rediscovering the best approach every time
  - Code generation, Scaffolding, etc…
Project Averroes
Averroes
 A metaprogramming framework
 - Auto-discovery of annotations
   - @Discoverable
 - Annotation processing
 - Metamodel construction
 - Metamodel validation
   - Annotations of annotations (meta-metadata)
     - @AccompaniedWith
     - @AccompaniedWithAtLeastOne
     - @ForType
     - @ForTypeAnnotatedWith
     - @ForTypeIncluding
     - @NotAccompaniedWith
 A work in progress…
 - https://github.com/PolymathicCoder/Averroes
Questions?
Thank You!


@PolymathicCoder

Más contenido relacionado

La actualidad más candente

스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기 스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기 Chanwook Park
 
Concurrency Control in MongoDB 3.0
Concurrency Control in MongoDB 3.0Concurrency Control in MongoDB 3.0
Concurrency Control in MongoDB 3.0MongoDB
 
Logging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & KibanaLogging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & KibanaAmazee Labs
 
Elastic stack Presentation
Elastic stack PresentationElastic stack Presentation
Elastic stack PresentationAmr Alaa Yassen
 
Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019
Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019
Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019Loiane Groner
 
Performance Analysis: The USE Method
Performance Analysis: The USE MethodPerformance Analysis: The USE Method
Performance Analysis: The USE MethodBrendan Gregg
 
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안SANG WON PARK
 
Kafka Streams vs. KSQL for Stream Processing on top of Apache Kafka
Kafka Streams vs. KSQL for Stream Processing on top of Apache KafkaKafka Streams vs. KSQL for Stream Processing on top of Apache Kafka
Kafka Streams vs. KSQL for Stream Processing on top of Apache KafkaKai Wähner
 
Reactive Microservices with Spring 5: WebFlux
Reactive Microservices with Spring 5: WebFlux Reactive Microservices with Spring 5: WebFlux
Reactive Microservices with Spring 5: WebFlux Trayan Iliev
 
Prometheus – a next-gen Monitoring System
Prometheus – a next-gen Monitoring SystemPrometheus – a next-gen Monitoring System
Prometheus – a next-gen Monitoring SystemFabian Reinartz
 
ElasticSearch in action
ElasticSearch in actionElasticSearch in action
ElasticSearch in actionCodemotion
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11 Knoldus Inc.
 
Performance Testing Cloud-Based Systems
Performance Testing Cloud-Based SystemsPerformance Testing Cloud-Based Systems
Performance Testing Cloud-Based SystemsTechWell
 
API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)Peter Thomas
 
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-JupiterToolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-JupiterBoni García
 
Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...
Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...
Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...confluent
 
Building a fully managed stream processing platform on Flink at scale for Lin...
Building a fully managed stream processing platform on Flink at scale for Lin...Building a fully managed stream processing platform on Flink at scale for Lin...
Building a fully managed stream processing platform on Flink at scale for Lin...Flink Forward
 

La actualidad más candente (20)

스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기 스프링처럼 JDBC 리팩터링하기
스프링처럼 JDBC 리팩터링하기
 
Concurrency Control in MongoDB 3.0
Concurrency Control in MongoDB 3.0Concurrency Control in MongoDB 3.0
Concurrency Control in MongoDB 3.0
 
Logging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & KibanaLogging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & Kibana
 
Elastic stack Presentation
Elastic stack PresentationElastic stack Presentation
Elastic stack Presentation
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019
Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019
Full-Stack Reactive with Spring WebFlux + Angular - JConf Colombia 2019
 
Performance Analysis: The USE Method
Performance Analysis: The USE MethodPerformance Analysis: The USE Method
Performance Analysis: The USE Method
 
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
 
Kafka Streams vs. KSQL for Stream Processing on top of Apache Kafka
Kafka Streams vs. KSQL for Stream Processing on top of Apache KafkaKafka Streams vs. KSQL for Stream Processing on top of Apache Kafka
Kafka Streams vs. KSQL for Stream Processing on top of Apache Kafka
 
ELK Stack
ELK StackELK Stack
ELK Stack
 
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
 
Reactive Microservices with Spring 5: WebFlux
Reactive Microservices with Spring 5: WebFlux Reactive Microservices with Spring 5: WebFlux
Reactive Microservices with Spring 5: WebFlux
 
Prometheus – a next-gen Monitoring System
Prometheus – a next-gen Monitoring SystemPrometheus – a next-gen Monitoring System
Prometheus – a next-gen Monitoring System
 
ElasticSearch in action
ElasticSearch in actionElasticSearch in action
ElasticSearch in action
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11
 
Performance Testing Cloud-Based Systems
Performance Testing Cloud-Based SystemsPerformance Testing Cloud-Based Systems
Performance Testing Cloud-Based Systems
 
API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)
 
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-JupiterToolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
 
Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...
Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...
Performance Tuning RocksDB for Kafka Streams' State Stores (Dhruba Borthakur,...
 
Building a fully managed stream processing platform on Flink at scale for Lin...
Building a fully managed stream processing platform on Flink at scale for Lin...Building a fully managed stream processing platform on Flink at scale for Lin...
Building a fully managed stream processing platform on Flink at scale for Lin...
 

Destacado

Tools for Meta-Programming
Tools for Meta-ProgrammingTools for Meta-Programming
Tools for Meta-Programmingelliando dias
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling SoftwareAbdelmonaim Remani
 
Scala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingScala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingMeir Maor
 
Building web apps with Vaadin 8
Building web apps with Vaadin 8 Building web apps with Vaadin 8
Building web apps with Vaadin 8 Marcus Hellberg
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinC* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinDataStax Academy
 
Apache 핵심 프로젝트 camel 엿보기
Apache 핵심 프로젝트 camel 엿보기Apache 핵심 프로젝트 camel 엿보기
Apache 핵심 프로젝트 camel 엿보기Hwang Sun Oh Kelly
 
Elastic search overview
Elastic search overviewElastic search overview
Elastic search overviewABC Talks
 
Akka in Production - ScalaDays 2015
Akka in Production - ScalaDays 2015Akka in Production - ScalaDays 2015
Akka in Production - ScalaDays 2015Evan Chan
 
Reactive app using actor model & apache spark
Reactive app using actor model & apache sparkReactive app using actor model & apache spark
Reactive app using actor model & apache sparkRahul Kumar
 
Reactive dashboard’s using apache spark
Reactive dashboard’s using apache sparkReactive dashboard’s using apache spark
Reactive dashboard’s using apache sparkRahul Kumar
 
Why vREST?
Why vREST?Why vREST?
Why vREST?vrest_io
 
Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...
Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...
Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...Anton Kirillov
 
Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심흥배 최
 
How to build a great coding culture
How to build a great coding cultureHow to build a great coding culture
How to build a great coding cultureMark Halvorson
 
Sass Code Reviews - How one code review changed my life #SassConf2015
Sass Code Reviews - How one code review changed my life #SassConf2015Sass Code Reviews - How one code review changed my life #SassConf2015
Sass Code Reviews - How one code review changed my life #SassConf2015Stacy Kvernmo
 

Destacado (18)

The Eschatology of Java
The Eschatology of JavaThe Eschatology of Java
The Eschatology of Java
 
Tools for Meta-Programming
Tools for Meta-ProgrammingTools for Meta-Programming
Tools for Meta-Programming
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
 
Scala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingScala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgramming
 
GWT Reloaded
GWT ReloadedGWT Reloaded
GWT Reloaded
 
How RESTful Is Your REST?
How RESTful Is Your REST?How RESTful Is Your REST?
How RESTful Is Your REST?
 
Building web apps with Vaadin 8
Building web apps with Vaadin 8 Building web apps with Vaadin 8
Building web apps with Vaadin 8
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinC* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
 
Apache 핵심 프로젝트 camel 엿보기
Apache 핵심 프로젝트 camel 엿보기Apache 핵심 프로젝트 camel 엿보기
Apache 핵심 프로젝트 camel 엿보기
 
Elastic search overview
Elastic search overviewElastic search overview
Elastic search overview
 
Akka in Production - ScalaDays 2015
Akka in Production - ScalaDays 2015Akka in Production - ScalaDays 2015
Akka in Production - ScalaDays 2015
 
Reactive app using actor model & apache spark
Reactive app using actor model & apache sparkReactive app using actor model & apache spark
Reactive app using actor model & apache spark
 
Reactive dashboard’s using apache spark
Reactive dashboard’s using apache sparkReactive dashboard’s using apache spark
Reactive dashboard’s using apache spark
 
Why vREST?
Why vREST?Why vREST?
Why vREST?
 
Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...
Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...
Data processing platforms architectures with Spark, Mesos, Akka, Cassandra an...
 
Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심
 
How to build a great coding culture
How to build a great coding cultureHow to build a great coding culture
How to build a great coding culture
 
Sass Code Reviews - How one code review changed my life #SassConf2015
Sass Code Reviews - How one code review changed my life #SassConf2015Sass Code Reviews - How one code review changed my life #SassConf2015
Sass Code Reviews - How one code review changed my life #SassConf2015
 

Similar a The Art of Metaprogramming in Java

JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...Jorge Hidalgo
 
ContextualContinuous Profilng
ContextualContinuous ProfilngContextualContinuous Profilng
ContextualContinuous ProfilngJaroslav Bachorik
 
Playing with Java Classes and Bytecode
Playing with Java Classes and BytecodePlaying with Java Classes and Bytecode
Playing with Java Classes and BytecodeYoav Avrahami
 
Manuel - SPR - Intro to Java Language_2016
Manuel - SPR - Intro to Java Language_2016Manuel - SPR - Intro to Java Language_2016
Manuel - SPR - Intro to Java Language_2016Manuel Fomitescu
 
Alive and Well with Java 8
Alive and Well with Java 8Alive and Well with Java 8
Alive and Well with Java 8Adam Pelsoczi
 
Maven introduction in Mule
Maven introduction in MuleMaven introduction in Mule
Maven introduction in MuleShahid Shaik
 
3 rad extensibility-srilakshmi_s_rajesh_k
3 rad extensibility-srilakshmi_s_rajesh_k3 rad extensibility-srilakshmi_s_rajesh_k
3 rad extensibility-srilakshmi_s_rajesh_kIBM
 
Rad Extensibility - Srilakshmi S Rajesh K
Rad Extensibility - Srilakshmi S Rajesh KRad Extensibility - Srilakshmi S Rajesh K
Rad Extensibility - Srilakshmi S Rajesh KRoopa Nadkarni
 
Gwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIGwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIArnaud Tournier
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, androidi i
 
Java bytecode and classes
Java bytecode and classesJava bytecode and classes
Java bytecode and classesyoavwix
 
eXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework IntroductioneXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework Introductionvstorm83
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsmichaelaaron25322
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep DiveGabriel Walt
 

Similar a The Art of Metaprogramming in Java (20)

JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
 
Laravel 4 presentation
Laravel 4 presentationLaravel 4 presentation
Laravel 4 presentation
 
ContextualContinuous Profilng
ContextualContinuous ProfilngContextualContinuous Profilng
ContextualContinuous Profilng
 
Playing with Java Classes and Bytecode
Playing with Java Classes and BytecodePlaying with Java Classes and Bytecode
Playing with Java Classes and Bytecode
 
Java SE 8 & EE 7 Launch
Java SE 8 & EE 7 LaunchJava SE 8 & EE 7 Launch
Java SE 8 & EE 7 Launch
 
Manuel - SPR - Intro to Java Language_2016
Manuel - SPR - Intro to Java Language_2016Manuel - SPR - Intro to Java Language_2016
Manuel - SPR - Intro to Java Language_2016
 
Alive and Well with Java 8
Alive and Well with Java 8Alive and Well with Java 8
Alive and Well with Java 8
 
Maven introduction in Mule
Maven introduction in MuleMaven introduction in Mule
Maven introduction in Mule
 
3 rad extensibility-srilakshmi_s_rajesh_k
3 rad extensibility-srilakshmi_s_rajesh_k3 rad extensibility-srilakshmi_s_rajesh_k
3 rad extensibility-srilakshmi_s_rajesh_k
 
Rad Extensibility - Srilakshmi S Rajesh K
Rad Extensibility - Srilakshmi S Rajesh KRad Extensibility - Srilakshmi S Rajesh K
Rad Extensibility - Srilakshmi S Rajesh K
 
Gwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIGwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing API
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 
oop unit1.pptx
oop unit1.pptxoop unit1.pptx
oop unit1.pptx
 
Java bytecode and classes
Java bytecode and classesJava bytecode and classes
Java bytecode and classes
 
eXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework IntroductioneXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework Introduction
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
Software Development
Software DevelopmentSoftware Development
Software Development
 
1.introduction to java
1.introduction to java1.introduction to java
1.introduction to java
 

Último

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Último (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

The Art of Metaprogramming in Java

  • 1. The Art of Metaprogramming in Java Abdelmonaim Remani @PolymathicCoder
  • 2. About Me  Software Architect at Just.me Inc.  Interested in technology evangelism and enterprise software development and architecture  Frequent speaker (JavaOne, JAX, OSCON, OREDEV, etc…)  Open-source advocate  President and founder of a number of user groups - NorCal Java User Group - The Silicon Valley Spring User Group - The Silicon Valley Dart Meetup  Bio: http://about.me/PolymathicCoder  Twitter: @PolymathicCoder  Email: abdelmonaim.remani@gmail.com
  • 3. About Me  Today is my birthday! 01001000 01100001 01110000 01110000 01111001 00100000 01000010 01101001 01110010 01110100 01101000 01100100 01100001 01111001
  • 4. License  Creative Commons Attribution Non-Commercial 3.0 Unported - http://creativecommons.org/licenses/by-nc/3.0  Disclaimer: The graphics, logos, and trademarks used this presentation belong to their rightful owners.
  • 6. What Is Metadata?  The term “Metadata” was coined by Philip Bagley in 1986 in “Extension of Programming Language Concepts”  Data about data or data that describes other data - Structural (Before there is any data) - Its type - Its valid values - How it relates to other data - Its purpose - Etc… - Descriptive (After data is there) - How it was created - The context within which it exists - Etc…
  • 7. How Is Metadata Expressed?  Implicit - Adhering to a certain convention  Explicit - External to the code - DSL (Domain-Specific Language) - Markup: XML, RDF, etc… - Etc… - Internal to the code - As comments on code - Javadoc: @author, @version, @since, etc… - XDoclet - Etc… - As code itself - HTML meta tags: <meta name=“author” content=“Abdelmonaim Remani”> - Java Annotations: @Override, @Deprecated, etc… - Embedded DSL - Etc…
  • 8. How Is Metadata Being Used?  Schema - Data Dictionary in RDBMS - Check Constraints (JSR 303 Validation, Etc…) - Etc…  Semantics - WSDL (Web Services Description Language) - RDF (Resource Description Framework) - Etc…  Data Management - Build/deployment instructions (.svn files, .git, etc…) - Etc…  Configurations  Etc…
  • 9. Metadata in Java: Annotations
  • 10. Java Annotations  JSR 175 (A Metadata Facility for the Java Programming Language) - Introduced in J2SE 5.0 (September 30, 2004) - Standardized how annotations are declared in Java code  An alternative way to Javadoc comments, externally as XML, etc… - More readable - Closer to the code - Statically-typed - Can be retained until runtime
  • 11. JDK Annotations  Java Language Spec Annotations - @Override - @Deprecated - @SuppressWarning  JSR 250 (Common Annotations for the Java Platform) - @PostConstruct - @Resource - @DenyAll - Etc…
  • 12. Write Your Own  You need to tell the compiler how the annotation is to be treated  In java.lang.annotation package - @Target - The element type the annotation can be applied to - ElememtType.ANNOTATION_TYPE - ElememtType.CONSTRUCTOR - ElememtType.FIELD - ElememtType.LOCAL_VARIABLE - ElememtType.METHOD - ElememtType.PACKAGE - ElememtType.PARAMETER - ElememtType.TYPE
  • 13. Write Your Own  In java.lang.annotation package - @Retention - RetentionPolicy.SOURCE - Discarded by the compiler - RetentionPolicy.CLASS - Included in the class file but ignored the JVM. This is the default - RetentionPolicy.RUNTINE - Included in the class file and read by the JVM. - @Documented - Whether it should be shown in the javadoc or not - @Inherited - Allowed to be inherited by subclasses
  • 14. Write Your Own  Attributes - Carry additional metadata details - May only be - Primitives - Enums - Java.lang.String - Java.lang.Class - One-dimensional arrays of the above - May have a default value - No modifiers or parameters
  • 15. Code
  • 16. Notes  Annotating a package - Must create a file named package-info.java in the target package - Note that “package-info” is not a valid Java identifier - Most IDEs you will prevent you from creating a class name “package-info” - Make sure you create it as a regular file with .java extension instead
  • 18. What is Metaprogramming?  Writing programs that write or manipulate other programs or themselves based on some metadata  Metaprogramming -!= Generative/Automatic Programming  Ralph Johnson - “It is about programming at the meta level, that is about changing your interpreter or changing how code is compiled”  Black art and a Big-boy’s toy  An underused feature  Supported in many languages and across several platforms  The backbone of many of the most successful frameworks
  • 19. How is Metaprogramming Supported?  Exposing the internals of the runtime/compiler as an API  Dynamic execution of expressions containing programming commands - Code as Strings - Code as a series if method calls  A program transformation system - A description gets transformed it to a target language - The compiler itself - YACC takes in grammar, and produces C/C++ code containing yyparse() - ANTLR (ANother Tool for Language Recognition)
  • 20. Concepts  Metalanguage - The language in which the metaprogram is written in  Object Language - The language in which the target (produced) program is written in  Reflection or Reflexivity - When metalanguage == object language - No translation necessary
  • 21. Usage in code  Static data that can be pre-computed or pre-generated at compile time  Eliminate boiler-plate - Code that cannot be abstracted in functions for DRYness sake - Think Aspects in AOP - Stereotypes in Spring - Etc… - Code of common methods - Getters/setters, toString(), hashCode(), equals(), etc…  Etc…
  • 22. Benefits  In code - Performance gain - Flexibility - Simplicity - Etc…  Development - Minimize the LOC to express a solution - Productivity gain - Reduced development time/efficiency - Etc…
  • 23. How to?  Many techniques focused on specific aspects of metaprogramming  No well-defined best practices  This presentation is an attempt to bring in some structure through defining a process - Defining the metadata - Processing the metadata - Metamodel construction - Validating the metamodel - Metamodel interpretation
  • 25. Metadata Processing  Programmatically reading/accessing metadata - Parsing raw metadata - Call to an API - Reflection - Query - Etc… - Tools
  • 26. Metadata Processing in Java: Annotation Processing
  • 27. At Runtime: JSR 175  JSR 175: A Metadata Facility for the Java Programming Language - Defined the core reflection API for reading/accessing annotations on annotated elements at runtime as long as their retention policy extends to the runtime - Reflection - Reading annotations is done in reference to the structure of the program  Libraries - Reflections: http://code.google.com/p/reflections/ - FEST-Reflect: http://fest.easytesting.org/ - ReflectASM: http://code.google.com/p/reflectasm/ - Etc…
  • 29. At Build Time: JSR 269  Mirror-Based Reflection - Reflective capabilities are encapsulated in intermediary objects called mirrors - Annotations are accessible through a processor API  In J2SE 5.0 didn’t standardize a processing API - We used apt, a stand-alone tool, along with the Mirror API (com.sun.mirror)
  • 30. At Build Time: JSR 269  In Java SE 6 (December 11, 2006) - JSR 269: Pluggable Annotation Processing API -http://docs.oracle.com/javase/6/docs/technotes/guides/javac/index.htm - Leverages JSR 199 (Java Compiler API) - Javax.tools - Programmatically invoke javac - Implements ServiceLoader interface of SPI (JSR 24 - ServiceProvider API)) - Provides the DiagnosticListener interface to allow listening for warnings and error by the compiler - Extends javac as a plug-in allowing to write custom annotation processors - Seamless integration with javac - Finds if there is an annotation registered to process a particular annotation - Plugs it into the compiler
  • 31. At Build Time: JSR 269  JSR 269 Defines 2 APIs - Writing annotation processors and interacting with the processing environment - Javax.annotation.processing - Modeling the Java Programming Language - Javax.lang.model - Type declarations and types (Accommodates Generics)
  • 32. Code
  • 33. Code  Implement javax.annotation.processing.Processor or extend javax.annotation.processing.AbstractProcessor - Process method - Returns whether or not the annotations processed are claimed by the processor. This determines whether other processors can further process them or not  Configure the processor using annotations - @SupportedAnnotationTypes - Register the annotations to be processed with their full-qualified name. Wildcats are supported as well - @SupportedSourceVersion - The Java version supported - @SupportedOptions - Register supported command-line options
  • 34. Code  Annotation processor registration - Javac command-line options - -processor <processor classes> - -proc:none or -proc:only. It is enabled by default - -processorpath <annotation path> - Leverage JSR 24 (ServiceProvider API) to automatically register processors - Compile the processor with –proc:none and package it in a jar - Include in META-INF/services a file named javax.annotation.processing.Processor containing a text entry of the full- qualified name of the processor class - Optional: Multiple processor can be registered or ordered
  • 35. Metadata Processing  Annotated elements might have nested annotated elements and so forth  Visitor Design Pattern - Separation of annotation processing code from the object structure
  • 37. What is a Metamodel?  Metadata is processed into a model that can be accessed programmatically - Static - Dynamic  A association of the data and its metadata
  • 38. Static Metamodel - A metamodel based on - One all-knowing “god object” encapsulating all possible metadata values that could be associated with the one annotated element - Advantages - Simple - Statically typed - Disadvantages - Nulls all over
  • 40. Dynamic Metamodel  Ravioli Code - Metamodel is structured in small and loosely-coupled components  Decorator Design Pattern - Annotated elements are decorated with annotations that bring in metadata  Advantages - Flexible  Disadvantages - Complex
  • 43. Validation?  Ensuring the validity or correctness of semantics of the metamodel - Verification that a set of constraints are satisfied  Compliance additional consistency constraints outside the Java language specification - Java only has @Target  Example - Assuming that you are writing your own JSR 318 (Enterprise javabeans 3.1) implementation - You should not allow a POJO to be annotated with both @Statefull and @Stateless
  • 44. Constraint Satisfaction  This is anything but simple - A Constraint Satisfaction Problem - Can be resolved by multiple algorithms (Backtracking, Constrain reparation, and local search) - These algorithms are out of the scope of this presentation
  • 45. Validating the Metamodel  The imperative way - In Java - A jungle of conditional statements
  • 46. Validating the Metamodel  The logical way (Logic Programming) - Semantics are represented declaratively as predicates (Facts and Rules) - Procedurally interpreted query resolution - Constraint Logic Programming (An extension of Logic Programming) - Used for digital circuit verification • Prolog is King - tuProlog: Implementation of the Prolog interpreter in Java - http://alice.unibo.it/xwiki/bin/view/Tuprolog/ - Jlog: Implementation of the Prolog interpreter in Java - http://jlogic.sourceforge.net/ - JPL: Java Interface to SWI Prolog - http://www.swi-prolog.org/FAQ/Java.html  Yeah you’re gonna have to learn that
  • 47. Validating the Metamodel  Clojure - Lisp-like language on the JVM - core.logic supports logic and constraint programming  Other - Prova Rule Language - http://www.prova.ws/ - Mercury - http://www.mercury.csse.unimelb.edu.au/
  • 48. Validating the Metamodel  Rules Engine - Drools Expert - A highly performance optimized rules engine - Rules are written in mvel (A powerful expression language) or XML - Integrates with Java collaborator POJOs
  • 50. Transformations  Changing the structure of existing code - AST (Abstract Syntax Tree) rewriting - Adding/removing behavior - Automatic generation of cookie-cutter code - Create complex class hierarchies  Notable byte code manipulation libraries - ASM - GCLIB - Javassist - Etc…
  • 51. Transformations  ASM - Small - Fast - Low-level - Require a good understanding of byte-code and Java Language Spec - Good documentation - http://asm.ow2.org/  GCLIB - Built on top of ASM - High performance - Issues with signed jars - Dropped by Hibernate in favor of Javassist for lack of support and active - http://cglib.sourceforge.net/
  • 52. Transformations  Javassist - Very easy to use - Works with signed jars - Good documentation - Excellent Javadoc - Adopted by hibernate - Slower than GCLIB - http://www.jboss.org/javassist/
  • 53. Weaving in Cross-Cutting Concerns  OOP creates a hierarchical object model by nature  Certain things are not necessary a part of the application logic - Logging - Transactions - Security - Etc…  AOP (Aspect Oriented Programming) - Code weaving - Dynamic Proxies
  • 54. Mapping  Map one object to another based on the metamodel  Example - Dozer - http://dozer.sourceforge.net/ - ORM Frameworks
  • 55. Mapping Metadata Row Class Metamodel POJO Class Mapper Creates Row Instance POJO Instance
  • 56. Other  Inversion of Control - Dependency Injection (JSR 330)  Instrumentation - Monitor and measure performance - Diagnostics - Trace info - Etc…  Etc…
  • 58. Metaprogramming in the Wild  Spring - Lightweight POJO-based frameworks - Non-invasiveness - Stereotyping a class in lieu of inheritance to acquire behavior - Arbitrary methods signature instead of overriding inherited ones - Declarative configuration - Dependency Injection  Rails - Rapid Development - Convention over configuration - Active Record Pattern  Hibernate/JPA (JSR 317) - ORM  Guice  Spring AOP  Etc…
  • 59. Model-Driven Engineering  Philosophy - In an application - Artifacts that are good candidates for auto-generation - Why? - The architecture is defined by - Making abstractions guided by the separation of concerns - Most deal with the same concerns - Avoid rediscovering the best approach every time - Code generation, Scaffolding, etc…
  • 61. Averroes  A metaprogramming framework - Auto-discovery of annotations - @Discoverable - Annotation processing - Metamodel construction - Metamodel validation - Annotations of annotations (meta-metadata) - @AccompaniedWith - @AccompaniedWithAtLeastOne - @ForType - @ForTypeAnnotatedWith - @ForTypeIncluding - @NotAccompaniedWith  A work in progress… - https://github.com/PolymathicCoder/Averroes