Skip to content

Commit 8555da3

Browse files
committed
Prevent NPE when javadoc contains '@Formatter:off'
Update `JavadocLineBreakPreparator` to protect against a `null` return from `commentToken.getInternalStructure()`. Fixes gh-410
1 parent ad9c60a commit 8555da3

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
3+
* @formatter:off
4+
* @formatter:on
5+
*/
6+
public class Format {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
3+
* @formatter:off
4+
* @formatter:on
5+
*/
6+
public class Format {
7+
}

Diff for: spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk17/eclipse/JavadocLineBreakPreparator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ private static class Vistor extends ASTVisitor {
9393
public boolean visit(Javadoc node) {
9494
int commentIndex = this.tokenManager.firstIndexIn(node, TerminalTokens.TokenNameCOMMENT_JAVADOC);
9595
Token commentToken = this.tokenManager.get(commentIndex);
96-
this.commentTokenManager = new TokenManager(commentToken.getInternalStructure(), this.tokenManager);
96+
this.commentTokenManager = (commentToken.getInternalStructure() != null)
97+
? new TokenManager(commentToken.getInternalStructure(), this.tokenManager)
98+
: null;
9799
this.declaration = node.getParent();
98100
this.firstTagElement = true;
99101
this.hasText = false;
@@ -108,7 +110,7 @@ public boolean visit(TextElement node) {
108110

109111
@Override
110112
public boolean visit(TagElement node) {
111-
if (isSquashRequired(node, this.declaration)) {
113+
if (this.commentTokenManager != null && isSquashRequired(node, this.declaration)) {
112114
int startIndex = this.commentTokenManager.findIndex(node.getStartPosition(), -1, false);
113115
Token token = this.commentTokenManager.get(startIndex);
114116
token.clearLineBreaksBefore();

Diff for: spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk8/eclipse/JavadocLineBreakPreparator.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,7 +93,9 @@ private static class Vistor extends ASTVisitor {
9393
public boolean visit(Javadoc node) {
9494
int commentIndex = this.tokenManager.firstIndexIn(node, TerminalTokens.TokenNameCOMMENT_JAVADOC);
9595
Token commentToken = this.tokenManager.get(commentIndex);
96-
this.commentTokenManager = new TokenManager(commentToken.getInternalStructure(), this.tokenManager);
96+
this.commentTokenManager = (commentToken.getInternalStructure() != null)
97+
? new TokenManager(commentToken.getInternalStructure(), this.tokenManager)
98+
: null;
9799
this.declaration = node.getParent();
98100
this.firstTagElement = true;
99101
this.hasText = false;
@@ -108,7 +110,7 @@ public boolean visit(TextElement node) {
108110

109111
@Override
110112
public boolean visit(TagElement node) {
111-
if (isSquashRequired(node, this.declaration)) {
113+
if (this.commentTokenManager != null && isSquashRequired(node, this.declaration)) {
112114
int startIndex = this.commentTokenManager.findIndex(node.getStartPosition(), -1, false);
113115
Token token = this.commentTokenManager.get(startIndex);
114116
token.clearLineBreaksBefore();

0 commit comments

Comments
 (0)