Skip to content

Commit

Permalink
Lazy init DefaultOutput, to break class loading circular dependency
Browse files Browse the repository at this point in the history
Fixes #1910

Unfortunately I couldn't find a way to add a testcase for this without changing every test to run as a fork. Verified manually.
  • Loading branch information
jhy committed Mar 2, 2023
1 parent a96ebc9 commit b6f652c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Release 1.16.1 [PENDING]
again, causing errors when fetched.
<https://github.com/jhy/jsoup/issues/1902>

* Bugfix: If the Document.OutputSettings class was initialized, and then Entities.escape(String) called, an NPE may be
thrown due to a class loading circular dependency.
<https://github.com/jhy/jsoup/issues/1910>

Release 1.15.4 [18-Feb-2023]
* Improvement: added the ability to escape CSS selectors (tags, IDs, classes) to match elements that don't follow
regular CSS syntax. For example, to match by classname <p class="one.two">, use document.select("p.one\\.two");
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jsoup/nodes/Entities.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jsoup.parser.CharacterReader;
import org.jsoup.parser.Parser;

import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.charset.CharsetEncoder;
import java.util.Arrays;
Expand All @@ -26,7 +27,6 @@ public class Entities {
static final int codepointRadix = 36;
private static final char[] codeDelims = {',', ';'};
private static final HashMap<String, String> multipoints = new HashMap<>(); // name -> multiple character references
private static final OutputSettings DefaultOutput = new OutputSettings();

public enum EscapeMode {
/**
Expand Down Expand Up @@ -157,8 +157,11 @@ public static String escape(String string, OutputSettings out) {
* @return the escaped string
*/
public static String escape(String string) {
if (DefaultOutput == null)
DefaultOutput = new OutputSettings();
return escape(string, DefaultOutput);
}
private static @Nullable OutputSettings DefaultOutput; // lazy-init, to break circular dependency with OutputSettings

// this method does a lot, but other breakups cause rescanning and stringbuilder generations
static void escape(Appendable accum, String string, OutputSettings out,
Expand Down

0 comments on commit b6f652c

Please sign in to comment.