Skip to content

Commit e010bee

Browse files
committed
DATAMONGO-2119 - Polishing.
Convert anonymous JSON callback class into a private static one. Use an expressive Pattern constant. Original pull request: #621.
1 parent 07b8e82 commit e010bee

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQuery.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -228,36 +228,8 @@ public String parseAndCollectParameterBindingsFromQueryIntoBindings(String input
228228
String transformedInput = transformQueryAndCollectExpressionParametersIntoBindings(input, bindings);
229229
String parseableInput = makeParameterReferencesParseable(transformedInput);
230230

231-
collectParameterReferencesIntoBindings(bindings, JSON.parse(parseableInput, new JSONCallback() {
232-
233-
/*
234-
* (non-Javadoc)
235-
* @see com.mongodb.util.JSONCallback#objectDone()
236-
*/
237-
@Override
238-
public Object objectDone() {
239-
return exceptionSwallowingStackReducingObjectDone();
240-
}
241-
242-
private Object exceptionSwallowingStackReducingObjectDone/*CauseWeJustNeedTheStructureNotTheActualValue*/() {
243-
244-
Object value;
245-
246-
try {
247-
return super.objectDone();
248-
} catch (PatternSyntaxException e) {
249-
value = Pattern.compile("");
250-
}
251-
252-
if (!isStackEmpty()) {
253-
_put(curName(), value);
254-
} else {
255-
value = !BSON.hasDecodeHooks() ? value : BSON.applyDecodingHooks(value);
256-
setRoot(value);
257-
}
258-
return value;
259-
}
260-
}));
231+
collectParameterReferencesIntoBindings(bindings,
232+
JSON.parse(parseableInput, new LenientPatternDecodingCallback()));
261233

262234
return transformedInput;
263235
}
@@ -392,6 +364,43 @@ private static int getIndexOfExpressionParameter(String input, int position) {
392364
}
393365
}
394366

367+
/**
368+
* {@link JSONCallback} with lenient handling for {@link PatternSyntaxException} falling back to a placeholder
369+
* {@link Pattern} for intermediate query document rendering.
370+
*/
371+
private static class LenientPatternDecodingCallback extends JSONCallback {
372+
373+
private static final Pattern EMPTY_MARKER = Pattern.compile("__Spring_Data_MongoDB_Bind_Marker__");
374+
375+
/*
376+
* (non-Javadoc)
377+
* @see com.mongodb.util.JSONCallback#objectDone()
378+
*/
379+
@Override
380+
public Object objectDone() {
381+
return exceptionSwallowingStackReducingObjectDone();
382+
}
383+
384+
private Object exceptionSwallowingStackReducingObjectDone/*CauseWeJustNeedTheStructureNotTheActualValue*/() {
385+
386+
Object value;
387+
388+
try {
389+
return super.objectDone();
390+
} catch (PatternSyntaxException e) {
391+
value = EMPTY_MARKER;
392+
}
393+
394+
if (!isStackEmpty()) {
395+
_put(curName(), value);
396+
} else {
397+
value = !BSON.hasDecodeHooks() ? value : BSON.applyDecodingHooks(value);
398+
setRoot(value);
399+
}
400+
return value;
401+
}
402+
}
403+
395404
/**
396405
* A generic parameter binding with name or position information.
397406
*

0 commit comments

Comments
 (0)