Details
-
New Feature
-
Resolution: Won't Fix
-
Major - P3
-
None
-
4.1.1
-
None
Description
I'm evaluating steps required to migrate some legacy code to using of current mongodb java driver with embedded POJO mappgin and have problems with POJOs having '_id' field exposed via 'get_id()' accessor.
I tried adding @BsonId annotation with no help. After digging through sources I've found the problem - @BsonId and @BsonProperty annotations are not used properly during introspection.
The problem is here: https://github.com/mongodb/mongo-java-driver/blob/r4.1.1/bson/src/main/org/bson/codecs/pojo/PropertyReflectionUtils.java#L33
Code excludes all methods that do not follow getters/setters naming conventions for java beans before annotations processing comes into place - this way user has no chance to override non-standard cases via @BsonId and @BsonProperty annotations.
Here is the portion of code I used locally to fix the problem. Please apply if it seems appropriate:
final class PropertyReflectionUtils { |
private PropertyReflectionUtils() {} |
|
|
private static final String IS_PREFIX = "is"; |
private static final String GET_PREFIX = "get"; |
private static final String SET_PREFIX = "set"; |
|
|
static boolean isGetter(final Method method) { |
if (method.getParameterCount() > 0) |
return false; |
|
|
if (isPropertyAccessor(method, GET_PREFIX) || isPropertyAccessor(method, SET_PREFIX)) |
return true; |
|
|
return false; |
}
|
|
|
static boolean isSetter(final Method method) { |
if (method.getParameterCount() != 1) |
return false; |
|
|
if (isPropertyAccessor(method, SET_PREFIX)) |
return true; |
|
|
return false; |
}
|
|
|
static boolean isPropertyAccessor(Method method, String prefix) { |
String name = method.getName();
|
return name.length() > prefix.length() |
&& name.startsWith(prefix)
|
&& (Character.isUpperCase(name.charAt(prefix.length()))
|
|| method.isAnnotationPresent(BsonId.class) |
|| method.isAnnotationPresent(BsonProperty.class)); |
}
|
|