Decorating Code A Survey of Meta-programming Jenna Pederson June 17, 2006 © 2006 Jenna Pederson
Goals <ul><li>Show how meta-programming is used </li></ul><ul><li>Show its strengths and weaknesses </li></ul><ul><li>Expl...
Background <ul><li>Definition: writing of programs that write themselves </li></ul><ul><ul><li>Generating boilerplate code...
Programming Paradigms <ul><li>Imperative Programming </li></ul><ul><li>Describes  how  to do something </li></ul><ul><li>S...
Declarative Programming in an Imperative Language <ul><li>Combination allows developers to be more expressive </li></ul><u...
Declarative Programming in an Imperative Language <ul><li>Example: Domain Specific Languages </li></ul><ul><ul><li>Languag...
Java <ul><li>Imperative </li></ul><ul><li>Becoming necessary to define relationships between containers, components, and s...
XML <ul><li>First declarative approach to problem </li></ul><ul><li>Separates configuration from code </li></ul><ul><li>“ ...
xDoclet <ul><li>Framework embeds tags in javadoc comments </li></ul><ul><li>Tags specify: </li></ul><ul><ul><li>where to d...
xDoclet <ul><li>Generated code/template programming more accurate </li></ul><ul><li>Difficult to maintain </li></ul><ul><u...
xDoclet <ul><li>Cannot access metadata at runtime </li></ul><ul><li>Other frameworks: </li></ul><ul><ul><li>Jakarta Common...
Microsoft <ul><li>Meta-programming supported in: </li></ul><ul><ul><li>Microsoft Transaction Services (MTS) </li></ul></ul...
Microsoft .NET Attributes <ul><li>Solves early COM/MTS issues </li></ul><ul><li>Declarative tags reside in code </li></ul>...
Microsoft .NET Attributes <ul><li>Framework provides ability to create and customize </li></ul><ul><li>Not language-specif...
Ruby <ul><li>Interpreted OO language </li></ul><ul><li>Gained momentum because of meta-programming capabilities </li></ul>...
Ruby <ul><li>Objects can change behavior on the fly </li></ul><ul><li>Generate classes and methods at runtime </li></ul><u...
Java Annotations <ul><li>First-class citizens in Java code </li></ul><ul><li>Part of APIs </li></ul><ul><li>Checked at com...
Java Annotations Metadata and Code Coupled <ul><li>Pros: </li></ul><ul><li>Easier to understand context of metadata </li><...
JSR 175 <ul><li>Introduced Annotations to Java, JDK 5.0 </li></ul><ul><li>Improve developer productivity </li></ul><ul><li...
Annotation Type Declaration  <ul><li>Defines how an annotation can be used </li></ul>1 @Target({ElementType.METHOD, Elemen...
Decorating Elements <ul><li>Can be applied to: </li></ul><ul><ul><li>Classes, Interfaces, Annotations, Enums </li></ul></u...
Annotation Processors <ul><li>JSR 175 added to reflection and javadoc APIs </li></ul><ul><li>Tools </li></ul><ul><ul><li>G...
Other JSRs <ul><li>JSR 250 </li></ul><ul><ul><li>Common set of annotations for J2SE and J2EE applications </li></ul></ul><...
JSR 181 <ul><li>Common theme: Increase developer productivity </li></ul><ul><ul><li>Reduce XML configuration code </li></u...
JSR 181 Annotations <ul><li>@WebService </li></ul><ul><ul><li>Exposes class or interface as Web Service </li></ul></ul><ul...
JSR 181 Annotations <ul><li>@Oneway </li></ul><ul><ul><li>Denote an operation has only input </li></ul></ul><ul><li>@Handl...
Web Service @WebService public class MyWebService { @WebMethod public void aWebServiceMethod() {   // do something } } © 2...
Apache Axis <ul><li>SOAP-based implementation of Web Services </li></ul><ul><li>WSDD file configures and exposes a Web Ser...
Key Features <ul><li>Uses JSR 181 annotations </li></ul><ul><li>Annotations read at runtime </li></ul><ul><li>No WSDD conf...
Analysis <ul><li>Must learn concept of annotations to be productive </li></ul><ul><li>XML configuration is hairy </li></ul...
Recap <ul><li>Java Annotations: </li></ul><ul><ul><li>Convenient for developers </li></ul></ul><ul><ul><li>Easier to under...
Upcoming SlideShare
Loading in …5
×

Decorating Code (Presentation)

878 views
814 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
878
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Decorating Code (Presentation)

  1. 1. Decorating Code A Survey of Meta-programming Jenna Pederson June 17, 2006 © 2006 Jenna Pederson
  2. 2. Goals <ul><li>Show how meta-programming is used </li></ul><ul><li>Show its strengths and weaknesses </li></ul><ul><li>Explore the use of Java annotations </li></ul><ul><ul><li>Creating annotation types </li></ul></ul><ul><ul><li>Using annotations </li></ul></ul><ul><ul><li>Processing annotations </li></ul></ul><ul><li>See Annotations in Action </li></ul><ul><ul><li>Implement and use annotation-based Axis system </li></ul></ul>© 2006 Jenna Pederson
  3. 3. Background <ul><li>Definition: writing of programs that write themselves </li></ul><ul><ul><li>Generating boilerplate code at compile-time </li></ul></ul><ul><ul><li>Generating other programs at compile-time </li></ul></ul><ul><ul><li>Modifying other programs or itself at runtime </li></ul></ul><ul><li>Adding levels of abstraction helps developers </li></ul><ul><ul><li>Compiler – most common form </li></ul></ul>© 2006 Jenna Pederson
  4. 4. Programming Paradigms <ul><li>Imperative Programming </li></ul><ul><li>Describes how to do something </li></ul><ul><li>Specifies an algorithm </li></ul><ul><li>Examples: </li></ul><ul><ul><li>Computer hardware </li></ul></ul><ul><ul><li>Java </li></ul></ul><ul><ul><li>Fortran </li></ul></ul><ul><li>Declarative Programming </li></ul><ul><li>Describes what is needed to do something </li></ul><ul><li>Example: </li></ul><ul><ul><li>SQL </li></ul></ul>© 2006 Jenna Pederson
  5. 5. Declarative Programming in an Imperative Language <ul><li>Combination allows developers to be more expressive </li></ul><ul><li>Encapsulate non-declarative information into a framework </li></ul>© 2006 Jenna Pederson
  6. 6. Declarative Programming in an Imperative Language <ul><li>Example: Domain Specific Languages </li></ul><ul><ul><li>Language closely resembles the problem promotes a better understanding </li></ul></ul><ul><li>Example: JUnit </li></ul><ul><ul><li>Using reflection, JUnit knows that any public method whose name begins with ‘test’ is a JUnit test </li></ul></ul><ul><li>Example: Meta-programming </li></ul><ul><ul><li>.NET attributes, Java annotations </li></ul></ul>© 2006 Jenna Pederson
  7. 7. Java <ul><li>Imperative </li></ul><ul><li>Becoming necessary to define relationships between containers, components, and services </li></ul><ul><li>Lots of boilerplate code </li></ul><ul><ul><li>Complex </li></ul></ul><ul><ul><li>Duplicate code </li></ul></ul><ul><ul><li>Maintenance Issues </li></ul></ul>© 2006 Jenna Pederson
  8. 8. XML <ul><li>First declarative approach to problem </li></ul><ul><li>Separates configuration from code </li></ul><ul><li>“ XML Hell” </li></ul><ul><li>Redundant </li></ul><ul><ul><li>Often paired with a Java class </li></ul></ul><ul><ul><li>Elements repeated </li></ul></ul><ul><li>Not checked at compile-time </li></ul>© 2006 Jenna Pederson
  9. 9. xDoclet <ul><li>Framework embeds tags in javadoc comments </li></ul><ul><li>Tags specify: </li></ul><ul><ul><li>where to deploy services </li></ul></ul><ul><ul><li>what code to generate </li></ul></ul><ul><ul><li>what configuration files to generate </li></ul></ul><ul><li>Doclet processes tags prior to compilation to generate code or configuration files </li></ul>© 2006 Jenna Pederson
  10. 10. xDoclet <ul><li>Generated code/template programming more accurate </li></ul><ul><li>Difficult to maintain </li></ul><ul><ul><li>Cannot edit/customize generated code </li></ul></ul><ul><li>Must maintain doclet processor and the template </li></ul>© 2006 Jenna Pederson
  11. 11. xDoclet <ul><li>Cannot access metadata at runtime </li></ul><ul><li>Other frameworks: </li></ul><ul><ul><li>Jakarta Commons-Attributes </li></ul></ul><ul><ul><li>Attrib4J </li></ul></ul>© 2006 Jenna Pederson
  12. 12. Microsoft <ul><li>Meta-programming supported in: </li></ul><ul><ul><li>Microsoft Transaction Services (MTS) </li></ul></ul><ul><ul><li>COM+ environments </li></ul></ul><ul><ul><li>.NET Attributes </li></ul></ul><ul><li>COM </li></ul><ul><ul><li>Explore late binding </li></ul></ul><ul><ul><li>Metadata spread across several files </li></ul></ul>© 2006 Jenna Pederson
  13. 13. Microsoft .NET Attributes <ul><li>Solves early COM/MTS issues </li></ul><ul><li>Declarative tags reside in code </li></ul><ul><li>Stored in .NET assembly </li></ul><ul><li>Available at design-time, compile-time, and runtime </li></ul><ul><li>200 attributes available out of the box </li></ul>© 2006 Jenna Pederson
  14. 14. Microsoft .NET Attributes <ul><li>Framework provides ability to create and customize </li></ul><ul><li>Not language-specific </li></ul><ul><ul><li>Can be used across .NET framework </li></ul></ul><ul><ul><li>Not required to modify language-specific compilers </li></ul></ul>© 2006 Jenna Pederson
  15. 15. Ruby <ul><li>Interpreted OO language </li></ul><ul><li>Gained momentum because of meta-programming capabilities </li></ul><ul><li>Simple, concise syntax with dynamic typing </li></ul><ul><li>Reflective features enable easy meta-programming </li></ul>© 2006 Jenna Pederson
  16. 16. Ruby <ul><li>Objects can change behavior on the fly </li></ul><ul><li>Generate classes and methods at runtime </li></ul><ul><li>Methods invoked as part of class declaration </li></ul><ul><li>Ruby on Rails </li></ul><ul><ul><li>Dynamic typing, reflection, and meta-programming generate core application at runtime </li></ul></ul>© 2006 Jenna Pederson
  17. 17. Java Annotations <ul><li>First-class citizens in Java code </li></ul><ul><li>Part of APIs </li></ul><ul><li>Checked at compile-time </li></ul><ul><li>Supported by IDEs, modeling tools </li></ul>© 2006 Jenna Pederson
  18. 18. Java Annotations Metadata and Code Coupled <ul><li>Pros: </li></ul><ul><li>Easier to understand context of metadata </li></ul><ul><li>Maintenance easier </li></ul><ul><li>Visible, not forgotten </li></ul><ul><li>Define objects more uniquely – becomes DSL </li></ul><ul><li>Increased productivity </li></ul><ul><li>Cons: </li></ul><ul><li>Blur line of separation between code and configuration </li></ul><ul><ul><li>Configuration difficult </li></ul></ul><ul><li>Tie application to specific frameworks </li></ul><ul><ul><li>Upgrades/migrations difficult </li></ul></ul>© 2006 Jenna Pederson
  19. 19. JSR 175 <ul><li>Introduced Annotations to Java, JDK 5.0 </li></ul><ul><li>Improve developer productivity </li></ul><ul><li>Defines syntax and semantics of annotations </li></ul><ul><li>Framework to create annotations </li></ul><ul><li>Enables Java to be programmed declaratively </li></ul>© 2006 Jenna Pederson
  20. 20. Annotation Type Declaration <ul><li>Defines how an annotation can be used </li></ul>1 @Target({ElementType.METHOD, ElementType.TYPE}) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Inherited 4 @Documented 5 public @interface Transactional { 6 Propagation propagation() default Propagation.REQUIRED; 7 Isolation isolation() default Isolation.DEFAULT; 8 boolean readOnly() default false; 9 Class[] rollbackFor() default {}; 10 String[] rollbackForClassName() default {}; 11 Class[] noRollbackFor() default {}; 12 String[] noRollbackForClassName() default {}; 13 } © 2006 Jenna Pederson
  21. 21. Decorating Elements <ul><li>Can be applied to: </li></ul><ul><ul><li>Classes, Interfaces, Annotations, Enums </li></ul></ul><ul><ul><li>Fields, Parameters, Enum constants </li></ul></ul><ul><ul><li>Methods, Constructors </li></ul></ul><ul><ul><li>Packages </li></ul></ul><ul><li>Must be imported into class of annotated element </li></ul><ul><li>Cannot add classes or methods at runtime </li></ul>© 2006 Jenna Pederson
  22. 22. Annotation Processors <ul><li>JSR 175 added to reflection and javadoc APIs </li></ul><ul><li>Tools </li></ul><ul><ul><li>General – read external programs, not loaded into VM </li></ul></ul><ul><ul><li>Specific – read known annotations and load interfaces into VM </li></ul></ul><ul><ul><li>Introspectors – use reflection and inspect at runtime, classes and interfaces loaded into VM </li></ul></ul>© 2006 Jenna Pederson
  23. 23. Other JSRs <ul><li>JSR 250 </li></ul><ul><ul><li>Common set of annotations for J2SE and J2EE applications </li></ul></ul><ul><li>JSR 269 </li></ul><ul><ul><li>Common framework for processing annotations </li></ul></ul><ul><li>JSR 181 </li></ul><ul><ul><li>Common set of annotations for exposing and configuring Web Services </li></ul></ul>© 2006 Jenna Pederson
  24. 24. JSR 181 <ul><li>Common theme: Increase developer productivity </li></ul><ul><ul><li>Reduce XML configuration code </li></ul></ul><ul><li>Develop standard for creating Java Web Services </li></ul>© 2006 Jenna Pederson
  25. 25. JSR 181 Annotations <ul><li>@WebService </li></ul><ul><ul><li>Exposes class or interface as Web Service </li></ul></ul><ul><li>@WebMethod </li></ul><ul><ul><li>Exposes method as Web Service operation </li></ul></ul><ul><li>@WebParam </li></ul><ul><ul><li>Map parameter of an operation to an XML element </li></ul></ul><ul><li>@WebResult </li></ul><ul><ul><li>Map return value of an operation to an XML element </li></ul></ul>© 2006 Jenna Pederson
  26. 26. JSR 181 Annotations <ul><li>@Oneway </li></ul><ul><ul><li>Denote an operation has only input </li></ul></ul><ul><li>@HandlerChain </li></ul><ul><ul><li>Map Web Service to an externally defined handler chain </li></ul></ul><ul><li>@SOAPBinding </li></ul><ul><ul><li>Bind Web Service to a SOAP message protocol </li></ul></ul><ul><li>@SOAPMessageHandlers </li></ul><ul><ul><li>Define SOAP handlers that run in response to a Web Service call </li></ul></ul>© 2006 Jenna Pederson
  27. 27. Web Service @WebService public class MyWebService { @WebMethod public void aWebServiceMethod() { // do something } } © 2006 Jenna Pederson
  28. 28. Apache Axis <ul><li>SOAP-based implementation of Web Services </li></ul><ul><li>WSDD file configures and exposes a Web Service </li></ul><ul><li>Why not use JSR 181 annotations? </li></ul><ul><ul><li>Need to implement JSR 181 processor in Axis </li></ul></ul>© 2006 Jenna Pederson
  29. 29. Key Features <ul><li>Uses JSR 181 annotations </li></ul><ul><li>Annotations read at runtime </li></ul><ul><li>No WSDD configuration file </li></ul><ul><li>Let’s see it in action! </li></ul>© 2006 Jenna Pederson
  30. 30. Analysis <ul><li>Must learn concept of annotations to be productive </li></ul><ul><li>XML configuration is hairy </li></ul><ul><li>JSR 181 reduces learning curve for writing Web Services </li></ul><ul><li>Limited set of Web Service annotations prevents tying to a specific framework like Axis </li></ul>© 2006 Jenna Pederson
  31. 31. Recap <ul><li>Java Annotations: </li></ul><ul><ul><li>Convenient for developers </li></ul></ul><ul><ul><li>Easier to understand context of metadata </li></ul></ul><ul><ul><li>Changes must be recompiled </li></ul></ul><ul><ul><li>Ties code to configuration </li></ul></ul><ul><li>Definite trend </li></ul><ul><li>Options </li></ul><ul><li>Should we use annotations? </li></ul><ul><ul><li>It depends! </li></ul></ul>© 2006 Jenna Pederson

×