14
14
15
15
package com .google .devtools .build .buildjar .javac .plugins .dependency ;
16
16
17
+ import static com .google .common .collect .ImmutableSet .toImmutableSet ;
17
18
import static com .google .common .collect .Iterables .getOnlyElement ;
18
19
import static com .google .devtools .build .buildjar .javac .plugins .dependency .DependencyModule .StrictJavaDeps .ERROR ;
19
20
20
21
import com .google .auto .value .AutoValue ;
21
22
import com .google .common .annotations .VisibleForTesting ;
22
23
import com .google .common .collect .ImmutableSet ;
23
- import com .google .common .collect .Ordering ;
24
24
import com .google .devtools .build .buildjar .JarOwner ;
25
25
import com .google .devtools .build .buildjar .javac .plugins .BlazeJavaCompilerPlugin ;
26
26
import com .google .devtools .build .buildjar .javac .plugins .dependency .DependencyModule .StrictJavaDeps ;
45
45
import java .io .PrintWriter ;
46
46
import java .io .UncheckedIOException ;
47
47
import java .nio .file .Path ;
48
- import java .text .MessageFormat ;
49
48
import java .util .ArrayList ;
49
+ import java .util .Comparator ;
50
50
import java .util .HashSet ;
51
- import java .util .LinkedHashSet ;
52
51
import java .util .List ;
53
52
import java .util .Map ;
53
+ import java .util .Optional ;
54
54
import java .util .Properties ;
55
55
import java .util .Set ;
56
56
import java .util .jar .Attributes ;
@@ -209,14 +209,18 @@ public void finish() {
209
209
? null
210
210
// we don't use the target mapping for the target, just the missing deps
211
211
: canonicalizeTarget (dependencyModule .getTargetLabel ());
212
- LinkedHashSet <JarOwner > canonicalizedMissing = new LinkedHashSet <>();
213
- for (JarOwner owner :
214
- Ordering .natural ().onResultOf (JarOwner .LABEL ).immutableSortedCopy (missingTargets )) {
215
- // for dependencies that are missing we canonicalize and remap the target so we don't
216
- // suggest private build labels.
217
- String actualTarget = canonicalizeTarget (remapTarget (owner .label ()));
218
- canonicalizedMissing .add (JarOwner .create (actualTarget , owner .aspect ()));
219
- }
212
+ Set <JarOwner > canonicalizedMissing =
213
+ missingTargets
214
+ .stream ()
215
+ .filter (owner -> owner .label ().isPresent ())
216
+ .sorted (Comparator .comparing ((JarOwner owner ) -> owner .label ().get ()))
217
+ // for dependencies that are missing we canonicalize and remap the target so we don't
218
+ // suggest private build labels.
219
+ .map (
220
+ owner ->
221
+ owner .withLabel (
222
+ owner .label ().map (label -> canonicalizeTarget (remapTarget (label )))))
223
+ .collect (toImmutableSet ());
220
224
errWriter .print (
221
225
dependencyModule
222
226
.getFixMessage ()
@@ -231,10 +235,6 @@ public void finish() {
231
235
*/
232
236
private static class CheckingTreeScanner extends TreeScanner {
233
237
234
- private static final String TRANSITIVE_DEP_MESSAGE =
235
- "[strict] Using {0} from an indirect dependency (TOOL_INFO: \" {1}\" ). "
236
- + "See command below **" ;
237
-
238
238
private final ImmutableSet <Path > directJars ;
239
239
240
240
/** Strict deps diagnostics. */
@@ -308,20 +308,27 @@ private void collectExplicitDependency(Path jarPath, JCTree node, Symbol sym) {
308
308
// IO cost here is fine because we only hit this path for an explicit dependency
309
309
// _not_ in the direct jars, i.e. an error
310
310
JarOwner owner = readJarOwnerFromManifest (jarPath );
311
- if (owner != null && seenTargets .add (owner )) {
311
+ if (seenTargets .add (owner )) {
312
312
// owner is of the form "//label/of:rule <Aspect name>" where <Aspect name> is
313
313
// optional.
314
- String canonicalTargetName = canonicalizeTarget (remapTarget (owner .label ()));
314
+ Optional <String > canonicalTargetName =
315
+ owner .label ().map (label -> canonicalizeTarget (remapTarget (label )));
315
316
missingTargets .add (owner );
316
317
String toolInfo =
317
- owner .aspect () == null
318
- ? canonicalTargetName
319
- : String .format ("%s wrapped in %s" , canonicalTargetName , owner .aspect ());
318
+ owner .aspect ().isPresent ()
319
+ ? String .format (
320
+ "%s wrapped in %s" , canonicalTargetName .get (), owner .aspect ().get ())
321
+ : canonicalTargetName .isPresent ()
322
+ ? canonicalTargetName .get ()
323
+ : owner .jar ().toString ();
320
324
String used =
321
325
sym .getSimpleName ().contentEquals ("package-info" )
322
326
? "package " + sym .getEnclosingElement ()
323
327
: "type " + sym ;
324
- String message = MessageFormat .format (TRANSITIVE_DEP_MESSAGE , used , toolInfo );
328
+ String message =
329
+ String .format (
330
+ "[strict] Using %s from an indirect dependency (TOOL_INFO: \" %s\" ).%s" ,
331
+ used , toolInfo , (owner .label ().isPresent () ? " See command below **" : "" ));
325
332
diagnostics .add (SjdDiagnostic .create (node .pos , message , source ));
326
333
}
327
334
}
@@ -346,15 +353,15 @@ private static JarOwner readJarOwnerFromManifest(Path jarPath) {
346
353
try (JarFile jarFile = new JarFile (jarPath .toFile ())) {
347
354
Manifest manifest = jarFile .getManifest ();
348
355
if (manifest == null ) {
349
- return null ;
356
+ return JarOwner . create ( jarPath ) ;
350
357
}
351
358
Attributes attributes = manifest .getMainAttributes ();
352
359
String label = (String ) attributes .get (TARGET_LABEL );
353
360
if (label == null ) {
354
- return null ;
361
+ return JarOwner . create ( jarPath ) ;
355
362
}
356
363
String injectingRuleKind = (String ) attributes .get (INJECTING_RULE_KIND );
357
- return JarOwner .create (label , injectingRuleKind );
364
+ return JarOwner .create (jarPath , label , Optional . ofNullable ( injectingRuleKind ) );
358
365
} catch (IOException e ) {
359
366
// This jar file pretty much has to exist, we just used it in the compiler. Throw unchecked.
360
367
throw new UncheckedIOException (e );
0 commit comments