langtools/src/share/classes/com/sun/tools/javac/model/JavacElements.java
author jjg
Mon, 14 Jan 2013 13:50:01 -0800
changeset 15356 cf312dc54c60
parent 15355 a4757c33cae9
child 15365 1f33464bbf46
permissions -rw-r--r--
8006119: update javac to follow latest spec for repeatable annotations Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    23
 * questions.
10
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.model;
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 java.lang.annotation.Inherited;
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
    31
import java.lang.reflect.Method;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
    33
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.type.DeclaredType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.util.Elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
    39
import static javax.lang.model.util.ElementFilter.methodsIn;
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
    40
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.code.Symbol.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 13077
diff changeset
    43
import com.sun.tools.javac.code.TypeTag;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.comp.Enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.main.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.processing.PrintingProcessor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.javac.tree.TreeScanner;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    53
import com.sun.tools.javac.util.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.javac.util.Name;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 13077
diff changeset
    55
import static com.sun.tools.javac.code.TypeTag.CLASS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
    56
import static com.sun.tools.javac.tree.JCTree.Tag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * Utility methods for operating on program elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    61
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
public class JavacElements implements Elements {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private JavaCompiler javaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private Symtab syms;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    70
    private Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public static JavacElements instance(Context context) {
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    75
        JavacElements instance = context.get(JavacElements.class);
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    76
        if (instance == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            instance = new JavacElements(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * Public for use only by JavacProcessingEnvironment
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    84
    protected JavacElements(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * Use a new context.  May be called from outside to update
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * internal state for a new annotation-processing round.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    public void setContext(Context context) {
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    93
        context.put(JavacElements.class, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        javaCompiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        syms = Symtab.instance(context);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    96
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    /**
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   102
     * An internal-use utility that creates a runtime view of an
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   103
     * annotation. This is the implementation of
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   104
     * Element.getAnnotation(Class).
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    public static <A extends Annotation> A getAnnotation(Symbol annotated,
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                                                         Class<A> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        if (!annoType.isAnnotation())
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            throw new IllegalArgumentException("Not an annotation type: "
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                                               + annoType);
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   111
        Attribute.Compound c;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   112
        if (annotated.kind == Kinds.TYP && annotated instanceof ClassSymbol) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   113
            c = getAttributeOnClass((ClassSymbol)annotated, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   114
        } else {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   115
            c = getAttribute(annotated, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   116
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   117
        return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   120
    // Helper to getAnnotation[s]
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   121
    private static <A extends Annotation> Attribute.Compound getAttribute(Symbol annotated,
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   122
                                                                          Class<A> annoType) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   123
        String name = annoType.getName();
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   124
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   125
        for (Attribute.Compound anno : annotated.getRawAttributes())
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   126
            if (name.equals(anno.type.tsym.flatName().toString()))
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   127
                return anno;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   128
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   129
        return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   130
    }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   131
    // Helper to getAnnotation[s]
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   132
    private static <A extends Annotation> Attribute.Compound getAttributeOnClass(ClassSymbol annotated,
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   133
                                                                Class<A> annoType) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        boolean inherited = annoType.isAnnotationPresent(Inherited.class);
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   135
        Attribute.Compound result = null;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   136
        while (annotated.name != annotated.name.table.names.java_lang_Object) {
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   137
            result = getAttribute(annotated, annoType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            if (result != null || !inherited)
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            Type sup = annotated.getSuperclass();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 13077
diff changeset
   141
            if (!sup.hasTag(CLASS) || sup.isErroneous())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            annotated = (ClassSymbol) sup.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   148
    /**
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   149
     * An internal-use utility that creates a runtime view of
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   150
     * annotations. This is the implementation of
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   151
     * Element.getAnnotations(Class).
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   152
     */
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   153
    public static <A extends Annotation> A[] getAnnotations(Symbol annotated,
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   154
                                                            Class<A> annoType) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   155
        if (!annoType.isAnnotation())
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   156
            throw new IllegalArgumentException("Not an annotation type: "
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   157
                                               + annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   158
        // If annoType does not declare a container this is equivalent to wrapping
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   159
        // getAnnotation(...) in an array.
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   160
        Class <? extends Annotation> containerType = getContainer(annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   161
        if (containerType == null) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   162
            A res = getAnnotation(annotated, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   163
            int size;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   164
            if (res == null) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   165
                size = 0;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   166
            } else {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   167
                size = 1;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   168
            }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   169
            @SuppressWarnings("unchecked") // annoType is the Class for A
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   170
            A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   171
            if (res != null)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   172
                arr[0] = res;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   173
            return arr;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   174
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   175
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   176
        // So we have a containing type
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   177
        String name = annoType.getName();
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   178
        String annoTypeName = annoType.getSimpleName();
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   179
        String containerTypeName = containerType.getSimpleName();
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   180
        int directIndex = -1, containerIndex = -1;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   181
        Attribute.Compound direct = null, container = null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   182
        Attribute.Compound[] rawAttributes = annotated.getRawAttributes().toArray(new Attribute.Compound[0]);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   183
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   184
        // Find directly present annotations
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   185
        for (int i = 0; i < rawAttributes.length; i++) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   186
            if (annoTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   187
                directIndex = i;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   188
                direct = rawAttributes[i];
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   189
            } else if(containerTypeName != null &&
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   190
                      containerTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   191
                containerIndex = i;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   192
                container = rawAttributes[i];
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   193
            }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   194
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   195
        // Deal with inherited annotations
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   196
        if (annotated.kind == Kinds.TYP &&
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   197
                (annotated instanceof ClassSymbol)) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   198
            ClassSymbol s = (ClassSymbol)annotated;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   199
            if (direct == null && container == null) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   200
                direct = getAttributeOnClass(s, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   201
                container = getAttributeOnClass(s, containerType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   202
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   203
                // both are inherited and found, put container last
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   204
                if (direct != null && container != null) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   205
                    directIndex = 0;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   206
                    containerIndex = 1;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   207
                } else if (direct != null) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   208
                    directIndex = 0;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   209
                } else {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   210
                    containerIndex = 0;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   211
                }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   212
            } else if (direct == null) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   213
                direct = getAttributeOnClass(s, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   214
                if (direct != null)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   215
                    directIndex = containerIndex + 1;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   216
            } else if (container == null) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   217
                container = getAttributeOnClass(s, containerType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   218
                if (container != null)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   219
                    containerIndex = directIndex + 1;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   220
            }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   221
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   222
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   223
        // Pack them in an array
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   224
        Attribute[] contained0 = new Attribute[0];
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   225
        if (container != null)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   226
            contained0 = unpackAttributes(container);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   227
        ListBuffer<Attribute.Compound> compounds = ListBuffer.lb();
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   228
        for (Attribute a : contained0)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   229
            if (a instanceof Attribute.Compound)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   230
                compounds = compounds.append((Attribute.Compound)a);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   231
        Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[0]);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   232
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   233
        int size = (direct == null ? 0 : 1) + contained.length;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   234
        @SuppressWarnings("unchecked") // annoType is the Class for A
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   235
        A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   236
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   237
        // if direct && container, which is first?
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   238
        int insert = -1;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   239
        int length = arr.length;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   240
        if (directIndex >= 0 && containerIndex >= 0) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   241
            if (directIndex < containerIndex) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   242
                arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   243
                insert = 1;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   244
            } else {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   245
                arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   246
                insert = 0;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   247
                length--;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   248
            }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   249
        } else if (directIndex >= 0) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   250
            arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   251
            return arr;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   252
        } else {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   253
            // Only container
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   254
            insert = 0;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   255
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   256
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   257
        for (int i = 0; i + insert < length; i++)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   258
            arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   259
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   260
        return arr;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   261
    }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   262
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   263
    // Needed to unpack the runtime view of containing annotations
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   264
    private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable();
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   265
    private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod();
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   266
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   267
    private static Class<? extends Annotation> initRepeatable() {
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   268
        try {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   269
            @SuppressWarnings("unchecked") // java.lang.annotation.Repeatable extends Annotation by being an annotation type
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   270
            Class<? extends Annotation> c = (Class)Class.forName("java.lang.annotation.Repeatable");
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   271
            return c;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   272
        } catch (ClassNotFoundException e) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   273
            return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   274
        } catch (SecurityException e) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   275
            return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   276
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   277
    }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   278
    private static Method initValueElementMethod() {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   279
        if (REPEATABLE_CLASS == null)
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   280
            return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   281
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   282
        Method m = null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   283
        try {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   284
            m = REPEATABLE_CLASS.getMethod("value");
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   285
            if (m != null)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   286
                m.setAccessible(true);
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   287
            return m;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   288
        } catch (NoSuchMethodException e) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   289
            return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   290
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   291
    }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   292
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   293
    // Helper to getAnnotations
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   294
    private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   295
        // Since we can not refer to java.lang.annotation.Repeatable until we are
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   296
        // bootstrapping with java 8 we need to get the Repeatable annotation using
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   297
        // reflective invocations instead of just using its type and element method.
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   298
        if (REPEATABLE_CLASS != null &&
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   299
            VALUE_ELEMENT_METHOD != null) {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   300
            // Get the Repeatable instance on the annotations declaration
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   301
            Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS);
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   302
            if (repeatable != null) {
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   303
                try {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   304
                    // Get the value element, it should be a class
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   305
                    // indicating the containing annotation type
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   306
                    @SuppressWarnings("unchecked")
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 15355
diff changeset
   307
                    Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable);
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   308
                    if (containerType == null)
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   309
                        return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   310
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   311
                    return containerType;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   312
                } catch (ClassCastException e) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   313
                    return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   314
                } catch (IllegalAccessException e) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   315
                    return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   316
                } catch (InvocationTargetException e ) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   317
                    return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   318
                }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   319
            }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   320
        }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   321
        return null;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   322
    }
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   323
    // Helper to getAnnotations
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   324
    private static Attribute[] unpackAttributes(Attribute.Compound container) {
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   325
        // We now have an instance of the container,
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   326
        // unpack it returning an instance of the
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   327
        // contained type or null
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   328
        return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   329
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    public PackageSymbol getPackageElement(CharSequence name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        String strName = name.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        if (strName.equals(""))
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            return syms.unnamedPackage;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        return SourceVersion.isName(strName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            ? nameToSymbol(strName, PackageSymbol.class)
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
    public ClassSymbol getTypeElement(CharSequence name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        String strName = name.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        return SourceVersion.isName(strName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            ? nameToSymbol(strName, ClassSymbol.class)
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
     * Returns a symbol given the type's or packages's canonical name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
     * or null if the name isn't found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    private <S extends Symbol> S nameToSymbol(String nameStr, Class<S> clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        Name name = names.fromString(nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        // First check cache.
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        Symbol sym = (clazz == ClassSymbol.class)
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                    ? syms.classes.get(name)
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                    : syms.packages.get(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            if (sym == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                sym = javaCompiler.resolveIdent(nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            sym.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            return (sym.kind != Kinds.ERR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                    sym.exists() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                    clazz.isInstance(sym) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                    name.equals(sym.getQualifiedName()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                ? clazz.cast(sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        } catch (CompletionFailure e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
    public JavacSourcePosition getSourcePosition(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
        Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        if (treeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        JCTree tree = treeTop.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        JCCompilationUnit toplevel = treeTop.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
        JavaFileObject sourcefile = toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        if (sourcefile == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
    public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        if (treeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        JCTree tree = treeTop.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        JCCompilationUnit toplevel = treeTop.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        JavaFileObject sourcefile = toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        if (sourcefile == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        JCTree annoTree = matchAnnoToTree(a, e, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        if (annoTree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        return new JavacSourcePosition(sourcefile, annoTree.pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                                       toplevel.lineMap);
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a,
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                                            AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        // TODO: better accuracy in getSourcePosition(... AnnotationValue)
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        return getSourcePosition(e, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
     * Returns the tree for an annotation given the annotated element
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
     * and the element's own tree.  Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
    private JCTree matchAnnoToTree(AnnotationMirror findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                                   Element e, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        class Vis extends JCTree.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            List<JCAnnotation> result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            public void visitTopLevel(JCCompilationUnit tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                result = tree.packageAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
            public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
            public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        Vis vis = new Vis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        tree.accept(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        if (vis.result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            return null;
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   436
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   437
        List<Attribute.Compound> annos = sym.getRawAttributes();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        return matchAnnoToTree(cast(Attribute.Compound.class, findme),
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   439
                               annos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                               vis.result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
     * Returns the tree for an annotation given a list of annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
     * in which to search (recursively) and their corresponding trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
     * Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    private JCTree matchAnnoToTree(Attribute.Compound findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                                   List<Attribute.Compound> annos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                                   List<JCAnnotation> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        for (Attribute.Compound anno : annos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            for (JCAnnotation tree : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                JCTree match = matchAnnoToTree(findme, anno, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                if (match != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                    return match;
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
     * Returns the tree for an annotation given an Attribute to
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
     * search (recursively) and its corresponding tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     * Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    private JCTree matchAnnoToTree(final Attribute.Compound findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                                   final Attribute attr,
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                                   final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        if (attr == findme)
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            return (tree.type.tsym == findme.type.tsym) ? tree : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        class Vis implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            JCTree result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
            public void visitConstant(Attribute.Constant value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            public void visitClass(Attribute.Class clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            public void visitCompound(Attribute.Compound anno) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                for (Pair<MethodSymbol, Attribute> pair : anno.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                    JCExpression expr = scanForAssign(pair.fst, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                    if (expr != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                        JCTree match = matchAnnoToTree(findme, pair.snd, expr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                        if (match != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                            result = match;
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
            public void visitArray(Attribute.Array array) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
   491
                if (tree.hasTag(NEWARRAY) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                        types.elemtype(array.type).tsym == findme.type.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                    List<JCExpression> elems = ((JCNewArray) tree).elems;
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                    for (Attribute value : array.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                        if (value == findme) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                            result = elems.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                        elems = elems.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        Vis vis = new Vis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        attr.accept(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        return vis.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
     * Scans for a JCAssign node with a LHS matching a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
     * symbol, and returns its RHS.  Does not scan nested JCAnnotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    private JCExpression scanForAssign(final MethodSymbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                                       final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        class TS extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            JCExpression result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
            public void scan(JCTree t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                if (t != null && result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                    t.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            public void visitAnnotation(JCAnnotation t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                if (t == tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                    scan(t.args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            public void visitAssign(JCAssign t) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
   530
                if (t.lhs.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
                    JCIdent ident = (JCIdent) t.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
                    if (ident.sym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                        result = t.rhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        TS scanner = new TS();
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        tree.accept(scanner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        return scanner.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
     * Returns the tree node corresponding to this element, or null
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
     * if none can be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
    public JCTree getTree(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
        Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        return (treeTop != null) ? treeTop.fst : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
    public String getDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        // Our doc comment is contained in a map in our toplevel,
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        // indexed by our tree.  Find our enter environment, which gives
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        // us our toplevel.  It also gives us a tree that contains our
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        // tree:  walk it to find our tree.  This is painful.
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
        Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        if (treeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        JCTree tree = treeTop.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        JCCompilationUnit toplevel = treeTop.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        if (toplevel.docComments == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            return null;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
   563
        return toplevel.docComments.getCommentText(tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
    public PackageElement getPackageOf(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        return cast(Symbol.class, e).packge();
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
    public boolean isDeprecated(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
        return (sym.flags() & Flags.DEPRECATED) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
    public Name getBinaryName(TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        return cast(TypeSymbol.class, type).flatName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
    public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
                                                        AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        Attribute.Compound anno = cast(Attribute.Compound.class, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        DeclaredType annotype = a.getAnnotationType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        Map<MethodSymbol, Attribute> valmap = anno.getElementValues();
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        for (ExecutableElement ex :
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
                 methodsIn(annotype.asElement().getEnclosedElements())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
            MethodSymbol meth = (MethodSymbol) ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            Attribute defaultValue = meth.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
            if (defaultValue != null && !valmap.containsKey(meth)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
                valmap.put(meth, defaultValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        return valmap;
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
    public FilteredMemberList getAllMembers(TypeElement element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        Symbol sym = cast(Symbol.class, element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
        Scope scope = sym.members().dupUnshared();
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
        List<Type> closure = types.closure(sym.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        for (Type t : closure)
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            addMembers(scope, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
        return new FilteredMemberList(scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
        private void addMembers(Scope scope, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
            members:
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
            for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
                Scope.Entry overrider = scope.lookup(e.sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                while (overrider.scope != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
                    if (overrider.sym.kind == e.sym.kind
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
                        && (overrider.sym.flags() & Flags.SYNTHETIC) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
                    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
                        if (overrider.sym.getKind() == ElementKind.METHOD
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
                        && overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
                            continue members;
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                    overrider = overrider.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
                boolean derived = e.sym.getEnclosingElement() != scope.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                ElementKind kind = e.sym.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                boolean initializer = kind == ElementKind.CONSTRUCTOR
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                    || kind == ElementKind.INSTANCE_INIT
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
                    || kind == ElementKind.STATIC_INIT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
                if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
                    scope.enter(e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
     * Returns all annotations of an element, whether
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
     * inherited or directly present.
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
     * @param e  the element being examined
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
     * @return all annotations of the element
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
        Symbol sym = cast(Symbol.class, e);
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   642
        List<Attribute.Compound> annos = sym.getRawAttributes();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        while (sym.getKind() == ElementKind.CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
            Type sup = ((ClassSymbol) sym).getSuperclass();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 13077
diff changeset
   645
            if (!sup.hasTag(CLASS) || sup.isErroneous() ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
                    sup.tsym == syms.objectType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
            sym = sup.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
            List<Attribute.Compound> oldAnnos = annos;
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   651
            List<Attribute.Compound> newAnnos = sym.getRawAttributes();
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   652
            for (Attribute.Compound anno : newAnnos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                if (isInherited(anno.type) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
                        !containsAnnoOfType(oldAnnos, anno.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
                    annos = annos.prepend(anno);
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
        return annos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
     * Tests whether an annotation type is @Inherited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
    private boolean isInherited(Type annotype) {
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   666
        return annotype.tsym.attribute(syms.inheritedType.tsym) != null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
     * Tests whether a list of annotations contains an annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
     * of a given type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
    private static boolean containsAnnoOfType(List<Attribute.Compound> annos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
                                              Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
        for (Attribute.Compound anno : annos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
            if (anno.type.tsym == type.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
    public boolean hides(Element hiderEl, Element hideeEl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        Symbol hider = cast(Symbol.class, hiderEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        Symbol hidee = cast(Symbol.class, hideeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
        // Fields only hide fields; methods only methods; types only types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
        // Names must match.  Nothing hides itself (just try it).
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
        if (hider == hidee ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
                hider.kind != hidee.kind ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
                hider.name != hidee.name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
        // Only static methods can hide other methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        // Methods only hide methods with matching signatures.
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
        if (hider.kind == Kinds.MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
            if (!hider.isStatic() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
                        !types.isSubSignature(hider.type, hidee.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
        // Hider must be in a subclass of hidee's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
        // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
        // in M1's class, then M1 and M2 both hide M3.
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        ClassSymbol hiderClass = hider.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        ClassSymbol hideeClass = hidee.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
        if (hiderClass == null || hideeClass == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
                !hiderClass.isSubClass(hideeClass, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
        // Hidee must be accessible in hider's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
        // The method isInheritedIn is poorly named:  it checks only access.
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
        return hidee.isInheritedIn(hiderClass, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
    public boolean overrides(ExecutableElement riderEl,
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                             ExecutableElement rideeEl, TypeElement typeEl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
        MethodSymbol rider = cast(MethodSymbol.class, riderEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
        MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
        ClassSymbol origin = cast(ClassSymbol.class, typeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
        return rider.name == ridee.name &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
               // not reflexive as per JLS
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
               rider != ridee &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
               // we don't care if ridee is static, though that wouldn't
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
               // compile
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
               !rider.isStatic() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
               // Symbol.overrides assumes the following
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
               ridee.isMemberOf(origin, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
               // check access and signatures; don't check return types
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
               rider.overrides(ridee, origin, types, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
    public String getConstantExpression(Object value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
        return Constants.format(value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
     * Print a representation of the elements to the given writer in
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
     * the specified order.  The main purpose of this method is for
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
     * diagnostics.  The exact format of the output is <em>not</em>
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
     * specified and is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
     * @param w the writer to print the output to
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
     * @param elements the elements to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
    public void printElements(java.io.Writer w, Element... elements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
        for (Element element : elements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
            (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
    public Name getName(CharSequence cs) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   759
        return names.fromString(cs.toString());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
     * Returns the tree node and compilation unit corresponding to this
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
     * element, or null if they can't be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
    private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        Env<AttrContext> enterEnv = getEnterEnv(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
        if (enterEnv == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
        JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
        if (tree == null || enterEnv.toplevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        return new Pair<JCTree,JCCompilationUnit>(tree, enterEnv.toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
     * Returns the best approximation for the tree node and compilation unit
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
     * corresponding to the given element, annotation and value.
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
     * If the element is null, null is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
     * If the annotation is null or cannot be found, the tree node and
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
     * compilation unit for the element is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
     * If the annotation value is null or cannot be found, the tree node and
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
     * compilation unit for the annotation is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
    public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
                      Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
        if (e == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
        Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        if (elemTreeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
        if (a == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
            return elemTreeTop;
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
        JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
        if (annoTree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
            return elemTreeTop;
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        // 6388543: if v != null, we should search within annoTree to find
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        // the tree matching v. For now, we ignore v and return the tree of
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
        // the annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
        return new Pair<JCTree, JCCompilationUnit>(annoTree, elemTreeTop.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
     * Returns a symbol's enter environment, or null if it has none.
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
    private Env<AttrContext> getEnterEnv(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
        // Get enclosing class of sym, or sym itself if it is a class
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        // or package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        TypeSymbol ts = (sym.kind != Kinds.PCK)
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
                        ? sym.enclClass()
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
                        : (PackageSymbol) sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
        return (ts != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
                ? enter.getEnv(ts)
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
                : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
     * Returns an object cast to the specified type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
     * @throws NullPointerException if the object is {@code null}
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
     * @throws IllegalArgumentException if the object is of the wrong type
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
    private static <T> T cast(Class<T> clazz, Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
        if (! clazz.isInstance(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
            throw new IllegalArgumentException(o.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        return clazz.cast(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
}