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

Fri, 26 Jun 2009 19:12:41 -0700

author
jjg
date
Fri, 26 Jun 2009 19:12:41 -0700
changeset 309
664edca41e34
child 310
7c154fdc3547
permissions
-rw-r--r--

6855544: add missing files
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

     1 /*
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import static com.sun.tools.javac.code.TargetType.TargetAttribute.*;
    30 import java.util.EnumSet;
    31 import java.util.Set;
    33 /**
    34  * Describes the type of program element an extended annotation (or extended
    35  * compound attribute) targets.
    36  *
    37  * By comparison, a Tree.Kind has enum values for all elements in the AST, and
    38  * it does not provide enough resolution for type arguments (i.e., whether an
    39  * annotation targets a type argument in a local variable, method return type,
    40  * or a typecast).
    41  *
    42  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    43  *  you write code that depends on this, you do so at your own risk.
    44  *  This code and its internal interfaces are subject to change or
    45  *  deletion without notice.</b>
    46  */
    47 public enum TargetType {
    49     //
    50     // Some target types are commented out, because Java doesn't permit such
    51     // targets.  They are included here to confirm that their omission is
    52     // intentional omission not an accidental omission.
    53     //
    55     /** For annotations on typecasts. */
    56     TYPECAST(0x00),
    58     /** For annotations on a type argument or nested array of a typecast. */
    59     TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation),
    61     /** For annotations on type tests. */
    62     INSTANCEOF(0x02),
    64     /** For annotations on a type argument or nested array of a type test. */
    65     INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation),
    67     /** For annotations on object creation expressions. */
    68     NEW(0x04),
    70     /**
    71      * For annotations on a type argument or nested array of an object creation
    72      * expression.
    73      */
    74     NEW_GENERIC_OR_ARRAY(0x05, HasLocation),
    77     /** For annotations on the method receiver. */
    78     METHOD_RECEIVER(0x06),
    80     // invalid location
    81     //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
    83     /** For annotations on local variables. */
    84     LOCAL_VARIABLE(0x08),
    86     /** For annotations on a type argument or nested array of a local. */
    87     LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation),
    89     // handled by regular annotations
    90     //@Deprecated METHOD_RETURN(0x0A),
    92     /**
    93      * For annotations on a type argument or nested array of a method return
    94      * type.
    95      */
    96     METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
    98     // handled by regular annotations
    99     //@Deprecated METHOD_PARAMETER(0x0C),
   101     /** For annotations on a type argument or nested array of a method parameter. */
   102     METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
   104     // handled by regular annotations
   105     //@Deprecated FIELD(0x0E),
   107     /** For annotations on a type argument or nested array of a field. */
   108     FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
   110     /** For annotations on a bound of a type parameter of a class. */
   111     CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
   113     /**
   114      * For annotations on a type argument or nested array of a bound of a type
   115      * parameter of a class.
   116      */
   117     CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
   119     /** For annotations on a bound of a type parameter of a method. */
   120     METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
   122     /**
   123      * For annotations on a type argument or nested array of a bound of a type
   124      * parameter of a method.
   125      */
   126     METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
   128     /** For annotations on the type of an "extends" or "implements" clause. */
   129     CLASS_EXTENDS(0x14),
   131     /** For annotations on the inner type of an "extends" or "implements" clause. */
   132     CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
   134     /** For annotations on a throws clause in a method declaration. */
   135     THROWS(0x16),
   137     // invalid location
   138     //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
   140     /** For annotations in type arguments of object creation expressions. */
   141     NEW_TYPE_ARGUMENT(0x18),
   142     NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation),
   144     METHOD_TYPE_ARGUMENT(0x1A),
   145     METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation),
   147     WILDCARD_BOUND(0x1C, HasBound),
   148     WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
   150     CLASS_LITERAL(0x1E),
   151     CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation),
   153     METHOD_TYPE_PARAMETER(0x20, HasParameter),
   155     // invalid location
   156     //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
   158     CLASS_TYPE_PARAMETER(0x22, HasParameter),
   160     // invalid location
   161     //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
   163     /** For annotations with an unknown target. */
   164     UNKNOWN(-1);
   166     static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
   168     private final int targetTypeValue;
   169     private Set<TargetAttribute> flags;
   171     TargetType(int targetTypeValue, TargetAttribute... attributes) {
   172         if (targetTypeValue < Byte.MIN_VALUE
   173                 || targetTypeValue > Byte.MAX_VALUE)
   174                 throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
   175         this.targetTypeValue = (byte)targetTypeValue;
   176         flags = EnumSet.noneOf(TargetAttribute.class);
   177         for (TargetAttribute attr : attributes)
   178             flags.add(attr);
   179     }
   181     /**
   182      * Returns whether or not this TargetType represents an annotation whose
   183      * target is an inner type of a generic or array type.
   184      *
   185      * @return true if this TargetType represents an annotation on an inner
   186      *         type, false otherwise
   187      */
   188     public boolean hasLocation() {
   189         return flags.contains(HasLocation);
   190     }
   192     public TargetType getGenericComplement() {
   193         if (hasLocation())
   194             return this;
   195         else
   196             return fromTargetTypeValue(targetTypeValue() + 1);
   197     }
   199     /**
   200      * Returns whether or not this TargetType represents an annotation whose
   201      * target has a parameter index.
   202      *
   203      * @return true if this TargetType has a parameter index,
   204      *         false otherwise
   205      */
   206     public boolean hasParameter() {
   207         return flags.contains(HasParameter);
   208     }
   210     /**
   211      * Returns whether or not this TargetType represents an annotation whose
   212      * target is a type parameter bound.
   213      *
   214      * @return true if this TargetType represents an type parameter bound
   215      *         annotation, false otherwise
   216      */
   217     public boolean hasBound() {
   218         return flags.contains(HasBound);
   219     }
   221     public int targetTypeValue() {
   222         return this.targetTypeValue;
   223     }
   225     private static TargetType[] targets = null;
   227     private static TargetType[] buildTargets() {
   228         TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
   229         TargetType[] alltargets = values();
   230         for (TargetType target : alltargets) {
   231             if (target.targetTypeValue >= 0)
   232                 targets[target.targetTypeValue] = target;
   233         }
   234         for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
   235             if (targets[i] == null)
   236                 targets[i] = UNKNOWN;
   237         }
   238         return targets;
   239     }
   241     public static boolean isValidTargetTypeValue(int tag) {
   242         if (targets == null)
   243             targets = buildTargets();
   245         if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
   246             return true;
   248         return (tag >= 0 && tag < targets.length);
   249     }
   251     public static TargetType fromTargetTypeValue(int tag) {
   252         if (targets == null)
   253             targets = buildTargets();
   255         if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
   256             return UNKNOWN;
   258         if (tag < 0 || tag >= targets.length)
   259             throw new IllegalArgumentException("Unknown TargetType: " + tag);
   260         return targets[tag];
   261     }
   263     static enum TargetAttribute {
   264         HasLocation, HasParameter, HasBound;
   265     }
   266 }

mercurial