1/* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.util.*; 25 26import javax.annotation.processing.*; 27import javax.lang.model.*; 28import javax.lang.model.element.*; 29 30import com.sun.source.doctree.DocCommentTree; 31import com.sun.source.doctree.ErroneousTree; 32import com.sun.source.tree.ClassTree; 33import com.sun.source.tree.MethodTree; 34import com.sun.source.tree.VariableTree; 35import com.sun.source.util.DocTreeScanner; 36import com.sun.source.util.DocTrees; 37import com.sun.source.util.TreePath; 38import com.sun.source.util.TreePathScanner; 39import com.sun.source.util.TreeScanner; 40import java.io.PrintWriter; 41import javax.tools.Diagnostic; 42 43/** 44 * Standard annotation processor for use by examples to 45 * scan DocCommentTree nodes looking for ErroneousTree, 46 * on which to call {@code getMessage}. 47 */ 48@SupportedAnnotationTypes("*") 49public class DocCommentProcessor extends AbstractProcessor { 50 @Override 51 public SourceVersion getSupportedSourceVersion() { 52 return SourceVersion.latest(); 53 } 54 55 @Override 56 public void init(ProcessingEnvironment pEnv) { 57 super.init(pEnv); 58 trees = DocTrees.instance(pEnv); 59 messager = pEnv.getMessager(); 60 } 61 62 @Override 63 public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { 64 for (Element e : rEnv.getRootElements()) { 65 new DocCommentScanner().scan(e); 66 } 67 return true; 68 } 69 70 class DocCommentScanner extends TreePathScanner<Void,Void> { 71 public void scan(Element e) { 72 scan(trees.getPath(e), null); 73 } 74 75 @Override 76 public Void visitClass(ClassTree tree, Void ignore) { 77 check(); 78 return super.visitClass(tree, ignore); 79 } 80 81 @Override 82 public Void visitMethod(MethodTree tree, Void ignore) { 83 check(); 84 return super.visitMethod(tree, ignore); 85 } 86 87 @Override 88 public Void visitVariable(VariableTree tree, Void ignore) { 89 check(); 90 return super.visitVariable(tree, ignore); 91 } 92 93 private void check() { 94 DocCommentTree dc = trees.getDocCommentTree(getCurrentPath()); 95 if (dc == null) 96 return; 97 98 DocTreeScanner<Void, Void> s = new DocTreeScanner<Void, Void>() { 99 @Override 100 public Void visitErroneous(ErroneousTree tree, Void ignore) { 101 messager.printMessage(Diagnostic.Kind.NOTE, tree.getDiagnostic().getMessage(null)); 102 return null; 103 } 104 }; 105 106 s.scan(dc, null); 107 } 108 109 } 110 111 private DocTrees trees; 112 private Messager messager; 113} 114