/*
* Copyright 2012 Harlan Noonkester
*
* 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.couchace.jackson.internal;
import com.couchace.core.api.annotation.CouchAttachment;
import com.couchace.core.api.annotation.CouchRevision;
import com.couchace.core.internal.util.ClassUtil;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.beans.Introspector;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
public class CouchJacksonSerializerIntrospector extends JacksonAnnotationIntrospector {
private static final Log log = LogFactory.getLog(CouchJacksonSerializerIntrospector.class);
// CRITICAL - we need access to the EntityMeta here so we don't have to look for annotations again
@Override
public String[] findPropertiesToIgnore(Annotated ac) {
if (log.isTraceEnabled()) {
log.trace("Finding properties to ignore on: " + ac);
}
// Let super determine base
String[] propertiesToIgnore = super.findPropertiesToIgnore(ac);
if (ac != null && ac.getAnnotated() != null) {
Set<String> ignorePropertySet = new HashSet<>();
for (Method readMethod : ClassUtil.listGetterMethods(ac.getRawType())) {
CouchAttachment couchAttachment = readMethod.getAnnotation(CouchAttachment.class);
if (couchAttachment != null) {
String propertyName = Introspector.decapitalize(readMethod.getName().substring(3));
ignorePropertySet.add(propertyName);
}
CouchRevision couchRevision = readMethod.getAnnotation(CouchRevision.class);
if (couchRevision != null) {
String propertyName = Introspector.decapitalize(readMethod.getName().substring(3));
ignorePropertySet.add(propertyName);
}
}
propertiesToIgnore = ignorePropertySet.toArray(new String[ignorePropertySet.size()]);
}
return propertiesToIgnore;
}
}