src/share/classes/com/sun/tools/classfile/ExtendedAnnotation.java

Thu, 23 Jul 2009 11:37:44 -0700

author
jjg
date
Thu, 23 Jul 2009 11:37:44 -0700
changeset 328
99b7a25185aa
parent 309
664edca41e34
child 338
777a3efad0d5
permissions
-rw-r--r--

6863814: javap crashes when facing array class literals
Reviewed-by: jjg
Contributed-by: mali@csail.mit.edu

     1 /*
     2  * Copyright 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.classfile;
    28 import java.io.IOException;
    29 import java.util.ArrayList;
    30 import java.util.EnumSet;
    31 import java.util.List;
    32 import java.util.Set;
    34 import static com.sun.tools.classfile.ExtendedAnnotation.TargetAttribute.*;
    36 /**
    37  * See JSR 308 specification, section 4.1
    38  *
    39  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    40  *  you write code that depends on this, you do so at your own risk.
    41  *  This code and its internal interfaces are subject to change or
    42  *  deletion without notice.</b>
    43  */
    44 public class ExtendedAnnotation {
    45     ExtendedAnnotation(ClassReader cr) throws IOException, Annotation.InvalidAnnotation {
    46         annotation = new Annotation(cr);
    47         position = read_position(cr);
    48     }
    50     public ExtendedAnnotation(ConstantPool constant_pool,
    51             Annotation annotation, Position position) {
    52         this.annotation = annotation;
    53         this.position = position;
    54     }
    56     public int length() {
    57         int n = annotation.length();
    58         n += position_length(position);
    59         return n;
    60     }
    62     public final Annotation annotation;
    63     public final Position position;
    65     private static Position read_position(ClassReader cr) throws IOException, Annotation.InvalidAnnotation {
    66         // Copied from ClassReader
    67         int tag = (byte)cr.readUnsignedByte();  // cast to introduce signedness
    68         if (!TargetType.isValidTargetTypeValue(tag))
    69             throw new Annotation.InvalidAnnotation("invalid type annotation target type value: " + tag);
    71         TargetType type = TargetType.fromTargetTypeValue(tag);
    73         Position position = new Position();
    74         position.type = type;
    76         switch (type) {
    77         // type case
    78         case TYPECAST:
    79         case TYPECAST_GENERIC_OR_ARRAY:
    80         // object creation
    81         case INSTANCEOF:
    82         case INSTANCEOF_GENERIC_OR_ARRAY:
    83         // new expression
    84         case NEW:
    85         case NEW_GENERIC_OR_ARRAY:
    86             position.offset = cr.readUnsignedShort();
    87             break;
    88          // local variable
    89         case LOCAL_VARIABLE:
    90         case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
    91             int table_length = cr.readUnsignedShort();
    92             position.lvarOffset = new int[table_length];
    93             position.lvarLength = new int[table_length];
    94             position.lvarIndex = new int[table_length];
    95             for (int i = 0; i < table_length; ++i) {
    96                 position.lvarOffset[i] = cr.readUnsignedShort();
    97                 position.lvarLength[i] = cr.readUnsignedShort();
    98                 position.lvarIndex[i] = cr.readUnsignedShort();
    99             }
   100             break;
   101          // method receiver
   102         case METHOD_RECEIVER:
   103             // Do nothing
   104             break;
   105         // type parameters
   106         case CLASS_TYPE_PARAMETER:
   107         case METHOD_TYPE_PARAMETER:
   108             position.parameter_index = cr.readUnsignedByte();
   109             break;
   110         // type parameter bounds
   111         case CLASS_TYPE_PARAMETER_BOUND:
   112         case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   113         case METHOD_TYPE_PARAMETER_BOUND:
   114         case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   115             position.parameter_index = cr.readUnsignedByte();
   116             position.bound_index = cr.readUnsignedByte();
   117             break;
   118          // wildcards
   119         case WILDCARD_BOUND:
   120         case WILDCARD_BOUND_GENERIC_OR_ARRAY:
   121             position.wildcard_position = read_position(cr);
   122             break;
   123          // Class extends and implements clauses
   124         case CLASS_EXTENDS:
   125         case CLASS_EXTENDS_GENERIC_OR_ARRAY:
   126             position.type_index = cr.readUnsignedByte();
   127             break;
   128         // throws
   129         case THROWS:
   130             position.type_index = cr.readUnsignedByte();
   131             break;
   132         case CLASS_LITERAL:
   133         case CLASS_LITERAL_GENERIC_OR_ARRAY:
   134             position.offset = cr.readUnsignedShort();
   135             break;
   136         // method parameter: not specified
   137         case METHOD_PARAMETER_GENERIC_OR_ARRAY:
   138             position.parameter_index = cr.readUnsignedByte();
   139             break;
   140         // method type argument: wasn't specified
   141         case NEW_TYPE_ARGUMENT:
   142         case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   143         case METHOD_TYPE_ARGUMENT:
   144         case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   145             position.offset = cr.readUnsignedShort();
   146             position.type_index = cr.readUnsignedByte();
   147             break;
   148         // We don't need to worry abut these
   149         case METHOD_RETURN_GENERIC_OR_ARRAY:
   150         case FIELD_GENERIC_OR_ARRAY:
   151             break;
   152         case UNKNOWN:
   153             break;
   154         default:
   155             throw new AssertionError("Cannot be here");
   156         }
   158         if (type.hasLocation()) {
   159             int len = cr.readUnsignedShort();
   160             List<Integer> loc = new ArrayList<Integer>(len);
   161             for (int i = 0; i < len; i++)
   162                 loc.add(cr.readUnsignedByte());
   163             position.location = loc;
   164         }
   165         return position;
   166     }
   168     private static int position_length(Position pos) {
   169         int n = 0;
   170         n += 1; // target_type
   171         switch (pos.type) {
   172         // type case
   173         case TYPECAST:
   174         case TYPECAST_GENERIC_OR_ARRAY:
   175         // object creation
   176         case INSTANCEOF:
   177         case INSTANCEOF_GENERIC_OR_ARRAY:
   178         // new expression
   179         case NEW:
   180         case NEW_GENERIC_OR_ARRAY:
   181             n += 2;
   182             break;
   183          // local variable
   184         case LOCAL_VARIABLE:
   185         case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
   186             n += 2; // table_length;
   187             int table_length = pos.lvarOffset.length;
   188             n += 2 * table_length; // offset
   189             n += 2 * table_length; // length;
   190             n += 2 * table_length; // index
   191             break;
   192          // method receiver
   193         case METHOD_RECEIVER:
   194             // Do nothing
   195             break;
   196         // type parameters
   197         case CLASS_TYPE_PARAMETER:
   198         case METHOD_TYPE_PARAMETER:
   199             n += 1; // parameter_index;
   200             break;
   201         // type parameter bounds
   202         case CLASS_TYPE_PARAMETER_BOUND:
   203         case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   204         case METHOD_TYPE_PARAMETER_BOUND:
   205         case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   206             n += 1; // parameter_index
   207             n += 1; // bound_index
   208             break;
   209         case WILDCARD_BOUND:
   210         case WILDCARD_BOUND_GENERIC_OR_ARRAY:
   211             n += position_length(pos.wildcard_position);
   212             break;
   213          // Class extends and implements clauses
   214         case CLASS_EXTENDS:
   215         case CLASS_EXTENDS_GENERIC_OR_ARRAY:
   216             n += 1; // type_index
   217             break;
   218         // throws
   219         case THROWS:
   220             n += 1; // type_index
   221             break;
   222         case CLASS_LITERAL:
   223         case CLASS_LITERAL_GENERIC_OR_ARRAY:
   224             n += 1; // offset
   225             break;
   226         // method parameter: not specified
   227         case METHOD_PARAMETER_GENERIC_OR_ARRAY:
   228             n += 1; // parameter_index
   229             break;
   230         // method type argument: wasn't specified
   231         case NEW_TYPE_ARGUMENT:
   232         case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   233         case METHOD_TYPE_ARGUMENT:
   234         case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   235             n += 2; // offset
   236             n += 1; // type index
   237             break;
   238         // We don't need to worry abut these
   239         case METHOD_RETURN_GENERIC_OR_ARRAY:
   240         case FIELD_GENERIC_OR_ARRAY:
   241             break;
   242         case UNKNOWN:
   243             break;
   244         default:
   245         }
   247         if (pos.type.hasLocation()) {
   248             n += 2; // length
   249             n += 1 * pos.location.size(); // actual array size
   250         }
   252         return n;
   253     }
   255     // Code duplicated from com.sun.tools.javac.code.TypeAnnotations.Position
   256     public static class Position {
   258         public TargetType type = TargetType.UNKNOWN;
   260         // For generic/array types.
   261         public List<Integer> location = new ArrayList<Integer>();
   263         // Tree position.
   264         public int pos = -1;
   266         // For typecasts, type tests, new (and locals, as start_pc).
   267         public int offset = -1;
   269         // For locals.
   270         public int[] lvarOffset = new int[] { -1 };
   271         public int[] lvarLength = new int[] { -1 };
   272         public int[] lvarIndex = new int[] { -1 };
   274         // For type parameter bound
   275         public int bound_index = -1;
   277         // For type parameter and method parameter
   278         public int parameter_index = -1;
   280         // For class extends, implements, and throws classes
   281         public int type_index = -2;
   283         // For wildcards
   284         public Position wildcard_position = null;
   286         @Override
   287         public String toString() {
   288             StringBuilder sb = new StringBuilder();
   289             sb.append('[');
   290             sb.append(type);
   292             switch (type) {
   293             // type case
   294             case TYPECAST:
   295             case TYPECAST_GENERIC_OR_ARRAY:
   296             // object creation
   297             case INSTANCEOF:
   298             case INSTANCEOF_GENERIC_OR_ARRAY:
   299             // new expression
   300             case NEW:
   301             case NEW_GENERIC_OR_ARRAY:
   302             case NEW_TYPE_ARGUMENT:
   303             case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   304                 sb.append(", offset = ");
   305                 sb.append(offset);
   306                 break;
   307              // local variable
   308             case LOCAL_VARIABLE:
   309             case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
   310                 sb.append(", {");
   311                 for (int i = 0; i < lvarOffset.length; ++i) {
   312                     if (i != 0) sb.append("; ");
   313                     sb.append(", start_pc = ");
   314                     sb.append(lvarOffset[i]);
   315                     sb.append(", length = ");
   316                     sb.append(lvarLength[i]);
   317                     sb.append(", index = ");
   318                     sb.append(lvarIndex[i]);
   319                 }
   320                 sb.append("}");
   321                 break;
   322              // method receiver
   323             case METHOD_RECEIVER:
   324                 // Do nothing
   325                 break;
   326             // type parameters
   327             case CLASS_TYPE_PARAMETER:
   328             case METHOD_TYPE_PARAMETER:
   329                 sb.append(", param_index = ");
   330                 sb.append(parameter_index);
   331                 break;
   332             // type parameters bound
   333             case CLASS_TYPE_PARAMETER_BOUND:
   334             case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   335             case METHOD_TYPE_PARAMETER_BOUND:
   336             case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   337                 sb.append(", param_index = ");
   338                 sb.append(parameter_index);
   339                 sb.append(", bound_index = ");
   340                 sb.append(bound_index);
   341                 break;
   342              // wildcard
   343             case WILDCARD_BOUND:
   344             case WILDCARD_BOUND_GENERIC_OR_ARRAY:
   345                 sb.append(", wild_card = ");
   346                 sb.append(wildcard_position);
   347                 break;
   348              // Class extends and implements clauses
   349             case CLASS_EXTENDS:
   350             case CLASS_EXTENDS_GENERIC_OR_ARRAY:
   351                 sb.append(", type_index = ");
   352                 sb.append(type_index);
   353                 break;
   354             // throws
   355             case THROWS:
   356                 sb.append(", type_index = ");
   357                 sb.append(type_index);
   358                 break;
   359             case CLASS_LITERAL:
   360             case CLASS_LITERAL_GENERIC_OR_ARRAY:
   361                 sb.append(", offset = ");
   362                 sb.append(offset);
   363                 break;
   364             // method parameter: not specified
   365             case METHOD_PARAMETER_GENERIC_OR_ARRAY:
   366                 sb.append(", param_index = ");
   367                 sb.append(parameter_index);
   368                 break;
   369             // method type argument: wasn't specified
   370             case METHOD_TYPE_ARGUMENT:
   371             case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   372                 sb.append(", offset = ");
   373                 sb.append(offset);
   374                 sb.append(", type_index = ");
   375                 sb.append(type_index);
   376                 break;
   377             // We don't need to worry abut these
   378             case METHOD_RETURN_GENERIC_OR_ARRAY:
   379             case FIELD_GENERIC_OR_ARRAY:
   380                 break;
   381             case UNKNOWN:
   382                 break;
   383             default:
   384                 throw new AssertionError("unknown type: " + type);
   385             }
   387             // Append location data for generics/arrays.
   388             if (type.hasLocation()) {
   389                 sb.append(", location = (");
   390                 sb.append(location);
   391                 sb.append(")");
   392             }
   394             sb.append(", pos = ");
   395             sb.append(pos);
   397             sb.append(']');
   398             return sb.toString();
   399         }
   400     }
   402     // Code duplicated from com.sun.tools.javac.comp.TargetType
   403     public enum TargetType {
   405         /** For annotations on typecasts. */
   406         TYPECAST(0x00),
   408         /** For annotations on a type argument or nested array of a typecast. */
   409         TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation),
   411         /** For annotations on type tests. */
   412         INSTANCEOF(0x02),
   414         /** For annotations on a type argument or nested array of a type test. */
   415         INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation),
   417         /** For annotations on object creation expressions. */
   418         NEW(0x04),
   420         /**
   421          * For annotations on a type argument or nested array of an object creation
   422          * expression.
   423          */
   424         NEW_GENERIC_OR_ARRAY(0x05, HasLocation),
   427         /** For annotations on the method receiver. */
   428         METHOD_RECEIVER(0x06),
   430         // invalid location
   431         // METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
   433         /** For annotations on local variables. */
   434         LOCAL_VARIABLE(0x08),
   436         /** For annotations on a type argument or nested array of a local. */
   437         LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation),
   439         // already handled by regular annotations
   440         // METHOD_RETURN(0x0A),
   442         /**
   443          * For annotations on a type argument or nested array of a method return
   444          * type.
   445          */
   446         METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
   448         // already handled by regular annotations
   449         // METHOD_PARAMETER(0x0C),
   451         /** For annotations on a type argument or nested array of a method parameter. */
   452         METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
   454         // already handled by regular annotations
   455         // FIELD(0x0E),
   457         /** For annotations on a type argument or nested array of a field. */
   458         FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
   460         /** For annotations on a bound of a type parameter of a class. */
   461         CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
   463         /**
   464          * For annotations on a type argument or nested array of a bound of a type
   465          * parameter of a class.
   466          */
   467         CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
   469         /** For annotations on a bound of a type parameter of a method. */
   470         METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
   472         /**
   473          * For annotations on a type argument or nested array of a bound of a type
   474          * parameter of a method.
   475          */
   476         METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
   478         /** For annotations on the type of an "extends" or "implements" clause. */
   479         CLASS_EXTENDS(0x14),
   481         /** For annotations on the inner type of an "extends" or "implements" clause. */
   482         CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
   484         /** For annotations on a throws clause in a method declaration. */
   485         THROWS(0x16),
   487         // invalid location
   488         // THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
   490         /** For annotations in type arguments of object creation expressions. */
   491         NEW_TYPE_ARGUMENT(0x18),
   492         NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation),
   494         METHOD_TYPE_ARGUMENT(0x1A),
   495         METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation),
   497         WILDCARD_BOUND(0x1C, HasBound),
   498         WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
   500         CLASS_LITERAL(0x1E),
   501         CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation),
   503         METHOD_TYPE_PARAMETER(0x20, HasParameter),
   505         // invalid location
   506         // METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
   508         CLASS_TYPE_PARAMETER(0x22, HasParameter),
   510         // invalid location
   511         // CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
   513         /** For annotations with an unknown target. */
   514         UNKNOWN(-1);
   516         static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
   518         private final int targetTypeValue;
   519         private Set<TargetAttribute> flags;
   521         TargetType(int targetTypeValue, TargetAttribute... attrs) {
   522             if (targetTypeValue < Byte.MIN_VALUE
   523                 || targetTypeValue > Byte.MAX_VALUE)
   524                 throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
   525             this.targetTypeValue = (byte)targetTypeValue;
   526             this.flags = EnumSet.noneOf(TargetAttribute.class);
   527             for (TargetAttribute attr : attrs)
   528                 this.flags.add(attr);
   529         }
   531         /**
   532          * Returns whether or not this TargetType represents an annotation whose
   533          * target is an inner type of a generic or array type.
   534          *
   535          * @return true if this TargetType represents an annotation on an inner
   536          *         type, false otherwise
   537          */
   538         public boolean hasLocation() {
   539             return flags.contains(HasLocation);
   540         }
   542         public TargetType getGenericComplement() {
   543             if (hasLocation())
   544                 return this;
   545             else
   546                 return fromTargetTypeValue(targetTypeValue() + 1);
   547         }
   549         /**
   550          * Returns whether or not this TargetType represents an annotation whose
   551          * target has a parameter index.
   552          *
   553          * @return true if this TargetType has a parameter index,
   554          *         false otherwise
   555          */
   556         public boolean hasParameter() {
   557             return flags.contains(HasParameter);
   558         }
   560         /**
   561          * Returns whether or not this TargetType represents an annotation whose
   562          * target is a type parameter bound.
   563          *
   564          * @return true if this TargetType represents an type parameter bound
   565          *         annotation, false otherwise
   566          */
   567         public boolean hasBound() {
   568             return flags.contains(HasBound);
   569         }
   571         public int targetTypeValue() {
   572             return this.targetTypeValue;
   573         }
   575         private static TargetType[] targets = null;
   577         private static TargetType[] buildTargets() {
   578             TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
   579             TargetType[] alltargets = values();
   580             for (TargetType target : alltargets)
   581                 if (target.targetTypeValue >= 0)
   582                     targets[target.targetTypeValue] = target;
   583             for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i)
   584                 if (targets[i] == null)
   585                     targets[i] = UNKNOWN;
   586             return targets;
   587         }
   589         public static boolean isValidTargetTypeValue(int tag) {
   590             if (targets == null)
   591                 targets = buildTargets();
   593             if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
   594                 return true;
   596             return (tag >= 0 && tag < targets.length);
   597         }
   599         public static TargetType fromTargetTypeValue(int tag) {
   600             if (targets == null)
   601                 targets = buildTargets();
   603             if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
   604                 return UNKNOWN;
   606             if (tag < 0 || tag >= targets.length)
   607                 throw new IllegalArgumentException("Unknown TargetType: " + tag);
   608             return targets[tag];
   609         }
   610     }
   612     static enum TargetAttribute {
   613         HasLocation, HasParameter, HasBound;
   614     }
   615 }

mercurial