From 1b37a0574e503bf89b37256182a2057eaff66342 Mon Sep 17 00:00:00 2001 From: MEHER SRUJANA MATCHA Date: Thu, 13 Aug 2026 16:17:37 +0100 Subject: [PATCH 1/2] #3870 Add setting to default new Java files to package-private visibility --- package.json | 7 +++++++ src/fileEventHandler.ts | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a31fc8a99..fff1f8de0 100644 --- a/package.json +++ b/package.json @@ -1247,6 +1247,13 @@ ], "order": 30 }, + "java.templates.preferPackagePrivateVisibility": { + "type": "boolean", + "markdownDescription": "Specifies whether newly created top-level types (class/interface/record) should be package-private instead of public.", + "scope": "window", + "default": false, + "order": 40 + }, "java.templates.methodBodySuper": { "type": "array", "markdownDescription": "Specifies the method body snippet for overridden methods that call `super` (e.g. generated by \"Override/Implement Methods\"). Supports configuring multi-line content with an array of strings, and using ${variable} to reference the [predefined variables](command:_java.templateVariables).", diff --git a/src/fileEventHandler.ts b/src/fileEventHandler.ts index e2268c5f8..2fa9a35cf 100644 --- a/src/fileEventHandler.ts +++ b/src/fileEventHandler.ts @@ -145,12 +145,14 @@ async function handleNewJavaFiles(e: FileCreateEvent) { } } let declaration: string; + const preferPackagePrivate = getJavaConfiguration().get("templates.preferPackagePrivateVisibility", false); + const visibilityPrefix = preferPackagePrivate ? "" : "public "; if (isModuleInfo) { declaration = `module \${1:name}`; } else if (!serverReady || await isVersionLessThan(emptyFiles[i].toString(), 14)) { - declaration = `public \${1|class,interface,enum,abstract class,@interface|} ${typeName}`; + declaration = `${visibilityPrefix}\${1|class,interface,enum,abstract class,@interface|} ${typeName}`; } else { - declaration = `public \${1|class ${typeName},interface ${typeName},enum ${typeName},record ${typeName}(),abstract class ${typeName},@interface ${typeName}|}`; + declaration = `${visibilityPrefix}\${1|class ${typeName},interface ${typeName},enum ${typeName},record ${typeName}(),abstract class ${typeName},@interface ${typeName}|}`; } let bracePosition = projectSetting[BRACE_POSITION_KEY]; if (bracePosition !== END_OF_LINE && bracePosition !== NEXT_LINE && bracePosition !== NEXT_LINE_SHIFTED) { From 9ed4a4aa53dcefcb0ab72aece5e0a0d98febe6c6 Mon Sep 17 00:00:00 2001 From: MEHER SRUJANA MATCHA Date: Thu, 13 Aug 2026 16:26:26 +0100 Subject: [PATCH 2/2] #3870 Document preferPackagePrivateVisibility setting in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3eaacf920..488ac8885 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,7 @@ The following settings are supported: * `java.project.resourceFilters`: Excludes files and folders from being refreshed by the Java Language Server, which can improve the overall performance. For example, ["node_modules","\.git"] will exclude all files and folders named 'node_modules' or '.git'. Pattern expressions must be compatible with `java.util.regex.Pattern`. Defaults to ["node_modules","\.git"]. * `java.templates.fileHeader`: Specifies the file header comment for new Java file. Supports configuring multi-line comments with an array of strings, and using ${variable} to reference the [predefined variables](https://github.com/redhat-developer/vscode-java/wiki/Predefined-Variables-for-Java-Template-Snippets). * `java.templates.methodBody`: Specifies the method body snippet for unimplemented methods (e.g. generated by "Add unimplemented methods"). Supports configuring multi-line content with an array of strings, and using ${variable} to reference the [predefined variables](https://github.com/redhat-developer/vscode-java/wiki/Predefined-Variables-for-Java-Template-Snippets). +* `java.templates.preferPackagePrivateVisibility`: Specifies whether newly created top-level types (class/interface/enum/record) should be package-private instead of public. Defaults to `false`. * `java.templates.methodBodySuper`: Specifies the method body snippet for overridden methods that call `super` (e.g. generated by "Override/Implement Methods"). Supports configuring multi-line content with an array of strings, and using ${variable} to reference the [predefined variables](https://github.com/redhat-developer/vscode-java/wiki/Predefined-Variables-for-Java-Template-Snippets). * `java.templates.catchBody`: Specifies the catch block body snippet (e.g. generated by "Surround with try/catch"). Supports configuring multi-line content with an array of strings, and using ${variable} to reference the [predefined variables](https://github.com/redhat-developer/vscode-java/wiki/Predefined-Variables-for-Java-Template-Snippets). * `java.templates.typeComment`: Specifies the type comment for new Java type. Supports configuring multi-line comments with an array of strings, and using ${variable} to reference the [predefined variables](https://github.com/redhat-developer/vscode-java/wiki/Predefined-Variables-for-Java-Template-Snippets).