Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.introspect;
  
  import java.util.*;
  
Default com.fasterxml.jackson.databind.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<BeanPropertyDefinition_properties;

    
Details of Object Id to include, if any
 
     protected ObjectIdInfo _objectIdInfo;
     
     // // for deserialization
     
     protected AnnotatedMethod _anySetterMethod;
 
     protected Map<ObjectAnnotatedMember_injectables;
    
    
Set of properties that can be ignored during deserialization, due to being marked as ignored.
 
     protected Set<String_ignoredPropertyNames;
 
     // // for serialization
     
     protected AnnotatedMethod _jsonValueMethod;
 
     protected AnnotatedMember _anyGetter;
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
 
     protected BasicBeanDescription(MapperConfig<?> config,
             JavaType typeAnnotatedClass classDef,
             List<BeanPropertyDefinitionprops)
     {
         super(type);
          = config;
          = (config == null) ? null : config.getAnnotationIntrospector();
          = classDef;
          = props;
     }
     
     {
         this(coll.getConfig(), coll.getType(), coll.getClassDef(), coll.getProperties());
     	 = coll.getObjectIdInfo();
     }

    
Factory method to use for constructing an instance to use for building deserializers.
    {
        BasicBeanDescription desc = new BasicBeanDescription(coll);
        desc._anySetterMethod = coll.getAnySetterMethod();
        desc._ignoredPropertyNames = coll.getIgnoredPropertyNames();
        desc._injectables = coll.getInjectables();
        desc._jsonValueMethod = coll.getJsonValueMethod();
        return desc;
    }

    
Factory method to use for constructing an instance to use for building serializers.
    {
        BasicBeanDescription desc = new BasicBeanDescription(coll);
        desc._jsonValueMethod = coll.getJsonValueMethod();
        desc._anyGetter = coll.getAnyGetter();
        return desc;
    }

    
Factory method to use for constructing an instance to use for purposes other than building serializers or deserializers; will only have information on class, not on properties.
    public static BasicBeanDescription forOtherUse(MapperConfig<?> config,
            JavaType typeAnnotatedClass ac)
    {
        return new BasicBeanDescription(configtype,
                ac, Collections.<BeanPropertyDefinition>emptyList());
    }
    /*
    /**********************************************************
    /* Limited modifications by core databind functionality
    /**********************************************************
     */

    
Method that can be used to prune unwanted properties, during construction of serializers and deserializers. Use with utmost care, if at all...

