langtools/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java
author jjg
Fri, 29 Aug 2008 11:10:12 -0700
changeset 1206 3a05355982a9
parent 10 06bc494ca11e
child 1264 076a3cde30d5
permissions -rw-r--r--
6597471: unused imports in javax.tools.JavaCompiler 6597531: unused imports and unused private const. in com.sun.tools.javac.Server.java Reviewed-by: mcimadamore Contributed-by: davide.angelocola@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.lang.annotation.Annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.type.DeclaredType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.type.TypeMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * Object providing state about a prior round of annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class JavacRoundEnvironment implements RoundEnvironment {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    // Default equals and hashCode methods are okay.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private final boolean processingOver;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private final boolean errorRaised;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private final ProcessingEnvironment processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    // Caller must pass in an immutable set
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private final Set<? extends Element> rootElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    JavacRoundEnvironment(boolean processingOver,
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
                          boolean errorRaised,
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
                          Set<? extends Element> rootElements,
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
                          ProcessingEnvironment processingEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        this.processingOver = processingOver;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        this.errorRaised = errorRaised;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        this.rootElements = rootElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        this.processingEnv = processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]",
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                             errorRaised,
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                             rootElements,
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                             processingOver);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public boolean processingOver() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        return processingOver;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * Returns {@code true} if an error was raised in the prior round
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * of processing; returns {@code false} otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * @return {@code true} if an error was raised in the prior round
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * of processing; returns {@code false} otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public boolean errorRaised() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return errorRaised;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * Returns the type elements specified by the prior round.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * @return the types elements specified by the prior round, or an
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * empty set if there were none
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    public Set<? extends Element> getRootElements() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        return rootElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    private static final String NOT_AN_ANNOTATION_TYPE =
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        "The argument does not represent an annotation type: ";
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * Returns the elements annotated with the given annotation type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * Only type elements <i>included</i> in this round of annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * processing, or declarations of members, parameters, or type
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * parameters declared within those, are returned.  Included type
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     * elements are {@linkplain #getSpecifiedTypeElements specified
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     * types} and any types nested within them.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * @param a  annotation type being requested
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * @return the elements annotated with the given annotation type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * or an empty set if there are none
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        Set<Element> result = Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        if (a.getKind() != ElementKind.ANNOTATION_TYPE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        DeclaredType annotationTypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        TypeMirror tm = a.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        if ( tm instanceof DeclaredType )
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            annotationTypeElement = (DeclaredType) a.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            throw new AssertionError("Bad implementation type for " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        ElementScanner6<Set<Element>, DeclaredType> scanner =
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            new AnnotationSetScanner(result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        for (Element element : rootElements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            result = scanner.scan(element, annotationTypeElement);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    // Could be written as a local class inside getElementsAnnotatedWith
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    private class AnnotationSetScanner extends
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        ElementScanner6<Set<Element>, DeclaredType> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        // Insertion-order preserving set
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        Set<Element> annotatedElements = new LinkedHashSet<Element>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        AnnotationSetScanner(Set<Element> defaultSet) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            super(defaultSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        public Set<Element> scan(Element e, DeclaredType p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            java.util.List<? extends AnnotationMirror> annotationMirrors =
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                processingEnv.getElementUtils().getAllAnnotationMirrors(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            for (AnnotationMirror annotationMirror : annotationMirrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                if (annotationMirror.getAnnotationType().equals(p))
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                    annotatedElements.add(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            e.accept(this, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            return annotatedElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * {@inheritdoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        if (!a.isAnnotation())
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        String name = a.getCanonicalName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        if (name == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            if (annotationType == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                return getElementsAnnotatedWith(annotationType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
}