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

changeset 309
664edca41e34
child 310
7c154fdc3547
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/TargetType.java	Fri Jun 26 19:12:41 2009 -0700
     1.3 @@ -0,0 +1,266 @@
     1.4 +/*
     1.5 + * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.code;
    1.30 +
    1.31 +import static com.sun.tools.javac.code.TargetType.TargetAttribute.*;
    1.32 +
    1.33 +import java.util.EnumSet;
    1.34 +import java.util.Set;
    1.35 +
    1.36 +/**
    1.37 + * Describes the type of program element an extended annotation (or extended
    1.38 + * compound attribute) targets.
    1.39 + *
    1.40 + * By comparison, a Tree.Kind has enum values for all elements in the AST, and
    1.41 + * it does not provide enough resolution for type arguments (i.e., whether an
    1.42 + * annotation targets a type argument in a local variable, method return type,
    1.43 + * or a typecast).
    1.44 + *
    1.45 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.46 + *  you write code that depends on this, you do so at your own risk.
    1.47 + *  This code and its internal interfaces are subject to change or
    1.48 + *  deletion without notice.</b>
    1.49 + */
    1.50 +public enum TargetType {
    1.51 +
    1.52 +    //
    1.53 +    // Some target types are commented out, because Java doesn't permit such
    1.54 +    // targets.  They are included here to confirm that their omission is
    1.55 +    // intentional omission not an accidental omission.
    1.56 +    //
    1.57 +
    1.58 +    /** For annotations on typecasts. */
    1.59 +    TYPECAST(0x00),
    1.60 +
    1.61 +    /** For annotations on a type argument or nested array of a typecast. */
    1.62 +    TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation),
    1.63 +
    1.64 +    /** For annotations on type tests. */
    1.65 +    INSTANCEOF(0x02),
    1.66 +
    1.67 +    /** For annotations on a type argument or nested array of a type test. */
    1.68 +    INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation),
    1.69 +
    1.70 +    /** For annotations on object creation expressions. */
    1.71 +    NEW(0x04),
    1.72 +
    1.73 +    /**
    1.74 +     * For annotations on a type argument or nested array of an object creation
    1.75 +     * expression.
    1.76 +     */
    1.77 +    NEW_GENERIC_OR_ARRAY(0x05, HasLocation),
    1.78 +
    1.79 +
    1.80 +    /** For annotations on the method receiver. */
    1.81 +    METHOD_RECEIVER(0x06),
    1.82 +
    1.83 +    // invalid location
    1.84 +    //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
    1.85 +
    1.86 +    /** For annotations on local variables. */
    1.87 +    LOCAL_VARIABLE(0x08),
    1.88 +
    1.89 +    /** For annotations on a type argument or nested array of a local. */
    1.90 +    LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation),
    1.91 +
    1.92 +    // handled by regular annotations
    1.93 +    //@Deprecated METHOD_RETURN(0x0A),
    1.94 +
    1.95 +    /**
    1.96 +     * For annotations on a type argument or nested array of a method return
    1.97 +     * type.
    1.98 +     */
    1.99 +    METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
   1.100 +
   1.101 +    // handled by regular annotations
   1.102 +    //@Deprecated METHOD_PARAMETER(0x0C),
   1.103 +
   1.104 +    /** For annotations on a type argument or nested array of a method parameter. */
   1.105 +    METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
   1.106 +
   1.107 +    // handled by regular annotations
   1.108 +    //@Deprecated FIELD(0x0E),
   1.109 +
   1.110 +    /** For annotations on a type argument or nested array of a field. */
   1.111 +    FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
   1.112 +
   1.113 +    /** For annotations on a bound of a type parameter of a class. */
   1.114 +    CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
   1.115 +
   1.116 +    /**
   1.117 +     * For annotations on a type argument or nested array of a bound of a type
   1.118 +     * parameter of a class.
   1.119 +     */
   1.120 +    CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
   1.121 +
   1.122 +    /** For annotations on a bound of a type parameter of a method. */
   1.123 +    METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
   1.124 +
   1.125 +    /**
   1.126 +     * For annotations on a type argument or nested array of a bound of a type
   1.127 +     * parameter of a method.
   1.128 +     */
   1.129 +    METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
   1.130 +
   1.131 +    /** For annotations on the type of an "extends" or "implements" clause. */
   1.132 +    CLASS_EXTENDS(0x14),
   1.133 +
   1.134 +    /** For annotations on the inner type of an "extends" or "implements" clause. */
   1.135 +    CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
   1.136 +
   1.137 +    /** For annotations on a throws clause in a method declaration. */
   1.138 +    THROWS(0x16),
   1.139 +
   1.140 +    // invalid location
   1.141 +    //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
   1.142 +
   1.143 +    /** For annotations in type arguments of object creation expressions. */
   1.144 +    NEW_TYPE_ARGUMENT(0x18),
   1.145 +    NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation),
   1.146 +
   1.147 +    METHOD_TYPE_ARGUMENT(0x1A),
   1.148 +    METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation),
   1.149 +
   1.150 +    WILDCARD_BOUND(0x1C, HasBound),
   1.151 +    WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
   1.152 +
   1.153 +    CLASS_LITERAL(0x1E),
   1.154 +    CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation),
   1.155 +
   1.156 +    METHOD_TYPE_PARAMETER(0x20, HasParameter),
   1.157 +
   1.158 +    // invalid location
   1.159 +    //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
   1.160 +
   1.161 +    CLASS_TYPE_PARAMETER(0x22, HasParameter),
   1.162 +
   1.163 +    // invalid location
   1.164 +    //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
   1.165 +
   1.166 +    /** For annotations with an unknown target. */
   1.167 +    UNKNOWN(-1);
   1.168 +
   1.169 +    static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
   1.170 +
   1.171 +    private final int targetTypeValue;
   1.172 +    private Set<TargetAttribute> flags;
   1.173 +
   1.174 +    TargetType(int targetTypeValue, TargetAttribute... attributes) {
   1.175 +        if (targetTypeValue < Byte.MIN_VALUE
   1.176 +                || targetTypeValue > Byte.MAX_VALUE)
   1.177 +                throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
   1.178 +        this.targetTypeValue = (byte)targetTypeValue;
   1.179 +        flags = EnumSet.noneOf(TargetAttribute.class);
   1.180 +        for (TargetAttribute attr : attributes)
   1.181 +            flags.add(attr);
   1.182 +    }
   1.183 +
   1.184 +    /**
   1.185 +     * Returns whether or not this TargetType represents an annotation whose
   1.186 +     * target is an inner type of a generic or array type.
   1.187 +     *
   1.188 +     * @return true if this TargetType represents an annotation on an inner
   1.189 +     *         type, false otherwise
   1.190 +     */
   1.191 +    public boolean hasLocation() {
   1.192 +        return flags.contains(HasLocation);
   1.193 +    }
   1.194 +
   1.195 +    public TargetType getGenericComplement() {
   1.196 +        if (hasLocation())
   1.197 +            return this;
   1.198 +        else
   1.199 +            return fromTargetTypeValue(targetTypeValue() + 1);
   1.200 +    }
   1.201 +
   1.202 +    /**
   1.203 +     * Returns whether or not this TargetType represents an annotation whose
   1.204 +     * target has a parameter index.
   1.205 +     *
   1.206 +     * @return true if this TargetType has a parameter index,
   1.207 +     *         false otherwise
   1.208 +     */
   1.209 +    public boolean hasParameter() {
   1.210 +        return flags.contains(HasParameter);
   1.211 +    }
   1.212 +
   1.213 +    /**
   1.214 +     * Returns whether or not this TargetType represents an annotation whose
   1.215 +     * target is a type parameter bound.
   1.216 +     *
   1.217 +     * @return true if this TargetType represents an type parameter bound
   1.218 +     *         annotation, false otherwise
   1.219 +     */
   1.220 +    public boolean hasBound() {
   1.221 +        return flags.contains(HasBound);
   1.222 +    }
   1.223 +
   1.224 +    public int targetTypeValue() {
   1.225 +        return this.targetTypeValue;
   1.226 +    }
   1.227 +
   1.228 +    private static TargetType[] targets = null;
   1.229 +
   1.230 +    private static TargetType[] buildTargets() {
   1.231 +        TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
   1.232 +        TargetType[] alltargets = values();
   1.233 +        for (TargetType target : alltargets) {
   1.234 +            if (target.targetTypeValue >= 0)
   1.235 +                targets[target.targetTypeValue] = target;
   1.236 +        }
   1.237 +        for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
   1.238 +            if (targets[i] == null)
   1.239 +                targets[i] = UNKNOWN;
   1.240 +        }
   1.241 +        return targets;
   1.242 +    }
   1.243 +
   1.244 +    public static boolean isValidTargetTypeValue(int tag) {
   1.245 +        if (targets == null)
   1.246 +            targets = buildTargets();
   1.247 +
   1.248 +        if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
   1.249 +            return true;
   1.250 +
   1.251 +        return (tag >= 0 && tag < targets.length);
   1.252 +    }
   1.253 +
   1.254 +    public static TargetType fromTargetTypeValue(int tag) {
   1.255 +        if (targets == null)
   1.256 +            targets = buildTargets();
   1.257 +
   1.258 +        if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
   1.259 +            return UNKNOWN;
   1.260 +
   1.261 +        if (tag < 0 || tag >= targets.length)
   1.262 +            throw new IllegalArgumentException("Unknown TargetType: " + tag);
   1.263 +        return targets[tag];
   1.264 +    }
   1.265 +
   1.266 +    static enum TargetAttribute {
   1.267 +        HasLocation, HasParameter, HasBound;
   1.268 +    }
   1.269 +}

mercurial