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

Thu, 04 Aug 2016 23:36:47 -0700

author
asaha
date
Thu, 04 Aug 2016 23:36:47 -0700
changeset 3270
8a30511b2ea4
parent 2156
f003f09144ff
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8162511: 8u111 L10n resource file updates
Summary: 8u111 L10n resource file updates
Reviewed-by: coffeys
Contributed-by: li.jiang@oracle.com

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

mercurial