Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.fitbur.fasterxml.jackson.databind.introspect.BasicBeanDescription Maven / Gradle / Ivy
package com.fitbur.fasterxml.jackson.databind.introspect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.*;
import com.fitbur.fasterxml.jackson.annotation.JsonFormat;
import com.fitbur.fasterxml.jackson.annotation.JsonInclude;
import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.fitbur.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fitbur.fasterxml.jackson.databind.type.TypeBindings;
import com.fitbur.fasterxml.jackson.databind.util.Annotations;
/**
* Default {@link BeanDescription} implementation.
* Can theoretically be subclassed to customize
* some aspects of property introspection.
*/
public class BasicBeanDescription extends BeanDescription
{
/*
/**********************************************************
/* General configuration
/**********************************************************
*/
final protected MapperConfig> _config;
final protected AnnotationIntrospector _annotationIntrospector;
/**
* Information collected about the class introspected.
*/
final protected AnnotatedClass _classInfo;
/**
* We may need type bindings for the bean type. If so, we'll
* construct it lazily
*/
protected TypeBindings _bindings;
/*
/**********************************************************
/* Member information
/**********************************************************
*/
/**
* Properties collected for the POJO.
*/
protected final List _properties;
/**
* Details of Object Id to include, if any
*/
protected ObjectIdInfo _objectIdInfo;
// // for com.fitburserialization
protected AnnotatedMethod _anySetterMethod;
protected Map _injectables;
/**
* Set of properties that can be ignored during com.fitburserialization, due
* to being marked as ignored.
*/
protected Set _ignoredPropertyNames;
// // for serialization
protected AnnotatedMethod _jsonValueMethod;
protected AnnotatedMember _anyGetter;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
protected BasicBeanDescription(MapperConfig> config,
JavaType type, AnnotatedClass classDef,
List props)
{
super(type);
_config = config;
_annotationIntrospector = (config == null) ? null : config.getAnnotationIntrospector();
_classInfo = classDef;
_properties = props;
}
protected BasicBeanDescription(POJOPropertiesCollector coll)
{
this(coll.getConfig(), coll.getType(), coll.getClassDef(), coll.getProperties());
_objectIdInfo = coll.getObjectIdInfo();
}
/**
* Factory method to use for constructing an instance to use for building
* com.fitburserializers.
*/
public static BasicBeanDescription forDeserialization(POJOPropertiesCollector coll)
{
BasicBeanDescription com.fitbursc = new BasicBeanDescription(coll);
com.fitbursc._anySetterMethod = coll.getAnySetterMethod();
com.fitbursc._ignoredPropertyNames = coll.getIgnoredPropertyNames();
com.fitbursc._injectables = coll.getInjectables();
com.fitbursc._jsonValueMethod = coll.getJsonValueMethod();
return com.fitbursc;
}
/**
* Factory method to use for constructing an instance to use for building
* serializers.
*/
public static BasicBeanDescription forSerialization(POJOPropertiesCollector coll)
{
BasicBeanDescription com.fitbursc = new BasicBeanDescription(coll);
com.fitbursc._jsonValueMethod = coll.getJsonValueMethod();
com.fitbursc._anyGetter = coll.getAnyGetter();
return com.fitbursc;
}
/**
* Factory method to use for constructing an instance to use for purposes
* other than building serializers or com.fitburserializers; will only have information
* on class, not on properties.
*/
public static BasicBeanDescription forOtherUse(MapperConfig> config,
JavaType type, AnnotatedClass ac)
{
return new BasicBeanDescription(config, type,
ac, Collections.emptyList());
}
/*
/**********************************************************
/* Limited modifications by core databind functionality
/**********************************************************
*/
/**
* Method that can be used to prune unwanted properties, during
* construction of serializers and com.fitburserializers.
* Use with utmost care, if at all...
*
* @since 2.1
*/
public boolean removeProperty(String propName)
{
Iterator it = _properties.iterator();
while (it.hasNext()) {
BeanPropertyDefinition prop = it.next();
if (prop.getName().equals(propName)) {
it.remove();
return true;
}
}
return false;
}
/*
/**********************************************************
/* Simple accessors from BeanDescription
/**********************************************************
*/
@Override
public AnnotatedClass getClassInfo() { return _classInfo; }
@Override
public ObjectIdInfo getObjectIdInfo() { return _objectIdInfo; }
@Override
public List findProperties() {
return _properties;
}
@Override
public AnnotatedMethod findJsonValueMethod() {
return _jsonValueMethod;
}
@Override
public Set getIgnoredPropertyNames() {
if (_ignoredPropertyNames == null) {
return Collections.emptySet();
}
return _ignoredPropertyNames;
}
@Override
public boolean hasKnownClassAnnotations() {
return _classInfo.hasAnnotations();
}
@Override
public Annotations getClassAnnotations() {
return _classInfo.getAnnotations();
}
@Override
public TypeBindings bindingsForBeanType()
{
if (_bindings == null) {
_bindings = new TypeBindings(_config.getTypeFactory(), _type);
}
return _bindings;
}
@Override
public JavaType resolveType(java.lang.reflect.Type jdkType) {
if (jdkType == null) {
return null;
}
return bindingsForBeanType().resolveType(jdkType);
}
@Override
public AnnotatedConstructor findDefaultConstructor() {
return _classInfo.getDefaultConstructor();
}
@Override
public AnnotatedMethod findAnySetter() throws IllegalArgumentException
{
if (_anySetterMethod != null) {
/* Also, let's be somewhat strict on how field name is to be
* passed; String, Object make sense, others not
* so much.
*/
/* !!! 18-May-2009, tatu: how about enums? Can add support if
* requested; easy enough for com.fitburvs to add support within
* method.
*/
Class> type = _anySetterMethod.getRawParameterType(0);
if (type != String.class && type != Object.class) {
throw new IllegalArgumentException("Invalid 'any-setter' annotation on method "+_anySetterMethod.getName()+"(): first argument not of type String or Object, but "+type.getName());
}
}
return _anySetterMethod;
}
@Override
public Map findInjectables() {
return _injectables;
}
@Override
public List getConstructors() {
return _classInfo.getConstructors();
}
@Override
public Object instantiateBean(boolean fixAccess)
{
AnnotatedConstructor ac = _classInfo.getDefaultConstructor();
if (ac == null) {
return null;
}
if (fixAccess) {
ac.fixAccess();
}
try {
return ac.getAnnotated().newInstance();
} catch (Exception e) {
Throwable t = e;
while (t.getCause() != null) {
t = t.getCause();
}
if (t instanceof Error) throw (Error) t;
if (t instanceof RuntimeException) throw (RuntimeException) t;
throw new IllegalArgumentException("Failed to instantiate bean of type "+_classInfo.getAnnotated().getName()+": ("+t.getClass().getName()+") "+t.getMessage(), t);
}
}
/*
/**********************************************************
/* Simple accessors, extended
/**********************************************************
*/
@Override
public AnnotatedMethod findMethod(String name, Class>[] paramTypes) {
return _classInfo.findMethod(name, paramTypes);
}
/*
/**********************************************************
/* General per-class annotation introspection
/**********************************************************
*/
@Override
public JsonFormat.Value findExpectedFormat(JsonFormat.Value com.fitburfValue)
{
if (_annotationIntrospector != null) {
JsonFormat.Value v = _annotationIntrospector.findFormat(_classInfo);
if (v != null) {
return v;
}
}
return com.fitburfValue;
}
/*
/**********************************************************
/* Introspection for serialization, factories
/**********************************************************
*/
@Override
public List getFactoryMethods()
{
// must filter out anything that clearly is not a factory method
List candidates = _classInfo.getStaticMethods();
if (candidates.isEmpty()) {
return candidates;
}
ArrayList result = new ArrayList();
for (AnnotatedMethod am : candidates) {
if (isFactoryMethod(am)) {
result.add(am);
}
}
return result;
}
@Override
public Constructor> findSingleArgConstructor(Class>... argTypes)
{
for (AnnotatedConstructor ac : _classInfo.getConstructors()) {
// This list is already filtered to only include accessible
/* (note: for now this is a redundant check; but in future
* that may change; thus leaving here for now)
*/
if (ac.getParameterCount() == 1) {
Class> actArg = ac.getRawParameterType(0);
for (Class> expArg : argTypes) {
if (expArg == actArg) {
return ac.getAnnotated();
}
}
}
}
return null;
}
@Override
public Method findFactoryMethod(Class>... expArgTypes)
{
// So, of all single-arg static methods:
for (AnnotatedMethod am : _classInfo.getStaticMethods()) {
if (isFactoryMethod(am)) {
// And must take one of expected arg types (or supertype)
Class> actualArgType = am.getRawParameterType(0);
for (Class> expArgType : expArgTypes) {
// And one that matches what we would pass in
if (actualArgType.isAssignableFrom(expArgType)) {
return am.getAnnotated();
}
}
}
}
return null;
}
protected boolean isFactoryMethod(AnnotatedMethod am)
{
/* First: return type must be com.fitburpatible with the introspected class
* (i.e. allowed to be sub-class, although usually is the same
* class)
*/
Class> rt = am.getRawReturnType();
if (!getBeanClass().isAssignableFrom(rt)) {
return false;
}
/* Also: must be a recognized factory method, meaning:
* (a) marked with @JsonCreator annotation, or
* (a) "valueOf" (at this point, need not be public)
*/
if (_annotationIntrospector.hasCreatorAnnotation(am)) {
return true;
}
if ("valueOf".equals(am.getName())) {
return true;
}
return false;
}
/**
* Method for getting ordered list of named Creator properties.
* Returns an empty list is none found. If multiple Creator
* methods are com.fitburfined, order between properties from different
* methods is undefined; however, properties for each such
* Creator are ordered properly relative to each other. For the
* usual case of just a single Creator, named properties are
* thus properly ordered.
*/
public List findCreatorPropertyNames()
{
List names = null;
for (int i = 0; i < 2; ++i) {
List extends AnnotatedWithParams> l = (i == 0)
? getConstructors() : getFactoryMethods();
for (AnnotatedWithParams creator : l) {
int argCount = creator.getParameterCount();
if (argCount < 1) continue;
PropertyName name = _annotationIntrospector.findNameForDeserialization(creator.getParameter(0));
if (name == null) {
continue;
}
if (names == null) {
names = new ArrayList();
}
names.add(name.getSimpleName());
for (int p = 1; p < argCount; ++p) {
name = _annotationIntrospector.findNameForDeserialization(creator.getParameter(p));
names.add((name == null) ? null : name.getSimpleName());
}
}
}
if (names == null) {
return Collections.emptyList();
}
return names;
}
/*
/**********************************************************
/* Introspection for serialization, other
/**********************************************************
*/
/**
* Method for com.fitburtermining whether null properties should be written
* out for a Bean of introspected type. This is based on global
* feature (lowest priority, passed as argument)
* and per-class annotation (highest priority).
*/
@Override
public JsonInclude.Include findSerializationInclusion(JsonInclude.Include com.fitburfValue)
{
if (_annotationIntrospector == null) {
return com.fitburfValue;
}
return _annotationIntrospector.findSerializationInclusion(_classInfo, com.fitburfValue);
}
/**
* Method used to locate the method of introspected class that
* implements {@link com.fitbur.fasterxml.jackson.annotation.JsonAnyGetter}.
* If no such method exists null is returned.
* If more than one are found, an exception is thrown.
*/
@Override
public AnnotatedMember findAnyGetter() throws IllegalArgumentException
{
if (_anyGetter != null) {
/* For now let's require a Map; in future can add support for other
* types like perhaps Iterable?
*/
Class> type = _anyGetter.getRawType();
if (!Map.class.isAssignableFrom(type)) {
throw new IllegalArgumentException("Invalid 'any-getter' annotation on method "+_anyGetter.getName()+"(): return type is not instance of java.util.Map");
}
}
return _anyGetter;
}
@Override
public Map findBackReferenceProperties()
{
HashMap result = null;
for (BeanPropertyDefinition property : _properties) {
AnnotatedMember am = property.getMutator();
if (am == null) {
continue;
}
AnnotationIntrospector.ReferenceProperty refDef = _annotationIntrospector.findReferenceType(am);
if (refDef != null && refDef.isBackReference()) {
if (result == null) {
result = new HashMap();
}
String refName = refDef.getName();
if (result.put(refName, am) != null) {
throw new IllegalArgumentException("Multiple back-reference properties with name '"+refName+"'");
}
}
}
return result;
}
@Override
public Class> findPOJOBuilder()
{
return (_annotationIntrospector == null) ?
null : _annotationIntrospector.findPOJOBuilder(_classInfo);
}
@Override
public JsonPOJOBuilder.Value findPOJOBuilderConfig()
{
return (_annotationIntrospector == null) ?
null : _annotationIntrospector.findPOJOBuilderConfig(_classInfo);
}
/*
/**********************************************************
/* Helper methods for field introspection
/**********************************************************
*/
/**
* @param ignoredProperties (optional) names of properties to ignore;
* any fields that would be recognized as one of these properties
* is ignored.
* @param forSerialization If true, will collect serializable property
* fields; if false, com.fitburserializable
*
* @return Ordered Map with logical property name as key, and
* matching field as value.
*/
public LinkedHashMap _findPropertyFields(
Collection ignoredProperties, boolean forSerialization)
{
LinkedHashMap results = new LinkedHashMap();
for (BeanPropertyDefinition property : _properties) {
AnnotatedField f = property.getField();
if (f != null) {
String name = property.getName();
if (ignoredProperties != null) {
if (ignoredProperties.contains(name)) {
continue;
}
}
results.put(name, f);
}
}
return results;
}
}