Since:
2.1
    public boolean removeProperty(String propName)
    {
        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 ; }
    @Override
    public ObjectIdInfo getObjectIdInfo() { return  ; }
    
    @Override
        return ;
    }
    @Override
        return ;
    }
    @Override
    public Set<StringgetIgnoredPropertyNames() {
        if ( == null) {
            return Collections.emptySet();
        }
        return ;
    }
    
    @Override
    public boolean hasKnownClassAnnotations() {
        return .hasAnnotations();
    }
    @Override
    public Annotations getClassAnnotations() {
        return .getAnnotations();
    }
    @Override
    {
        if ( == null) {
             = new TypeBindings(.getTypeFactory(), );
        }
        return ;
    }
    @Override
    public JavaType resolveType(java.lang.reflect.Type jdkType) {
        if (jdkType == null) {
            return null;
        }
        return bindingsForBeanType().resolveType(jdkType);
    }
    @Override
        return .getDefaultConstructor();
    }
    @Override
    {
        if ( != 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 devs to add support within
             *  method.
             */
            Class<?> type = .getRawParameterType(0);
            if (type != String.class && type != Object.class) {
                throw new IllegalArgumentException("Invalid 'any-setter' annotation on method "+.getName()+"(): first argument not of type String or Object, but "+type.getName());
            }
        }
        return ;
    }
    @Override
    public Map<ObjectAnnotatedMemberfindInjectables() {
        return ;
    }
    @Override
        return .getConstructors();
    }
    @Override
    public Object instantiateBean(boolean fixAccess)
    {
        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 Errorthrow (Errort;
            if (t instanceof RuntimeExceptionthrow (RuntimeExceptiont;
            throw new IllegalArgumentException("Failed to instantiate bean of type "+.getAnnotated().getName()+": ("+t.getClass().getName()+") "+t.getMessage(), t);
        }
    }
    
    /*
    /**********************************************************
    /* Simple accessors, extended
    /**********************************************************
     */
    @Override
    public AnnotatedMethod findMethod(String nameClass<?>[] paramTypes) {
        return .findMethod(nameparamTypes);
    }
    /*
    /**********************************************************
    /* General per-class annotation introspection
    /**********************************************************
     */
    @Override
    {
        if ( != null) {
            if (v != null) {
                return v;
            }
        }
        return defValue;
    }
    
    /*
    /**********************************************************
    /* Introspection for serialization, factories
    /**********************************************************
     */
    @Override
    {
        // must filter out anything that clearly is not a factory method
        List<AnnotatedMethodcandidates = .getStaticMethods();
        if (candidates.isEmpty()) {
            return candidates;
        }
        ArrayList<AnnotatedMethodresult = new ArrayList<AnnotatedMethod>();
        for (AnnotatedMethod am : candidates) {
            if (isFactoryMethod(am)) {
                result.add(am);
            }
        }
        return result;
    }
    @Override
    public Constructor<?> findSingleArgConstructor(Class<?>... argTypes)
    {
        for (AnnotatedConstructor ac : .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 : .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 compatible 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)
         */
            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 defined, 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.
    {
        List<Stringnames = null;
        for (int i = 0; i < 2; ++i) {
            List<? extends AnnotatedWithParamsl = (i == 0)
                ? getConstructors() : getFactoryMethods();
            for (AnnotatedWithParams creator : l) {
                int argCount = creator.getParameterCount();
                if (argCount < 1) continue;
                PropertyName name = .findNameForDeserialization(creator.getParameter(0));
                if (name == null) {
                    continue;
                }
                if (names == null) {
                    names = new ArrayList<String>();
                }
                names.add(name.getSimpleName());
                for (int p = 1; p < argCount; ++p) {
                    name = .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 determining 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
    {
        if ( == null) {
            return defValue;
        }
    }
    
    
Method used to locate the method of introspected class that implements com.fasterxml.jackson.annotation.JsonAnyGetter. If no such method exists null is returned. If more than one are found, an exception is thrown.
    @Override
    {
        if ( != null) {
            /* For now let's require a Map; in future can add support for other
             * types like perhaps Iterable<Map.Entry>?
             */
            Class<?> type = .getRawType();
            if (!Map.class.isAssignableFrom(type)) {
                throw new IllegalArgumentException("Invalid 'any-getter' annotation on method "+.getName()+"(): return type is not instance of java.util.Map");
            }
        }
        return ;
    }
    
    @Override
    {
        HashMap<String,AnnotatedMemberresult = null;
        for (BeanPropertyDefinition property : ) {
            AnnotatedMember am = property.getMutator();
            if (am == null) {
                continue;
            }
            if (refDef != null && refDef.isBackReference()) {
                if (result == null) {
                    result = new HashMap<String,AnnotatedMember>();
                }
                String refName = refDef.getName();
                if (result.put(refNameam) != null) {
                    throw new IllegalArgumentException("Multiple back-reference properties with name '"+refName+"'");
                }
            }
        }
        return result;
    }
    @Override
    public Class<?> findPOJOBuilder()
    {
    	return ( == null) ?
    }
    @Override
    {
        return ( == null) ?
                null : .findPOJOBuilderConfig();
    }
    
    /*
    /**********************************************************
    /* Helper methods for field introspection
    /**********************************************************
     */

    

Parameters:
ignoredProperties (optional) names of properties to ignore; any fields that would be recognized as one of these properties is ignored.
forSerialization If true, will collect serializable property fields; if false, deserializable
Returns:
Ordered Map with logical property name as key, and matching field as value.
            Collection<StringignoredPropertiesboolean forSerialization)
    {
        for (BeanPropertyDefinition property : ) {
            AnnotatedField f = property.getField();
            if (f != null) {
                String name = property.getName();
                if (ignoredProperties != null) {
                    if (ignoredProperties.contains(name)) {
                        continue;
                    }
                }
                results.put(namef);
            }
        }
        return results;
    }
New to GrepCode? Check out our FAQ X