Debian Codegraph @

org.eclipse.jdt.apt.core/design-notes.html

raw
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
6 <title>Eclipse APT Design Notes</title>
7 </head>
8 <body lang="EN-US">
9 <h1>Overview</h1>
10 <p>Annotation processors may implement either the Java 5 com.sun.mirror.apt interfaces or the
11 Java 6 javax.annotation.processing interfaces. The two interfaces are functionally similar but
12 the details are different. Note that although the Java 5 apt tool has been deprecated by Sun,
13 it is still included in the Java 6 JDK and developers continue to write processors against it.
14 However, the com.sun.mirror.apt interface is considered proprietary and there are no Java
15 compliance tests which validate it.
16 </p>
17
18 <h1>Build Process</h1>
19 <p>The JDT system performs three kinds of build: full build, incremental build, and reconcile. When
20 a single file is edited and saved (if autobuild is enabled) or manually built, an incremental
21 build is performed: only the affected files will be rebuilt. This will include the edited file
22 as well as any other files with relevant dependencies on the edited file. After a clean, or
23 when incremental build detects an overwhelming number of files needing to be rebuilt, a full
24 build will be performed; in this case every file in the project will be rebuilt. During an
25 editing session, prior to saving, a file will frequently be reconciled, in order to identify
26 errors while typing. Reconcile operates on only a single file at a time.</p>
27 <p>
28 Java 5 and Java 6 processors are handled by different code and interact with the Eclipse Java
29 compiler in different ways and at different points in the build cycle. This is partly for
30 historical reasons and partly in order to support Java 6 processors in the context of the
31 command-line Eclipse compiler (ecj.jar). The Java 5 annotation processing code is built against
32 the JDT's public API; the Java 6 code uses internal interfaces within the compiler, which was
33 necessary because the public APIs use platform classes not available in the command-line package.
34 A pleasant consequence is that the Java 6 annotation processing code is somewhat easier
35 to understand.</p>
36 <p>
37 The main challenge of introducing APT into Eclipse is that annotation processors can contribute
38 additional Java types, which must in turn be compiled; this compilation can affect previously
39 compiled types, because a formerly unresolved reference can be resolved. Because of this, it
40 is necessary for annotation processing to be implemented within the compile process, rather than
41 as a later build step.</p>
42 <p>
43 TODO: overview of build process, i.e., when Java 5 processors are called relative to Java 6 processors
44 </p>
45 <h2>Java 5 Build-time Processing</h2>
46 <p>
47 The invocation path for Java 5 processors is quite complex, in part because when Java 5 processing
48 was implemented it was not possible to integrate it closely with the compiler, and in part for
49 silly historical reasons that serve no current purpose. It is ripe for refactoring.</p>
50 <p>
51 A typical call stack when a Java 5 processor is invoked looks like this:</p>
52 <pre>
53 SomeProcessor.process() line: xxx
54 APTDispatchRunnable.dispatchToFileBasedProcessor(AbstractCompilationEnv, boolean, boolean) line: 628
55 APTDispatchRunnable.runAPTInFileBasedMode(BuildEnv) line: 317
56 APTDispatchRunnable.build(BuildEnv) line: 655
57 APTDispatchRunnable.access$1(APTDispatchRunnable, BuildEnv) line: 647
58 APTDispatchRunnable$1.run(AbstractCompilationEnv) line: 265
59 BuildEnv$CallbackRequestor.acceptBinding(String, IBinding) line: 611
60 CompilationUnitResolver.resolve(ICompilationUnit[], String[], ASTRequestor, int, Map, WorkingCopyOwner, int) line: 766
61 CompilationUnitResolver.resolve(ICompilationUnit[], String[], ASTRequestor, int, Map, IJavaProject, WorkingCopyOwner, int, IProgressMonitor) line: 478
62 ASTParser.createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor) line: 737
63 BaseProcessorEnv.createASTs(IJavaProject, ICompilationUnit[], ASTRequestor) line: 856
64 BuildEnv.createASTs(BuildContext[]) line: 356
65 AbstractCompilationEnv.newBuildEnv(BuildContext[], BuildContext[], IJavaProject, AbstractCompilationEnv$EnvCallback) line: 111
66 APTDispatchRunnable.build() line: 271
67 APTDispatchRunnable.run(IProgressMonitor) line: 217
68 Workspace.run(IWorkspaceRunnable, ISchedulingRule, int, IProgressMonitor) line: 1800
69 APTDispatchRunnable.runAPTDuringBuild(BuildContext[], BuildContext[], Map&lt;IFile,CategorizedProblem[]&gt;, AptProject, Map&lt;AnnotationProcessorFactory,Attributes&gt;, Set&lt;AnnotationProcessorFactory&gt;, boolean) line: 142
70 AptCompilationParticipant.processAnnotations(BuildContext[]) line: 193
71 IncrementalImageBuilder(AbstractImageBuilder).processAnnotations(CompilationParticipantResult[]) line: 627
72 IncrementalImageBuilder(AbstractImageBuilder).compile(SourceFile[]) line: 338
73 IncrementalImageBuilder.build(SimpleLookupTable) line: 134
74 JavaBuilder.buildDeltas(SimpleLookupTable) line: 265
75 JavaBuilder.build(int, Map, IProgressMonitor) line: 193
76 [higher calls omitted for brevity]
77 </pre>
78 <p>
79 Java 5 processors are invoked via the CompilationParticipant interface. The Java compiler calls
80 AptCompilationParticipant.processAnnotations(), passing in a list of all the files being built.
81 Note that the compiler may break large projects into multiple groups of files, resulting in
82 multiple calls to processAnnotations(). Files are marked with whether or not they contain annotations.</p>
83 <p>
84 Within processAnnotations(), annotation processor factories are discovered from the processor
85 factory path (and cached for subsequent invocations); factories and files are then passed in to
86 APTDispatchRunnable.runAPTDuringBuild(). This in turn stores the information in a new instance of
87 APTDispatchRunnable, and invokes its run() method within a workspace.run() operation.</p>
88 <p>
89 The run() method invokes BuildEnv.newBuildEnv(), passing in the AptDispatchRunnable as a callback.
90 The newBuildEnv() method creates a BuildEnv, gets compilation units for each of the files being
91 compiled, and then requests ASTs for each of the files. The parser is passed an ASTRequestor
92 callback that is implemented by an inner class of the BuildEnv; in this way, when the ASTs are
93 ready for consumption, APTDispatchRunnable.build(BuildEnv) is ultimately called, within the
94 ASTRequestor callback.</p>
95 <p>
96 Finally, within build(), processors are selected on the basis of the
97 annotations they support, and each processor's process() method is called on each appropriate file,
98 configuring the BuildEnv before each process() invocation so that it will provide the correct data.
99 The details of this vary depending on whether the processor is normal (file-based) or is marked
100 as requiring batch mode.</p>
101 <h3>Batch-mode Processors</h3>
102 <p>Processors can be marked in the Advanced Factory Path Options dialog as requiring batch mode.
103 If any of the processors on the project's factory path require batch mode,
104 AptDispatchRunnable.build(BuildEnv) calls runAPTInMixedMode(); if none of the processors
105 requires batch mode, runAPTInFileBasedMode() is called instead. The chief difference is that
106 batch-mode processors are only run during a full build, and that the batch-mode implementation of
107 AnnotationProcessorEnvironment.getTypeDeclarations() returns all the compilation units in the
108 build, whereas the normal implementation returns only a single compilation unit at a time.</p>
109 <p>
110 Additionally, a separate classloader is used to load batch-mode processors, and it is
111 discarded after each build; thus the classes are reloaded for each build, and static variables
112 are reinitialized. This is important because some processors written with command-line
113 (non-incremental) compilation in mind store dynamic state in static variables and thus cannot
114 be invoked multiple times.
115 </p>
116 <h3>File-mode Processors</h3>
117 <p>Most processors are designed (or at least should be designed) to support incremental compilation,
118 meaning that within the AnnotationProcessor.process() invocation, if the processor calls
119 AnnotationProcessorEnvironment.getTypeDeclarations(), it will only retrieve a single type at a
120 time; the process() method will be called repeatedly during the course of a build so that all
121 files are eventually processed.
122 </p>
123 <h2>Java 6 Build-time Processing</h2>
124 <p>
125 The invocation process for Java 6 processors is more straightforward. A typical call stack looks
126 like this:</p>
127 <pre>
128 ModelTesterProc.process(Set&lt;TypeElement&gt;, RoundEnvironment) line: 107
129 RoundDispatcher.handleProcessor(ProcessorInfo) line: 139
130 RoundDispatcher.round() line: 121
131 IdeAnnotationProcessorManager(BaseAnnotationProcessorManager).processAnnotations(CompilationUnitDeclaration[], ReferenceBinding[], boolean) line: 159
132 IdeAnnotationProcessorManager.processAnnotations(CompilationUnitDeclaration[], ReferenceBinding[], boolean) line: 134
133 Compiler.processAnnotations() line: 810
134 Compiler.compile(ICompilationUnit[]) line: 428
135 IncrementalImageBuilder(AbstractImageBuilder).compile(SourceFile[], SourceFile[], boolean) line: 364
136 IncrementalImageBuilder.compile(SourceFile[], SourceFile[], boolean) line: 321
137 IncrementalImageBuilder(AbstractImageBuilder).compile(SourceFile[]) line: 301
138 IncrementalImageBuilder.build(SimpleLookupTable) line: 134
139 JavaBuilder.buildDeltas(SimpleLookupTable) line: 265
140 JavaBuilder.build(int, Map, IProgressMonitor) line: 193
141 [higher calls omitted for brevity]
142 </pre>
143 <p>
144 Note that the processing is performed by an IdeAnnotationProcessorManager; this is distinguished
145 from the BatchAnnotationProcessorManager, which would be used if processing was invoked from a
146 command line (ecj.jar) compilation. Command-line compilation with ecj.jar uses the same typesystem
147 implementation as IDE compilation, but somewhat different Messager and Filer implementations.
148 </p>
149 <h2>Reconcile-time Processing</h2>
150 <p>
151 Reconcile-time processing is only supported for Java 5 processors. This is chiefly because, after
152 developing the Java 5 processing implementation, it became clear that writing highly-performant
153 annotation processors requires a level of compiler experience that most programmers do not have,
154 and if less-performant processors are inserted into the reconcile step, reconcile can become
155 painfully and confusingly slow. Thus when the Java 6 processing implementation was added it was
156 decided to not support reconcile-time processing. Eclipse's effectiveness as an IDE depends on
157 reconcile being an extremely fast operation. In hindsight, edit-time processing should have been
158 limited to reporting errors, and should have been implemented as a background validation task
159 rather than within the reconcile step.</p>
160 <p>
161 TODO: describe practical differences between reconcile and build.</p>
162 <p>
163 The stack trace of a typical reconcile-time invocation looks like this:</p>
164 <pre>
165 SomeProcessor.process() line: xxx
166 APTDispatchRunnable.dispatchToFileBasedProcessor(AbstractCompilationEnv, boolean, boolean) line: 628
167 APTDispatchRunnable.access$0(APTDispatchRunnable, AbstractCompilationEnv, boolean, boolean) line: 598
168 APTDispatchRunnable$ReconcileEnvCallback.run(AbstractCompilationEnv) line: 77
169 ReconcileEnv$CallbackRequestor.acceptBinding(String, IBinding) line: 135
170 CompilationUnitResolver.resolve(ICompilationUnit[], String[], ASTRequestor, int, Map, WorkingCopyOwner, int) line: 766
171 CompilationUnitResolver.resolve(ICompilationUnit[], String[], ASTRequestor, int, Map, IJavaProject, WorkingCopyOwner, int, IProgressMonitor) line: 478
172 ASTParser.createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor) line: 737
173 BaseProcessorEnv.createASTs(IJavaProject, ICompilationUnit[], ASTRequestor) line: 856
174 ReconcileEnv.openPipeline() line: 108
175 AbstractCompilationEnv.newReconcileEnv(ReconcileContext, AbstractCompilationEnv$EnvCallback) line: 97
176 APTDispatchRunnable.reconcile(ReconcileContext, IJavaProject) line: 211
177 APTDispatchRunnable.runAPTDuringReconcile(ReconcileContext, AptProject, Map&lt;AnnotationProcessorFactory,Attributes&gt;) line: 159
178 AptCompilationParticipant.reconcile(ReconcileContext) line: 223
179 ReconcileWorkingCopyOperation$1.run() line: 257
180 SafeRunner.run(ISafeRunnable) line: 42
181 ReconcileWorkingCopyOperation.notifyParticipants(CompilationUnit) line: 244
182 ReconcileWorkingCopyOperation.executeOperation() line: 94
183 ReconcileWorkingCopyOperation(JavaModelOperation).run(IProgressMonitor) line: 728
184 ReconcileWorkingCopyOperation(JavaModelOperation).runOperation(IProgressMonitor) line: 788
185 CompilationUnit.reconcile(int, int, WorkingCopyOwner, IProgressMonitor) line: 1242
186 JavaReconcilingStrategy.reconcile(ICompilationUnit, boolean) line: 126
187 [higher calls omitted for brevity]
188 </pre>
189
190 <h1>Processor Callbacks</h1>
191 <p>When annotation processors are invoked, they can in turn call back into the compiler through various
192 interfaces in order to generate new files and report errors. The interfaces are naturally not the same as
193 those used within the JDT compiler, so wrappers and proxies are employed to glue the different systems
194 together. The most significant difference in systems is that, in both the Java 5 and Java 6 processor
195 interfaces, the typesystem is separated into Type and Element hierarchies, in which a Type object
196 represents a particular reification of a type while an Element represents a declaration in code. The
197 distinction is easiest to understand in the context of generics: for example, the following code sample
198 contains declarations of classes Lister and Test, which contain declarations of method get() and fields
199 'strings' and 'numbers' respectively. The type of the return of get() is List&lt;T&gt;, the type of strings is
200 Lister&lt;String&gt;, and the type of numbers is Lister&lt;Number&gt;. Note that the single declaration of Lister&lt;T&gt;
201 was able to generate multiple Types.
202 </p>
203 <pre>
204 import java.util.List;
205 class Lister&lt;T&gt; {
206 List&lt;T&gt; get() { ... }
207 }
208 class Test {
209 Lister&lt;String&gt; strings;
210 Lister&lt;Number&gt; numbers;
211 }
212 </pre>
213
214 <h2>Java 5 Type System</h2>
215 <p>TODO</p>
216
217 <h2>Java 5 Filer and Messager</h2>
218 <p>TODO</p>
219
220 <h2>Java 6 Type System</h2>
221 <p>TODO</p>
222
223 <h2>Java 6 Filer and Messager</h2>
224 <p>TODO</p>
225
226 </body>
227 </html>