Save This Page
Home » openjdk-7 » com.sun.tools » javac » code » [javadoc | source]
    1   /*
    2    * Copyright (c) 2008, 2009, Oracle and/or its affiliates. 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.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package com.sun.tools.javac.code;
   27   
   28   import static com.sun.tools.javac.code.TargetType.TargetAttribute.*;
   29   
   30   import java.util.EnumSet;
   31   import java.util.Set;
   32   
   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 supported API.
   43    *  If 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 {
   48   
   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       //
   54   
   55       /** For annotations on typecasts. */
   56       TYPECAST(0x00, IsLocal),
   57   
   58       /** For annotations on a type argument or nested array of a typecast. */
   59       TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation, IsLocal),
   60   
   61       /** For annotations on type tests. */
   62       INSTANCEOF(0x02, IsLocal),
   63   
   64       /** For annotations on a type argument or nested array of a type test. */
   65       INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation, IsLocal),
   66   
   67       /** For annotations on object creation expressions. */
   68       NEW(0x04, IsLocal),
   69   
   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, IsLocal),
   75   
   76   
   77       /** For annotations on the method receiver. */
   78       METHOD_RECEIVER(0x06),
   79   
   80       // invalid location
   81       //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
   82   
   83       /** For annotations on local variables. */
   84       LOCAL_VARIABLE(0x08, IsLocal),
   85   
   86       /** For annotations on a type argument or nested array of a local. */
   87       LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation, IsLocal),
   88   
   89       // handled by regular annotations
   90       //@Deprecated METHOD_RETURN(0x0A),
   91   
   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),
   97   
   98       // handled by regular annotations
   99       //@Deprecated METHOD_PARAMETER(0x0C),
  100   
  101       /** For annotations on a type argument or nested array of a method parameter. */
  102       METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
  103   
  104       // handled by regular annotations
  105       //@Deprecated FIELD(0x0E),
  106   
  107       /** For annotations on a type argument or nested array of a field. */
  108       FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
  109   
  110       /** For annotations on a bound of a type parameter of a class. */
  111       CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
  112   
  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),
  118   
  119       /** For annotations on a bound of a type parameter of a method. */
  120       METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
  121   
  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),
  127   
  128       /** For annotations on the type of an "extends" or "implements" clause. */
  129       CLASS_EXTENDS(0x14),
  130   
  131       /** For annotations on the inner type of an "extends" or "implements" clause. */
  132       CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
  133   
  134       /** For annotations on a throws clause in a method declaration. */
  135       THROWS(0x16),
  136   
  137       // invalid location
  138       //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
  139   
  140       /** For annotations in type arguments of object creation expressions. */
  141       NEW_TYPE_ARGUMENT(0x18, IsLocal),
  142       NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation, IsLocal),
  143   
  144       METHOD_TYPE_ARGUMENT(0x1A, IsLocal),
  145       METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation, IsLocal),
  146   
  147       WILDCARD_BOUND(0x1C, HasBound),
  148       WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
  149   
  150       CLASS_LITERAL(0x1E, IsLocal),
  151       CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation, IsLocal),
  152   
  153       METHOD_TYPE_PARAMETER(0x20, HasParameter),
  154   
  155       // invalid location
  156       //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
  157   
  158       CLASS_TYPE_PARAMETER(0x22, HasParameter),
  159   
  160       // invalid location
  161       //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
  162   
  163       /** For annotations with an unknown target. */
  164       UNKNOWN(-1);
  165   
  166       static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
  167   
  168       private final int targetTypeValue;
  169       private Set<TargetAttribute> flags;
  170   
  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       }
  180   
  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       }
  191   
  192       public TargetType getGenericComplement() {
  193           if (hasLocation())
  194               return this;
  195           else
  196               return fromTargetTypeValue(targetTypeValue() + 1);
  197       }
  198   
  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       }
  209   
  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       }
  220   
  221       /**
  222        * Returns whether or not this TargetType represents an annotation whose
  223        * target is exclusively a tree in a method body
  224        *
  225        * Note: wildcard bound targets could target a local tree and a class
  226        * member declaration signature tree
  227        */
  228       public boolean isLocal() {
  229           return flags.contains(IsLocal);
  230       }
  231   
  232       public int targetTypeValue() {
  233           return this.targetTypeValue;
  234       }
  235   
  236       private static TargetType[] targets = null;
  237   
  238       private static TargetType[] buildTargets() {
  239           TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
  240           TargetType[] alltargets = values();
  241           for (TargetType target : alltargets) {
  242               if (target.targetTypeValue >= 0)
  243                   targets[target.targetTypeValue] = target;
  244           }
  245           for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
  246               if (targets[i] == null)
  247                   targets[i] = UNKNOWN;
  248           }
  249           return targets;
  250       }
  251   
  252       public static boolean isValidTargetTypeValue(int tag) {
  253           if (targets == null)
  254               targets = buildTargets();
  255   
  256           if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
  257               return true;
  258   
  259           return (tag >= 0 && tag < targets.length);
  260       }
  261   
  262       public static TargetType fromTargetTypeValue(int tag) {
  263           if (targets == null)
  264               targets = buildTargets();
  265   
  266           if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
  267               return UNKNOWN;
  268   
  269           if (tag < 0 || tag >= targets.length)
  270               throw new IllegalArgumentException("Unknown TargetType: " + tag);
  271           return targets[tag];
  272       }
  273   
  274       static enum TargetAttribute {
  275           HasLocation, HasParameter, HasBound, IsLocal;
  276       }
  277   }

Save This Page
Home » openjdk-7 » com.sun.tools » javac » code » [javadoc | source]