Skip to content

Commit

Permalink
Prevent NPE when javadoc contains '@Formatter:off'
Browse files Browse the repository at this point in the history
Update `JavadocLineBreakPreparator` to protect against a `null` return
from `commentToken.getInternalStructure()`.

Fixes gh-410
  • Loading branch information
philwebb committed May 14, 2024
1 parent ad9c60a commit 8555da3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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.
* @formatter:off
* @formatter:on
*/
public class Format {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 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.
* @formatter:off
* @formatter:on
*/
public class Format {
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ private static class Vistor extends ASTVisitor {
public boolean visit(Javadoc node) {
int commentIndex = this.tokenManager.firstIndexIn(node, TerminalTokens.TokenNameCOMMENT_JAVADOC);
Token commentToken = this.tokenManager.get(commentIndex);
this.commentTokenManager = new TokenManager(commentToken.getInternalStructure(), this.tokenManager);
this.commentTokenManager = (commentToken.getInternalStructure() != null)
? new TokenManager(commentToken.getInternalStructure(), this.tokenManager)
: null;
this.declaration = node.getParent();
this.firstTagElement = true;
this.hasText = false;
Expand All @@ -108,7 +110,7 @@ public boolean visit(TextElement node) {

@Override
public boolean visit(TagElement node) {
if (isSquashRequired(node, this.declaration)) {
if (this.commentTokenManager != null && isSquashRequired(node, this.declaration)) {
int startIndex = this.commentTokenManager.findIndex(node.getStartPosition(), -1, false);
Token token = this.commentTokenManager.get(startIndex);
token.clearLineBreaksBefore();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2023 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,7 +93,9 @@ private static class Vistor extends ASTVisitor {
public boolean visit(Javadoc node) {
int commentIndex = this.tokenManager.firstIndexIn(node, TerminalTokens.TokenNameCOMMENT_JAVADOC);
Token commentToken = this.tokenManager.get(commentIndex);
this.commentTokenManager = new TokenManager(commentToken.getInternalStructure(), this.tokenManager);
this.commentTokenManager = (commentToken.getInternalStructure() != null)
? new TokenManager(commentToken.getInternalStructure(), this.tokenManager)
: null;
this.declaration = node.getParent();
this.firstTagElement = true;
this.hasText = false;
Expand All @@ -108,7 +110,7 @@ public boolean visit(TextElement node) {

@Override
public boolean visit(TagElement node) {
if (isSquashRequired(node, this.declaration)) {
if (this.commentTokenManager != null && isSquashRequired(node, this.declaration)) {
int startIndex = this.commentTokenManager.findIndex(node.getStartPosition(), -1, false);
Token token = this.commentTokenManager.get(startIndex);
token.clearLineBreaksBefore();
Expand Down

0 comments on commit 8555da3

Please sign in to comment.