© 2006 by BEA Systems Inc; made available under the EPL v1.0 | March 2006 | Java Annotation Processing (APT) in the Eclipse JDT Gary Horen BEA Systems.

Slides:



Advertisements
Similar presentations
AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems.
Advertisements

Copyright © IBM Corp., Introducing the new Web Tools JavaScript™ Features Phil Berkland IBM Software Group 9/26/2007.
2005 JavaOne SM Conference | Session BOF-9161 Exploring Annotation-Based Programming through the APT and Mirror APIs Tim Wagner, Senior Manager Gary Horen,
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
6/11/2015Page 1 Web Services-based Distributed System B. Ramamurthy.
Copyright BEA 2005, made available under EPL 1.0 | 1 Using Java Annotations in Eclipse Gary Horen BEA Systems Tim Wagner BEA Systems.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Eclipse Web Tools Platform.
20-Jun-15 Eclipse. Most slides from: 2 About IDEs An IDE is an Integrated Development Environment.
Eclipse Introduction Dwight Deugo Nesa Matic
IBM Software Group © 2005 IBM Corporation University of Nantes Eclipse dayMarch 2005 The Eclipse Modeling Framework and the IBM Model Transformation Framework.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
1 Lecture 22 George Koutsogiannakis Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Pittsburgh Java User Group– Dec Java PureFaces: A JSF Framework Extension.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
The NetBeans IDE CSIS 3701: Advanced Object Oriented Programming.
© 2005 by IBM; made available under the EPL v1.0 | March 1, 2005 Tim deBoer Gorkem Ercan Extend WTP Server Tools for your.
© 2006 IBM Corporation IBM WebSphere Portlet Factory Architecture.
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
Web servisu izstrāde JAX-WS. Ievads JAX-WS = Java API for XML Web Services JAX-WS ir fundamentāla Web servisu izstrādes tehnoloģija Java EE 5 un Java.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
© 2008 by BEA Systems Inc.; made available under the EPL v1.0 | 3/18/2008 | Java Annotations an introduction to Java Annotations Walter Harley BEA Systems.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Eclipse Eclipse An IDE is an Integrated Development Environment Different IDEs meet different needs – BlueJ, DrJava are designed as teaching.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Working with arrays (we will use an array of double as example)
Eclipse. An IDE is an Integrated Development Environment Different IDEs meet different needs BlueJ, DrJava are designed as teaching tools Emphasis is.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Software Documentation Section 5.5 ALBING’s Section JIA’s Appendix B JIA’s.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Eclipse 24-Apr-17.
Eclipse. An IDE is an Integrated Development Environment Different IDEs meet different needs BlueJ, DrJava are designed as teaching tools Emphasis is.
Session 13 Pinball Game Construction Kit (Version 3):
M1G Introduction to Programming 2 5. Completing the program.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
GOSS iCM Forms Gary Ratcliffe. 2 Agenda Webinar Programme Form Groups Publish Multiple Visual Script Editor Scripted Actions Form Examples.
March 19th 2007 TANGO collaboration 1 POGO-7 Pascal Verdier ESRF- Hamburg - 25 th of October 2010 A Short History of Pogo A Short History of Pogo New Technology.
© 2002 IBM Corporation Transaction Processing Facility TPF Users Group Acapulco May 2004 © 2004 IBM Corporation Pete Nicholls IBM Toronto Lab TPF Toolkit.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
© 2009 by IBM; made available under the EPL v1.0 | Feb 1, 2009 Seneca 2009 Tim deBoer Gorkem Ercan Extend WTP Server Tools.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.
Eclipse 27-Apr-17.
DEVELOPING WEB SERVICES WITH JAVA DESIGN WEB SERVICE ENDPOINT.
1 Budapest University of Technology and Economics Department of Measurement and Information Systems Budapest University of Technology and Economics Fault.
The Java API for XML-Based Web Services. A Web Service example in Java SOAP-awareServlet (e.g. Apache Axis2) SOAP-awareServlet Any class processing the.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Java IDE Dwight Deugo Nesa Matic
Chapter 5: Enhancing Classes
Web Services-JAX-RPC JAX-RPC enables a Web Service endpoint to be developed using either a Java Servlet or Enterprise JavaBeans (EJB) component model.
Testing and Debugging.
Sakai WebApp Structure
Eclipse 20-Sep-18.
CMPE 152: Compiler Design February 6 Class Meeting
Embracing Java 9 and beyond with Eclipse JDT
Developing and testing enterprise Java applications
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Enterprise Java Beans.
Computer Science 340 Software Design & Testing
CMPE212 – Reminders Assignment 2 due today, 7pm.
Presentation transcript:

