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

org.checkerframework.common.reflection.MethodValVisitor 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.MethodVal;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType;
import org.checkerframework.javacutil.AnnotationUtils;

import java.util.List;

import javax.lang.model.element.AnnotationMirror;

public class MethodValVisitor extends BaseTypeVisitor {

    public MethodValVisitor(BaseTypeChecker checker) {
        super(checker);
    }

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

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

class MethodNameValidator extends BaseTypeValidator {

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

    @Override
    public Void visitDeclared(AnnotatedDeclaredType type, Tree tree) {
        AnnotationMirror methodVal = type.getAnnotation(MethodVal.class);
        if (methodVal != null) {
            AnnotatedTypeFactory atypeFactory = checker.getTypeFactory();
            List classNames =
                    AnnotationUtils.getElementValueArray(
                            methodVal, atypeFactory.methodValClassNameElement, String.class);
            List methodNames =
                    AnnotationUtils.getElementValueArray(
                            methodVal, atypeFactory.methodValMethodNameElement, String.class);
            List params =
                    AnnotationUtils.getElementValueArray(
                            methodVal, atypeFactory.methodValParamsElement, Integer.class);
            if (!(classNames.size() == methodNames.size() && classNames.size() == params.size())) {
                checker.reportError(tree, "invalid.methodval", methodVal);
            }

            for (String methodName : methodNames) {
                if (!legalMethodName(methodName)) {
                    checker.reportError(tree, "illegal.methodname", methodName, type);
                }
            }
        }
        return super.visitDeclared(type, tree);
    }

    private boolean legalMethodName(String methodName) {
        if (methodName.equals(ReflectionResolver.INIT)) {
            return true;
        }
        if (methodName.length() < 1) {
            return false;
        }
        char[] methodNameChars = methodName.toCharArray();
        if (!Character.isJavaIdentifierStart(methodNameChars[0])) {
            return false;
        }
        for (int i = 1; i < methodNameChars.length; i++) {
            if (!Character.isJavaIdentifierPart(methodNameChars[i])) {
                return false;
            }
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy