Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only remove Throwable if unconsumed by pattern. #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 57 additions & 3 deletions slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java
Expand Up @@ -152,20 +152,74 @@ final public static FormattingTuple format(final String messagePattern, Object a
}


static final Throwable getThrowableCandidate(Object[] argArray) {
/**
* Counts the number of unescaped placeholders in the given messagePattern.
*
* @param messagePattern the message pattern to be analyzed.
* @return the number of unescaped placeholders.
*/
private static int countArgumentPlaceholders(String messagePattern) {
if(messagePattern == null) {
return 0;
}

if(-1 == messagePattern.indexOf(DELIM_START))
{
// Special case: no placeholders at all.

// This is an optimization because charAt checks bounds for every
// single call while indexOf(char) isn't.

// Big messages without placeholders will benefit from this shortcut.

// the result of indexOf can't be used as start index in the loop
// below because it could still be escaped.
return 0;
}

int result = 0;
boolean isEscaped = false;
for(int i = 0; i < messagePattern.length(); i++) {
char curChar = messagePattern.charAt(i);
if(curChar == ESCAPE_CHAR) {
isEscaped = !isEscaped;
}
else if(curChar == DELIM_START) {
if(!isEscaped) {
if(i < messagePattern.length() - 1) {
if(messagePattern.charAt(i + 1) == DELIM_STOP) {
result++;
i++;
}
}
}
isEscaped = false;
}
else {
isEscaped = false;
}
}
return result;
}


public static Throwable extractUnconsumedThrowable(String messagePattern, Object[] argArray) {
if (argArray == null || argArray.length == 0) {
return null;
}

final Object lastEntry = argArray[argArray.length - 1];
if (lastEntry instanceof Throwable) {
return (Throwable) lastEntry;
int argumentCount = countArgumentPlaceholders(messagePattern);
if(argumentCount < argArray.length) {
return (Throwable) lastEntry;
}
}
return null;
}

final public static FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray) {
Throwable throwableCandidate = getThrowableCandidate(argArray);
Throwable throwableCandidate = extractUnconsumedThrowable(messagePattern, argArray);
Object[] args = argArray;
if (throwableCandidate != null) {
args = trimmedCopy(argArray);
Expand Down
Expand Up @@ -317,7 +317,12 @@ public void testArrayThrowable() {
assertEquals(t, ft.getThrowable());

ft = MessageFormatter.arrayFormat("{}{}{}{}", ia);
assertEquals("123{}", ft.getMessage());
assertEquals("123java.lang.Throwable", ft.getMessage());
assertTrue(Arrays.equals(ia, ft.getArgArray()));
assertNull(ft.getThrowable());

ft = MessageFormatter.arrayFormat("{}{}{}", ia);
assertEquals("123", ft.getMessage());
assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
assertEquals(t, ft.getThrowable());

Expand Down