test/tools/javac/api/T6412669.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 739
a0d9d642f65b
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2006, 2010, 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  */
    24 /*
    25  * @test
    26  * @bug 6412669 6997958
    27  * @summary Should be able to get SourcePositions from 269 world
    28  */
    30 import java.io.*;
    31 import java.net.*;
    32 import java.util.*;
    33 import javax.annotation.*;
    34 import javax.annotation.processing.*;
    35 import javax.lang.model.*;
    36 import javax.lang.model.element.*;
    37 import javax.tools.*;
    38 import com.sun.source.util.*;
    39 import com.sun.tools.javac.api.*;
    41 @SupportedAnnotationTypes("*")
    42 public class T6412669 extends AbstractProcessor {
    43     public static void main(String... args) throws Exception {
    44         File testSrc = new File(System.getProperty("test.src", "."));
    45         File testClasses = new File(System.getProperty("test.classes", "."));
    47         // Determine location of necessary tools classes. Assume all in one place.
    48         // Likely candidates are typically tools.jar (when testing JDK build)
    49         // or build/classes or dist/javac.jar (when testing langtools, using -Xbootclasspath/p:)
    50         File toolsClasses;
    51         URL u = T6412669.class.getClassLoader().getResource("com/sun/source/util/JavacTask.class");
    52         switch (u.getProtocol()) {
    53             case "file":
    54                 toolsClasses = new File(u.toURI());
    55                 break;
    56             case "jar":
    57                 String s = u.getFile(); // will be file:path!/entry
    58                 int sep = s.indexOf("!");
    59                 toolsClasses = new File(new URI(s.substring(0, sep)));
    60                 break;
    61             default:
    62                 throw new AssertionError("Cannot locate tools classes");
    63         }
    64         //System.err.println("toolsClasses: " + toolsClasses);
    66         JavacTool tool = JavacTool.create();
    67         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    68         fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(testClasses, toolsClasses));
    69         Iterable<? extends JavaFileObject> files =
    70             fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java")));
    71         String[] opts = { "-proc:only", "-processor", T6412669.class.getName()};
    72         StringWriter sw = new StringWriter();
    73         JavacTask task = tool.getTask(sw, fm, null, Arrays.asList(opts), null, files);
    74         boolean ok = task.call();
    75         String out = sw.toString();
    76         if (!out.isEmpty())
    77             System.err.println(out);
    78         if (!ok)
    79             throw new AssertionError("compilation of test program failed");
    80         // verify we found an annotated element to exercise the SourcePositions API
    81         if (!out.contains("processing element"))
    82             throw new AssertionError("expected text not found in compilation output");
    83     }
    85     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    86         Trees trees = Trees.instance(processingEnv);
    87         SourcePositions sp = trees.getSourcePositions();
    88         Messager m = processingEnv.getMessager();
    89         m.printMessage(Diagnostic.Kind.NOTE, "processing annotations");
    90         int count = 0;
    91         for (TypeElement anno: annotations) {
    92             count++;
    93             m.printMessage(Diagnostic.Kind.NOTE, "  processing annotation " + anno);
    94             for (Element e: roundEnv.getElementsAnnotatedWith(anno)) {
    95                 m.printMessage(Diagnostic.Kind.NOTE, "    processing element " + e);
    96                 TreePath p = trees.getPath(e);
    97                 long start = sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
    98                 long end = sp.getEndPosition(p.getCompilationUnit(), p.getLeaf());
    99                 Diagnostic.Kind k = (start > 0 && end > 0 && start < end
   100                                      ? Diagnostic.Kind.NOTE : Diagnostic.Kind.ERROR);
   101                 m.printMessage(k, "test [" + start + "," + end + "]", e);
   102             }
   103         }
   104         if (count == 0)
   105             m.printMessage(Diagnostic.Kind.NOTE, "no annotations found");
   106         return true;
   107     }
   109     @Override
   110     public SourceVersion getSupportedSourceVersion() {
   111         return SourceVersion.latest();
   112     }
   113 }

mercurial