diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/IProblem.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/IProblem.java index eae9c3536e3..6e27b8e594a 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/IProblem.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/IProblem.java @@ -2089,7 +2089,8 @@ public interface IProblem { int UndefinedModuleAddReads = ModuleRelated + 1319; /** @since 3.20 */ int ExportingForeignPackage = ModuleRelated + 1320; - + /** @since 3.40 */ + int IllegalModifierInRequires = ModuleRelated + 1321; /** @since 3.14 */ int DuplicateResource = Internal + 1251; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ModuleDeclaration.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ModuleDeclaration.java index 44f567bafc5..90b85888c49 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ModuleDeclaration.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ModuleDeclaration.java @@ -41,6 +41,7 @@ import org.eclipse.jdt.internal.compiler.lookup.Scope; import org.eclipse.jdt.internal.compiler.lookup.SourceModuleBinding; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; +import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; import org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit; import org.eclipse.jdt.internal.compiler.problem.AbortMethod; @@ -155,8 +156,13 @@ public void resolveModuleDirectives(CompilationUnitScope cuScope) { if (!requiredModules.add(ref.resolvedBinding)) { cuScope.problemReporter().duplicateModuleReference(IProblem.DuplicateRequires, ref.module); } - if (ref.isTransitive()) - requiredTransitiveModules.add(ref.resolvedBinding); + if (ref.modifiers != 0) { + if (!CharOperation.equals(TypeConstants.JAVA_BASE, this.tokens) && + CharOperation.equals(TypeConstants.JAVA_BASE, ref.module.tokens)) { + cuScope.problemReporter().illegalModifierInRequires(ref.module); + } else if (ref.isTransitive()) + requiredTransitiveModules.add(ref.resolvedBinding); + } Collection deps = ref.resolvedBinding.dependencyGraphCollector().get(); if (deps.contains(this.binding)) { cuScope.problemReporter().cyclicModuleDependency(this.binding, ref.module); diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java index d1ac75e2e53..3142a2b03d9 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java @@ -11547,6 +11547,13 @@ public void exportingForeignPackage(PackageVisibilityStatement ref, ModuleBindin ref.pkgRef.sourceStart, ref.pkgRef.sourceEnd); } +public void illegalModifierInRequires(ModuleReference ref) { + this.handle(IProblem.IllegalModifierInRequires, + NoArgument, + new String[] { CharOperation.charToString(ref.moduleName) }, + ref.sourceStart, + ref.sourceEnd); +} public void duplicateModuleReference(int problem, ModuleReference ref) { this.handle(problem, NoArgument, new String[] { CharOperation.charToString(ref.moduleName) }, diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/messages.properties b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/messages.properties index 005270500fc..ad2f182fdd9 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/messages.properties +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/messages.properties @@ -949,7 +949,7 @@ 1318 = Illegal modifier for module {0}; only open is permitted 1319 = {0} cannot be resolved to a module, it is referenced from an add-reads directive 1320 = Cannot export the package {0} which belongs to module {1} - +1321 = Modifiers not allowed in requires statement for ''java.base'' #### Java 9 1351 = Variable resource not allowed here for source level below 9 diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java index f0ecad9241e..519518a03fd 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java @@ -565,7 +565,6 @@ class ProblemAttributes { expectedProblemAttributes.put("IllegalModifierForArgument", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); expectedProblemAttributes.put("IllegalModifierForClass", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); expectedProblemAttributes.put("IllegalModifierForConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); - expectedProblemAttributes.put("StrictfpNotRequired", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("IllegalModifierForEnum", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); expectedProblemAttributes.put("IllegalModifierForEnumConstant", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); expectedProblemAttributes.put("IllegalModifierForEnumConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); @@ -585,6 +584,7 @@ class ProblemAttributes { expectedProblemAttributes.put("IllegalModifierForVariable", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); expectedProblemAttributes.put("IllegalModifiersForElidedType", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("IllegalModifiers", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); + expectedProblemAttributes.put("IllegalModifierInRequires", new ProblemAttributes(CategorizedProblem.CAT_MODULE)); expectedProblemAttributes.put("IllegalParameterNullityRedefinition", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM)); expectedProblemAttributes.put("IllegalPrimitiveOrArrayTypeForEnclosingInstance", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); expectedProblemAttributes.put("IllegalQualifiedEnumConstantLabel", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); @@ -1050,6 +1050,7 @@ class ProblemAttributes { expectedProblemAttributes.put("StaticMethodRequested", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); expectedProblemAttributes.put("StaticMethodShouldBeAccessedStatically", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); expectedProblemAttributes.put("StaticResourceField", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM)); + expectedProblemAttributes.put("StrictfpNotRequired", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("StringConstantIsExceedingUtf8Limit", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("SuperAccessCannotBypassDirectSuper", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); expectedProblemAttributes.put("SuperCallCannotBypassOverride", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); @@ -1699,7 +1700,6 @@ class ProblemAttributes { expectedProblemAttributes.put("IllegalModifierForArgument", SKIP); expectedProblemAttributes.put("IllegalModifierForClass", SKIP); expectedProblemAttributes.put("IllegalModifierForConstructor", SKIP); - expectedProblemAttributes.put("StrictfpNotRequired", SKIP); expectedProblemAttributes.put("IllegalModifierForEnum", SKIP); expectedProblemAttributes.put("IllegalModifierForEnumConstant", SKIP); expectedProblemAttributes.put("IllegalModifierForEnumConstructor", SKIP); @@ -1719,6 +1719,7 @@ class ProblemAttributes { expectedProblemAttributes.put("IllegalModifierForVariable", SKIP); expectedProblemAttributes.put("IllegalModifiersForElidedType", SKIP); expectedProblemAttributes.put("IllegalModifiers", SKIP); + expectedProblemAttributes.put("IllegalModifierInRequires", SKIP); expectedProblemAttributes.put("IllegalParameterNullityRedefinition", SKIP); expectedProblemAttributes.put("IllegalPrimitiveOrArrayTypeForEnclosingInstance", SKIP); expectedProblemAttributes.put("IllegalQualifiedEnumConstantLabel", SKIP); @@ -2187,6 +2188,7 @@ class ProblemAttributes { expectedProblemAttributes.put("StaticMethodRequested", SKIP); expectedProblemAttributes.put("StaticMethodShouldBeAccessedStatically", SKIP); expectedProblemAttributes.put("StaticResourceField", new ProblemAttributes(JavaCore.COMPILER_PB_RECOMMENDED_RESOURCE_MANAGEMENT)); + expectedProblemAttributes.put("StrictfpNotRequired", SKIP); expectedProblemAttributes.put("StringConstantIsExceedingUtf8Limit", SKIP); expectedProblemAttributes.put("SuperAccessCannotBypassDirectSuper", SKIP); expectedProblemAttributes.put("SuperCallCannotBypassOverride", SKIP); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java index d70df1a670d..09f1407f7ff 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java @@ -6068,4 +6068,80 @@ public class AppServiceImpl implements IService { """, outFinal); } + public void testGH3151_1() { + File outputDirectory = new File(OUTPUT_DIR); + Util.flushDirectoryContent(outputDirectory); + String out = "bin"; + String directory = OUTPUT_DIR + File.separator + "src"; + String moduleLoc = directory + File.separator + "mod.one"; + List files = new ArrayList<>(); + writeFileCollecting(files, moduleLoc, "module-info.java", + "module mod.one { \n" + + " requires transitive java.base;\n" + + "}"); + writeFileCollecting(files, moduleLoc + File.separator + "p", "X.java", + "package p;\n" + + "public class X {\n" + + "}"); + + StringBuilder buffer = new StringBuilder(); + buffer.append("-d " + OUTPUT_DIR + File.separator + out ) + .append(" -9 ") + .append(" -classpath \"") + .append(Util.getJavaClassLibsAsString()) + .append("\" ") + .append(" --module-source-path " + "\"" + directory + "\""); + + runNegativeModuleTest(files, + buffer, + "", + """ + ---------- + 1. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/src/mod.one/module-info.java (at line 2) + requires transitive java.base; + ^^^^^^^^^ + Modifiers not allowed in requires statement for 'java.base' + ---------- + 1 problem (1 error) + """, + "option"); + } + public void testGH3151_2() { + File outputDirectory = new File(OUTPUT_DIR); + Util.flushDirectoryContent(outputDirectory); + String out = "bin"; + String directory = OUTPUT_DIR + File.separator + "src"; + String moduleLoc = directory + File.separator + "mod.one"; + List files = new ArrayList<>(); + writeFileCollecting(files, moduleLoc, "module-info.java", + "module mod.one { \n" + + " requires static java.base;\n" + + "}"); + writeFileCollecting(files, moduleLoc + File.separator + "p", "X.java", + "package p;\n" + + "public class X {\n" + + "}"); + + StringBuilder buffer = new StringBuilder(); + buffer.append("-d " + OUTPUT_DIR + File.separator + out ) + .append(" -9 ") + .append(" -classpath \"") + .append(Util.getJavaClassLibsAsString()) + .append("\" ") + .append(" --module-source-path " + "\"" + directory + "\""); + + runNegativeModuleTest(files, + buffer, + "", + """ + ---------- + 1. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/src/mod.one/module-info.java (at line 2) + requires static java.base; + ^^^^^^^^^ + Modifiers not allowed in requires statement for 'java.base' + ---------- + 1 problem (1 error) + """, + "option"); + } }