TestElementsAnnotatedWith.java 7.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24 25
 */

/*
 * @test
26
 * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854
D
duke 已提交
27 28
 * @summary Tests that getElementsAnnotatedWith works properly.
 * @author  Joseph D. Darcy
29 30
 * @library ../../../lib
 * @build   JavacTestingAbstractProcessor
D
duke 已提交
31 32 33 34 35 36
 * @compile TestElementsAnnotatedWith.java
 * @compile InheritedAnnotation.java
 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
37
 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
38 39
 * @compile Foo.java
 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
D
duke 已提交
40 41 42
 */

import java.lang.annotation.Annotation;
43
import java.io.*;
D
duke 已提交
44 45 46
import java.util.Collections;
import java.util.Set;
import java.util.HashSet;
47 48
import java.util.List;
import java.util.ArrayList;
D
duke 已提交
49 50
import java.util.Arrays;
import javax.annotation.processing.*;
51
import javax.tools.*;
D
duke 已提交
52 53 54 55 56 57 58 59 60 61 62
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.util.*;
import static javax.lang.model.util.ElementFilter.*;

/**
 * This processor verifies that the information returned by
 * getElementsAnnotatedWith is consistent with the expected results
 * stored in an AnnotatedElementInfo annotation.
 */
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
63
public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor {
D
duke 已提交
64 65 66 67

    public boolean process(Set<? extends TypeElement> annotations,
                           RoundEnvironment roundEnvironment) {
        TypeElement annotatedElementInfoElement =
68
            elements.getTypeElement("AnnotatedElementInfo");
D
duke 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        Set<? extends Element> resultsMeta = Collections.emptySet();
        Set<? extends Element> resultsBase = Collections.emptySet();

        if (!roundEnvironment.processingOver()) {
            testNonAnnotations(roundEnvironment);

            // Verify AnnotatedElementInfo is present on the first
            // specified type.

            TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next();

            AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class);

            boolean failed = false;

            if (annotatedElementInfo == null)
                throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " +
                                                  firstType);
            else {
                // Verify that the annotation information is as
                // expected.

91
                Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names()));
D
duke 已提交
92 93 94

                resultsMeta =
                    roundEnvironment.
95
                    getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName()));
D
duke 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

                System.err.println("Results: " + resultsMeta);

                if (resultsMeta.size() != annotatedElementInfo.expectedSize()) {
                    failed = true;
                    System.err.printf("Bad number of elements; expected %d, got %d%n",
                                      annotatedElementInfo.expectedSize(), resultsMeta.size());
                } else {
                    for(Element element : resultsMeta) {
                        String simpleName = element.getSimpleName().toString();
                        if (!expectedNames.contains(simpleName) ) {
                            failed = true;
                            System.err.println("Name ``" + simpleName + "'' not expected.");
                        }
                    }
                }
            }

            resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName());

            if (!resultsMeta.equals(resultsBase)) {
                failed = true;
                System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta +
                                   "\nbase: " + resultsBase);
            }

            if (failed) {
                System.err.println("AnnotatedElementInfo: " + annotatedElementInfo);
                throw new RuntimeException();
            }
        } else {
            // If processing is over without an error, the specified
            // elements should be empty so an empty set should be returned.
            resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement);
            resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class);
            if (!resultsMeta.isEmpty())
                throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta);
            if (!resultsBase.isEmpty())
                throw new RuntimeException("Nonempty resultsBase: " + resultsBase);

        }
        return true;
    }

    private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) {
        try {
            return roundEnvironment.
                getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class));
        } catch(ClassNotFoundException cnfe) {
            throw new RuntimeException(cnfe);
        }
    }

    /**
     * Verify non-annotation types result in
     * IllegalArgumentExceptions.
     */
    private void testNonAnnotations(RoundEnvironment roundEnvironment) {
        try {
            Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class );
            throw new RuntimeException("Illegal argument exception not thrown");
        } catch(IllegalArgumentException iae) {}

        try {
160 161 162 163
            Set<? extends Element> elements =
                roundEnvironment.getElementsAnnotatedWith(processingEnv.
                                                          getElementUtils().
                                                          getTypeElement("java.lang.Object") );
D
duke 已提交
164 165 166 167
            throw new RuntimeException("Illegal argument exception not thrown");
        } catch(IllegalArgumentException iae) {}
    }
}