1212import com .sun .tools .javac .util .Context ;
1313
1414import static javax .tools .StandardLocation .CLASS_OUTPUT ;
15+ import static javax .tools .StandardLocation .SOURCE_OUTPUT ;
1516
1617/** Settings that can be configured alongside the -Xplugin compiler option. */
1718public class SemanticdbJavacOptions {
@@ -25,6 +26,7 @@ public class SemanticdbJavacOptions {
2526 public final ArrayList <String > errors ;
2627 public boolean alreadyReportedErrors = false ;
2728 public UriScheme uriScheme = UriScheme .DEFAULT ;
29+ public Path generatedTargetRoot ;
2830
2931 public static String stubClassName = "META-INF-semanticdb-stub" ;
3032
@@ -49,7 +51,7 @@ public static SemanticdbJavacOptions parse(String[] args, Context ctx) {
4951 String argValue = arg .substring ("-targetroot:" .length ());
5052 if (argValue .equals (JAVAC_CLASSES_DIR_ARG )) {
5153 useJavacClassesDir = true ;
52- result .targetroot = getJavacClassesDir (result , ctx );
54+ result .targetroot = getJavacClassesDir (result , ctx ). classes ;
5355 } else {
5456 result .targetroot = Paths .get (argValue );
5557 }
@@ -60,7 +62,9 @@ public static SemanticdbJavacOptions parse(String[] args, Context ctx) {
6062 } else if (arg .equals ("-build-tool:bazel" )) {
6163 result .uriScheme = UriScheme .BAZEL ;
6264 useJavacClassesDir = true ;
63- result .targetroot = getJavacClassesDir (result , ctx );
65+ TargetPaths paths = getJavacClassesDir (result , ctx );
66+ result .targetroot = paths .classes ;
67+ result .generatedTargetRoot = paths .sources ;
6468 } else if (arg .equals ("-text:on" )) {
6569 result .includeText = true ;
6670 } else if (arg .equals ("-text:off" )) {
@@ -79,9 +83,11 @@ public static SemanticdbJavacOptions parse(String[] args, Context ctx) {
7983 result .errors .add (missingRequiredDirectoryOption ("targetroot" ));
8084 }
8185 if (!isSourcerootDefined (result )) {
82- // When using -build-tool:bazel, the sourceroot is automatically inferred from the first
86+ // When using -build-tool:bazel, the sourceroot is automatically inferred from
87+ // the first
8388 // compilation unit.
84- // See `SemanticdbTaskListener.inferBazelSourceroot()` for the method that infers the
89+ // See `SemanticdbTaskListener.inferBazelSourceroot()` for the method that
90+ // infers the
8591 // sourceroot.
8692 result .errors .add (missingRequiredDirectoryOption ("sourceroot" ));
8793 }
@@ -95,14 +101,18 @@ private static boolean isSourcerootDefined(SemanticdbJavacOptions options) {
95101 return options .sourceroot != null ;
96102 }
97103
98- private static Path getJavacClassesDir (SemanticdbJavacOptions result , Context ctx ) {
104+ private static TargetPaths getJavacClassesDir (SemanticdbJavacOptions result , Context ctx ) {
99105 // I'm not aware of a better way to get the class output directory from javac
100- Path outputDir = null ;
106+ Path classOutputDir = null ;
107+ Path sourceOutputDir = null ;
101108 try {
102109 JavaFileManager fm = ctx .get (JavaFileManager .class );
103- FileObject outputDirStub =
110+ FileObject sourceOutputDirStub =
111+ fm .getJavaFileForOutput (SOURCE_OUTPUT , stubClassName , JavaFileObject .Kind .SOURCE , null );
112+ FileObject clasSOutputDirStub =
104113 fm .getJavaFileForOutput (CLASS_OUTPUT , stubClassName , JavaFileObject .Kind .CLASS , null );
105- outputDir = Paths .get (outputDirStub .toUri ()).toAbsolutePath ().getParent ();
114+ classOutputDir = Paths .get (clasSOutputDirStub .toUri ()).toAbsolutePath ().getParent ();
115+ sourceOutputDir = Paths .get (sourceOutputDirStub .toUri ()).toAbsolutePath ().getParent ();
106116 } catch (Exception e ) {
107117 ByteArrayOutputStream out = new ByteArrayOutputStream ();
108118 e .printStackTrace (new PrintStream (out ));
@@ -112,6 +122,6 @@ private static Path getJavacClassesDir(SemanticdbJavacOptions result, Context ct
112122 JAVAC_CLASSES_DIR_ARG , out .toString ());
113123 result .errors .add (errorMsg );
114124 }
115- return outputDir ;
125+ return new TargetPaths ( classOutputDir , sourceOutputDir ) ;
116126 }
117127}
0 commit comments