From f02732738d6b8e360d16d561fcf60871aae8dbd2 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 | 22 ++++------------- firebase-firestore/CHANGELOG.md | 1 - .../firestore/util/CustomClassMapper.java | 24 ++++--------------- 3 files changed, 10 insertions(+), 37 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 8268605c247..0705ff33aaa 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 @@ -38,11 +38,9 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; 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; @@ -497,7 +495,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 +513,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 +552,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 +742,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 28124f343c0..44981553530 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 4054bacb8c8..9852524f51b 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()) { @@ -672,11 +670,8 @@ private static class BeanMapper { Method existingSetter = setters.get(propertyName); Method correspondingBridgeMethod = bridgeMethods.get(propertyName); if (existingSetter == null) { - setters.put(propertyName, method); method.setAccessible(true); - if (method.isAnnotationPresent(Exclude.class)) { - propertyNamesOfExcludedSetters.add(propertyName); - } + setters.put(propertyName, method); 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,7 +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; }