21
21
import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
22
22
import org .gradle .api .NamedDomainObjectContainer ;
23
23
import org .gradle .api .Project ;
24
+ import org .gradle .api .Task ;
24
25
import org .gradle .api .artifacts .Configuration ;
25
26
import org .gradle .api .file .SourceDirectorySet ;
26
27
import org .gradle .api .initialization .Settings ;
31
32
import org .gradle .api .plugins .JavaPluginExtension ;
32
33
import org .gradle .api .tasks .SourceSet ;
33
34
import org .gradle .api .tasks .compile .CompileOptions ;
35
+ import org .gradle .api .tasks .compile .GroovyCompile ;
34
36
import org .gradle .api .tasks .compile .JavaCompile ;
35
37
import org .gradle .internal .service .ServiceRegistry ;
36
38
import org .gradle .invocation .DefaultGradle ;
@@ -686,8 +688,6 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
686
688
ctx ));
687
689
}
688
690
689
- Charset sourceCharset = Charset .forName (System .getProperty ("file.encoding" , "UTF-8" ));
690
-
691
691
Path buildDirPath = baseDir .relativize (subproject .getLayout ()
692
692
.getBuildDirectory ()
693
693
.get ()
@@ -700,7 +700,6 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
700
700
subproject ,
701
701
progressBar ,
702
702
buildDirPath ,
703
- sourceCharset ,
704
703
alreadyParsed ,
705
704
exclusions ,
706
705
ctx );
@@ -709,7 +708,6 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
709
708
subproject ,
710
709
progressBar ,
711
710
buildDirPath ,
712
- sourceCharset ,
713
711
alreadyParsed ,
714
712
exclusions ,
715
713
ctx );
@@ -747,7 +745,6 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
747
745
private SourceFileStream parseGradleProjectSourceSets (Project subproject ,
748
746
ProgressBar progressBar ,
749
747
Path buildDir ,
750
- Charset sourceCharset ,
751
748
Set <Path > alreadyParsed ,
752
749
Collection <PathMatcher > exclusions ,
753
750
ExecutionContext ctx ) {
@@ -764,8 +761,6 @@ private SourceFileStream parseGradleProjectSourceSets(Project subproject,
764
761
.getByName (sourceSet .getCompileJavaTaskName ());
765
762
JavaVersion javaVersion = getJavaVersion (javaCompileTask );
766
763
767
- final Charset javaSourceCharset = getSourceFileEncoding (javaCompileTask , sourceCharset );
768
-
769
764
List <Path > unparsedSources = sourceSet .getAllSource ()
770
765
.getSourceDirectories ()
771
766
.filter (File ::exists )
@@ -818,7 +813,7 @@ private SourceFileStream parseGradleProjectSourceSets(Project subproject,
818
813
ctx ,
819
814
buildDir ,
820
815
exclusions ,
821
- javaSourceCharset ,
816
+ getSourceFileEncoding ( javaCompileTask . getOptions ()) ,
822
817
javaVersion ,
823
818
dependencyPaths ,
824
819
javaTypeCache );
@@ -845,7 +840,6 @@ private SourceFileStream parseGradleProjectSourceSets(Project subproject,
845
840
ctx ,
846
841
buildDir ,
847
842
exclusions ,
848
- javaSourceCharset ,
849
843
javaVersion ,
850
844
dependencyPaths ,
851
845
javaTypeCache );
@@ -873,11 +867,18 @@ private SourceFileStream parseGradleProjectSourceSets(Project subproject,
873
867
874
868
alreadyParsed .addAll (groovyPaths );
875
869
870
+ GroovyCompile groovyCompileTask = (GroovyCompile ) subproject .getTasks ()
871
+ .getByName (sourceSet .getCompileTaskName ("groovy" ));
876
872
Stream <SourceFile > cus = Stream .of ((Supplier <GroovyParser >) () -> GroovyParser .builder ()
877
873
.classpath (dependenciesWithBuildDirs )
878
874
.typeCache (javaTypeCache )
879
875
.logCompilationWarningsAndErrors (false )
880
- .build ()).map (Supplier ::get ).flatMap (gp -> gp .parse (groovyPaths , baseDir , ctx )).map (cu -> {
876
+ .build ())
877
+ .map (Supplier ::get )
878
+ .flatMap (gp -> {
879
+ view (ctx ).setCharset (getSourceFileEncoding (groovyCompileTask .getOptions ()));
880
+ return gp .parse (groovyPaths , baseDir , ctx ).onClose (() -> view (ctx ).setCharset (null ));
881
+ }).map (cu -> {
881
882
if (isExcluded (repository , exclusions , cu .getSourcePath ()) || cu .getSourcePath ().startsWith (buildDir )) {
882
883
return null ;
883
884
}
@@ -923,15 +924,13 @@ private SourceFileStream parseAndroidProjectSourceSets(
923
924
Project subproject ,
924
925
ProgressBar progressBar ,
925
926
Path buildDir ,
926
- Charset sourceCharset ,
927
927
Set <Path > alreadyParsed ,
928
928
Collection <PathMatcher > exclusions ,
929
929
ExecutionContext ctx ) {
930
930
return getAndroidProjectParser ().parseProjectSourceSets (
931
931
subproject ,
932
932
progressBar ,
933
933
buildDir ,
934
- sourceCharset ,
935
934
alreadyParsed ,
936
935
exclusions ,
937
936
ctx ,
@@ -947,14 +946,17 @@ private Stream<SourceFile> parseJavaFiles(
947
946
JavaVersion javaVersion ,
948
947
Set <Path > dependencyPaths ,
949
948
JavaTypeCache javaTypeCache ) {
950
- view (ctx ).setCharset (javaSourceCharset );
951
-
952
949
return Stream .of ((Supplier <JavaParser >) () -> JavaParser .fromJavaVersion ()
953
950
.classpath (dependencyPaths )
954
951
.typeCache (javaTypeCache )
955
952
.logCompilationWarningsAndErrors (extension .getLogCompilationWarningsAndErrors ())
956
953
.build ())
957
- .map (Supplier ::get ).flatMap (jp -> jp .parse (javaPaths , baseDir , ctx )).map (cu -> {
954
+ .map (Supplier ::get )
955
+ .flatMap (jp -> {
956
+ view (ctx ).setCharset (javaSourceCharset );
957
+ return jp .parse (javaPaths , baseDir , ctx ).onClose (() -> view (ctx ).setCharset (null ));
958
+ })
959
+ .map (cu -> {
958
960
if (isExcluded (repository , exclusions , cu .getSourcePath ()) || cu .getSourcePath ().startsWith (buildDir )) {
959
961
return null ;
960
962
}
@@ -966,22 +968,25 @@ private Stream<SourceFile> parseKotlinFiles(List<Path> kotlinPaths,
966
968
ExecutionContext ctx ,
967
969
Path buildDir ,
968
970
Collection <PathMatcher > exclusions ,
969
- Charset javaSourceCharset ,
970
971
JavaVersion javaVersion ,
971
972
Set <Path > dependencyPaths ,
972
973
JavaTypeCache javaTypeCache ) {
973
- view (ctx ).setCharset (javaSourceCharset );
974
-
975
974
return Stream .of ((Supplier <KotlinParser >) () -> KotlinParser .builder ()
976
- .classpath (dependencyPaths )
977
- .typeCache (javaTypeCache )
978
- .logCompilationWarningsAndErrors (extension .getLogCompilationWarningsAndErrors ())
979
- .build ()).map (Supplier ::get ).flatMap (kp -> kp .parse (kotlinPaths , baseDir , ctx )).map (cu -> {
980
- if (isExcluded (repository , exclusions , cu .getSourcePath ()) || cu .getSourcePath ().startsWith (buildDir )) {
981
- return null ;
982
- }
983
- return cu ;
984
- }).filter (Objects ::nonNull ).map (it -> it .withMarkers (it .getMarkers ().add (javaVersion )));
975
+ .classpath (dependencyPaths )
976
+ .typeCache (javaTypeCache )
977
+ .logCompilationWarningsAndErrors (extension .getLogCompilationWarningsAndErrors ())
978
+ .build ())
979
+ .map (Supplier ::get )
980
+ .flatMap (kp -> {
981
+ view (ctx ).setCharset (StandardCharsets .UTF_8 ); // Kotlin requires UTF-8
982
+ return kp .parse (kotlinPaths , baseDir , ctx ).onClose (() -> view (ctx ).setCharset (null ));
983
+ })
984
+ .map (cu -> {
985
+ if (isExcluded (repository , exclusions , cu .getSourcePath ()) || cu .getSourcePath ().startsWith (buildDir )) {
986
+ return null ;
987
+ }
988
+ return cu ;
989
+ }).filter (Objects ::nonNull ).map (it -> it .withMarkers (it .getMarkers ().add (javaVersion )));
985
990
}
986
991
987
992
private GradleParser gradleParser () {
@@ -1576,13 +1581,10 @@ private JavaVersion getJavaVersion(@Nullable JavaCompile javaCompileTask) {
1576
1581
1577
1582
}
1578
1583
1579
- private Charset getSourceFileEncoding (@ Nullable JavaCompile javaCompileTask ,
1580
- Charset defaultCharset ) {
1581
- String sourceEncoding = null ;
1582
- if (javaCompileTask != null ) {
1583
- CompileOptions compileOptions = javaCompileTask .getOptions ();
1584
- sourceEncoding = compileOptions .getEncoding ();
1584
+ private Charset getSourceFileEncoding (@ Nullable CompileOptions compileOptions ) {
1585
+ if (compileOptions != null && compileOptions .getEncoding () != null ) {
1586
+ return Charset .forName (compileOptions .getEncoding ());
1585
1587
}
1586
- return Optional . ofNullable ( sourceEncoding ). map ( Charset :: forName ). orElse ( defaultCharset );
1588
+ return Charset . defaultCharset ( );
1587
1589
}
1588
1590
}
0 commit comments