langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java
changeset 13635 5c742eabba7c
parent 12335 4725d88691dd
child 13689 4d519199a6aa
equal deleted inserted replaced
13634:91f0b2d4d816 13635:5c742eabba7c
   186      */
   186      */
   187     byte[] buf = new byte[INITIAL_BUFFER_SIZE];
   187     byte[] buf = new byte[INITIAL_BUFFER_SIZE];
   188 
   188 
   189     /** The current input pointer.
   189     /** The current input pointer.
   190      */
   190      */
   191     int bp;
   191     protected int bp;
   192 
   192 
   193     /** The objects of the constant pool.
   193     /** The objects of the constant pool.
   194      */
   194      */
   195     Object[] poolObj;
   195     Object[] poolObj;
   196 
   196 
   888  * Reading Attributes
   888  * Reading Attributes
   889  ***********************************************************************/
   889  ***********************************************************************/
   890 
   890 
   891     protected enum AttributeKind { CLASS, MEMBER };
   891     protected enum AttributeKind { CLASS, MEMBER };
   892     protected abstract class AttributeReader {
   892     protected abstract class AttributeReader {
   893         AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) {
   893         protected AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) {
   894             this.name = name;
   894             this.name = name;
   895             this.version = version;
   895             this.version = version;
   896             this.kinds = kinds;
   896             this.kinds = kinds;
   897         }
   897         }
   898 
   898 
   899         boolean accepts(AttributeKind kind) {
   899         protected boolean accepts(AttributeKind kind) {
   900             if (kinds.contains(kind)) {
   900             if (kinds.contains(kind)) {
   901                 if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor))
   901                 if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor))
   902                     return true;
   902                     return true;
   903 
   903 
   904                 if (lintClassfile && !warnedAttrs.contains(name)) {
   904                 if (lintClassfile && !warnedAttrs.contains(name)) {
   913                 }
   913                 }
   914             }
   914             }
   915             return false;
   915             return false;
   916         }
   916         }
   917 
   917 
   918         abstract void read(Symbol sym, int attrLen);
   918         protected abstract void read(Symbol sym, int attrLen);
   919 
   919 
   920         final Name name;
   920         protected final Name name;
   921         final ClassFile.Version version;
   921         protected final ClassFile.Version version;
   922         final Set<AttributeKind> kinds;
   922         protected final Set<AttributeKind> kinds;
   923     }
   923     }
   924 
   924 
   925     protected Set<AttributeKind> CLASS_ATTRIBUTE =
   925     protected Set<AttributeKind> CLASS_ATTRIBUTE =
   926             EnumSet.of(AttributeKind.CLASS);
   926             EnumSet.of(AttributeKind.CLASS);
   927     protected Set<AttributeKind> MEMBER_ATTRIBUTE =
   927     protected Set<AttributeKind> MEMBER_ATTRIBUTE =
   934     private void initAttributeReaders() {
   934     private void initAttributeReaders() {
   935         AttributeReader[] readers = {
   935         AttributeReader[] readers = {
   936             // v45.3 attributes
   936             // v45.3 attributes
   937 
   937 
   938             new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
   938             new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
   939                 void read(Symbol sym, int attrLen) {
   939                 protected void read(Symbol sym, int attrLen) {
   940                     if (readAllOfClassFile || saveParameterNames)
   940                     if (readAllOfClassFile || saveParameterNames)
   941                         ((MethodSymbol)sym).code = readCode(sym);
   941                         ((MethodSymbol)sym).code = readCode(sym);
   942                     else
   942                     else
   943                         bp = bp + attrLen;
   943                         bp = bp + attrLen;
   944                 }
   944                 }
   945             },
   945             },
   946 
   946 
   947             new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
   947             new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
   948                 void read(Symbol sym, int attrLen) {
   948                 protected void read(Symbol sym, int attrLen) {
   949                     Object v = readPool(nextChar());
   949                     Object v = readPool(nextChar());
   950                     // Ignore ConstantValue attribute if field not final.
   950                     // Ignore ConstantValue attribute if field not final.
   951                     if ((sym.flags() & FINAL) != 0)
   951                     if ((sym.flags() & FINAL) != 0)
   952                         ((VarSymbol) sym).setData(v);
   952                         ((VarSymbol) sym).setData(v);
   953                 }
   953                 }
   954             },
   954             },
   955 
   955 
   956             new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
   956             new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
   957                 void read(Symbol sym, int attrLen) {
   957                 protected void read(Symbol sym, int attrLen) {
   958                     sym.flags_field |= DEPRECATED;
   958                     sym.flags_field |= DEPRECATED;
   959                 }
   959                 }
   960             },
   960             },
   961 
   961 
   962             new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
   962             new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
   963                 void read(Symbol sym, int attrLen) {
   963                 protected void read(Symbol sym, int attrLen) {
   964                     int nexceptions = nextChar();
   964                     int nexceptions = nextChar();
   965                     List<Type> thrown = List.nil();
   965                     List<Type> thrown = List.nil();
   966                     for (int j = 0; j < nexceptions; j++)
   966                     for (int j = 0; j < nexceptions; j++)
   967                         thrown = thrown.prepend(readClassSymbol(nextChar()).type);
   967                         thrown = thrown.prepend(readClassSymbol(nextChar()).type);
   968                     if (sym.type.getThrownTypes().isEmpty())
   968                     if (sym.type.getThrownTypes().isEmpty())
   969                         sym.type.asMethodType().thrown = thrown.reverse();
   969                         sym.type.asMethodType().thrown = thrown.reverse();
   970                 }
   970                 }
   971             },
   971             },
   972 
   972 
   973             new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
   973             new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
   974                 void read(Symbol sym, int attrLen) {
   974                 protected void read(Symbol sym, int attrLen) {
   975                     ClassSymbol c = (ClassSymbol) sym;
   975                     ClassSymbol c = (ClassSymbol) sym;
   976                     readInnerClasses(c);
   976                     readInnerClasses(c);
   977                 }
   977                 }
   978             },
   978             },
   979 
   979 
   980             new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
   980             new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
   981                 void read(Symbol sym, int attrLen) {
   981                 protected void read(Symbol sym, int attrLen) {
   982                     int newbp = bp + attrLen;
   982                     int newbp = bp + attrLen;
   983                     if (saveParameterNames) {
   983                     if (saveParameterNames) {
   984                         // Pick up parameter names from the variable table.
   984                         // Pick up parameter names from the variable table.
   985                         // Parameter names are not explicitly identified as such,
   985                         // Parameter names are not explicitly identified as such,
   986                         // but all parameter name entries in the LocalVariableTable
   986                         // but all parameter name entries in the LocalVariableTable
  1012                     bp = newbp;
  1012                     bp = newbp;
  1013                 }
  1013                 }
  1014             },
  1014             },
  1015 
  1015 
  1016             new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
  1016             new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
  1017                 void read(Symbol sym, int attrLen) {
  1017                 protected void read(Symbol sym, int attrLen) {
  1018                     ClassSymbol c = (ClassSymbol) sym;
  1018                     ClassSymbol c = (ClassSymbol) sym;
  1019                     Name n = readName(nextChar());
  1019                     Name n = readName(nextChar());
  1020                     c.sourcefile = new SourceFileObject(n, c.flatname);
  1020                     c.sourcefile = new SourceFileObject(n, c.flatname);
  1021                 }
  1021                 }
  1022             },
  1022             },
  1023 
  1023 
  1024             new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
  1024             new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
  1025                 void read(Symbol sym, int attrLen) {
  1025                 protected void read(Symbol sym, int attrLen) {
  1026                     // bridge methods are visible when generics not enabled
  1026                     // bridge methods are visible when generics not enabled
  1027                     if (allowGenerics || (sym.flags_field & BRIDGE) == 0)
  1027                     if (allowGenerics || (sym.flags_field & BRIDGE) == 0)
  1028                         sym.flags_field |= SYNTHETIC;
  1028                         sym.flags_field |= SYNTHETIC;
  1029                 }
  1029                 }
  1030             },
  1030             },
  1031 
  1031 
  1032             // standard v49 attributes
  1032             // standard v49 attributes
  1033 
  1033 
  1034             new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
  1034             new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
  1035                 void read(Symbol sym, int attrLen) {
  1035                 protected void read(Symbol sym, int attrLen) {
  1036                     int newbp = bp + attrLen;
  1036                     int newbp = bp + attrLen;
  1037                     readEnclosingMethodAttr(sym);
  1037                     readEnclosingMethodAttr(sym);
  1038                     bp = newbp;
  1038                     bp = newbp;
  1039                 }
  1039                 }
  1040             },
  1040             },
  1041 
  1041 
  1042             new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1042             new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1043                 @Override
  1043                 @Override
  1044                 boolean accepts(AttributeKind kind) {
  1044                 protected boolean accepts(AttributeKind kind) {
  1045                     return super.accepts(kind) && allowGenerics;
  1045                     return super.accepts(kind) && allowGenerics;
  1046                 }
  1046                 }
  1047 
  1047 
  1048                 void read(Symbol sym, int attrLen) {
  1048                 protected void read(Symbol sym, int attrLen) {
  1049                     if (sym.kind == TYP) {
  1049                     if (sym.kind == TYP) {
  1050                         ClassSymbol c = (ClassSymbol) sym;
  1050                         ClassSymbol c = (ClassSymbol) sym;
  1051                         readingClassAttr = true;
  1051                         readingClassAttr = true;
  1052                         try {
  1052                         try {
  1053                             ClassType ct1 = (ClassType)c.type;
  1053                             ClassType ct1 = (ClassType)c.type;
  1072             },
  1072             },
  1073 
  1073 
  1074             // v49 annotation attributes
  1074             // v49 annotation attributes
  1075 
  1075 
  1076             new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1076             new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1077                 void read(Symbol sym, int attrLen) {
  1077                 protected void read(Symbol sym, int attrLen) {
  1078                     attachAnnotationDefault(sym);
  1078                     attachAnnotationDefault(sym);
  1079                 }
  1079                 }
  1080             },
  1080             },
  1081 
  1081 
  1082             new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1082             new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1083                 void read(Symbol sym, int attrLen) {
  1083                 protected void read(Symbol sym, int attrLen) {
  1084                     attachAnnotations(sym);
  1084                     attachAnnotations(sym);
  1085                 }
  1085                 }
  1086             },
  1086             },
  1087 
  1087 
  1088             new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1088             new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1089                 void read(Symbol sym, int attrLen) {
  1089                 protected void read(Symbol sym, int attrLen) {
  1090                     attachParameterAnnotations(sym);
  1090                     attachParameterAnnotations(sym);
  1091                 }
  1091                 }
  1092             },
  1092             },
  1093 
  1093 
  1094             new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1094             new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1095                 void read(Symbol sym, int attrLen) {
  1095                 protected void read(Symbol sym, int attrLen) {
  1096                     attachAnnotations(sym);
  1096                     attachAnnotations(sym);
  1097                 }
  1097                 }
  1098             },
  1098             },
  1099 
  1099 
  1100             new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1100             new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1101                 void read(Symbol sym, int attrLen) {
  1101                 protected void read(Symbol sym, int attrLen) {
  1102                     attachParameterAnnotations(sym);
  1102                     attachParameterAnnotations(sym);
  1103                 }
  1103                 }
  1104             },
  1104             },
  1105 
  1105 
  1106             // additional "legacy" v49 attributes, superceded by flags
  1106             // additional "legacy" v49 attributes, superceded by flags
  1107 
  1107 
  1108             new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1108             new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1109                 void read(Symbol sym, int attrLen) {
  1109                 protected void read(Symbol sym, int attrLen) {
  1110                     if (allowAnnotations)
  1110                     if (allowAnnotations)
  1111                         sym.flags_field |= ANNOTATION;
  1111                         sym.flags_field |= ANNOTATION;
  1112                 }
  1112                 }
  1113             },
  1113             },
  1114 
  1114 
  1115             new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) {
  1115             new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) {
  1116                 void read(Symbol sym, int attrLen) {
  1116                 protected void read(Symbol sym, int attrLen) {
  1117                     sym.flags_field |= BRIDGE;
  1117                     sym.flags_field |= BRIDGE;
  1118                     if (!allowGenerics)
  1118                     if (!allowGenerics)
  1119                         sym.flags_field &= ~SYNTHETIC;
  1119                         sym.flags_field &= ~SYNTHETIC;
  1120                 }
  1120                 }
  1121             },
  1121             },
  1122 
  1122 
  1123             new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1123             new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1124                 void read(Symbol sym, int attrLen) {
  1124                 protected void read(Symbol sym, int attrLen) {
  1125                     sym.flags_field |= ENUM;
  1125                     sym.flags_field |= ENUM;
  1126                 }
  1126                 }
  1127             },
  1127             },
  1128 
  1128 
  1129             new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1129             new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1130                 void read(Symbol sym, int attrLen) {
  1130                 protected void read(Symbol sym, int attrLen) {
  1131                     if (allowVarargs)
  1131                     if (allowVarargs)
  1132                         sym.flags_field |= VARARGS;
  1132                         sym.flags_field |= VARARGS;
  1133                 }
  1133                 }
  1134             },
  1134             },
  1135 
  1135 
  1151             printCCF("ccf.unrecognized.attribute", attrName);
  1151             printCCF("ccf.unrecognized.attribute", attrName);
  1152     }
  1152     }
  1153 
  1153 
  1154 
  1154 
  1155 
  1155 
  1156     void readEnclosingMethodAttr(Symbol sym) {
  1156     protected void readEnclosingMethodAttr(Symbol sym) {
  1157         // sym is a nested class with an "Enclosing Method" attribute
  1157         // sym is a nested class with an "Enclosing Method" attribute
  1158         // remove sym from it's current owners scope and place it in
  1158         // remove sym from it's current owners scope and place it in
  1159         // the scope specified by the attribute
  1159         // the scope specified by the attribute
  1160         sym.owner.members().remove(sym);
  1160         sym.owner.members().remove(sym);
  1161         ClassSymbol self = (ClassSymbol)sym;
  1161         ClassSymbol self = (ClassSymbol)sym;