src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java

Wed, 12 Mar 2008 13:06:00 -0700

author
jjg
date
Wed, 12 Mar 2008 13:06:00 -0700
changeset 12
7366066839bb
parent 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

6668794: javac puts localized text in raw diagnostics
6668796: bad diagnostic "bad class file" given for source files
Summary: Replace internal use of localized text with JCDiagnostic fragments; fix diagnostic for bad source file
Reviewed-by: mcimadamore

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

mercurial