1
1
package com .vertispan .j2cl .build ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .File ;
5
- import java .io .FileReader ;
3
+ import com .google .common .collect .Streams ;
4
+
6
5
import java .io .IOException ;
7
6
import java .nio .file .Files ;
8
7
import java .nio .file .Path ;
9
- import java .nio .file .Paths ;
10
- import java .nio .file .attribute .FileTime ;
11
8
import java .util .ArrayList ;
12
9
import java .util .Collection ;
13
10
import java .util .HashMap ;
14
11
import java .util .HashSet ;
15
- import java .util .Iterator ;
16
12
import java .util .List ;
17
13
import java .util .Map ;
18
14
import java .util .Set ;
19
15
import java .util .stream .Collectors ;
20
- import java .util .stream .Stream ;
21
-
22
- import com .google .common .base .Splitter ;
23
- import com .google .common .collect .Streams ;
24
16
25
17
public class BuildMap {
26
- private Project project ;
18
+ private Project project ;
27
19
28
20
29
- private Map <String , TypeInfo > typeInfos = new HashMap <>();
21
+ private Map <String , TypeInfo > typeInfos = new HashMap <>();
30
22
31
- private Map <String , String > pathToQualifiedSourceName = new HashMap <>();
32
- private Map <String , String > qualifiedSourceNameToPath = new HashMap <>();
23
+ private Map <String , String > pathToQualifiedSourceName = new HashMap <>();
24
+ private Map <String , String > qualifiedSourceNameToPath = new HashMap <>();
33
25
34
- private List <String > childrenChangedFiles = new ArrayList <>();;
26
+ private List <String > childrenChangedFiles = new ArrayList <>();
27
+ ;
35
28
36
- private Set <String > changedFiles = new HashSet <>();;
29
+ private Set <String > changedFiles = new HashSet <>();
30
+ ;
37
31
38
- private Set <String > expandedFiles = new HashSet <>();
32
+ private Set <String > expandedFiles = new HashSet <>();
39
33
40
- private List <String > filesToDelete = new ArrayList <>();
34
+ private List <String > filesToDelete = new ArrayList <>();
41
35
42
36
private Map <String , ProjectFiles > dirToprojectFiles ;
43
37
@@ -63,7 +57,6 @@ public Map<String, ProjectFiles> getDirToprojectFiles() {
63
57
}
64
58
65
59
/**
66
- *
67
60
* @param dir
68
61
*/
69
62
public void build (Path dir ) {
@@ -72,7 +65,7 @@ public void build(Path dir) {
72
65
}
73
66
74
67
private void populateFilesToDelete () {
75
- // Merge all except added - which by it's nature has nothing nothing needed deleting
68
+ // Merge all except added - which by it's nature has nothing needed deleting
76
69
for (ProjectFiles p : dirToprojectFiles .values ()) {
77
70
filesToDelete .addAll (p .getRemoved ());
78
71
filesToDelete .addAll (p .getUpdated ());
@@ -86,7 +79,7 @@ private void buildAndProcessChangedFiles(Path dir) {
86
79
expandChangedFiles ();
87
80
88
81
// Populate the complete list of potentially changed files
89
- for (ProjectFiles projectFiles : dirToprojectFiles .values ()) {
82
+ for (ProjectFiles projectFiles : dirToprojectFiles .values ()) {
90
83
changedFiles .addAll (projectFiles .getUpdated ());
91
84
changedFiles .addAll (projectFiles .getAdded ());
92
85
}
@@ -98,42 +91,45 @@ public List<String> getFilesToDelete() {
98
91
}
99
92
100
93
public void expandChangedFiles () {
101
- for (ProjectFiles projectFiles : dirToprojectFiles .values ()) {
94
+ for (ProjectFiles projectFiles : dirToprojectFiles .values ()) {
102
95
expandChangedFiles (projectFiles .getUpdated (), expandedFiles );
103
96
}
104
97
expandChangedFiles (childrenChangedFiles , expandedFiles );
105
98
}
99
+
106
100
public void expandChangedFiles (Collection <String > files , Set <String > expanded ) {
107
101
for (String file : files ) {
108
102
if (!file .endsWith (".java" )) {
109
103
continue ;
110
104
}
111
- String typeName = pathToQualifiedSourceName .get (file );
105
+ String typeName = pathToQualifiedSourceName .get (file );
112
106
TypeInfo typeInfo = typeInfos .get (typeName );
113
107
expandChangedFiles (typeInfo , expanded );
114
108
}
115
109
}
116
110
117
111
private void expandChangedFiles (TypeInfo typeInfo , Set <String > changedFiles ) {
118
112
// Anything that extends this is added to the set, and it also recurses through the extends
119
- for (TypeDependency dep : typeInfo .getSuperIn () ) {
113
+ for (TypeDependency dep : typeInfo .getSuperIn ()) {
120
114
maybeAddNativeFile (dep .outgoing );
121
115
changedFiles .add (qualifiedSourceNameToPath .get (dep .outgoing .getQualifiedSourceName ()));
122
116
expandChangedFiles (dep .outgoing , changedFiles );
123
117
}
124
118
125
119
// Anything that implements (or extends) this interface, is added to the set.
126
120
// TODO Does this need to be done for transitive interface impl? (mdp)
127
- for (TypeDependency dep : typeInfo .getInterfacesIn () ) {
121
+ for (TypeDependency dep : typeInfo .getInterfacesIn ()) {
128
122
maybeAddNativeFile (dep .outgoing );
129
123
changedFiles .add (qualifiedSourceNameToPath .get (dep .outgoing .getQualifiedSourceName ()));
130
-
131
124
// Recurse the ancestors, as the interface may have default methods
132
125
// that changes the call hieararchy of the implementor.
133
126
expandChangedFiles (dep .outgoing , changedFiles );
134
127
}
135
128
129
+
136
130
// Now add all the dependencies
131
+
132
+
137
133
for (TypeDependency dep : typeInfo .getMethodFieldIn ()) {
138
134
maybeAddNativeFile (dep .outgoing );
139
135
changedFiles .add (qualifiedSourceNameToPath .get (dep .outgoing .getQualifiedSourceName ()));
@@ -189,12 +185,11 @@ private void createBuildMaps(Map<String, TypeInfoDescr> typeInfoDescrs) {
189
185
incoming .getInterfacesIn ().add (d );
190
186
}
191
187
}
192
-
193
188
// Add dependencies, that are not one of the above.
194
189
for (String type : typeInfoDescr .dependencies ) {
195
190
TypeInfo incoming = getType (type );
196
191
if (incoming != null ) {
197
- TypeDependency d = new TypeDependency (incoming ,outgoing );
192
+ TypeDependency d = new TypeDependency (incoming , outgoing );
198
193
outgoing .getMethodFieldOut ().add (d );
199
194
incoming .getMethodFieldIn ().add (d );
200
195
}
@@ -208,7 +203,7 @@ private Map<String, TypeInfoDescr> readBuildMapDescrForAllFiles(Path dir, Map<St
208
203
// need to handle references.
209
204
// Must also put in stuff that was just deleted, so we can handle the actual deletion.
210
205
for (String javaFileName : Streams .concat (projectFiles .getAll ().stream (),
211
- projectFiles .getRemoved ().stream ()).collect (Collectors .toList ())) {
206
+ projectFiles .getRemoved ().stream ()).collect (Collectors .toList ())) {
212
207
if (projectFiles .getAdded ().contains (javaFileName )) {
213
208
// ignore just added files, they won't have a build.map yet
214
209
continue ;
@@ -221,10 +216,13 @@ private Map<String, TypeInfoDescr> readBuildMapDescrForAllFiles(Path dir, Map<St
221
216
}
222
217
223
218
private void readBuildMapDescrForFileName (String javaFileName , Map <String , TypeInfoDescr > typeInfoDescrs , Path dir ) {
219
+
220
+
224
221
if (javaFileName .endsWith (".java" )) {
225
222
String fileName = javaFileName .substring (0 , javaFileName .lastIndexOf (".java" ));
226
223
String buildMapFileName = fileName + ".build.map" ;
227
- Path buildMapPath = dir .resolve ("results" ).resolve (buildMapFileName );
224
+ Path buildMapPath = dir .resolve (buildMapFileName );
225
+
228
226
if (Files .notExists (buildMapPath )) {
229
227
throw new RuntimeException ("build.map files must exist for all changed .java files" );
230
228
}
@@ -305,7 +303,7 @@ String getAndInc() {
305
303
306
304
// skip any empty lines
307
305
while (lineNbr < lines .size () &&
308
- lines .get (lineNbr ).trim ().isEmpty ()) {
306
+ lines .get (lineNbr ).trim ().isEmpty ()) {
309
307
lineNbr ++;
310
308
}
311
309
@@ -334,7 +332,7 @@ static class TypeInfoDescr {
334
332
335
333
public TypeInfoDescr (String qualifiedSourceName , String qualifiedBinaryName , String nativePathName ) {
336
334
this .qualifiedSourceName = qualifiedSourceName ;
337
- this .qualifiedBinaryName = qualifiedBinaryName != null && !qualifiedBinaryName .trim ().isEmpty () ? qualifiedBinaryName : qualifiedSourceName ;
335
+ this .qualifiedBinaryName = qualifiedBinaryName != null && !qualifiedBinaryName .trim ().isEmpty () ? qualifiedBinaryName : qualifiedSourceName ;
338
336
this .nativePathName = nativePathName ;
339
337
340
338
this .innerTypes = new ArrayList <>();
@@ -346,17 +344,18 @@ public List<String> dependencies() {
346
344
return dependencies ;
347
345
}
348
346
349
- @ Override public String toString () {
347
+ @ Override
348
+ public String toString () {
350
349
return "TypeInfoDescr{" +
351
- "qualifiedSourceName='" + qualifiedSourceName + '\'' +
352
- ", qualifiedBinaryName='" + qualifiedBinaryName + '\'' +
353
- ", superTypeName='" + superTypeName + '\'' +
354
- ", nativePathName='" + nativePathName + '\'' +
355
- ", enclosingType='" + enclosingType + '\'' +
356
- ", innerTypes=" + innerTypes +
357
- ", interfaces=" + interfaces +
358
- ", dependencies=" + dependencies +
359
- '}' ;
350
+ "qualifiedSourceName='" + qualifiedSourceName + '\'' +
351
+ ", qualifiedBinaryName='" + qualifiedBinaryName + '\'' +
352
+ ", superTypeName='" + superTypeName + '\'' +
353
+ ", nativePathName='" + nativePathName + '\'' +
354
+ ", enclosingType='" + enclosingType + '\'' +
355
+ ", innerTypes=" + innerTypes +
356
+ ", interfaces=" + interfaces +
357
+ ", dependencies=" + dependencies +
358
+ '}' ;
360
359
}
361
360
}
362
361
@@ -374,7 +373,7 @@ TypeInfoDescr readBuildMapSources(LineReader reader, Map<String, TypeInfoDescr>
374
373
}
375
374
376
375
String nativePathName = null ; // optional
377
- if (!reader .peekNext ().startsWith ("-" ) ) {
376
+ if (!reader .peekNext ().startsWith ("-" )) {
378
377
// native file specified
379
378
nativePathName = reader .getAndInc ();
380
379
}
@@ -390,7 +389,7 @@ public void readHierarchyAndInnerTypes(LineReader reader, TypeInfoDescr typeInfo
390
389
if (!line .startsWith ("- hierarchy" )) {
391
390
throw new RuntimeException ("Illegal File Format, the next element must be '-hierarchy' at line " + reader .lineNbr );
392
391
}
393
- if (!reader .peekNext ().startsWith ("-" ) ) {
392
+ if (!reader .peekNext ().startsWith ("-" )) {
394
393
line = reader .getAndInc ();
395
394
String [] segments = line .split (":" , -1 );
396
395
if (segments .length != 2 ) {
@@ -415,7 +414,7 @@ public void readHierarchyAndInnerTypes(LineReader reader, TypeInfoDescr typeInfo
415
414
if (!line .startsWith ("- innerTypes" )) {
416
415
throw new RuntimeException ("Illegal File Format, the next element must be '-innerTypes' at line " + reader .lineNbr );
417
416
}
418
- if (!reader .peekNext ().startsWith ("-" ) ) {
417
+ if (!reader .peekNext ().startsWith ("-" )) {
419
418
String [] innerTypes = reader .getAndInc ().split (":" , -1 );
420
419
421
420
String ext = ".build.map" ;
@@ -432,9 +431,9 @@ public void readHierarchyAndInnerTypes(LineReader reader, TypeInfoDescr typeInfo
432
431
// String fileName = innerTypeName.substring(penDot+1) + "$" + innerTypeName.substring(lastDot+1) + ext;
433
432
434
433
String fileName = buildMapPath .getFileName ().toString ();
435
- fileName = fileName .substring (0 , fileName .length () -ext .length ()) + "$" + innerTypeName .substring (lastDot +1 ) + ext ;
436
-
437
- Path innerBuildMapPath = buildMapPath .getParent ().resolve ( fileName );
434
+ // fileName = fileName.substring(0, fileName.length() -ext.length()) + "$" + innerTypeName.substring(lastDot+1) + ext;
435
+ fileName = innerTypeName . substring ( lastDot + 1 ) + ext ;
436
+ Path innerBuildMapPath = buildMapPath .getParent ().resolve (fileName );
438
437
if (!Files .exists (innerBuildMapPath )) {
439
438
throw new RuntimeException ("InnerType .build.map file must exist: " + innerBuildMapPath );
440
439
}
@@ -447,15 +446,15 @@ public void readHierarchyAndInnerTypes(LineReader reader, TypeInfoDescr typeInfo
447
446
}
448
447
449
448
void readBuildMapInterfaces (LineReader reader , TypeInfoDescr typeInfoDescr ) {
450
-
451
- while ( reader .hasNext () && !reader .peekNext ().startsWith ("- " )) {
449
+
450
+ while ( reader .hasNext () && !reader .peekNext ().startsWith ("- " )) {
452
451
String typeName = reader .getAndInc ();
453
452
typeInfoDescr .interfaces .add (typeName );
454
453
}
455
454
}
456
455
457
456
void readBuildMapDependencies (LineReader reader , TypeInfoDescr typeInfoDescr ) {
458
- while (reader .hasNext () && !reader .peekNext ().startsWith ("-" )) {
457
+ while (reader .hasNext () && !reader .peekNext ().startsWith ("-" )) {
459
458
String typeName = reader .getAndInc ();
460
459
typeInfoDescr .dependencies .add (typeName );
461
460
}
@@ -475,6 +474,7 @@ private void checkFileFormat(String str, int i) {
475
474
/**
476
475
* Clone the SourceMap to the TargetMap. Exclude MethodField TypeDependency references, these are not relevant to the parent
477
476
* projects that consume this BuildMap.
477
+ *
478
478
* @param target
479
479
*/
480
480
public void cloneToTargetBuildMap (BuildMap target ) {
@@ -490,7 +490,7 @@ public void cloneToTargetBuildMap(BuildMap target) {
490
490
target .pathToQualifiedSourceName .putAll (pathToQualifiedSourceName );
491
491
target .qualifiedSourceNameToPath .putAll (qualifiedSourceNameToPath );
492
492
493
- for (ProjectFiles projectFiles : dirToprojectFiles .values ()) {
493
+ for (ProjectFiles projectFiles : dirToprojectFiles .values ()) {
494
494
target .childrenChangedFiles .addAll (projectFiles .getUpdated ());
495
495
}
496
496
0 commit comments