Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.introspect;
  
Helper class used for aggregating information about a single potential POJO property.
  
  public class POJOPropertyBuilder
     extends BeanPropertyDefinition
     implements Comparable<POJOPropertyBuilder>
 {
    
Whether property is being composed for serialization (true) or deserialization (false)
 
     protected final boolean _forSerialization;
 
     protected final AnnotationIntrospector _annotationIntrospector;
    
    
External name of logical property; may change with renaming (by new instance being constructed using a new name)
 
     protected final String _name;

    
Original internal name, derived from accessor, of this property. Will not be changed by renaming.
 
     protected final String _internalName;
 
     protected Linked<AnnotatedField_fields;
     
     
     protected Linked<AnnotatedMethod_getters;
 
     protected Linked<AnnotatedMethod_setters;
     
     public POJOPropertyBuilder(String internalName,
             AnnotationIntrospector annotationIntrospectorboolean forSerialization)
     {
          = internalName;
          = internalName;
          = annotationIntrospector;
          = forSerialization;
     }
 
     public POJOPropertyBuilder(POJOPropertyBuilder srcString newName)
     {
          = src._internalName;
          = newName;
          = src._annotationIntrospector;
          = src._fields;
          = src._ctorParameters;
          = src._getters;
          = src._setters;
          = src._forSerialization;
     }
     
     /*
     /**********************************************************
     /* Fluent factory methods
     /**********************************************************
      */
 
     @Override
     public POJOPropertyBuilder withName(String newName) {
         return new POJOPropertyBuilder(thisnewName);
     }
     
     /*
     /**********************************************************
     /* Comparable implementation: sort alphabetically, except
     /* that properties with constructor parameters sorted
     /* before other properties
     /**********************************************************
      */
 
 //    @Override
     public int compareTo(POJOPropertyBuilder other)
     {
         // first, if one has ctor params, that should come first:
         if ( != null) {
             if (other._ctorParameters == null) {
                 return -1;
             }
         } else if (other._ctorParameters != null) {
             return 1;
         }
         /* otherwise sort by external name (including sorting of
          * ctor parameters)
          */
         return getName().compareTo(other.getName());
     }
     
     /*
    /**********************************************************
    /* BeanPropertyDefinition implementation
    /**********************************************************
     */
    @Override
    public String getName() { return ; }
    @Override
    public String getInternalName() { return ; }
    @Override
    public boolean isExplicitlyIncluded() {
        return _anyExplicitNames()
                || _anyExplicitNames()
                || _anyExplicitNames()
                || _anyExplicitNames()
                ;
    }
    
    @Override
    public boolean hasGetter() { return  != null; }
    @Override
    public boolean hasSetter() { return  != null; }
    @Override
    public boolean hasField() { return  != null; }
    @Override
    public boolean hasConstructorParameter() { return  != null; }
    @Override
    public boolean couldSerialize() {
        return ( != null) || ( != null);
    }
    @Override
    public AnnotatedMethod getGetter()
    {
        if ( == null) {
            return null;
        }
        // If multiple, verify that they do not conflict...
        AnnotatedMethod getter = .;
        Linked<AnnotatedMethodnext = .;
        for (; next != nullnext = next.next) {
            /* [JACKSON-255] Allow masking, i.e. report exception only if
             *   declarations in same class, or there's no inheritance relationship
             *   (sibling interfaces etc)
             */
            AnnotatedMethod nextGetter = next.value;
            Class<?> getterClass = getter.getDeclaringClass();
            Class<?> nextClass = nextGetter.getDeclaringClass();
            if (getterClass != nextClass) {
                if (getterClass.isAssignableFrom(nextClass)) { // next is more specific
                    getter = nextGetter;
                    continue;
                }
                if (nextClass.isAssignableFrom(getterClass)) { // getter more specific
                    continue;
                }
            }
            throw new IllegalArgumentException("Conflicting getter definitions for property \""+getName()+"\": "
                    +getter.getFullName()+" vs "+nextGetter.getFullName());
        }
        return getter;
    }
    @Override
    public AnnotatedMethod getSetter()
    {
        if ( == null) {
            return null;
        }
        // If multiple, verify that they do not conflict...
        AnnotatedMethod setter = .;
        Linked<AnnotatedMethodnext = .;
        for (; next != nullnext = next.next) {
            /* [JACKSON-255] Allow masking, i.e. report exception only if
             *   declarations in same class, or there's no inheritance relationship
             *   (sibling interfaces etc)
             */
            AnnotatedMethod nextSetter = next.value;
            Class<?> setterClass = setter.getDeclaringClass();
            Class<?> nextClass = nextSetter.getDeclaringClass();
            if (setterClass != nextClass) {
                if (setterClass.isAssignableFrom(nextClass)) { // next is more specific
                    setter = nextSetter;
                    continue;
                }
                if (nextClass.isAssignableFrom(setterClass)) { // getter more specific
                    continue;
                }
            }
            throw new IllegalArgumentException("Conflicting setter definitions for property \""+getName()+"\": "
                    +setter.getFullName()+" vs "+nextSetter.getFullName());
        }
        return setter;
    }
    @Override
    public AnnotatedField getField()
    {
        if ( == null) {
            return null;
        }
        // If multiple, verify that they do not conflict...
        AnnotatedField field = .;
        Linked<AnnotatedFieldnext = .;
        for (; next != nullnext = next.next) {
            AnnotatedField nextField = next.value;
            Class<?> fieldClass = field.getDeclaringClass();
            Class<?> nextClass = nextField.getDeclaringClass();
            if (fieldClass != nextClass) {
                if (fieldClass.isAssignableFrom(nextClass)) { // next is more specific
                    field = nextField;
                    continue;
                }
                if (nextClass.isAssignableFrom(fieldClass)) { // getter more specific
                    continue;
                }
            }
            throw new IllegalArgumentException("Multiple fields representing property \""+getName()+"\": "
                    +field.getFullName()+" vs "+nextField.getFullName());
        }
        return field;
    }
    @Override
    {
        if ( == null) {
            return null;
        }
        /* Hmmh. Checking for constructor parameters is trickier; for one,
         * we must allow creator and factory method annotations.
         * If this is the case, constructor parameter has the precedence.
         * 
         * So, for now, just try finding the first constructor parameter;
         * if none, first factory method. And don't check for dups, if we must,
         * can start checking for them later on.
         */
        Linked<AnnotatedParametercurr = ;
        do {
            if (curr.value.getOwner() instanceof AnnotatedConstructor) {
                return curr.value;
            }
            curr = curr.next;
        } while (curr != null);
        return .;
    }
    
    @Override
    public AnnotatedMember getAccessor()
    {
        AnnotatedMember m = getGetter();
        if (m == null) {
            m = getField();
        }
        return m;
    }
    @Override
    public AnnotatedMember getMutator()
    {
        if (m == null) {
            m = getSetter();
            if (m == null) {
                m = getField();
            }
        }
        return m;
    }
    
    @Override
    public AnnotatedMember getPrimaryMember() {
        if () {
            return getAccessor();
        }
        return getMutator();
    }
    /*
    /**********************************************************
    /* Implementations of refinement accessors
    /**********************************************************
     */
    
    @Override
    public Class<?>[] findViews() {
        return fromMemberAnnotations(new WithMember<Class<?>[]>() {
            public Class<?>[] withMember(AnnotatedMember member) {
                return .findViews(member);
            }
        });
    }
    @Override
            public AnnotationIntrospector.ReferenceProperty withMember(AnnotatedMember member) {
                return .findReferenceType(member);
            }
        });
    }
    @Override
    public boolean isTypeId() {
        Boolean b = fromMemberAnnotations(new WithMember<Boolean>() {
            public Boolean withMember(AnnotatedMember member) {
                return .isTypeId(member);
            }
        });
        return (b != null) && b.booleanValue();
    }
    @Override
    public boolean isRequired() {
        Boolean b = fromMemberAnnotations(new WithMember<Boolean>() {
            public Boolean withMember(AnnotatedMember member) {
                return .hasRequiredMarker(member);
            }
        });
        return (b != null) && b.booleanValue();
    }
    
    @Override
    public ObjectIdInfo findObjectIdInfo() {
        return fromMemberAnnotations(new WithMember<ObjectIdInfo>() {
            public ObjectIdInfo withMember(AnnotatedMember member) {
                ObjectIdInfo info = .findObjectIdInfo(member);
                if (info != null) {
                    info = .findObjectReferenceInfo(memberinfo);
                }
                return info;
            }
        });
    }
    
    /*
    /**********************************************************
    /* Data aggregation
    /**********************************************************
     */
    
    public void addField(AnnotatedField aString enameboolean visibleboolean ignored) {
         = new Linked<AnnotatedField>(aenamevisibleignored);
    }
    public void addCtor(AnnotatedParameter aString enameboolean visibleboolean ignored) {
         = new Linked<AnnotatedParameter>(aenamevisibleignored);
    }
    public void addGetter(AnnotatedMethod aString enameboolean visibleboolean ignored) {
         = new Linked<AnnotatedMethod>(aenamevisibleignored);
    }
    public void addSetter(AnnotatedMethod aString enameboolean visibleboolean ignored) {
         = new Linked<AnnotatedMethod>(aenamevisibleignored);
    }

    
