Skip to content

Commit

Permalink
Mark RequiredDirective's name as MANDATORY (#3479)
Browse files Browse the repository at this point in the history
- Switch the value of the lazily initialized name to be a `SimpleName`
- Document that `RequiredDirective.setName(Name name)` throws an
  `IllegalArgumentException` when attempting to set the name to `null`

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored Jan 7, 2025
1 parent 8c8f5cd commit 895484c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,39 @@ public class Client {
deleteProject("P2");
}
}
public void testRequiresDirectiveNameIsMandatory() throws JavaModelException, CoreException {
try {
assertTrue(RequiresDirective.NAME_PROPERTY.isMandatory());

IJavaProject project1 = createJavaProject("ASTParserModelTests", new String[] { "src" },
new String[] { "CONVERTER_JCL9_LIB" }, "bin", "9");
project1.open(null);
addClasspathEntry(project1, JavaCore.newContainerEntry(new Path("org.eclipse.jdt.MODULE_PATH")));
String content = """
module first {
requires other;
}
""";
createFile("/ASTParserModelTests/src/module-info.java", content);
this.workingCopy = getCompilationUnit("/ASTParserModelTests/src/module-info.java");

ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
astParser.setSource(this.workingCopy);
astParser.setResolveBindings(true);
astParser.setStatementsRecovery(true);
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(new NullProgressMonitor());
ModuleDeclaration moduleDeclaration = compilationUnit.getModule();
RequiresDirective requiresDirective = (RequiresDirective)moduleDeclaration.moduleStatements().get(0);
try {
requiresDirective.setName(null);
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException e) {
// do nothing
}
} finally {
deleteProject("ASTParserModelTests");
}
}

// Add new tests here
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RequiresDirective extends ModuleDirective {
* The module structural property of this node type (child type: {@link Name}).
*/
public static final ChildPropertyDescriptor NAME_PROPERTY =
new ChildPropertyDescriptor(RequiresDirective.class, "name", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$
new ChildPropertyDescriptor(RequiresDirective.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$

/**
* A list of property descriptors (element type:
Expand Down Expand Up @@ -204,8 +204,7 @@ public Name getName() {
synchronized (this) {
if (this.name == null) {
preLazyInit();
this.name = postLazyInit(
this.ast.newQualifiedName(new SimpleName(this.ast), new SimpleName(this.ast)),
this.name = postLazyInit(new SimpleName(this.ast),
NAME_PROPERTY);
}
}
Expand All @@ -221,6 +220,7 @@ public Name getName() {
* <ul>
* <li>the node belongs to a different AST</li>
* <li>the node already has a parent</li>
* <li>the node is null</li>
* </ul>
*/
public void setName(Name name) {
Expand Down

0 comments on commit 895484c

Please sign in to comment.