langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
changeset 8031 d5fe2c1cecfc
parent 7681 1f0819a3341f
child 8032 e1aa25ccdabb
equal deleted inserted replaced
7848:884a6d60b235 8031:d5fe2c1cecfc
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    63 
    63 
    64     private final Symtab syms;
    64     private final Symtab syms;
    65 
    65 
    66     private final Options options;
    66     private final Options options;
    67 
    67 
    68     /** Switch: debugging output for JSR 308-related operations.
       
    69      */
       
    70     private boolean debugJSR308;
       
    71 
       
    72     /** Switch: verbose output.
    68     /** Switch: verbose output.
    73      */
    69      */
    74     private boolean verbose;
    70     private boolean verbose;
    75 
    71 
    76     /** Switch: scrable private names.
    72     /** Switch: scrable private names.
   178         target = Target.instance(context);
   174         target = Target.instance(context);
   179         source = Source.instance(context);
   175         source = Source.instance(context);
   180         types = Types.instance(context);
   176         types = Types.instance(context);
   181         fileManager = context.get(JavaFileManager.class);
   177         fileManager = context.get(JavaFileManager.class);
   182 
   178 
   183         debugJSR308    = options.isSet("TA:writer");
       
   184         verbose        = options.isSet(VERBOSE);
   179         verbose        = options.isSet(VERBOSE);
   185         scramble       = options.isSet("-scramble");
   180         scramble       = options.isSet("-scramble");
   186         scrambleAll    = options.isSet("-scrambleAll");
   181         scrambleAll    = options.isSet("-scrambleAll");
   187         retrofit       = options.isSet("-retrofit");
   182         retrofit       = options.isSet("-retrofit");
   188         genCrt         = options.isSet(XJCOV);
   183         genCrt         = options.isSet(XJCOV);
   675             databuf.appendChar(pool.put(typeSig(sym.type)));
   670             databuf.appendChar(pool.put(typeSig(sym.type)));
   676             endAttr(alenIdx);
   671             endAttr(alenIdx);
   677             acount++;
   672             acount++;
   678         }
   673         }
   679         acount += writeJavaAnnotations(sym.getAnnotationMirrors());
   674         acount += writeJavaAnnotations(sym.getAnnotationMirrors());
   680         acount += writeTypeAnnotations(sym.typeAnnotations);
       
   681         return acount;
   675         return acount;
   682     }
   676     }
   683 
   677 
   684     /** Write method parameter annotations;
   678     /** Write method parameter annotations;
   685      *  return number of attributes written.
   679      *  return number of attributes written.
   770             attrCount++;
   764             attrCount++;
   771         }
   765         }
   772         return attrCount;
   766         return attrCount;
   773     }
   767     }
   774 
   768 
   775     int writeTypeAnnotations(List<Attribute.TypeCompound> typeAnnos) {
       
   776         if (typeAnnos.isEmpty()) return 0;
       
   777 
       
   778         ListBuffer<Attribute.TypeCompound> visibles = ListBuffer.lb();
       
   779         ListBuffer<Attribute.TypeCompound> invisibles = ListBuffer.lb();
       
   780 
       
   781         for (Attribute.TypeCompound tc : typeAnnos) {
       
   782             if (tc.position.type == TargetType.UNKNOWN
       
   783                 || !tc.position.emitToClassfile())
       
   784                 continue;
       
   785             switch (types.getRetention(tc)) {
       
   786             case SOURCE: break;
       
   787             case CLASS: invisibles.append(tc); break;
       
   788             case RUNTIME: visibles.append(tc); break;
       
   789             default: ;// /* fail soft */ throw new AssertionError(vis);
       
   790             }
       
   791         }
       
   792 
       
   793         int attrCount = 0;
       
   794         if (visibles.length() != 0) {
       
   795             int attrIndex = writeAttr(names.RuntimeVisibleTypeAnnotations);
       
   796             databuf.appendChar(visibles.length());
       
   797             for (Attribute.TypeCompound p : visibles)
       
   798                 writeTypeAnnotation(p);
       
   799             endAttr(attrIndex);
       
   800             attrCount++;
       
   801         }
       
   802 
       
   803         if (invisibles.length() != 0) {
       
   804             int attrIndex = writeAttr(names.RuntimeInvisibleTypeAnnotations);
       
   805             databuf.appendChar(invisibles.length());
       
   806             for (Attribute.TypeCompound p : invisibles)
       
   807                 writeTypeAnnotation(p);
       
   808             endAttr(attrIndex);
       
   809             attrCount++;
       
   810         }
       
   811 
       
   812         return attrCount;
       
   813     }
       
   814 
       
   815     /** A visitor to write an attribute including its leading
   769     /** A visitor to write an attribute including its leading
   816      *  single-character marker.
   770      *  single-character marker.
   817      */
   771      */
   818     class AttributeWriter implements Attribute.Visitor {
   772     class AttributeWriter implements Attribute.Visitor {
   819         public void visitConstant(Attribute.Constant _value) {
   773         public void visitConstant(Attribute.Constant _value) {
   886         for (Pair<Symbol.MethodSymbol,Attribute> p : c.values) {
   840         for (Pair<Symbol.MethodSymbol,Attribute> p : c.values) {
   887             databuf.appendChar(pool.put(p.fst.name));
   841             databuf.appendChar(pool.put(p.fst.name));
   888             p.snd.accept(awriter);
   842             p.snd.accept(awriter);
   889         }
   843         }
   890     }
   844     }
   891 
       
   892     void writeTypeAnnotation(Attribute.TypeCompound c) {
       
   893         if (debugJSR308)
       
   894             System.out.println("TA: writing " + c + " at " + c.position
       
   895                     + " in " + log.currentSourceFile());
       
   896         writeCompoundAttribute(c);
       
   897         writePosition(c.position);
       
   898     }
       
   899 
       
   900     void writePosition(TypeAnnotationPosition p) {
       
   901         databuf.appendByte(p.type.targetTypeValue());
       
   902         switch (p.type) {
       
   903         // type case
       
   904         case TYPECAST:
       
   905         case TYPECAST_GENERIC_OR_ARRAY:
       
   906         // object creation
       
   907         case INSTANCEOF:
       
   908         case INSTANCEOF_GENERIC_OR_ARRAY:
       
   909         // new expression
       
   910         case NEW:
       
   911         case NEW_GENERIC_OR_ARRAY:
       
   912             databuf.appendChar(p.offset);
       
   913             break;
       
   914          // local variable
       
   915         case LOCAL_VARIABLE:
       
   916         case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
       
   917             databuf.appendChar(p.lvarOffset.length);  // for table length
       
   918             for (int i = 0; i < p.lvarOffset.length; ++i) {
       
   919                 databuf.appendChar(p.lvarOffset[i]);
       
   920                 databuf.appendChar(p.lvarLength[i]);
       
   921                 databuf.appendChar(p.lvarIndex[i]);
       
   922             }
       
   923             break;
       
   924          // method receiver
       
   925         case METHOD_RECEIVER:
       
   926             // Do nothing
       
   927             break;
       
   928         // type parameters
       
   929         case CLASS_TYPE_PARAMETER:
       
   930         case METHOD_TYPE_PARAMETER:
       
   931             databuf.appendByte(p.parameter_index);
       
   932             break;
       
   933         // type parameters bounds
       
   934         case CLASS_TYPE_PARAMETER_BOUND:
       
   935         case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
       
   936         case METHOD_TYPE_PARAMETER_BOUND:
       
   937         case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
       
   938             databuf.appendByte(p.parameter_index);
       
   939             databuf.appendByte(p.bound_index);
       
   940             break;
       
   941          // wildcards
       
   942         case WILDCARD_BOUND:
       
   943         case WILDCARD_BOUND_GENERIC_OR_ARRAY:
       
   944             writePosition(p.wildcard_position);
       
   945             break;
       
   946          // Class extends and implements clauses
       
   947         case CLASS_EXTENDS:
       
   948         case CLASS_EXTENDS_GENERIC_OR_ARRAY:
       
   949             databuf.appendChar(p.type_index);
       
   950             break;
       
   951         // throws
       
   952         case THROWS:
       
   953             databuf.appendChar(p.type_index);
       
   954             break;
       
   955         case CLASS_LITERAL:
       
   956         case CLASS_LITERAL_GENERIC_OR_ARRAY:
       
   957             databuf.appendChar(p.offset);
       
   958             break;
       
   959         // method parameter: not specified
       
   960         case METHOD_PARAMETER_GENERIC_OR_ARRAY:
       
   961             databuf.appendByte(p.parameter_index);
       
   962             break;
       
   963         // method type argument: wasn't specified
       
   964         case NEW_TYPE_ARGUMENT:
       
   965         case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
       
   966         case METHOD_TYPE_ARGUMENT:
       
   967         case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
       
   968             databuf.appendChar(p.offset);
       
   969             databuf.appendByte(p.type_index);
       
   970             break;
       
   971         // We don't need to worry abut these
       
   972         case METHOD_RETURN_GENERIC_OR_ARRAY:
       
   973         case FIELD_GENERIC_OR_ARRAY:
       
   974             break;
       
   975         case UNKNOWN:
       
   976             break;
       
   977         default:
       
   978             throw new AssertionError("unknown position: " + p);
       
   979         }
       
   980 
       
   981         // Append location data for generics/arrays.
       
   982         if (p.type.hasLocation()) {
       
   983             databuf.appendChar(p.location.size());
       
   984             for (int i : p.location)
       
   985                 databuf.appendByte((byte)i);
       
   986         }
       
   987     }
       
   988 
       
   989 /**********************************************************************
   845 /**********************************************************************
   990  * Writing Objects
   846  * Writing Objects
   991  **********************************************************************/
   847  **********************************************************************/
   992 
   848 
   993     /** Enter an inner class into the `innerClasses' set/queue.
   849     /** Enter an inner class into the `innerClasses' set/queue.
  1696             acount++;
  1552             acount++;
  1697         }
  1553         }
  1698 
  1554 
  1699         acount += writeFlagAttrs(c.flags());
  1555         acount += writeFlagAttrs(c.flags());
  1700         acount += writeJavaAnnotations(c.getAnnotationMirrors());
  1556         acount += writeJavaAnnotations(c.getAnnotationMirrors());
  1701         acount += writeTypeAnnotations(c.typeAnnotations);
       
  1702         acount += writeEnclosingMethodAttribute(c);
  1557         acount += writeEnclosingMethodAttribute(c);
  1703 
  1558 
  1704         poolbuf.appendInt(JAVA_MAGIC);
  1559         poolbuf.appendInt(JAVA_MAGIC);
  1705         poolbuf.appendChar(target.minorVersion);
  1560         poolbuf.appendChar(target.minorVersion);
  1706         poolbuf.appendChar(target.majorVersion);
  1561         poolbuf.appendChar(target.majorVersion);