Skip to content

Commit 76ad193

Browse files
committed
Add support for using directive in java files
1 parent 9f9cb73 commit 76ad193

File tree

3 files changed

+97
-10
lines changed

3 files changed

+97
-10
lines changed

modules/build/src/main/scala/scala/build/preprocessing/JavaPreprocessor.scala

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package scala.build.preprocessing
22

3-
import java.nio.charset.StandardCharsets
3+
import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
44

5-
import scala.build.errors.BuildException
5+
import java.nio.charset.StandardCharsets
6+
import scala.build.EitherCps.{either, value}
7+
import scala.build.errors.{BuildException, DirectiveErrors}
8+
import scala.build.options.BuildRequirements
9+
import scala.build.preprocessing.ScalaPreprocessor._
610
import scala.build.{Inputs, Logger}
711

812
case object JavaPreprocessor extends Preprocessor {
@@ -11,9 +15,27 @@ case object JavaPreprocessor extends Preprocessor {
1115
logger: Logger
1216
): Option[Either[BuildException, Seq[PreprocessedSource]]] =
1317
input match {
14-
case j: Inputs.JavaFile =>
15-
Some(Right(Seq(PreprocessedSource.OnDisk(j.path, None, None, Nil, None))))
16-
18+
case j: Inputs.JavaFile => Some(either {
19+
val content = value(PreprocessingUtil.maybeRead(j.path))
20+
val scopePath = ScopePath.fromPath(j.path)
21+
val ExtractedDirectives(_, directives0, kind) =
22+
value(extractUsingDirectives(content.toCharArray, Right(j.path), logger))
23+
val _ = value(assertKindIsNotCode(kind))
24+
val updatedOptions = value(DirectivesProcessor.process(
25+
directives0,
26+
usingDirectiveHandlers,
27+
Right(j.path),
28+
scopePath,
29+
logger
30+
))
31+
Seq(PreprocessedSource.OnDisk(
32+
j.path,
33+
Some(updatedOptions.global),
34+
Some(BuildRequirements()),
35+
Nil,
36+
None
37+
))
38+
})
1739
case v: Inputs.VirtualJavaFile =>
1840
val content = new String(v.content, StandardCharsets.UTF_8)
1941
val s = PreprocessedSource.InMemory(
@@ -31,4 +53,14 @@ case object JavaPreprocessor extends Preprocessor {
3153

3254
case _ => None
3355
}
56+
57+
private def assertKindIsNotCode(kind: UsingDirectiveKind) =
58+
kind match {
59+
case UsingDirectiveKind.PlainComment => Right(())
60+
case UsingDirectiveKind.SpecialComment => Right(())
61+
case UsingDirectiveKind.Code => Left(new DirectiveErrors(::(
62+
"Java doesn't support 'using' directives in the code",
63+
Nil
64+
)))
65+
}
3466
}

modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,8 @@ case object ScalaPreprocessor extends Preprocessor {
274274
cwd: ScopePath,
275275
logger: Logger
276276
): Either[BuildException, StrictDirectivesProcessingOutput] = either {
277-
278277
val contentChars = content.toCharArray
279-
val ExtractedDirectives(codeOffset, directives0) =
280-
value(extractUsingDirectives(contentChars, path, logger))
278+
val ExtractedDirectives(codeOffset, directives0, _) = value(extractUsingDirectives(contentChars, path, logger))
281279

282280
val updatedOptions = value {
283281
DirectivesProcessor.process(
@@ -347,7 +345,7 @@ case object ScalaPreprocessor extends Preprocessor {
347345
new UnusedDirectiveError(directive.key, values.map(_._1.value), values.flatMap(_._1.positions))
348346
}
349347

350-
case class ExtractedDirectives(offset: Int, directives: Seq[StrictDirective])
348+
case class ExtractedDirectives(offset: Int, directives: Seq[StrictDirective], kind: UsingDirectiveKind)
351349

352350
val changeToSpecialCommentMsg =
353351
"Using directive using plain comments are deprecated. Please use a special comment syntax: '//> ...' or '/*> ... */'"
@@ -436,7 +434,7 @@ case object ScalaPreprocessor extends Preprocessor {
436434
val offset =
437435
if (usedDirectives.getKind() != UsingDirectiveKind.Code) 0
438436
else usedDirectives.getCodeOffset()
439-
Right(ExtractedDirectives(offset, strictDirectives))
437+
Right(ExtractedDirectives(offset, strictDirectives, usedDirectives.getKind()))
440438
}
441439
else {
442440
val errors0 = errors.map(diag => new MalformedDirectiveError(diag.message, diag.positions))

modules/build/src/test/scala/scala/build/tests/SourcesTests.scala

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,63 @@ class SourcesTests extends munit.FunSuite {
221221
}
222222
}
223223

224+
test("dependencies in .java - //> using") {
225+
val testInputs = TestInputs(
226+
os.rel / "Something.java" ->
227+
"""//> using lib "org1:name1:1.1"
228+
|//> using lib "org2::name2:2.2"
229+
|//> using lib "org3:::name3:3.3"
230+
|
231+
|public class Something {
232+
| public Int a = 1;
233+
|}
234+
|""".stripMargin
235+
)
236+
val expectedDeps = Seq(
237+
dep"org1:name1:1.1",
238+
dep"org2::name2:2.2",
239+
dep"org3:::name3:3.3"
240+
)
241+
testInputs.withInputs { (_, inputs) =>
242+
val crossSources =
243+
CrossSources.forInputs(
244+
inputs,
245+
Sources.defaultPreprocessors(CustomCodeWrapper),
246+
TestLogger()
247+
).orThrow
248+
val scopedSources = crossSources.scopedSources(BuildOptions()).orThrow
249+
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()))
250+
251+
expect(
252+
sources.buildOptions.classPathOptions.extraDependencies.toSeq.map(_.value) == expectedDeps
253+
)
254+
expect(sources.paths.length == 1)
255+
expect(sources.paths.map(_._2) == Seq(os.rel / "Something.java"))
256+
expect(sources.inMemory.isEmpty)
257+
}
258+
}
259+
260+
test("should fail dependencies in .java - //> using") {
261+
val testInputs = TestInputs(
262+
os.rel / "Something.java" ->
263+
"""using lib "org3:::name3:3.3"
264+
|
265+
|public class Something {
266+
| public Int a = 1;
267+
|}
268+
|""".stripMargin
269+
)
270+
testInputs.withInputs { (_, inputs) =>
271+
val crossSources = CrossSources.forInputs(
272+
inputs,
273+
Sources.defaultPreprocessors(CustomCodeWrapper),
274+
TestLogger()
275+
)
276+
expect(crossSources.isLeft)
277+
}
278+
}
279+
280+
224281
test("dependencies in .sc - $ivy") {
225282
val testInputs = TestInputs(
226283
os.rel / "something.sc" ->

0 commit comments

Comments
 (0)