Method for adding all property members from specified collector into this collector.
    public void addAll(POJOPropertyBuilder src)
    {
         = merge(src._fields);
         = merge(src._ctorParameters);
        merge(src._getters);
         = merge(src._setters);
    }
    private static <T> Linked<T> merge(Linked<T> chain1Linked<T> chain2)
    {
        if (chain1 == null) {
            return chain2;
        }
        if (chain2 == null) {
            return chain1;
        }
        return chain1.append(chain2);
    }
    
    /*
    /**********************************************************
    /* Modifications
    /**********************************************************
     */

    
Method called to remove all entries that are marked as ignored.
    public void removeIgnored()
    {
         = _removeIgnored();
         = _removeIgnored();
         = _removeIgnored();
    }
    public void removeNonVisible()
    {
        /* 21-Aug-2011, tatu: This is tricky part -- if and when allow
         *   non-visible property elements to be "pulled in" by visible
         *   counterparts?
         *   For now, we will only do this to pull in setter or field used
         *   as setter, if an explicit getter is found.
         */
         = _removeNonVisible();
        if ( == null) {
             = _removeNonVisible();
             = _removeNonVisible();
        }
    }

    
Method called to trim unnecessary entries, such as implicit getter if there is an explict one available. This is important for later stages, to avoid unnecessary conflicts.
    public void trimByVisibility()
    {
         = _trimByVisibility();
         = _trimByVisibility();
         = _trimByVisibility();
    }
    @SuppressWarnings("unchecked")
    public void mergeAnnotations(boolean forSerialization)
    {
        if (forSerialization) {
            if ( != null) {
                AnnotationMap ann = _mergeAnnotations(0, );
                 = .withValue(..withAnnotations(ann));
            } else if ( != null) {
                AnnotationMap ann = _mergeAnnotations(0, );
                 = .withValue(..withAnnotations(ann));
            }
        } else {
            if ( != null) {
                AnnotationMap ann = _mergeAnnotations(0, );
            } else if ( != null) {
                AnnotationMap ann = _mergeAnnotations(0, );
                 = .withValue(..withAnnotations(ann));
            } else if ( != null) {
                AnnotationMap ann = _mergeAnnotations(0, );
                 = .withValue(..withAnnotations(ann));
            }
        }
    }
    private AnnotationMap _mergeAnnotations(int indexLinked<? extends AnnotatedMember>... nodes)
    {
        AnnotationMap ann = nodes[index]..getAllAnnotations();
        ++index;
        for (; index < nodes.length; ++index) {
            if (nodes[index] != null) {
              return AnnotationMap.merge(ann_mergeAnnotations(indexnodes));
            }
        }
        return ann;
    }
    
    private <T> Linked<T> _removeIgnored(Linked<T> node)
    {
        if (node == null) {
            return node;
        }
        return node.withoutIgnored();
    }
    private <T> Linked<T> _removeNonVisible(Linked<T> node)
    {
        if (node == null) {
            return node;
        }
        return node.withoutNonVisible();
    }
    private <T> Linked<T> _trimByVisibility(Linked<T> node)
    {
        if (node == null) {
            return node;
        }
        return node.trimByVisibility();
    }
        
    /*
    /**********************************************************
    /* Accessors for aggregate information
    /**********************************************************
     */
    private <T> boolean _anyExplicitNames(Linked<T> n)
    {
        for (; n != nulln = n.next) {
            if (n.explicitName != null && n.explicitName.length() > 0) {
                return true;
            }
        }
        return false;
    }
    public boolean anyVisible() {
        return _anyVisible()
            || _anyVisible()
            || _anyVisible()
            || _anyVisible()
        ;
    }
    private <T> boolean _anyVisible(Linked<T> n)
    {
        for (; n != nulln = n.next) {
            if (n.isVisible) {
                return true;
            }
        }
        return false;
    }
    
    public boolean anyIgnorals() {
        return _anyIgnorals()
            || _anyIgnorals()
            || _anyIgnorals()
            || _anyIgnorals()
        ;
    }
    private <T> boolean _anyIgnorals(Linked<T> n)
    {
        for (; n != nulln = n.next) {
            if (n.isMarkedIgnored) {
                return true;
            }
        }
        return false;
    }

    
