langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
changeset 36153 ed5063b304be
parent 34560 b6a567b677f7
child 36526 3b41f1c69604
equal deleted inserted replaced
36047:1c076468bf7d 36153:ed5063b304be
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2016, 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
    70 
    70 
    71     /** Switch: verbose output.
    71     /** Switch: verbose output.
    72      */
    72      */
    73     private boolean verbose;
    73     private boolean verbose;
    74 
    74 
    75     /** Switch: scramble private field names.
       
    76      */
       
    77     private boolean scramble;
       
    78 
       
    79     /** Switch: scramble all field names.
       
    80      */
       
    81     private boolean scrambleAll;
       
    82 
       
    83     /** Switch: retrofit mode.
       
    84      */
       
    85     private boolean retrofit;
       
    86 
       
    87     /** Switch: emit source file attribute.
    75     /** Switch: emit source file attribute.
    88      */
    76      */
    89     private boolean emitSourceFile;
    77     private boolean emitSourceFile;
    90 
    78 
    91     /** Switch: generate CharacterRangeTable attribute.
    79     /** Switch: generate CharacterRangeTable attribute.
   182         types = Types.instance(context);
   170         types = Types.instance(context);
   183         fileManager = context.get(JavaFileManager.class);
   171         fileManager = context.get(JavaFileManager.class);
   184         signatureGen = new CWSignatureGenerator(types);
   172         signatureGen = new CWSignatureGenerator(types);
   185 
   173 
   186         verbose        = options.isSet(VERBOSE);
   174         verbose        = options.isSet(VERBOSE);
   187         scramble       = options.isSet("-scramble");
       
   188         scrambleAll    = options.isSet("-scrambleAll");
       
   189         retrofit       = options.isSet("-retrofit");
       
   190         genCrt         = options.isSet(XJCOV);
   175         genCrt         = options.isSet(XJCOV);
   191         debugstackmap  = options.isSet("debugstackmap");
   176         debugstackmap  = options.isSet("debugstackmap");
   192 
   177 
   193         emitSourceFile = options.isUnset(G_CUSTOM) ||
   178         emitSourceFile = options.isUnset(G_CUSTOM) ||
   194                             options.isSet(G_CUSTOM, "source");
   179                             options.isSet(G_CUSTOM, "source");
   489         if (pool.pp > Pool.MAX_ENTRIES)
   474         if (pool.pp > Pool.MAX_ENTRIES)
   490             throw new PoolOverflow();
   475             throw new PoolOverflow();
   491         putChar(poolbuf, poolCountIdx, pool.pp);
   476         putChar(poolbuf, poolCountIdx, pool.pp);
   492     }
   477     }
   493 
   478 
   494     /** Given a field, return its name.
       
   495      */
       
   496     Name fieldName(Symbol sym) {
       
   497         if (scramble && (sym.flags() & PRIVATE) != 0 ||
       
   498             scrambleAll && (sym.flags() & (PROTECTED | PUBLIC)) == 0)
       
   499             return names.fromString("_$" + sym.name.getIndex());
       
   500         else
       
   501             return sym.name;
       
   502     }
       
   503 
       
   504     /** Given a symbol, return its name-and-type.
   479     /** Given a symbol, return its name-and-type.
   505      */
   480      */
   506     NameAndType nameType(Symbol sym) {
   481     NameAndType nameType(Symbol sym) {
   507         return new NameAndType(fieldName(sym),
   482         return new NameAndType(sym.name, sym.externalType(types), types);
   508                                retrofit
   483         // the NameAndType is generated from a symbol reference, and the
   509                                ? sym.erasure(types)
       
   510                                : sym.externalType(types), types);
       
   511         // if we retrofit, then the NameAndType has been read in as is
       
   512         // and no change is necessary. If we compile normally, the
       
   513         // NameAndType is generated from a symbol reference, and the
       
   514         // adjustment of adding an additional this$n parameter needs to be made.
   484         // adjustment of adding an additional this$n parameter needs to be made.
   515     }
   485     }
   516 
   486 
   517 /******************************************************************
   487 /******************************************************************
   518  * Writing Attributes
   488  * Writing Attributes
  1053     void writeField(VarSymbol v) {
  1023     void writeField(VarSymbol v) {
  1054         int flags = adjustFlags(v.flags());
  1024         int flags = adjustFlags(v.flags());
  1055         databuf.appendChar(flags);
  1025         databuf.appendChar(flags);
  1056         if (dumpFieldModifiers) {
  1026         if (dumpFieldModifiers) {
  1057             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
  1027             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
  1058             pw.println("FIELD  " + fieldName(v));
  1028             pw.println("FIELD  " + v.name);
  1059             pw.println("---" + flagNames(v.flags()));
  1029             pw.println("---" + flagNames(v.flags()));
  1060         }
  1030         }
  1061         databuf.appendChar(pool.put(fieldName(v)));
  1031         databuf.appendChar(pool.put(v.name));
  1062         databuf.appendChar(pool.put(typeSig(v.erasure(types))));
  1032         databuf.appendChar(pool.put(typeSig(v.erasure(types))));
  1063         int acountIdx = beginAttrs();
  1033         int acountIdx = beginAttrs();
  1064         int acount = 0;
  1034         int acount = 0;
  1065         if (v.getConstValue() != null) {
  1035         if (v.getConstValue() != null) {
  1066             int alenIdx = writeAttr(names.ConstantValue);
  1036             int alenIdx = writeAttr(names.ConstantValue);
  1077     void writeMethod(MethodSymbol m) {
  1047     void writeMethod(MethodSymbol m) {
  1078         int flags = adjustFlags(m.flags());
  1048         int flags = adjustFlags(m.flags());
  1079         databuf.appendChar(flags);
  1049         databuf.appendChar(flags);
  1080         if (dumpMethodModifiers) {
  1050         if (dumpMethodModifiers) {
  1081             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
  1051             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
  1082             pw.println("METHOD  " + fieldName(m));
  1052             pw.println("METHOD  " + m.name);
  1083             pw.println("---" + flagNames(m.flags()));
  1053             pw.println("---" + flagNames(m.flags()));
  1084         }
  1054         }
  1085         databuf.appendChar(pool.put(fieldName(m)));
  1055         databuf.appendChar(pool.put(m.name));
  1086         databuf.appendChar(pool.put(typeSig(m.externalType(types))));
  1056         databuf.appendChar(pool.put(typeSig(m.externalType(types))));
  1087         int acountIdx = beginAttrs();
  1057         int acountIdx = beginAttrs();
  1088         int acount = 0;
  1058         int acount = 0;
  1089         if (m.code != null) {
  1059         if (m.code != null) {
  1090             int alenIdx = writeAttr(names.Code);
  1060             int alenIdx = writeAttr(names.Code);