1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-06-24 12:34:52 +02:00

Log LazyStringBuilder instances throwing a NPE

This commit is contained in:
Florian Schmaus 2017-04-19 20:09:32 +02:00
parent 24f6d86452
commit b51d5070fc

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Florian Schmaus * Copyright 2014-2017 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,9 +19,13 @@ package org.jivesoftware.smack.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LazyStringBuilder implements Appendable, CharSequence { public class LazyStringBuilder implements Appendable, CharSequence {
private static final Logger LOGGER = Logger.getLogger(LazyStringBuilder.class.getName());
private final List<CharSequence> list; private final List<CharSequence> list;
private String cache; private String cache;
@ -69,8 +73,15 @@ public class LazyStringBuilder implements Appendable, CharSequence {
return cache.length(); return cache.length();
} }
int length = 0; int length = 0;
for (CharSequence csq : list) { try {
length += csq.length(); for (CharSequence csq : list) {
length += csq.length();
}
}
catch (NullPointerException npe) {
StringBuilder sb = safeToStringBuilder();
LOGGER.log(Level.SEVERE, "The following LazyStringBuilder threw a NullPointerException: " + sb, npe);
throw npe;
} }
return length; return length;
} }
@ -107,6 +118,14 @@ public class LazyStringBuilder implements Appendable, CharSequence {
return cache; return cache;
} }
public StringBuilder safeToStringBuilder() {
StringBuilder sb = new StringBuilder();
for (CharSequence csq : list) {
sb.append(csq);
}
return sb;
}
/** /**
* Get the List of CharSequences representation of this instance. The list is unmodifiable. If * Get the List of CharSequences representation of this instance. The list is unmodifiable. If
* the resulting String was already cached, a list with a single String entry will be returned. * the resulting String was already cached, a list with a single String entry will be returned.