2424import com .google .common .collect .ImmutableSet ;
2525import java .io .IOException ;
2626import java .nio .file .Path ;
27- import java .util .ArrayList ;
28- import java .util .List ;
2927import javax .xml .stream .XMLStreamException ;
3028import javax .xml .transform .TransformerException ;
3129import org .apache .commons .cli .ParseException ;
@@ -57,85 +55,19 @@ public static void main(String[] arguments)
5755 linkageCheckerArguments .printHelp ();
5856 }
5957
60- if (linkageCheckerArguments .hasInput ()) {
61- // This is non-empty if a BOM or artifacts are specified in the argument
62- ImmutableList <Artifact > artifacts = linkageCheckerArguments .getArtifacts ();
63-
64- // When JAR files are specified in the argument, artifacts are empty.
65- ImmutableList <ClassPathEntry > inputClassPath ;
66- ImmutableSet <ClassPathEntry > entryPoints ;
67- List <ArtifactProblem > artifactProblems = new ArrayList <>();
68- // classPathResult is kept null if JAR files are specified in the argument
69- ClassPathResult classPathResult = null ;
70-
71- if (artifacts .isEmpty ()) {
72- // When JAR files are passed as arguments, classPathResult is null, because there is no need
73- // to resolve Maven dependencies.
74- inputClassPath = linkageCheckerArguments .getJarFiles ();
75- entryPoints = ImmutableSet .copyOf (inputClassPath );
76- } else {
77- // When a BOM or Maven artifacts are passed as arguments, resolve the dependencies.
78- DependencyGraphBuilder dependencyGraphBuilder =
79- new DependencyGraphBuilder (linkageCheckerArguments .getMavenRepositoryUrls ());
80- ClassPathBuilder classPathBuilder = new ClassPathBuilder (dependencyGraphBuilder );
81- classPathResult = classPathBuilder .resolve (artifacts , false );
82- inputClassPath = classPathResult .getClassPath ();
83- artifactProblems .addAll (classPathResult .getArtifactProblems ());
84- entryPoints = ImmutableSet .copyOf (inputClassPath .subList (0 , artifacts .size ()));
85- }
86-
87- LinkageChecker linkageChecker =
88- LinkageChecker .create (
89- inputClassPath , entryPoints , linkageCheckerArguments .getInputExclusionFile ());
90- ImmutableSet <LinkageProblem > linkageProblems = linkageChecker .findLinkageProblems ();
91-
92- if (linkageCheckerArguments .getReportOnlyReachable ()) {
93- ClassReferenceGraph graph = linkageChecker .getClassReferenceGraph ();
94- linkageProblems =
95- linkageProblems .stream ()
96- .filter (
97- (LinkageProblem problem ) ->
98- graph .isReachable (problem .getSourceClass ().getBinaryName ()))
99- .collect (toImmutableSet ());
100- }
101-
102- if (classPathResult != null ) {
103- LinkageProblemCauseAnnotator .annotate (classPathResult , linkageProblems );
104- }
105-
106- Path writeAsExclusionFile = linkageCheckerArguments .getOutputExclusionFile ();
107- if (writeAsExclusionFile != null ) {
108- ExclusionFiles .write (writeAsExclusionFile , linkageProblems );
109- System .out .println ("Wrote the linkage errors as exclusion file: " + writeAsExclusionFile );
110- return ;
111- }
58+ if (linkageCheckerArguments .hasInput ()) {
59+ // artifactsInArguments is not empty if a BOM or artifacts are specified in the argument.
60+ // If JAR files are specified, it's empty.
61+ ImmutableList <Artifact > artifactsInArguments = linkageCheckerArguments .getArtifacts ();
11262
63+ ImmutableSet <LinkageProblem > linkageProblems =
64+ artifactsInArguments .isEmpty ()
65+ ? checkJarFiles (linkageCheckerArguments )
66+ : checkArtifacts (linkageCheckerArguments );
11367 if (!linkageProblems .isEmpty ()) {
114- System .out .println (LinkageProblem .formatLinkageProblems (linkageProblems ));
115- }
116-
117- if (classPathResult != null && !linkageProblems .isEmpty ()) {
118- ImmutableSet .Builder <ClassPathEntry > problematicJars = ImmutableSet .builder ();
119- for (LinkageProblem linkageProblem : linkageProblems ) {
120- ClassFile targetClass = linkageProblem .getTargetClass ();
121- if (targetClass != null ) {
122- problematicJars .add (targetClass .getClassPathEntry ());
123- }
124- ClassFile sourceClassFile = linkageProblem .getSourceClass ();
125- problematicJars .add (sourceClassFile .getClassPathEntry ());
126- }
127- System .out .println (classPathResult .formatDependencyPaths (problematicJars .build ()));
128- }
129-
130- if (!artifactProblems .isEmpty ()) {
131- System .out .println ("\n " );
132- System .out .println (ArtifactProblem .formatProblems (artifactProblems ));
13368 System .out .println (
13469 "For the details of the linkage errors, see "
13570 + "https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/Linkage-Checker-Messages" );
136- }
137-
138- if (!linkageProblems .isEmpty ()) {
13971 // Throwing an exception is more test-friendly compared with System.exit(1). The latter
14072 // abruptly stops test execution.
14173 throw new LinkageCheckResultException (linkageProblems .size ());
@@ -145,4 +77,84 @@ public static void main(String[] arguments)
14577 System .err .println (ex .getMessage ());
14678 }
14779 }
80+
81+ private static ImmutableSet <LinkageProblem > checkJarFiles (
82+ LinkageCheckerArguments linkageCheckerArguments )
83+ throws IOException , TransformerException , XMLStreamException {
84+ ImmutableList <ClassPathEntry > inputClassPath = linkageCheckerArguments .getJarFiles ();
85+ ImmutableSet <ClassPathEntry > entryPoints = ImmutableSet .copyOf (inputClassPath );
86+ LinkageChecker linkageChecker =
87+ LinkageChecker .create (
88+ inputClassPath , entryPoints , linkageCheckerArguments .getInputExclusionFile ());
89+
90+ ImmutableSet <LinkageProblem > linkageProblems =
91+ findLinkageProblems (linkageCheckerArguments , linkageChecker );
92+
93+ if (!linkageProblems .isEmpty ()) {
94+ System .out .println (LinkageProblem .formatLinkageProblems (linkageProblems , null ));
95+ }
96+
97+ return linkageProblems ;
98+ }
99+
100+ private static ImmutableSet <LinkageProblem > checkArtifacts (
101+ LinkageCheckerArguments linkageCheckerArguments )
102+ throws IOException , RepositoryException , TransformerException , XMLStreamException {
103+ ImmutableList <Artifact > artifactsInArguments = linkageCheckerArguments .getArtifacts ();
104+
105+ // When a BOM or Maven artifacts are passed as arguments, resolve the dependencies.
106+ DependencyGraphBuilder dependencyGraphBuilder =
107+ new DependencyGraphBuilder (linkageCheckerArguments .getMavenRepositoryUrls ());
108+ ClassPathBuilder classPathBuilder = new ClassPathBuilder (dependencyGraphBuilder );
109+ ClassPathResult classPathResult = classPathBuilder .resolve (artifactsInArguments , false );
110+ ImmutableList <ClassPathEntry > inputClassPath = classPathResult .getClassPath ();
111+ ImmutableList <ArtifactProblem > artifactProblems =
112+ ImmutableList .copyOf (classPathResult .getArtifactProblems ());
113+ ImmutableSet <ClassPathEntry > entryPoints =
114+ ImmutableSet .copyOf (inputClassPath .subList (0 , artifactsInArguments .size ()));
115+
116+ LinkageChecker linkageChecker =
117+ LinkageChecker .create (
118+ inputClassPath , entryPoints , linkageCheckerArguments .getInputExclusionFile ());
119+ ImmutableSet <LinkageProblem > linkageProblems =
120+ findLinkageProblems (linkageCheckerArguments , linkageChecker );
121+
122+ LinkageProblemCauseAnnotator .annotate (classPathBuilder , classPathResult , linkageProblems );
123+
124+ if (!linkageProblems .isEmpty ()) {
125+ System .out .println (LinkageProblem .formatLinkageProblems (linkageProblems , classPathResult ));
126+ }
127+
128+ if (!artifactProblems .isEmpty ()) {
129+ System .out .println ("\n " );
130+ System .out .println (ArtifactProblem .formatProblems (artifactProblems ));
131+ }
132+ return linkageProblems ;
133+ }
134+
135+ private static ImmutableSet <LinkageProblem > findLinkageProblems (
136+ LinkageCheckerArguments linkageCheckerArguments , LinkageChecker linkageChecker )
137+ throws IOException , TransformerException , XMLStreamException {
138+
139+ ImmutableSet <LinkageProblem > linkageProblems = linkageChecker .findLinkageProblems ();
140+
141+ if (linkageCheckerArguments .getReportOnlyReachable ()) {
142+ ClassReferenceGraph graph = linkageChecker .getClassReferenceGraph ();
143+ linkageProblems =
144+ linkageProblems .stream ()
145+ .filter (
146+ (LinkageProblem problem ) ->
147+ graph .isReachable (problem .getSourceClass ().getBinaryName ()))
148+ .collect (toImmutableSet ());
149+ }
150+
151+ Path outputExclusionFile = linkageCheckerArguments .getOutputExclusionFile ();
152+ if (outputExclusionFile != null ) {
153+ ExclusionFiles .write (outputExclusionFile , linkageProblems );
154+ System .out .println ("Wrote the linkage errors as exclusion file: " + outputExclusionFile );
155+ return ImmutableSet .of ();
156+ }
157+
158+ return linkageProblems ;
159+ }
148160}
0 commit comments