Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind;
  
  
Base class for type token classes used both to contain information and as keys for deserializers.

Instances can (only) be constructed by com.fasterxml.jackson.databind.TypeFactory (included in "jackson-databind")

 
 public abstract class JavaType
     extends ResolvedType
     implements java.io.Serializable
 {
     // for 2.1.0:
     private static final long serialVersionUID = -5321897246493723158L;

    
This is the nominal type-erased Class that would be close to the type represented (but not exactly type, due to type erasure: type instance may have more information on this). May be an interface or abstract class, so instantiation may not be possible.
 
     protected final Class<?> _class;
 
     protected final int _hashCode;

    
Optional handler (codec) that can be attached to indicate what to use for handling (serializing, deserializing) values of this specific type.

Note: untyped (i.e. caller has to cast) because it is used for different kinds of handlers, with unrelated types.

 
     protected final Object _valueHandler;

    
Optional handler that can be attached to indicate how to handle additional type metadata associated with this type.

Note: untyped (i.e. caller has to cast) because it is used for different kinds of handlers, with unrelated types.

 
     protected final Object _typeHandler;
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */

    

Parameters:
raw "Raw" (type-erased) class for this type
additionalHash Additional hash code to use, in addition to hash code of the class name
 
     protected JavaType(Class<?> rawint additionalHash,
             Object valueHandlerObject typeHandler)
     {
          = raw;
          = raw.getName().hashCode() + additionalHash;
          = valueHandler;
          = typeHandler;
     }

    
Serialization method called when no additional type information is to be included in serialization.
 
    
    
"Copy method" that will construct a new instance that is identical to this instance, except that it will have specified type handler assigned.

Returns:
Newly created type instance
 
     public abstract JavaType withTypeHandler(Object h);

    
"Copy method" that will construct a new instance that is identical to this instance, except that its content type will have specified type handler assigned.

Returns:
Newly created type instance
 
     public abstract JavaType withContentTypeHandler(Object h);

    
"Copy method" that will construct a new instance that is identical to this instance, except that it will have specified value handler assigned.

Returns:
Newly created type instance
 
    public abstract JavaType withValueHandler(Object h);

    
"Copy method" that will construct a new instance that is identical to this instance, except that it will have specified content value handler assigned.

Returns:
Newly created type instance
    public abstract JavaType withContentValueHandler(Object h);
    
    /*
    /**********************************************************
    /* Type coercion fluent factory methods
    /**********************************************************
     */
    
    
Method that can be called to do a "narrowing" conversions; that is, to return a type with a raw class that is assignable to the raw class of this type. If this is not possible, an java.lang.IllegalArgumentException is thrown. If class is same as the current raw class, instance itself is returned.
    public JavaType narrowBy(Class<?> subclass)
    {
        // First: if same raw class, just return this instance
        if (subclass == ) {
            return this;
        }
        // Otherwise, ensure compatibility
        _assertSubclass(subclass);
        JavaType result = _narrow(subclass);
        // TODO: these checks should NOT actually be needed; above should suffice:
        if ( != result.<Object>getValueHandler()) {
            result = result.withValueHandler();
        }
        if ( != result.<Object>getTypeHandler()) {
            result = result.withTypeHandler();
        }
        return result;
    }

    
More efficient version of narrowBy(java.lang.Class), called by internal framework in cases where compatibility checks are to be skipped.
    public JavaType forcedNarrowBy(Class<?> subclass)
    {
        if (subclass == ) { // can still optimize for simple case
            return this;
        }
        JavaType result = _narrow(subclass);
        // TODO: these checks should NOT actually be needed; above should suffice:
        if ( != result.<Object>getValueHandler()) {
            result = result.withValueHandler();
        }
        if ( != result.<Object>getTypeHandler()) {
            result = result.withTypeHandler();
        }
        return result;
    }

    
Method that can be called to do a "widening" conversions; that is, to return a type with a raw class that could be assigned from this type. If such conversion is not possible, an java.lang.IllegalArgumentException is thrown. If class is same as the current raw class, instance itself is returned.
    public JavaType widenBy(Class<?> superclass)
    {
        // First: if same raw class, just return this instance
        if (superclass == ) {
            return this;
        }
        // Otherwise, ensure compatibility
        _assertSubclass(superclass);
        return _widen(superclass);
    }
    protected abstract JavaType _narrow(Class<?> subclass);

    

Default implementation is just to call _narrow(java.lang.Class), since underlying type construction is usually identical

    protected JavaType _widen(Class<?> superclass) {
        return _narrow(superclass);
    }
    public abstract JavaType narrowContentsBy(Class<?> contentClass);
    public abstract JavaType widenContentsBy(Class<?> contentClass);
    
    /*
    /**********************************************************
    /* Implementation of ResolvedType API
    /**********************************************************
     */
    @Override
    public final Class<?> getRawClass() { return ; }

    
Method that can be used to check whether this type has specified Class as its type erasure. Put another way, returns true if instantiation of this Type is given (type-erased) Class.
    @Override
    public final boolean hasRawClass(Class<?> clz) {
        return  == clz;
    }
    @Override
    public boolean isAbstract() {
        return Modifier.isAbstract(.getModifiers());
    }

    
Convenience method for checking whether underlying Java type is a concrete class or not: abstract classes and interfaces are not.
    @Override
    public boolean isConcrete() {
        int mod = .getModifiers();
        if ((mod & (. | .)) == 0) {
            return true;
        }
        /* 19-Feb-2010, tatus: Holy mackarel; primitive types
         *    have 'abstract' flag set...
         */
        if (.isPrimitive()) {
            return true;
        }
        return false;
    }
    @Override
    public boolean isThrowable() {
        return Throwable.class.isAssignableFrom();
    }
    @Override
    public boolean isArrayType() { return false; }
    @Override
    public final boolean isEnumType() { return .isEnum(); }
    @Override
    public final boolean isInterface() { return .isInterface(); }
    @Override
    public final boolean isPrimitive() { return .isPrimitive(); }
    @Override
    public final boolean isFinal() { return Modifier.isFinal(.getModifiers()); }

    

Returns:
True if type represented is a container type; this includes array, Map and Collection types.
    @Override
    public abstract boolean isContainerType();

    

Returns:
True if type is either true java.util.Collection type, or something similar (meaning it has at least one type parameter, which describes type of contents)
    @Override
    public boolean isCollectionLikeType() { return false; }

    

Returns:
True if type is either true java.util.Map type, or something similar (meaning it has at least two type parameter; first one describing key type, second value type)
    @Override
    public boolean isMapLikeType() { return false; }
    
    /*
    /**********************************************************
    /* Public API, type parameter access; pass-through
    /**********************************************************
     */
    @Override
    public boolean hasGenericTypes()
    {
        return containedTypeCount() > 0;
    }
    @Override
    public JavaType getKeyType() { return null; }
    @Override
    public JavaType getContentType() { return null; }
    @Override
    public int containedTypeCount() { return 0; }
    @Override
    public JavaType containedType(int index) { return null; }
    @Override
    public String containedTypeName(int index) { return null; }
    /*
    /**********************************************************
    /* Semi-public API, accessing handlers
    /**********************************************************
     */
    
    
Method for accessing value handler associated with this type, if any
    @SuppressWarnings("unchecked")
    public <T> T getValueHandler() { return (T) ; }

    
Method for accessing type handler associated with this type, if any
    @SuppressWarnings("unchecked")
    public <T> T getTypeHandler() { return (T) ; }
    /*
    /**********************************************************
    /* Support for producing signatures
    /**********************************************************
     */
    
    //public abstract String toCanonical();

    
Method for accessing signature that contains generic type information, in form compatible with JVM 1.5 as per JLS. It is a superset of getErasedSignature(), in that generic information can be automatically removed if necessary (just remove outermost angle brackets along with content inside)
    public String getGenericSignature() {
        StringBuilder sb = new StringBuilder(40);
        getGenericSignature(sb);
        return sb.toString();        
    }

    

Parameters:
sb StringBuilder to append signature to
Returns:
StringBuilder that was passed in; returned to allow call chaining
    public abstract StringBuilder getGenericSignature(StringBuilder sb);
    
    
Method for accessing signature without generic type information, in form compatible with all versions of JVM, and specifically used for type descriptions when generating byte code.
    public String getErasedSignature() {
        StringBuilder sb = new StringBuilder(40);
        getErasedSignature(sb);
        return sb.toString();
    }

    
Method for accessing signature without generic type information, in form compatible with all versions of JVM, and specifically used for type descriptions when generating byte code.

Parameters:
sb StringBuilder to append signature to
Returns:
StringBuilder that was passed in; returned to allow call chaining
    public abstract StringBuilder getErasedSignature(StringBuilder sb);
    
    /*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */
    protected void _assertSubclass(Class<?> subclassClass<?> superClass)
    {
        if (!.isAssignableFrom(subclass)) {
            throw new IllegalArgumentException("Class "+subclass.getName()+" is not assignable to "+.getName());
        }
    }
    /*
    /**********************************************************
    /* Standard methods; let's make them abstract to force override
    /**********************************************************
     */
    @Override
    public abstract String toString();
    @Override
    public abstract boolean equals(Object o);
    @Override
    public final int hashCode() { return ; }
New to GrepCode? Check out our FAQ X