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

org.checkerframework.framework.ajava.AnnotationTransferVisitor 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.44.0
Show newest version
package org.checkerframework.framework.ajava;

import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
import com.github.javaparser.ast.type.ArrayType;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.PrimitiveType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.type.TypeKind;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable;

/**
 * A visitor that adds all annotations from a {@code AnnotatedTypeMirror} to the corresponding
 * JavaParser type, including nested types like array components.
 *
 * 

The {@code AnnotatedTypeMirror} is passed as the secondary parameter to the visit methods. */ public class AnnotationTransferVisitor extends VoidVisitorAdapter { @Override public void visit(ArrayType target, AnnotatedTypeMirror type) { target.getComponentType().accept(this, ((AnnotatedArrayType) type).getComponentType()); transferAnnotations(type, target); } @Override public void visit(ClassOrInterfaceType target, AnnotatedTypeMirror type) { if (type.getKind() == TypeKind.DECLARED) { AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) type; if (target.getTypeArguments().isPresent()) { NodeList types = target.getTypeArguments().get(); for (int i = 0; i < types.size(); i++) { types.get(i).accept(this, declaredType.getTypeArguments().get(i)); } } } transferAnnotations(type, target); } @Override public void visit(PrimitiveType target, AnnotatedTypeMirror type) { transferAnnotations(type, target); } @Override public void visit(TypeParameter target, AnnotatedTypeMirror type) { AnnotatedTypeVariable annotatedTypeVar = (AnnotatedTypeVariable) type; NodeList bounds = target.getTypeBound(); if (bounds.size() == 1) { bounds.get(0).accept(this, annotatedTypeVar.getUpperBound()); } } /** * Transfers annotations from {@code annotatedType} to {@code target}. Does nothing if {@code * annotatedType} is null. * * @param annotatedType type with annotations to transfer * @param target JavaParser node representing the type to transfer annotations to */ private void transferAnnotations( @Nullable AnnotatedTypeMirror annotatedType, NodeWithAnnotations target) { if (annotatedType == null) { return; } for (AnnotationMirror annotation : annotatedType.getAnnotations()) { AnnotationExpr convertedAnnotation = AnnotationMirrorToAnnotationExprConversion.annotationMirrorToAnnotationExpr(annotation); target.addAnnotation(convertedAnnotation); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy