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

org.checkerframework.common.reflection.ClassValVisitor 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-eisop4
Show newest version
package org.checkerframework.common.reflection;

import com.sun.source.tree.Tree;

import org.checkerframework.common.basetype.BaseTypeChecker;
import org.checkerframework.common.basetype.BaseTypeValidator;
import org.checkerframework.common.basetype.BaseTypeVisitor;
import org.checkerframework.common.reflection.qual.ClassBound;
import org.checkerframework.common.reflection.qual.ClassVal;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType;
import org.plumelib.reflection.Signatures;

import java.util.List;

import javax.lang.model.element.AnnotationMirror;

/** A visitor to verify validity of {@code @}{@link ClassVal} annotations. */
public class ClassValVisitor extends BaseTypeVisitor {
    /**
     * Create a new ClassValVisitor.
     *
     * @param checker the associated type-checker
     */
    public ClassValVisitor(BaseTypeChecker checker) {
        super(checker);
    }

    @Override
    protected ClassValAnnotatedTypeFactory createTypeFactory() {
        return new ClassValAnnotatedTypeFactory(checker);
    }

    @Override
    protected BaseTypeValidator createTypeValidator() {
        return new ClassNameValidator(checker, this, atypeFactory);
    }
}

class ClassNameValidator extends BaseTypeValidator {

    public ClassNameValidator(
            BaseTypeChecker checker,
            BaseTypeVisitor visitor,
            AnnotatedTypeFactory atypeFactory) {
        super(checker, visitor, atypeFactory);
    }

    /**
     * This implementation reports an "illegal.classname" error if the type contains a @ClassVal
     * annotation with a string that is not a valid class name.
     */
    @Override
    public Void visitDeclared(AnnotatedDeclaredType type, Tree tree) {
        AnnotationMirror classVal = type.getAnnotation(ClassVal.class);
        classVal = classVal == null ? type.getAnnotation(ClassBound.class) : classVal;
        if (classVal != null) {
            List classNames =
                    ((ClassValAnnotatedTypeFactory) atypeFactory)
                            .getClassNamesFromAnnotation(classVal);
            for (String className : classNames) {
                if (!Signatures.isFqBinaryName(className)) {
                    checker.reportError(tree, "illegal.classname", className, type);
                }
            }
        }
        return super.visitDeclared(type, tree);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy