class A declares multiple JSON fields

前端 未结 8 771
温柔的废话
温柔的废话 2020-12-01 02:21

i have a class A which has some private fields and the same class extends another class B which also has some private fields which are in class A.

public cla         


        
8条回答
  •  心在旅途
    2020-12-01 03:14

    Solution for Kotlin, as suggested @Adrian-Lee, you have to tweak some Null Checks

    class SuperclassExclusionStrategy : ExclusionStrategy {
    
        override fun shouldSkipClass(clazz: Class<*>?): Boolean {
            return false
        }
    
        override fun shouldSkipField(f: FieldAttributes?): Boolean {
            val fieldName = f?.name
            val theClass = f?.declaringClass
    
            return isFieldInSuperclass(theClass, fieldName)
        }
    
        private fun isFieldInSuperclass(subclass: Class<*>?, fieldName: String?): Boolean {
            var superclass: Class<*>? = subclass?.superclass
            var field: Field?
    
            while (superclass != null) {
                field = getField(superclass, fieldName)
    
                if (field != null)
                    return true
    
                superclass = superclass.superclass
            }
    
            return false
        }
    
        private fun getField(theClass: Class<*>, fieldName: String?): Field? {
            return try {
                theClass.getDeclaredField(fieldName)
            } catch (e: Exception) {
                null
            }
    
        }
    }
    

提交回复
热议问题