Method called to check whether property represented by this collector should be renamed from the implicit name; and also verify that there are no conflicting rename definitions.
    public String findNewName()
    {
        Linked<? extends AnnotatedMemberrenamed = null;
        renamed = findRenamed(renamed);
        renamed = findRenamed(renamed);
        renamed = findRenamed(renamed);
        renamed = findRenamed(renamed);
        return (renamed == null) ? null : renamed.explicitName;
    }
    private Linked<? extends AnnotatedMemberfindRenamed(Linked<? extends AnnotatedMembernode,
            Linked<? extends AnnotatedMemberrenamed)
    {
        for (; node != nullnode = node.next) {
            String explName = node.explicitName;
            if (explName == null) {
                continue;
            }
            // different from default name?
            if (explName.equals()) { // nope, skip
                continue;
            }
            if (renamed == null) {
                renamed = node;
            } else {
                // different from an earlier renaming? problem
                if (!explName.equals(renamed.explicitName)) {
                    throw new IllegalStateException("Conflicting property name definitions: '"
                            +renamed.explicitName+"' (for "+renamed.value+") vs '"
                            +node.explicitName+"' (for "+node.value+")");
                }
            }
        }
        return renamed;
    }
    
    // For trouble-shooting
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("[Property '").append()
          .append("'; ctors: ").append()
          .append(", field(s): ").append()
          .append(", getter(s): ").append()
          .append(", setter(s): ").append()
          ;
        sb.append("]");
        return sb.toString();
    }
    
    /*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */

    
