-
Notifications
You must be signed in to change notification settings - Fork 587
/
EclipseCollectionsCodeGenerator.java
234 lines (206 loc) · 8.79 KB
/
EclipseCollectionsCodeGenerator.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
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.codegenerator;
import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.zip.CRC32;
import org.eclipse.collections.codegenerator.model.Primitive;
import org.eclipse.collections.codegenerator.tools.FileUtils;
import org.eclipse.collections.codegenerator.tools.IntegerOrStringRenderer;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STErrorListener;
import org.stringtemplate.v4.STGroupFile;
public class EclipseCollectionsCodeGenerator
{
private final String templateDirectory;
private final List<URL> classPathURLs;
private final ErrorListener errorListener;
private final File outputDirectory;
private final String fileExtension;
private int numFileWritten = 0;
public EclipseCollectionsCodeGenerator(
String templateDirectory,
List<URL> classPathURLs,
ErrorListener errorListener,
File outputDirectory,
String fileExtension)
{
this.templateDirectory = Objects.requireNonNull(templateDirectory);
this.classPathURLs = Objects.requireNonNull(classPathURLs);
this.errorListener = Objects.requireNonNull(errorListener);
this.outputDirectory = Objects.requireNonNull(outputDirectory);
this.fileExtension = Objects.requireNonNull(fileExtension);
}
/**
* Generates code and only write contents to disk which differ from the current file contents.
*
* @return The number of files written.
*/
public int generateFiles()
{
List<URL> allTemplateFilesFromClassPath =
FileUtils.getAllTemplateFilesFromClasspath(this.templateDirectory, this.classPathURLs);
for (URL url : allTemplateFilesFromClassPath)
{
STGroupFile templateFile = new STGroupFile(url, "UTF-8", '<', '>');
if (!templateFile.isDefined("fileName"))
{
continue;
}
STErrorListener stErrorListener = new LoggingErrorListener(this.errorListener, url);
templateFile.setListener(stErrorListener);
templateFile.registerRenderer(String.class, new IntegerOrStringRenderer());
File targetPath = this.constructTargetPath(templateFile);
FileUtils.createDirectory(targetPath);
boolean hasTwoPrimitives = this.renderBooleanTemplate(templateFile, "hasTwoPrimitives");
boolean skipBoolean = this.renderBooleanTemplate(templateFile, "skipBoolean");
boolean skipBooleanKeys = this.renderBooleanTemplate(templateFile, "skipBooleanKeys");
boolean forSpecializedStream = this.renderBooleanTemplate(templateFile, "forSpecializedStream");
if (hasTwoPrimitives)
{
for (Primitive primitive1 : Primitive.values())
{
if (primitive1 == Primitive.BOOLEAN && (skipBoolean || skipBooleanKeys))
{
continue;
}
for (Primitive primitive2 : Primitive.values())
{
if (primitive2 == Primitive.BOOLEAN && skipBoolean)
{
continue;
}
String sourceFileName = this.executeTemplate(templateFile, "fileName", primitive1, primitive2);
File outputFile = new File(targetPath, sourceFileName + this.fileExtension);
if (!EclipseCollectionsCodeGenerator.sourceFileExists(outputFile))
{
String classContents = this.executeTemplate(templateFile, "class", primitive1, primitive2);
this.checkSumClassContentsAndWrite(classContents, outputFile);
}
}
}
}
else
{
for (Primitive primitive : Primitive.values())
{
if (primitive == Primitive.BOOLEAN && skipBoolean)
{
continue;
}
if (forSpecializedStream && !primitive.hasSpecializedStream())
{
continue;
}
String sourceFileName = this.executeTemplate(templateFile, "fileName", primitive);
File outputFile = new File(targetPath, sourceFileName + this.fileExtension);
if (!EclipseCollectionsCodeGenerator.sourceFileExists(outputFile))
{
String classContents = this.executeTemplate(templateFile, "class", primitive);
this.checkSumClassContentsAndWrite(classContents, outputFile);
}
}
}
}
return this.numFileWritten;
}
private void checkSumClassContentsAndWrite(String classContents, File outputFile)
{
long checksumValue = EclipseCollectionsCodeGenerator.calculateChecksum(classContents);
Path outputChecksumPath = Paths.get(outputFile.getAbsolutePath() + ".crc");
if (!outputChecksumPath.toFile().exists())
{
this.writeFileAndChecksum(outputFile, classContents, checksumValue, outputChecksumPath, false);
return;
}
String existingChecksum = FileUtils.readFile(outputChecksumPath);
if (existingChecksum.equals(String.valueOf(checksumValue)))
{
return;
}
this.writeFileAndChecksum(outputFile, classContents, checksumValue, outputChecksumPath, true);
}
private static long calculateChecksum(String string)
{
CRC32 checksum = new CRC32();
checksum.update(string.getBytes(StandardCharsets.UTF_8));
return checksum.getValue();
}
private void writeFileAndChecksum(
File outputFile,
String output,
long checksumValue,
Path outputChecksumPath,
boolean outputFileMustExist)
{
this.numFileWritten++;
FileUtils.writeToFile(output, outputFile, outputFileMustExist);
FileUtils.writeToFile(String.valueOf(checksumValue), outputChecksumPath.toFile(), outputFileMustExist);
}
private String executeTemplate(
STGroupFile templateFile,
String templateName,
Primitive primitive)
{
ST template = this.findTemplate(templateFile, templateName);
template.add("primitive", primitive);
return template.render();
}
private String executeTemplate(
STGroupFile templateFile,
String templateName,
Primitive primitive1,
Primitive primitive2)
{
ST template = this.findTemplate(templateFile, templateName);
template.add("primitive1", primitive1);
template.add("primitive2", primitive2);
template.add("sameTwoPrimitives", primitive1 == primitive2);
return template.render();
}
private ST findTemplate(STGroupFile templateFile, String templateName)
{
ST template = templateFile.getInstanceOf(templateName);
if (template == null)
{
throw new RuntimeException("Could not find template " + templateName + " in " + templateFile.getFileName());
}
return template;
}
private boolean renderBooleanTemplate(STGroupFile templateFile, String templateName)
{
if (!templateFile.isDefined(templateName))
{
return false;
}
ST template = templateFile.getInstanceOf(templateName);
String render = template.render();
return Boolean.valueOf(render);
}
private File constructTargetPath(STGroupFile templateFile)
{
ST targetPath = this.findTemplate(templateFile, "targetPath");
String renderedTargetPath = targetPath.render();
return new File(this.outputDirectory, renderedTargetPath);
}
private static boolean sourceFileExists(File outputFile)
{
File newPath = new File(outputFile.getAbsolutePath()
.replace("target", "src")
.replace("generated-sources", "main")
.replace("generated-test-sources", "test"));
return newPath.exists();
}
}