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

framework.src.org.checkerframework.common.util.count.AnnotationsCounter 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.common.util.count;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.util.ElementFilter;

import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;

/**
 * An annotation processor for counting the occurrences of annotations.
 * To invoke it, use
 * 
 * javac -proc:only -processor org.checkerframework.common.util.count.AnnotationsCounter MyFile.java ...
 * 
*/ @SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class AnnotationsCounter extends AbstractProcessor { final Map annotationCount = new HashMap(); protected void incrementCount(Name annoName) { if (!annotationCount.containsKey(annoName)) { annotationCount.put(annoName, 2); } else { annotationCount.put(annoName, annotationCount.get(annoName) + 1); } } @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { if (roundEnv.processingOver()) { System.out.println("Found annotations: "); for (Map.Entry entry : annotationCount.entrySet()) { System.out.println(entry.getKey() + "\t" + entry.getValue()); } return true; } else { for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) { ClassTree tree = Trees.instance(processingEnv).getTree(elem); if (tree!=null) { tree.accept(scanner, null); } } return false; } } private final TreeScanner scanner = new TreeScanner() { @Override public Void visitAnnotation(AnnotationTree node, Void p) { JCAnnotation anno = (JCAnnotation) node; Name annoName = anno.annotationType.type.tsym.name; incrementCount(annoName); return super.visitAnnotation(node, p); } }; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy