-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
SpoonModelBuilder.java
305 lines (272 loc) · 9.81 KB
/
SpoonModelBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2023 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon;
import spoon.compiler.SpoonResource;
import spoon.compiler.builder.JDTBuilder;
import spoon.processing.Processor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.Filter;
import spoon.support.compiler.jdt.CompilationUnitFilter;
import spoon.support.compiler.jdt.FactoryCompilerConfig;
import spoon.support.compiler.jdt.FileCompilerConfig;
import spoon.support.compiler.jdt.JDTBatchCompiler;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* Responsible for building a spoon model from Java source code.
*
* <p>
* The Spoon model (see {@link Factory} is built from input sources given as
* files. Use {@link #build()} to create the Spoon model.
* Once the model is built and stored in the factory, it
* can be processed by using a {@link #instantiateAndProcess(List)}.
* </p>
*
* <p>
* Create an instance of the default implementation of the Spoon compiler by
* using {@link spoon.Launcher#createCompiler()}. For example:
* </p>
*/
public interface SpoonModelBuilder {
/**
* Builds the program's model with this compiler's factory and stores the
* result into this factory. Note that this method should only be used once
* on a given factory.
*
* @return true if the Java was successfully compiled with the core Java
* compiler, false if some errors were encountered while compiling
* @throws spoon.SpoonException
* when a building problem occurs
* @see #getSourceClasspath()
* @see #getTemplateClasspath()
*/
boolean build();
/**
* Builds the program's model with this compiler's factory and stores the
* result into this factory. Note that this method should only be used once
* on a given factory.
*
* @param builder
* Parameters given at JDT compiler.
* @return true if the Java was successfully compiled with the core Java
* compiler, false if some errors were encountered while compiling
* @throws spoon.SpoonException
* when a building problem occurs
* @see #getSourceClasspath()
* @see #getTemplateClasspath()
*/
boolean build(JDTBuilder builder);
/** The types of compilable elements
* FILES - compiles the java files from the file system, which were registered by {@link #addInputSource(File)} and {@link #addTemplateSource(File)}
* CTTYPES - compiles virtual java files, which are dynamically generated from the all top level classes of the CtModel by {@link spoon.reflect.visitor.DefaultJavaPrettyPrinter}
*/
interface InputType {
InputType FILES = FileCompilerConfig.INSTANCE;
InputType CTTYPES = FactoryCompilerConfig.INSTANCE;
/**
* responsible for setting the parameters of JDTBatchCompiler, must call setCompilationUnits()
*/
void initializeCompiler(JDTBatchCompiler compiler);
}
/**
* Generates the bytecode associated to the classes stored in this
* compiler's factory. The bytecode is generated in the directory given by
* {@link #getBinaryOutputDirectory()}.
*
* The array of types must be of size 0 or 1. If it's empty,
* the types of the factory are compiled.
* If it's InputType.FILES, the files given as input are compiled.
*
*Note that the varargs ... enables this version to be backward compatible for callers.
*
* @see #getSourceClasspath()
*/
boolean compile(InputType... types);
/**
* Takes a list of fully qualified name processors and instantiates them to process
* the Java model.
*/
void instantiateAndProcess(List<String> processors);
/**
* Processes the Java model with the given processors.
*/
void process(Collection<Processor<? extends CtElement>> processors);
/**
* Generates the source code associated to the classes stored in this
* compiler's factory. The source code is generated in the directory given
* by {@link #getSourceOutputDirectory()}.
*
* @param outputType
* the output method
*/
void generateProcessedSourceFiles(OutputType outputType);
/**
* Generates the source code associated to the classes stored in this
* compiler's factory. The source code is generated in the directory given
* by {@link #getSourceOutputDirectory()}.
*
* @param outputType
* the output method
* @param typeFilter
* Filter on CtType to know which type Spoon must print.
*/
void generateProcessedSourceFiles(OutputType outputType, Filter<CtType<?>> typeFilter);
/**
* Adds a file/directory to be built. By default, the files could be Java
* source files or Jar files. Directories are processed recursively.
*
* @param source
* file or directory to add
*/
void addInputSource(File source);
/**
* Adds a file/directory (as a {@link SpoonResource}) to be built. By default, the
* files could be Java source files or Jar files. Directories are processed
* recursively.
*
* @param source
* file or directory to add
*/
void addInputSource(SpoonResource source);
/**
* Adds a list of files/directories (as a {@link SpoonResource} to be built.
* By default, the files could be Java source files of Java files. Directories
* are processed recursively.
*
* @param resources
* files or directories to add.
*/
void addInputSources(List<SpoonResource> resources);
/**
* Gets all the files/directories given as input sources to this builder
* (see {@link #addInputSource(File)}).
*/
Set<File> getInputSources();
/**
* Adds a file/directory to be used to build templates. By default, the
* files should be Java source files or Jar files containing the sources.
* Directories are processed recursively. Templates are set apart from the
* program to be processed for logical reasons. However, if a template was
* needed to be processed, it could be added as an input source.
*
* @param source
* file or directory to add
*/
void addTemplateSource(File source);
/**
* Adds a file/directory (as a {@link SpoonResource}) to be used to build templates. By
* default, the files should be Java source files or Jar files containing
* the sources. Directories are processed recursively. Templates are set
* apart from the program to be processed for logical reasons. However, if a
* template was needed to be processed, it could be added as an input
* source.
*
* @param source
* file or directory to add
*/
void addTemplateSource(SpoonResource source);
/**
* Adds a list of files/directories (as a CtResource) to be used to build templates. By
* default, the files should be Java source files or Jar files containing
* the sources. Directories are processed recursively. Templates are set
* apart from the program to be processed for logical reasons. However, if a
* template was needed to be processed, it could be added as an input
* source.
*
* @param resources
* files or directories to add.
*/
void addTemplateSources(List<SpoonResource> resources);
/**
* Gets all the files/directories given as template sources to this builder
* (see {@link #addTemplateSource(File)}).
*/
Set<File> getTemplateSources();
/**
* Gets the output directory of this compiler.
*/
File getSourceOutputDirectory();
/**
* Sets the output directory for binary generated.
*
* @param binaryOutputDirectory
* {@link File} for binary output directory.
*/
void setBinaryOutputDirectory(File binaryOutputDirectory);
/**
* Gets the binary output directory of the compiler.
*/
File getBinaryOutputDirectory();
/**
* Gets the classpath that is used to build/compile the input sources.
*/
String[] getSourceClasspath();
/**
* Sets the classpath that is used to build/compile the input sources.
*
* Each element of the array is either a jar file or a folder containing bytecode files.
*/
void setSourceClasspath(String... classpath);
/**
* Gets the module path used for sourcing the input modules.
* The returned list is immutable and does not contain null values.
*
* @return A list of strings representing the module path. Each string element
* is the path to a directory or a module jar file.
*/
List<String> getSourceModulePath();
/**
* Sets the module path that is used to build/compile the input sources.
* This is the equivalent to the {@code --module-path} option of {@code javac} and {@code java} executables.
*
* @param sourceModulePath The new module path to be set. Each string element
* should be the path to a directory or a module jar file.
* @throws NullPointerException if the argument is null or an element of the list is null.
*/
void setSourceModulePath(List<String> sourceModulePath);
/**
* Gets the classpath that is used to build the template sources.
*
* See {@link #setSourceClasspath} for the meaning of the returned string.
*/
String[] getTemplateClasspath();
/**
* Sets the classpath that is used to build the template sources.
*/
void setTemplateClasspath(String... classpath);
/**
* Returns the working factory
*/
Factory getFactory();
/**
* Adds {@code filter}.
*
* @param filter
* The {@link CompilationUnitFilter} to add.
*/
void addCompilationUnitFilter(CompilationUnitFilter filter);
/**
* Removes {@code filter}. Does nothing, if {@code filter} has not been
* added beforehand.
*
* @param filter
* The {@link CompilationUnitFilter} to remove.
*/
void removeCompilationUnitFilter(CompilationUnitFilter filter);
/**
* Returns a copy of the internal list of {@link CompilationUnitFilter}s.
*
* @return
* A copy of the internal list of {@link CompilationUnitFilter}s.
*/
List<CompilationUnitFilter> getCompilationUnitFilter();
}