Skip to content

Commit

Permalink
8308399: Recommend --release when -source and -target are misused
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Nov 29, 2023
1 parent 335f5db commit 65dfcae
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@
import com.sun.tools.javac.platform.PlatformDescription;
import com.sun.tools.javac.platform.PlatformUtils;
import com.sun.tools.javac.resources.CompilerProperties.Errors;
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticInfo;
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
Expand Down Expand Up @@ -521,9 +523,9 @@ public boolean validate() {
if (target.compareTo(source.requiredTarget()) < 0) {
if (targetString != null) {
if (sourceString == null) {
reportDiag(Warnings.TargetDefaultSourceConflict(targetString, source.requiredTarget()));
reportDiag(Errors.TargetDefaultSourceConflict(source.name, targetString));
} else {
reportDiag(Warnings.SourceTargetConflict(sourceString, source.requiredTarget()));
reportDiag(Errors.SourceTargetConflict(sourceString, targetString));
}
return false;
} else {
Expand Down Expand Up @@ -569,10 +571,10 @@ public boolean validate() {
if (fm instanceof BaseFileManager baseFileManager) {
if (source.compareTo(Source.JDK8) <= 0) {
if (baseFileManager.isDefaultBootClassPath())
log.warning(LintCategory.OPTIONS, Warnings.SourceNoBootclasspath(source.name));
log.warning(LintCategory.OPTIONS, Warnings.SourceNoBootclasspath(source.name, releaseNote(source, targetString)));
} else {
if (baseFileManager.isDefaultSystemModulesPath())
log.warning(LintCategory.OPTIONS, Warnings.SourceNoSystemModulesPath(source.name));
log.warning(LintCategory.OPTIONS, Warnings.SourceNoSystemModulesPath(source.name, releaseNote(source, targetString)));
}
}
}
Expand Down Expand Up @@ -640,6 +642,22 @@ public boolean validate() {
return !errors && (log.nerrors == 0);
}

private Fragment releaseNote(Source source, String targetString) {
if (source.compareTo(Source.JDK8) <= 0) {
if (targetString != null) {
return Fragments.SourceNoBootclasspathWithTarget(source.name, targetString);
} else {
return Fragments.SourceNoBootclasspath(source.name);
}
} else {
if (targetString != null) {
return Fragments.SourceNoSystemModulesPathWithTarget(source.name, targetString);
} else {
return Fragments.SourceNoSystemModulesPath(source.name);
}
}
}

private void validateAddExports(SourceVersion sv) {
String addExports = options.get(Option.ADD_EXPORTS);
if (addExports != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2119,13 +2119,33 @@ compiler.warn.static.not.qualified.by.type=\
compiler.warn.static.not.qualified.by.type2=\
static {0} should not be used as a member of an anonymous class

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

# 0: string
# 0: string, 1: fragment
compiler.warn.source.no.system.modules.path=\
system modules path not set in conjunction with -source {0}
location of system modules is not set in conjunction with -source {0}\n{1}

# 0: string
compiler.misc.source.no.bootclasspath=\
not setting the bootstrap class path may lead to class files that cannot run on JDK {0}\n\
--release {0} is recommended instead of -source {0} because it sets the bootstrap class path automatically

# 0: string
compiler.misc.source.no.system.modules.path=\
not setting the location of system modules may lead to class files that cannot run on JDK {0}\n\
--release {0} is recommended instead of -source {0} because it sets the location of system modules automatically

# 0: string, 1: string
compiler.misc.source.no.bootclasspath.with.target=\
not setting the bootstrap class path may lead to class files that cannot run on JDK 8\n\
--release {0} is recommended instead of -source {0} -target {1} because it sets the bootstrap class path automatically

# 0: string, 1: string
compiler.misc.source.no.system.modules.path.with.target=\
not setting the location of system modules may lead to class files that cannot run on JDK {0}\n\
--release {0} is recommended instead of -source {0} -target {1} because it sets the location of system modules automatically

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

# 0: string, 1: target
compiler.warn.source.target.conflict=\
source release {0} requires target release {1}
# 0: string, 1: string
compiler.err.source.target.conflict=\
specified target release {1} is too old for the specified source release {0}\n\
--release {1} is recommended when compiling code to run on JDK {1}

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

# 0: profile, 1: target
compiler.warn.profile.target.conflict=\
Expand Down
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/T8222035/MinContextOpTest_A.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- compiler.warn.source.no.system.modules.path: 15
- compiler.warn.source.no.system.modules.path: 15, (compiler.misc.source.no.system.modules.path: 15)
MinContextOpTest.java:16:25: compiler.err.mod.not.allowed.here: static
MinContextOpTest.java:22:25: compiler.err.mod.not.allowed.here: static
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<?>>))
Expand Down
4 changes: 2 additions & 2 deletions test/langtools/tools/javac/diags/examples.not-yet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ compiler.err.two.class.loaders.2
compiler.err.unmatched.quote
compiler.err.unsupported.release.version
compiler.warn.profile.target.conflict
compiler.warn.source.target.conflict
compiler.warn.target.default.source.conflict
compiler.err.source.target.conflict
compiler.err.target.default.source.conflict
compiler.err.preview.not.latest
compiler.err.preview.without.source.or.release
compiler.misc.illegal.signature # the compiler can now detect more non-denotable types before class writing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,8 +22,7 @@
*/

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

class EnumsMustBeStatic {
class Nested {
Expand Down
5 changes: 2 additions & 3 deletions test/langtools/tools/javac/diags/examples/Expected3.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,7 +22,6 @@
*/

// key: compiler.err.expected3
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15

int Expected3;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,8 +22,7 @@
*/

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

class InnerClassCantHaveStatic {
class Inner {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,8 +22,7 @@
*/

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

class InterfaceNotAllowed {
void m() {
Expand Down
5 changes: 2 additions & 3 deletions test/langtools/tools/javac/diags/examples/LocalEnum.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,8 +22,7 @@
*/

// key: compiler.err.local.enum
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15

class LocalEnum {
void m() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,7 @@
// key: compiler.warn.option.obsolete.target
// key: compiler.warn.option.obsolete.suppression
// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath.with.target
// options: -source 1.8 -target 1.8

class ObsoleteSourceAndTarget {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,6 +23,7 @@

// key: compiler.err.option.removed.source
// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath
// options: -source 1.5

class RemovedSourceAndTarget {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,6 +24,7 @@
// key: compiler.err.option.removed.source
// key: compiler.err.option.removed.target
// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath.with.target
// options: -source 1.5 -target 1.5

class RemovedSourceAndTarget {
Expand Down
5 changes: 2 additions & 3 deletions test/langtools/tools/javac/diags/examples/Records.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,6 @@

// key: compiler.misc.feature.records
// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15

record R() {}
5 changes: 2 additions & 3 deletions test/langtools/tools/javac/diags/examples/SealedTypes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,8 +23,7 @@

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

sealed class Sealed {}

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

// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath
// key: compiler.warn.option.obsolete.source
// key: compiler.warn.option.obsolete.suppression
// options: -source 8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,6 +22,7 @@
*/

// key: compiler.warn.source.no.system.modules.path
// key: compiler.misc.source.no.system.modules.path
// options: -source 9

class SourceNoSystemModulesPath { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

// key: compiler.warn.source.no.system.modules.path
// key: compiler.misc.source.no.system.modules.path.with.target
// options: -source 9 -target 9

class SourceNoSystemModulesPath { }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,8 +23,7 @@

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

class TextBlockSource {
String m() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,8 +23,7 @@

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

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

1 comment on commit 65dfcae

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.