© 2006 by BEA Systems Inc; made available under the EPL v1.0 | March 2006 | Java Annotation Processing (APT) in the Eclipse JDT Gary Horen BEA Systems Jess Garms BEA Systems Walter Harley BEA Systems

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 2 Agenda  Background  Demo of annotation editing tools  Mirror API and APT  Design Principles  Futures

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 3 History of metadata-driven programming model  First metadata lived in external files  e.g. deployment descriptor  Then in javadoc comments processed by XDoclet  Processed separately from compilation  Now in JSR 175 annotations  Formal part of language spec in JDK 1.5  Real Java types, processed during compilation  Compiler resolves references; does much of syntax analysis  Annotation developer worries about semantics

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 4 Who uses annotations?  J2SE – builtin annotations  J2EE – EJB 3.0, JSR 181, JSR 250, JAXB 2.0, JAX-WS 2.0  3 rd party frameworks: Hibernate, Beehive, – built into – JSR 181 web services – JSR 220 EJB Persistence

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 5 What do annotations do?  Usually, help a POJO run inside a framework.  EJB container  Web services stack  Annotation values can be stored in class file.  Annotation can cause new files to be generated at build time:  Arbitrary data files (e.g. XML)  Java types (may contain further annotations)  Framework uses values and generated files to support POJO

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 6 What annotations look like WebService { String endpointInterface(); String name(); … public class Foo { … } Declared by framework developer Used by application developer

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 7 Example: calling a method starts a transaction public class Stuff public void someMethod(String s)... } private void dispatchMethod(Method name) { Annotation a = name.getAnnotation(Transaction.class); if annotation on element says “start” this.startTransaction(); build parameter list name.invoke(parameters); } User annotates source code Container examines annotation values at run time with java.lang.reflect

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 8 Example: helping a POJO become a web public class Foo public Thing mumble() } Endpoint Interface XML-Java Bindings  Annotations on user code generate helper objects  Endpoint interface: receives and parses SOAP message  Bindings embody the message as parameters to the POJO method generates

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v1.0 9 Processing annotations at build time  Many annotation effects happen at build time, not run time  Verify that element values are valid  Verify arbitrary constraints on decorated declaration:  Does this method return the right type?  Does this class implement a required interface?  Generate files  The components we need to make this work:  Something to process a set of annotations – an annotation processor  Contributed by whoever defines the annotation(s)  A build time container to dispatch the available processors  Enhanced visual tools – Eclipse improves the editing experience

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Agenda  Background  Demo of annotation editing tools  Mirror API and APT  Design Principles  Futures

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Demo  Stuff that happens in both command-line build and Eclipse  Verifying element values  Generating (and deleting) new Java types  Stuff that only happens in Eclipse  Quick Fix  Auto Completion  Annotation Editing View

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Demo: processor finds invalid value

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Demo: annotation generates type

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Demo: quick fix for processor’s error message

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Demo: auto completion in annotation value

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Demo: annotation editing view

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Agenda  Background  Demo of annotation editing tools  Mirror API and APT  Design Principles  Futures

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v APIs in the Eclipse APT plugin  Sun’s Mirror API (com.sun.mirror.*)  Examining source code  Generating new files, java and non-java  Eclipse-specific APIs (org.eclipse.jdt.apt.*)  Quick Fix  Auto completion: prototype API  Visual annotation editor: prototype API

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Mirror API  Annotation processor runs in one of two ways:  Command line: Sun APT (batch processing)  In Eclipse: o.e.jdt.apt.core plug-in: (while user edits file)  Processor uses Mirror API to do its work  Binary compatible between APT and Eclipse  But, there are behavioral differences (as we will see)

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Mirror API Annotation Processor Environment Type system exploration File generation Error messages Declarations to class Bar {…}... b a c d

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Processor dispatch  APT sees annotation; dispatches corresponding processor  AnnotationProcessor.process()  Within process() call, AnnotationProcessorEnvironment available  environment.getSpecifiedTypeDeclarations()  returns list of files to be processed

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Processing rounds Round 1: Original source files Round 3: Types generated in round 2 Round 2: Types generated by processing original files in round 1

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Pitfall: APT round class class B Round class class class class C_Gen Round 2 All of round 1 arrives in first call to process() Then all of round 2 in second call Obtain types by calling AnnotationProcessorEnvironment.getSpecifiedTypeDeclarations()

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Pitfall: Eclipse round class class B Round class class class class C_Gen Round 2 Each file arrives in a separate call to process() Rounding in Eclipse must be file-at-a-time for performance reasons.

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Pitfall: referencing a generated type B_Gen class A { private B_Gen bg; } CAUTION From application code: process() { if annotatedDecl == A { Type t = env.getTypeDecl(B_Gen); t.foo(); // sometimes t will not exist! } } WRONG From processor: A B A_Gen A_Gen2

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Agenda  Background  Demo of annotation editing tools  Mirror API and APT  Design Principles  Futures

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Design Principles One year of user experience in the community: Three principles for good annotation processors  Be Fast  Watch Your References  Play Well With Others

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Design Principles: Be Fast Your processor may be called on every keystroke! So...  Don’t iterate over all files in the project  Don’t iterate over all discoverable types  Avoid APIs that might require additional compilation AnnotationProcessorEnvironment: getPackage(String name) – cached, but first call expensive getTypeDeclaration(String name) – likewise PackageDeclaration: just about every method is expensive, and there’s no cache  Execute long-running operations only on build, not on reconcile EclipseAnnotationProcessorEnvironment.getPhase()

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Design Principles: Be Fast – design annotations wisely  Use class literals rather than class names, in annotation values if your annotations look like // avoid! then your processor will need code like this: String typeName =... // read type name from annotation value AnnoProcEnv.getTypeDeclaration(typeName) // expensive instead, try for annotations like  In general, prefer strong types (enums, boolean,...) over strings

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Design Principles: Watch Your References  Avoid multiple source files contributing to a single output file. If you must – e.g., for a deployment descriptor – try to build it in a separate user-initiated operation, such as Publish.  Avoid the rounding pitfalls discussed earlier.  In user code, try not to reference generated types.  In the processor, don’t search for types generated from other files or types generated in later rounds.

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Design Principles: Watch Your References  Use caution when maintaining state within processors. Don’t make assumptions within the processor about how many times it will be called, or what files it will process on which call. Separate instances of processors may be executing on multiple threads simultaneously. Use caution with class-scoped variables.  Or else: run processor in “batch mode”.  No processing on reconcile; process all files on every build  Slow, and user experience is compromised (e.g. no error check while typing)

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Design Principles: Play Well With Others Your processor may be running alongside other processors and plug-ins.  Package as a plug-in, to take advantage of Eclipse-specific APIs e.g. DOM AST, quick-fix API,... (unless command-line apt compatibility is important)  Put processor and annotations in separate Java packages Annotations are public code; processors are private  Don’t claim “*” in supportedAnnotationTypes() Processors lower in factory path order will never get called

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v End-user Pitfall: no file generation during reconcile  New files are only generated during a build During reconcile, existing files can be modified but new files can’t be created.  Until all files are generated, user experience may be confusing E.g., generated types appear to be missing  Good reason to Watch Your References.  End Users: build new projects before starting to edit  Turn on project auto-build – build on every Save

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Agenda  Background  Demo of annotation editing tools  Mirror API and APT  Design Principles  Futures

Using Java Annotations in Eclipse | © 2006 by BEA Systems Inc; made available under the EPL v Present and Future  Future  Processor API for Java 1.6: JSR 269  Further Eclipse tooling  Code completion within string values  Reconcile-time type generation  Now  Comes built-in with Eclipse 3.2M5 and later  Beta available for Eclipse on update site  Try it out!

© 2006 by BEA Systems Inc; made available under the EPL v1.0 | March 2006 | Java Annotation Processing (APT) in the Eclipse JDT Gary Horen BEA Systems Jess Garms BEA Systems Walter Harley BEA Systems Try it out!