Java tutorial
/* *Copyright 2014 Evan Cleary. * *Licensed under the Apache License, Version 2.0 (the "License"); *you may not use this file except in compliance with the License. *You may obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0 * *Unless required by applicable law or agreed to in writing, software *distributed under the License is distributed on an "AS IS" BASIS, *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *See the License for the specific language governing permissions and *limitations under the License. */ package com.division.jsonrpc.api; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; import java.lang.reflect.Field; /** * Allows for classes to override their superclass' * fields and be properly output by the GSON engine. */ public class SuperclassExclusionStrategy implements ExclusionStrategy { public boolean shouldSkipClass(Class<?> arg0) { return false; } public boolean shouldSkipField(FieldAttributes fieldAttributes) { String fieldName = fieldAttributes.getName(); Class<?> theClass = fieldAttributes.getDeclaringClass(); return isFieldInSuperclass(theClass, fieldName); } private boolean isFieldInSuperclass(Class<?> subclass, String fieldName) { Class<?> superclass = subclass.getSuperclass(); Field field; while (superclass != null) { field = getField(superclass, fieldName); if (field != null) return true; superclass = superclass.getSuperclass(); } return false; } private Field getField(Class<?> theClass, String fieldName) { try { return theClass.getDeclaredField(fieldName); } catch (Exception e) { return null; } } }