src/share/classes/com/sun/tools/javac/code/AnnoConstruct.java

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

merge

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

mercurial