Skip to content

Commit 65dfcae

Browse files
committed
8308399: Recommend --release when -source and -target are misused
Reviewed-by: vromero
1 parent 335f5db commit 65dfcae

23 files changed

+119
-55
lines changed

Diff for: src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@
6262
import com.sun.tools.javac.platform.PlatformDescription;
6363
import com.sun.tools.javac.platform.PlatformUtils;
6464
import com.sun.tools.javac.resources.CompilerProperties.Errors;
65+
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
6566
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
6667
import com.sun.tools.javac.util.Context;
6768
import com.sun.tools.javac.util.JCDiagnostic;
6869
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticInfo;
70+
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
6971
import com.sun.tools.javac.util.List;
7072
import com.sun.tools.javac.util.ListBuffer;
7173
import com.sun.tools.javac.util.Log;
@@ -521,9 +523,9 @@ public boolean validate() {
521523
if (target.compareTo(source.requiredTarget()) < 0) {
522524
if (targetString != null) {
523525
if (sourceString == null) {
524-
reportDiag(Warnings.TargetDefaultSourceConflict(targetString, source.requiredTarget()));
526+
reportDiag(Errors.TargetDefaultSourceConflict(source.name, targetString));
525527
} else {
526-
reportDiag(Warnings.SourceTargetConflict(sourceString, source.requiredTarget()));
528+
reportDiag(Errors.SourceTargetConflict(sourceString, targetString));
527529
}
528530
return false;
529531
} else {
@@ -569,10 +571,10 @@ public boolean validate() {
569571
if (fm instanceof BaseFileManager baseFileManager) {
570572
if (source.compareTo(Source.JDK8) <= 0) {
571573
if (baseFileManager.isDefaultBootClassPath())
572-
log.warning(LintCategory.OPTIONS, Warnings.SourceNoBootclasspath(source.name));
574+
log.warning(LintCategory.OPTIONS, Warnings.SourceNoBootclasspath(source.name, releaseNote(source, targetString)));
573575
} else {
574576
if (baseFileManager.isDefaultSystemModulesPath())
575-
log.warning(LintCategory.OPTIONS, Warnings.SourceNoSystemModulesPath(source.name));
577+
log.warning(LintCategory.OPTIONS, Warnings.SourceNoSystemModulesPath(source.name, releaseNote(source, targetString)));
576578
}
577579
}
578580
}
@@ -640,6 +642,22 @@ public boolean validate() {
640642
return !errors && (log.nerrors == 0);
641643
}
642644

645+
private Fragment releaseNote(Source source, String targetString) {
646+
if (source.compareTo(Source.JDK8) <= 0) {
647+
if (targetString != null) {
648+
return Fragments.SourceNoBootclasspathWithTarget(source.name, targetString);
649+
} else {
650+
return Fragments.SourceNoBootclasspath(source.name);
651+
}
652+
} else {
653+
if (targetString != null) {
654+
return Fragments.SourceNoSystemModulesPathWithTarget(source.name, targetString);
655+
} else {
656+
return Fragments.SourceNoSystemModulesPath(source.name);
657+
}
658+
}
659+
}
660+
643661
private void validateAddExports(SourceVersion sv) {
644662
String addExports = options.get(Option.ADD_EXPORTS);
645663
if (addExports != null) {

Diff for: src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

+32-10
Original file line numberDiff line numberDiff line change
@@ -2119,13 +2119,33 @@ compiler.warn.static.not.qualified.by.type=\
21192119
compiler.warn.static.not.qualified.by.type2=\
21202120
static {0} should not be used as a member of an anonymous class
21212121

2122-
# 0: string
2122+
# 0: string, 1: fragment
21232123
compiler.warn.source.no.bootclasspath=\
2124-
bootstrap class path not set in conjunction with -source {0}
2124+
bootstrap class path is not set in conjunction with -source {0}\n{1}
21252125

2126-
# 0: string
2126+
# 0: string, 1: fragment
21272127
compiler.warn.source.no.system.modules.path=\
2128-
system modules path not set in conjunction with -source {0}
2128+
location of system modules is not set in conjunction with -source {0}\n{1}
2129+
2130+
# 0: string
2131+
compiler.misc.source.no.bootclasspath=\
2132+
not setting the bootstrap class path may lead to class files that cannot run on JDK {0}\n\
2133+
--release {0} is recommended instead of -source {0} because it sets the bootstrap class path automatically
2134+
2135+
# 0: string
2136+
compiler.misc.source.no.system.modules.path=\
2137+
not setting the location of system modules may lead to class files that cannot run on JDK {0}\n\
2138+
--release {0} is recommended instead of -source {0} because it sets the location of system modules automatically
2139+
2140+
# 0: string, 1: string
2141+
compiler.misc.source.no.bootclasspath.with.target=\
2142+
not setting the bootstrap class path may lead to class files that cannot run on JDK 8\n\
2143+
--release {0} is recommended instead of -source {0} -target {1} because it sets the bootstrap class path automatically
2144+
2145+
# 0: string, 1: string
2146+
compiler.misc.source.no.system.modules.path.with.target=\
2147+
not setting the location of system modules may lead to class files that cannot run on JDK {0}\n\
2148+
--release {0} is recommended instead of -source {0} -target {1} because it sets the location of system modules automatically
21292149

21302150
# 0: string
21312151
compiler.warn.option.obsolete.source=\
@@ -3972,13 +3992,15 @@ compiler.err.error.writing.file=\
39723992
compiler.err.sourcepath.modulesourcepath.conflict=\
39733993
cannot specify both --source-path and --module-source-path
39743994

3975-
# 0: string, 1: target
3976-
compiler.warn.source.target.conflict=\
3977-
source release {0} requires target release {1}
3995+
# 0: string, 1: string
3996+
compiler.err.source.target.conflict=\
3997+
specified target release {1} is too old for the specified source release {0}\n\
3998+
--release {1} is recommended when compiling code to run on JDK {1}
39783999

3979-
# 0: string, 1: target
3980-
compiler.warn.target.default.source.conflict=\
3981-
target release {0} conflicts with default source release {1}
4000+
# 0: string, 1: string
4001+
compiler.err.target.default.source.conflict=\
4002+
specified target release {1} is too old for the default source release {0}\n\
4003+
--release {1} is recommended when compiling code to run on JDK {1}
39824004

39834005
# 0: profile, 1: target
39844006
compiler.warn.profile.target.conflict=\

Diff for: test/langtools/tools/javac/T8222035/MinContextOpTest_A.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- compiler.warn.source.no.system.modules.path: 15
1+
- compiler.warn.source.no.system.modules.path: 15, (compiler.misc.source.no.system.modules.path: 15)
22
MinContextOpTest.java:16:25: compiler.err.mod.not.allowed.here: static
33
MinContextOpTest.java:22:25: compiler.err.mod.not.allowed.here: static
44
MinContextOpTest.java:28:34: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T,K,V,E, (compiler.misc.inconvertible.types: java.util.function.Function<MinContextOpTest.A.T,MinContextOpTest.A.T>, java.util.function.Function<? super MinContextOpTest.A.T,? extends MinContextOpTest.A.T<?>>))

Diff for: test/langtools/tools/javac/diags/examples.not-yet.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ compiler.err.two.class.loaders.2
206206
compiler.err.unmatched.quote
207207
compiler.err.unsupported.release.version
208208
compiler.warn.profile.target.conflict
209-
compiler.warn.source.target.conflict
210-
compiler.warn.target.default.source.conflict
209+
compiler.err.source.target.conflict
210+
compiler.err.target.default.source.conflict
211211
compiler.err.preview.not.latest
212212
compiler.err.preview.without.source.or.release
213213
compiler.misc.illegal.signature # the compiler can now detect more non-denotable types before class writing

Diff for: test/langtools/tools/javac/diags/examples/EnumsMustBeStatic.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,8 +22,7 @@
2222
*/
2323

2424
// key: compiler.err.static.declaration.not.allowed.in.inner.classes
25-
// key: compiler.warn.source.no.system.modules.path
26-
// options: -source 15
25+
// options: --release 15
2726

2827
class EnumsMustBeStatic {
2928
class Nested {

Diff for: test/langtools/tools/javac/diags/examples/Expected3.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,6 @@
2222
*/
2323

2424
// key: compiler.err.expected3
25-
// key: compiler.warn.source.no.system.modules.path
26-
// options: -source 15
25+
// options: --release 15
2726

2827
int Expected3;

Diff for: test/langtools/tools/javac/diags/examples/InnerClassCantHaveStatic.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,8 +22,7 @@
2222
*/
2323

2424
// key: compiler.err.icls.cant.have.static.decl
25-
// key: compiler.warn.source.no.system.modules.path
26-
// options: -source 15
25+
// options: --release 15
2726

2827
class InnerClassCantHaveStatic {
2928
class Inner {

Diff for: test/langtools/tools/javac/diags/examples/InterfaceNotAllowed.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,8 +22,7 @@
2222
*/
2323

2424
// key: compiler.err.intf.not.allowed.here
25-
// key: compiler.warn.source.no.system.modules.path
26-
// options: -source 15
25+
// options: --release 15
2726

2827
class InterfaceNotAllowed {
2928
void m() {

Diff for: test/langtools/tools/javac/diags/examples/LocalEnum.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,8 +22,7 @@
2222
*/
2323

2424
// key: compiler.err.local.enum
25-
// key: compiler.warn.source.no.system.modules.path
26-
// options: -source 15
25+
// options: --release 15
2726

2827
class LocalEnum {
2928
void m() {

Diff for: test/langtools/tools/javac/diags/examples/ObsoleteSourceAndTarget.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525
// key: compiler.warn.option.obsolete.target
2626
// key: compiler.warn.option.obsolete.suppression
2727
// key: compiler.warn.source.no.bootclasspath
28+
// key: compiler.misc.source.no.bootclasspath.with.target
2829
// options: -source 1.8 -target 1.8
2930

3031
class ObsoleteSourceAndTarget {

Diff for: test/langtools/tools/javac/diags/examples/OptionRemovedSource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
2323

2424
// key: compiler.err.option.removed.source
2525
// key: compiler.warn.source.no.bootclasspath
26+
// key: compiler.misc.source.no.bootclasspath
2627
// options: -source 1.5
2728

2829
class RemovedSourceAndTarget {

Diff for: test/langtools/tools/javac/diags/examples/OptionRemovedTarget.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
2424
// key: compiler.err.option.removed.source
2525
// key: compiler.err.option.removed.target
2626
// key: compiler.warn.source.no.bootclasspath
27+
// key: compiler.misc.source.no.bootclasspath.with.target
2728
// options: -source 1.5 -target 1.5
2829

2930
class RemovedSourceAndTarget {

Diff for: test/langtools/tools/javac/diags/examples/Records.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,6 @@
2323

2424
// key: compiler.misc.feature.records
2525
// key: compiler.err.feature.not.supported.in.source.plural
26-
// key: compiler.warn.source.no.system.modules.path
27-
// options: -source 15
26+
// options: --release 15
2827

2928
record R() {}

Diff for: test/langtools/tools/javac/diags/examples/SealedTypes.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,7 @@
2323

2424
// key: compiler.misc.feature.sealed.classes
2525
// key: compiler.err.feature.not.supported.in.source.plural
26-
// key: compiler.warn.source.no.system.modules.path
27-
// options: -source 16
26+
// options: --release 16
2827

2928
sealed class Sealed {}
3029

Diff for: test/langtools/tools/javac/diags/examples/SourceNoBootclasspath.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
2222
*/
2323

2424
// key: compiler.warn.source.no.bootclasspath
25+
// key: compiler.misc.source.no.bootclasspath
2526
// key: compiler.warn.option.obsolete.source
2627
// key: compiler.warn.option.obsolete.suppression
2728
// options: -source 8

Diff for: test/langtools/tools/javac/diags/examples/SourceNoSystemModulesPath.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
2222
*/
2323

2424
// key: compiler.warn.source.no.system.modules.path
25+
// key: compiler.misc.source.no.system.modules.path
2526
// options: -source 9
2627

2728
class SourceNoSystemModulesPath { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
// key: compiler.warn.source.no.system.modules.path
25+
// key: compiler.misc.source.no.system.modules.path.with.target
26+
// options: -source 9 -target 9
27+
28+
class SourceNoSystemModulesPath { }

Diff for: test/langtools/tools/javac/diags/examples/TextBlockSource.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,7 @@
2323

2424
// key: compiler.misc.feature.text.blocks
2525
// key: compiler.err.feature.not.supported.in.source.plural
26-
// key: compiler.warn.source.no.system.modules.path
27-
// options: -source 14
26+
// options: --release 14
2827

2928
class TextBlockSource {
3029
String m() {

Diff for: test/langtools/tools/javac/diags/examples/UnderscoreInLambdaExpression.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,7 @@
2323

2424
// key: compiler.err.feature.not.supported.in.source.plural
2525
// key: compiler.misc.feature.unnamed.variables
26-
// key: compiler.warn.source.no.system.modules.path
27-
// options: -source 21
26+
// options: --release 21
2827

2928
public class UnderscoreInLambdaExpression {
3029
java.util.function.Function<String,String> f = _ -> "x";

0 commit comments

Comments
 (0)