1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.tools.javac.code;
  26 
  27 import java.lang.annotation.Annotation;
  28 import java.lang.annotation.Inherited;
  29 import java.lang.annotation.Repeatable;
  30 import java.lang.reflect.InvocationTargetException;
  31 import java.lang.reflect.Method;
  32 
  33 import javax.lang.model.AnnotatedConstruct;
  34 
  35 import com.sun.tools.javac.model.AnnotationProxyMaker;
  36 import com.sun.tools.javac.util.DefinedBy;
  37 import com.sun.tools.javac.util.DefinedBy.Api;
  38 import com.sun.tools.javac.util.List;
  39 import com.sun.tools.javac.util.ListBuffer;
  40 
  41 /**
  42  * Common super type for annotated constructs such as Types and Symbols.
  43  *
  44  * This class should *not* contain any fields since it would have a significant
  45  * impact on the javac memory footprint.
  46  *
  47  * <p><b>This is NOT part of any supported API.
  48  * If you write code that depends on this, you do so at your own
  49  * risk.  This code and its internal interfaces are subject to change
  50  * or deletion without notice.</b></p>
  51  */
  52 public abstract class AnnoConstruct implements AnnotatedConstruct {
  53 
  54 
  55     // Override to enforce a narrower return type.
  56     @Override @DefinedBy(Api.LANGUAGE_MODEL)
  57     public abstract List<? extends Attribute.Compound> getAnnotationMirrors();
  58 
  59 
  60     // This method is part of the javax.lang.model API, do not use this in javac code.
  61     protected <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
  62         String name = annoType.getName();
  63 
  64         for (Attribute.Compound anno : getAnnotationMirrors()) {
  65             if (name.equals(anno.type.tsym.flatName().toString()))
  66                 return anno;
  67         }
  68 
  69         return null;
  70     }
  71 
  72 
  73     @SuppressWarnings("unchecked")
  74     protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
  75         return (A[]) java.lang.reflect.Array.newInstance(annoType, 0);  // annoType is the Class for A
  76     }
  77 
  78 
  79     // This method is part of the javax.lang.model API, do not use this in javac code.
  80     @DefinedBy(Api.LANGUAGE_MODEL)
  81     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annoType) {
  82 
  83         if (!annoType.isAnnotation())
  84             throw new IllegalArgumentException("Not an annotation type: "
  85                                                + annoType);
  86         // If annoType does not declare a container this is equivalent to wrapping
  87         // getAnnotation(...) in an array.
  88         Class <? extends Annotation> containerType = getContainer(annoType);
  89         if (containerType == null) {
  90             A res = getAnnotation(annoType);
  91             int size = res == null ? 0 : 1;
  92 
  93             @SuppressWarnings("unchecked") // annoType is the Class for A
  94             A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
  95             if (res != null)
  96                 arr[0] = res;
  97             return arr;
  98         }
  99 
 100         // So we have a containing type
 101         String annoTypeName = annoType.getName();
 102         String containerTypeName = containerType.getName();
 103         int directIndex = -1, containerIndex = -1;
 104         Attribute.Compound direct = null, container = null;
 105         // Find directly (explicit or implicit) present annotations
 106         int index = -1;
 107         for (Attribute.Compound attribute : getAnnotationMirrors()) {
 108             index++;
 109             if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) {
 110                 directIndex = index;
 111                 direct = attribute;
 112             } else if(containerTypeName != null &&
 113                       attribute.type.tsym.flatName().contentEquals(containerTypeName)) {
 114                 containerIndex = index;
 115                 container = attribute;
 116             }
 117         }
 118 
 119         // Deal with inherited annotations
 120         if (direct == null && container == null &&
 121                 annoType.isAnnotationPresent(Inherited.class))
 122             return getInheritedAnnotations(annoType);
 123 
 124         Attribute.Compound[] contained = unpackContained(container);
 125 
 126         // In case of an empty legacy container we might need to look for
 127         // inherited annos as well
 128         if (direct == null && contained.length == 0 &&
 129                 annoType.isAnnotationPresent(Inherited.class))
 130             return getInheritedAnnotations(annoType);
 131 
 132         int size = (direct == null ? 0 : 1) + contained.length;
 133         @SuppressWarnings("unchecked") // annoType is the Class for A
 134         A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
 135 
 136         // if direct && container, which is first?
 137         int insert = -1;
 138         int length = arr.length;
 139         if (directIndex >= 0 && containerIndex >= 0) {
 140             if (directIndex < containerIndex) {
 141                 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
 142                 insert = 1;
 143             } else {
 144                 arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
 145                 insert = 0;
 146                 length--;
 147             }
 148         } else if (directIndex >= 0) {
 149             arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
 150             return arr;
 151         } else {
 152             // Only container
 153             insert = 0;
 154         }
 155 
 156         for (int i = 0; i + insert < length; i++)
 157             arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
 158 
 159         return arr;
 160     }
 161 
 162     private Attribute.Compound[] unpackContained(Attribute.Compound container) {
 163         // Pack them in an array
 164         Attribute[] contained0 = null;
 165         if (container != null)
 166             contained0 = unpackAttributes(container);
 167         ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
 168         if (contained0 != null) {
 169             for (Attribute a : contained0)
 170                 if (a instanceof Attribute.Compound)
 171                     compounds = compounds.append((Attribute.Compound)a);
 172         }
 173         return compounds.toArray(new Attribute.Compound[compounds.size()]);
 174     }
 175 
 176     // This method is part of the javax.lang.model API, do not use this in javac code.
 177     @DefinedBy(Api.LANGUAGE_MODEL)
 178     public <A extends Annotation> A getAnnotation(Class<A> annoType) {
 179 
 180         if (!annoType.isAnnotation())
 181             throw new IllegalArgumentException("Not an annotation type: " + annoType);
 182 
 183         Attribute.Compound c = getAttribute(annoType);
 184         return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
 185     }
 186 
 187     // Helper to getAnnotationsByType
 188     private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
 189         return annoType.getAnnotation(Repeatable.class).value();
 190     }
 191 
 192     // Helper to getAnnotationsByType
 193     private static Attribute[] unpackAttributes(Attribute.Compound container) {
 194         // We now have an instance of the container,
 195         // unpack it returning an instance of the
 196         // contained type or null
 197         return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
 198     }
 199 
 200 }