Skip to content

Add synchronization around use of JDT code formatter to prevent NPE/race condition #6079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavaV2-e335804.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java V2",
"contributor": "",
"description": "Add synchronization around use of JDT code formatter to prevent NPE/race condition during code generation."
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class JavaCodeFormatter implements CodeTransformer {

private static final Map<String, Object> DEFAULT_FORMATTER_OPTIONS;
private static final Object lock = new Object();

static {
DEFAULT_FORMATTER_OPTIONS = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
Expand Down Expand Up @@ -89,10 +90,16 @@ public JavaCodeFormatter(final Map<String, Object> overrideOptions) {

@Override
public String apply(String contents) {
TextEdit edit = codeFormatter.format(
TextEdit edit;
// There is a race condition in the org.eclipse.jdt.internal.formatter.DefaultCodeFormatter in version 3.10.0.
// The static PROBING_SCANNER can have its state changed by multiple threads.
// Synchronize our usage of that class to ensure we don't hit this.
synchronized (lock) {
edit = codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT
| CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
contents.length(), 0, System.lineSeparator());
}

if (edit == null) {
// TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
public class JavaCodeFormatter implements CodeTransformer {

private static final Map<String, Object> DEFAULT_FORMATTER_OPTIONS;
private static final Object lock = new Object();

static {
DEFAULT_FORMATTER_OPTIONS = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
Expand Down Expand Up @@ -90,10 +91,16 @@ public JavaCodeFormatter(final Map<String, Object> overrideOptions) {

@Override
public String apply(String contents) {
TextEdit edit = codeFormatter.format(
TextEdit edit;
// There is a race condition in the org.eclipse.jdt.internal.formatter.DefaultCodeFormatter in version 3.10.0.
// The static PROBING_SCANNER can have its state changed by multiple threads.
// Synchronize our usage of that class to ensure we don't hit this.
synchronized (lock) {
edit = codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT
| CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
contents.length(), 0, Constant.LF);
}

if (edit == null) {
// TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost
Expand Down
Loading