From bb55dd0aa9d73df86dfc4ad80e714b0a31f0d7e8 Mon Sep 17 00:00:00 2001 From: cherylEnkidu Date: Thu, 15 Feb 2024 16:30:27 -0500 Subject: [PATCH] Remove Exclude effect on parent method --- .../utilities/encoding/CustomClassMapper.java | 20 +++++------------- firebase-firestore/CHANGELOG.md | 1 - .../firestore/util/CustomClassMapper.java | 21 ++++--------------- 3 files changed, 9 insertions(+), 33 deletions(-) diff --git a/firebase-database/src/main/java/com/google/firebase/database/core/utilities/encoding/CustomClassMapper.java b/firebase-database/src/main/java/com/google/firebase/database/core/utilities/encoding/CustomClassMapper.java index 8268605c2475..16180bfb1031 100644 --- a/firebase-database/src/main/java/com/google/firebase/database/core/utilities/encoding/CustomClassMapper.java +++ b/firebase-database/src/main/java/com/google/firebase/database/core/utilities/encoding/CustomClassMapper.java @@ -497,7 +497,6 @@ public BeanMapper(Class clazz) { // class hierarchy to find the appropriate setter or field. Class currentClass = clazz; Map bridgeMethods = new HashMap<>(); - Set propertyNamesOfExcludedSetters = new HashSet<>(); do { // Add any setters for (Method method : currentClass.getDeclaredMethods()) { @@ -516,12 +515,8 @@ public BeanMapper(Class clazz) { Method existingSetter = setters.get(propertyName); Method correspondingBridgeMethod = bridgeMethods.get(propertyName); if (existingSetter == null) { + method.setAccessible(true); setters.put(propertyName, method); - if (method.isAnnotationPresent(Exclude.class)) { - propertyNamesOfExcludedSetters.add(propertyName); - } else { - method.setAccessible(true); - } } else if (!isSetterOverride(method, existingSetter) && !(correspondingBridgeMethod != null && isSetterOverride(method, correspondingBridgeMethod))) { @@ -559,14 +554,6 @@ && isSetterOverride(method, correspondingBridgeMethod))) { currentClass = currentClass.getSuperclass(); } while (currentClass != null && !currentClass.equals(Object.class)); - // When subclass setter is annotated with `@Exclude`, the corresponding superclass setter - // also need to be filtered out. - for (String propertyName : propertyNamesOfExcludedSetters) { - Method superclassSetter = setters.get(propertyName); - superclassSetter.setAccessible(false); - setters.remove(propertyName); - } - if (properties.isEmpty()) { throw new DatabaseException("No properties to serialize found on class " + clazz.getName()); } @@ -757,7 +744,10 @@ private static boolean shouldIncludeSetter(Method method) { if (method.getParameterTypes().length != 1) { return false; } - + // Excluded methods + if (method.isAnnotationPresent(Exclude.class)) { + return false; + } return true; } diff --git a/firebase-firestore/CHANGELOG.md b/firebase-firestore/CHANGELOG.md index 28124f343c05..44981553530b 100644 --- a/firebase-firestore/CHANGELOG.md +++ b/firebase-firestore/CHANGELOG.md @@ -890,4 +890,3 @@ updates. or [`FieldValue.serverTimestamp()`](/docs/reference/android/com/google/firebase/firestore/FieldValue.html#serverTimestamp()) values. - diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java index 4054bacb8c83..10a4e2e4216d 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java @@ -50,7 +50,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -650,7 +649,6 @@ private static class BeanMapper { // class hierarchy to find the appropriate setter or field. Class currentClass = clazz; Map bridgeMethods = new HashMap<>(); - Set propertyNamesOfExcludedSetters = new HashSet<>(); do { // Add any setters for (Method method : currentClass.getDeclaredMethods()) { @@ -674,9 +672,6 @@ private static class BeanMapper { if (existingSetter == null) { setters.put(propertyName, method); method.setAccessible(true); - if (method.isAnnotationPresent(Exclude.class)) { - propertyNamesOfExcludedSetters.add(propertyName); - } applySetterAnnotations(method); } else if (!isSetterOverride(method, existingSetter) && !(correspondingBridgeMethod != null @@ -725,14 +720,6 @@ && isSetterOverride(method, correspondingBridgeMethod))) { currentClass = currentClass.getSuperclass(); } while (currentClass != null && !currentClass.equals(Object.class)); - // When subclass setter is annotated with `@Exclude`, the corresponding superclass setter - // also need to be filtered out. - for (String propertyName : propertyNamesOfExcludedSetters) { - Method superclassSetter = setters.get(propertyName); - superclassSetter.setAccessible(false); - setters.remove(propertyName); - } - if (properties.isEmpty()) { throw new RuntimeException("No properties to serialize found on class " + clazz.getName()); } @@ -981,10 +968,6 @@ private void applySetterAnnotations(Method method) { + " only be applied to fields and getters, not setters."); } - if (method.isAnnotationPresent(Exclude.class)) { - method.setAccessible(false); - } - if (method.isAnnotationPresent(DocumentId.class)) { Class paramType = method.getParameterTypes()[0]; ensureValidDocumentIdType("Method", "accepts", paramType); @@ -1059,6 +1042,10 @@ private static boolean shouldIncludeSetter(Method method) { if (method.getParameterTypes().length != 1) { return false; } + // Excluded methods + if (method.isAnnotationPresent(Exclude.class)) { + return false; + } return true; }