All Downloads are FREE. Search and download functionalities are using the official Maven repository.

framework.src.org.checkerframework.framework.type.HashcodeAtmVisitor Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java’s type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0
Show newest version
package org.checkerframework.framework.type;

import org.checkerframework.framework.type.visitor.AnnotatedTypeScanner;

/**
 * Computes the hashcode of an AnnotatedTypeMirror using the underlying type and
 * primary annotations of the type and its component type.
 *
 * This class should be synchronized with EqualityAtmComparer.
 * @see org.checkerframework.framework.type.EqualityAtmComparer
 * for more details.
 *
 * This is used by AnnotatedTypeMirror.hashcode.
 */
public class HashcodeAtmVisitor extends AnnotatedTypeScanner {

    /**
     * Generates the hashcode of type and combines it with the hashcode of its
     * component types (if any).
     */
    @Override
    protected Integer scan(AnnotatedTypeMirror type, Void v) {
        return reduce(super.scan(type, null), generateHashcode(type));
    }

    /**
     * Used to combine the hashcodes of component types or a type and its component types
     */
    @Override
    protected Integer reduce(Integer hashcode1, Integer hashcode2) {
        if (hashcode1 == null) {
            return hashcode2;
        }

        if (hashcode2 == null) {
            return hashcode1;
        }

        return hashcode1 + hashcode2;
    }

    /**
     * Generates hashcode for type using the underlying type and the primary
     * annotation.  This method does not descend into component types (this occurs in the
     * scan method)
     * @param type the type
     */
    private Integer generateHashcode(AnnotatedTypeMirror type) {
        // To differentiate between partially initialized type's (which may have null components)
        // and fully initialized types, null values are allowed
        if (type == null) {
            return null;
        }

        return type.getAnnotations().toString().hashCode() * 17
             + type.getUnderlyingType().toString().hashCode() * 13;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy