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

changeset 2100
1e7ad879f15e
child 2154
ac839d6f4953
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/AnnoConstruct.java	Thu Oct 10 08:51:55 2013 +0200
     1.3 @@ -0,0 +1,238 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.javac.code;
    1.29 +
    1.30 +import java.lang.annotation.Annotation;
    1.31 +import java.lang.reflect.InvocationTargetException;
    1.32 +import java.lang.reflect.Method;
    1.33 +
    1.34 +import javax.lang.model.AnnotatedConstruct;
    1.35 +
    1.36 +import com.sun.tools.javac.model.AnnotationProxyMaker;
    1.37 +import com.sun.tools.javac.util.List;
    1.38 +import com.sun.tools.javac.util.ListBuffer;
    1.39 +
    1.40 +/**
    1.41 + * Common super type for annotated constructs such as Types and Symbols.
    1.42 + *
    1.43 + * This class should *not* contain any fields since it would have a significant
    1.44 + * impact on the javac memory footprint.
    1.45 + *
    1.46 + * <p><b>This is NOT part of any supported API.
    1.47 + * If you write code that depends on this, you do so at your own
    1.48 + * risk.  This code and its internal interfaces are subject to change
    1.49 + * or deletion without notice.</b></p>
    1.50 + */
    1.51 +public abstract class AnnoConstruct implements AnnotatedConstruct {
    1.52 +
    1.53 +
    1.54 +    // Override to enforce a narrower return type.
    1.55 +    @Override
    1.56 +    public abstract List<? extends Attribute.Compound> getAnnotationMirrors();
    1.57 +
    1.58 +
    1.59 +    // This method is part of the javax.lang.model API, do not use this in javac code.
    1.60 +    protected <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
    1.61 +        String name = annoType.getName();
    1.62 +
    1.63 +        for (Attribute.Compound anno : getAnnotationMirrors()) {
    1.64 +            if (name.equals(anno.type.tsym.flatName().toString()))
    1.65 +                return anno;
    1.66 +        }
    1.67 +
    1.68 +        return null;
    1.69 +    }
    1.70 +
    1.71 +
    1.72 +    @SuppressWarnings("unchecked")
    1.73 +    protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
    1.74 +        return (A[]) java.lang.reflect.Array.newInstance(annoType, 0);  // annoType is the Class for A
    1.75 +    }
    1.76 +
    1.77 +
    1.78 +    // This method is part of the javax.lang.model API, do not use this in javac code.
    1.79 +    public <A extends Annotation> A[] getAnnotationsByType(Class<A> annoType) {
    1.80 +
    1.81 +        if (!annoType.isAnnotation())
    1.82 +            throw new IllegalArgumentException("Not an annotation type: "
    1.83 +                                               + annoType);
    1.84 +        // If annoType does not declare a container this is equivalent to wrapping
    1.85 +        // getAnnotation(...) in an array.
    1.86 +        Class <? extends Annotation> containerType = getContainer(annoType);
    1.87 +        if (containerType == null) {
    1.88 +            A res = getAnnotation(annoType);
    1.89 +            int size = res == null ? 0 : 1;
    1.90 +
    1.91 +            @SuppressWarnings("unchecked") // annoType is the Class for A
    1.92 +            A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
    1.93 +            if (res != null)
    1.94 +                arr[0] = res;
    1.95 +            return arr;
    1.96 +        }
    1.97 +
    1.98 +        // So we have a containing type
    1.99 +        String annoTypeName = annoType.getName();
   1.100 +        String containerTypeName = containerType.getName();
   1.101 +        int directIndex = -1, containerIndex = -1;
   1.102 +        Attribute.Compound direct = null, container = null;
   1.103 +        // Find directly (explicit or implicit) present annotations
   1.104 +        int index = -1;
   1.105 +        for (Attribute.Compound attribute : getAnnotationMirrors()) {
   1.106 +            index++;
   1.107 +            if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) {
   1.108 +                directIndex = index;
   1.109 +                direct = attribute;
   1.110 +            } else if(containerTypeName != null &&
   1.111 +                      attribute.type.tsym.flatName().contentEquals(containerTypeName)) {
   1.112 +                containerIndex = index;
   1.113 +                container = attribute;
   1.114 +            }
   1.115 +        }
   1.116 +
   1.117 +        // Deal with inherited annotations
   1.118 +        if (direct == null && container == null)
   1.119 +            return getInheritedAnnotations(annoType);
   1.120 +
   1.121 +        // Pack them in an array
   1.122 +        Attribute[] contained0 = null;
   1.123 +        if (container != null)
   1.124 +            contained0 = unpackAttributes(container);
   1.125 +        ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
   1.126 +        if (contained0 != null) {
   1.127 +            for (Attribute a : contained0)
   1.128 +                if (a instanceof Attribute.Compound)
   1.129 +                    compounds = compounds.append((Attribute.Compound)a);
   1.130 +        }
   1.131 +        Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]);
   1.132 +
   1.133 +        int size = (direct == null ? 0 : 1) + contained.length;
   1.134 +        @SuppressWarnings("unchecked") // annoType is the Class for A
   1.135 +        A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
   1.136 +
   1.137 +        // if direct && container, which is first?
   1.138 +        int insert = -1;
   1.139 +        int length = arr.length;
   1.140 +        if (directIndex >= 0 && containerIndex >= 0) {
   1.141 +            if (directIndex < containerIndex) {
   1.142 +                arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
   1.143 +                insert = 1;
   1.144 +            } else {
   1.145 +                arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
   1.146 +                insert = 0;
   1.147 +                length--;
   1.148 +            }
   1.149 +        } else if (directIndex >= 0) {
   1.150 +            arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
   1.151 +            return arr;
   1.152 +        } else {
   1.153 +            // Only container
   1.154 +            insert = 0;
   1.155 +        }
   1.156 +
   1.157 +        for (int i = 0; i + insert < length; i++)
   1.158 +            arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
   1.159 +
   1.160 +        return arr;
   1.161 +    }
   1.162 +
   1.163 +
   1.164 +    // This method is part of the javax.lang.model API, do not use this in javac code.
   1.165 +    public <A extends Annotation> A getAnnotation(Class<A> annoType) {
   1.166 +
   1.167 +        if (!annoType.isAnnotation())
   1.168 +            throw new IllegalArgumentException("Not an annotation type: " + annoType);
   1.169 +
   1.170 +        Attribute.Compound c = getAttribute(annoType);
   1.171 +        return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
   1.172 +    }
   1.173 +
   1.174 +    // Needed to unpack the runtime view of containing annotations
   1.175 +    private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable();
   1.176 +    private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod();
   1.177 +
   1.178 +    private static Class<? extends Annotation> initRepeatable() {
   1.179 +        try {
   1.180 +            // Repeatable will not be available when bootstrapping on
   1.181 +            // JDK 7 so use a reflective lookup instead of a class
   1.182 +            // literal for Repeatable.class.
   1.183 +            return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class);
   1.184 +        } catch (ClassNotFoundException | SecurityException e) {
   1.185 +            return null;
   1.186 +        }
   1.187 +    }
   1.188 +
   1.189 +    private static Method initValueElementMethod() {
   1.190 +        if (REPEATABLE_CLASS == null)
   1.191 +            return null;
   1.192 +
   1.193 +        Method m = null;
   1.194 +        try {
   1.195 +            m = REPEATABLE_CLASS.getMethod("value");
   1.196 +            if (m != null)
   1.197 +                m.setAccessible(true);
   1.198 +            return m;
   1.199 +        } catch (NoSuchMethodException e) {
   1.200 +            return null;
   1.201 +        }
   1.202 +    }
   1.203 +
   1.204 +
   1.205 +    // Helper to getAnnotationsByType
   1.206 +    private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
   1.207 +        // Since we can not refer to java.lang.annotation.Repeatable until we are
   1.208 +        // bootstrapping with java 8 we need to get the Repeatable annotation using
   1.209 +        // reflective invocations instead of just using its type and element method.
   1.210 +        if (REPEATABLE_CLASS != null &&
   1.211 +            VALUE_ELEMENT_METHOD != null) {
   1.212 +            // Get the Repeatable instance on the annotations declaration
   1.213 +            Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS);
   1.214 +            if (repeatable != null) {
   1.215 +                try {
   1.216 +                    // Get the value element, it should be a class
   1.217 +                    // indicating the containing annotation type
   1.218 +                    @SuppressWarnings("unchecked")
   1.219 +                    Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable);
   1.220 +                    if (containerType == null)
   1.221 +                        return null;
   1.222 +
   1.223 +                    return containerType;
   1.224 +                } catch (ClassCastException | IllegalAccessException | InvocationTargetException e) {
   1.225 +                    return null;
   1.226 +                }
   1.227 +            }
   1.228 +        }
   1.229 +        return null;
   1.230 +    }
   1.231 +
   1.232 +
   1.233 +    // Helper to getAnnotationsByType
   1.234 +    private static Attribute[] unpackAttributes(Attribute.Compound container) {
   1.235 +        // We now have an instance of the container,
   1.236 +        // unpack it returning an instance of the
   1.237 +        // contained type or null
   1.238 +        return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
   1.239 +    }
   1.240 +
   1.241 +}

mercurial