spring-io / spring-javaformat

Apache License 2.0
795 stars 110 forks source link

Formatter throws NullPointerException when javadoc contains '@formatter:off' #410

Closed ebernard closed 3 months ago

ebernard commented 4 months ago

Hi,

I'm trying to reformat my codebase, and everything seems to be working great, except for one class, for which I get a NullPointerException:

[...]
Caused by: java.lang.NullPointerException
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.TokenManager.size (TokenManager.java:84)
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.TokenManager.findIndex (TokenManager.java:150)
    at io.spring.javaformat.formatter.jdk11.eclipse.JavadocLineBreakPreparator$Vistor.visit (JavadocLineBreakPreparator.java:112)
    at io.spring.javaformat.eclipse.jdt.jdk11.core.dom.TagElement.accept0 (TagElement.java:371)
    at io.spring.javaformat.eclipse.jdt.jdk11.core.dom.ASTNode.accept (ASTNode.java:3214)
    at io.spring.javaformat.eclipse.jdt.jdk11.core.dom.ASTNode.acceptChildren (ASTNode.java:3285)
    at io.spring.javaformat.eclipse.jdt.jdk11.core.dom.Javadoc.accept0 (Javadoc.java:194)
    at io.spring.javaformat.eclipse.jdt.jdk11.core.dom.ASTNode.accept (ASTNode.java:3214)
    at io.spring.javaformat.formatter.jdk11.eclipse.JavadocLineBreakPreparator.apply (JavadocLineBreakPreparator.java:62)
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.ExtendedCodeFormatter.lambda$applyPreparators$1 (ExtendedCodeFormatter.java:112)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept (ForEachOps.java:183)
    at java.util.stream.ReferencePipeline$2$1.accept (ReferencePipeline.java:177)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining (ArrayList.java:1655)
    at java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:484)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:474)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential (ForEachOps.java:150)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential (ForEachOps.java:173)
    at java.util.stream.AbstractPipeline.evaluate (AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach (ReferencePipeline.java:497)
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.ExtendedCodeFormatter.applyPreparators (ExtendedCodeFormatter.java:112)
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.ExtendedCodeFormatter.prepareWraps (ExtendedCodeFormatter.java:106)
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.DefaultCodeFormatter.prepareFormattedCode (DefaultCodeFormatter.java:231)
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.DefaultCodeFormatter.format (DefaultCodeFormatter.java:185)
    at io.spring.javaformat.eclipse.jdt.jdk11.internal.formatter.DefaultCodeFormatter.format (DefaultCodeFormatter.java:167)
    at io.spring.javaformat.formatter.Formatter.format (Formatter.java:127)
    at io.spring.javaformat.formatter.Formatter.format (Formatter.java:121)
    at io.spring.javaformat.formatter.Formatter.format (Formatter.java:98)
    at io.spring.javaformat.formatter.FileFormatter.formatFile (FileFormatter.java:119)
    at io.spring.javaformat.formatter.FileFormatter.lambda$formatFiles$1 (FileFormatter.java:96)
    at java.util.stream.ReferencePipeline$3$1.accept (ReferencePipeline.java:195)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining (ArrayList.java:1655)
    at java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:484)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:474)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential (ForEachOps.java:150)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential (ForEachOps.java:173)
    at java.util.stream.AbstractPipeline.evaluate (AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach (ReferencePipeline.java:497)
    at io.spring.format.maven.ApplyMojo.execute (ApplyMojo.java:54)
    at io.spring.format.maven.FormatMojo.execute (FormatMojo.java:137)
[...]

I am using version 0.0.41.

I attach the offending class (in a TGZ, due to GitHub restrictions). unit.tgz

Thank you.

philwebb commented 3 months ago

Thanks for the sample. The root cause is the @formatter:off comment in the javadoc. The Eclipse formatter takes this as a signal to do no formatting.