Helper method used for finding annotation values, from accessors relevant to current usage (deserialization, serialization)
    protected <T> T fromMemberAnnotations(WithMember<T> func)
    {
        T result = null;
        if ( != null) {
            if () {
                if ( != null) {
                    result = func.withMember(.);
                }
            } else {
                if ( != null) {
                    result = func.withMember(.);
                }
                if (result == null &&  != null) {
                    result = func.withMember(.);
                }
            }
            if (result == null &&  != null) {
                result = func.withMember(.);
            }
        }
        return result;
    }
    
    /*
    /**********************************************************
    /* Helper classes
    /**********************************************************
     */
    private interface WithMember<T>
    {
        public T withMember(AnnotatedMember member);
    }
    
    
Node used for creating simple linked lists to efficiently store small sets of things.
    private final static class Linked<T>
    {
        public final T value;
        public final Linked<T> next;
        public final String explicitName;
        public final boolean isVisible;
        public final boolean isMarkedIgnored;
        
        public Linked(T vLinked<T> n,
                String explNameboolean visibleboolean ignored)
        {
             = v;
             = n;
            // ensure that we'll never have missing names
            if (explName == null) {
                 = null;
            } else {
                 = (explName.length() == 0) ? null : explName;
            }
             = visible;
             = ignored;
        }
        public Linked<T> withValue(T newValue)
        {
            if (newValue == ) {
                return this;
            }
            return new Linked<T>(newValue);
        }
        
        public Linked<T> withNext(Linked<T> newNext) {
            if (newNext == ) {
                return this;
            }
            return new Linked<T>(newNext);
        }
        
        public Linked<T> withoutIgnored()
        {
            if () {
                return ( == null) ? null : .withoutIgnored();
            }
            if ( != null) {
                Linked<T> newNext = .withoutIgnored();
                if (newNext != ) {
                    return withNext(newNext);
                }
            }
            return this;
        }
        
        public Linked<T> withoutNonVisible()
        {
            Linked<T> newNext = ( == null) ? null : .withoutNonVisible();
            return  ? withNext(newNext) : newNext;
        }

        
Method called to append given node(s) at the end of this node chain.
        private Linked<T> append(Linked<T> appendable
        {
            if ( == null) {
                return withNext(appendable);
            }
            return withNext(.append(appendable));
        }
        
        public Linked<T> trimByVisibility()
        {
            if ( == null) {
                return this;
            }
            Linked<T> newNext = .trimByVisibility();
            if ( != null) { // this already has highest; how about next one?
                if (newNext.explicitName == null) { // next one not, drop it
                    return withNext(null);
                }
                //  both have it, keep
                return withNext(newNext);
            }
            if (newNext.explicitName != null) { // next one has higher, return it...
                return newNext;
            }
            // neither has explicit name; how about visibility?
            if ( == newNext.isVisible) { // same; keep both in current order
                return withNext(newNext);
            }
            return  ? withNext(null) : newNext;
        }
        
        @Override
        public String toString() {
            String msg = .toString()+"[visible="++"]";
            if ( != null) {
                msg = msg + ", "+.toString();
            }
            return msg;
        }
    }
New to GrepCode? Check out our FAQ X