1 /*
   2  * Copyright (c) 2016, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.internal.org.objectweb.asm;
  25 
  26 import java.lang.reflect.InaccessibleObjectException;
  27 
  28 public class ClassWriterExt extends ClassWriter {
  29     private boolean cacheInvokeDynamic = true;
  30     private boolean cacheMTypes = true;
  31     private boolean cacheMHandles = true;
  32 
  33     private final Item key = new Item();
  34 
  35     public ClassWriterExt(ClassReader cr, int flags) {
  36         super(cr, flags);
  37     }
  38 
  39     public ClassWriterExt(int flags) {
  40         super(flags);
  41     }
  42 
  43     @Override
  44     Item newInvokeDynamicItem(final String name, final String desc,
  45                     final Handle bsm, final Object... bsmArgs) {
  46         if (cacheInvokeDynamic) {
  47             return super.newInvokeDynamicItem(name, desc, bsm, bsmArgs);
  48         }
  49         int type = ClassWriter.INDY;
  50         disableItemHashTableFor(type);
  51         Item result;
  52         try {
  53             return super.newInvokeDynamicItem(name, desc, bsm, bsmArgs);
  54         } finally {
  55             restoreItemHashTableFor(type);
  56         }
  57     }
  58 
  59     @Override
  60     Item newStringishItem(final int type, final String value) {
  61         if (type != ClassWriter.MTYPE) {
  62             return super.newStringishItem(type, value);
  63         }
  64         if (cacheMTypes) {
  65             return super.newStringishItem(type, value);
  66         }
  67         disableItemHashTableFor(type);
  68         try {
  69             return super.newStringishItem(type, value);
  70         } finally {
  71             restoreItemHashTableFor(type);
  72         }
  73     }
  74 
  75     @Override
  76     Item newHandleItem(final int tag, final String owner, final String name,
  77             final String desc, final boolean itf) {
  78         if (cacheMHandles) {
  79             return super.newHandleItem(tag, owner, name, desc, itf);
  80         }
  81         int type = ClassWriter.HANDLE_BASE + tag;
  82         disableItemHashTableFor(type);
  83         try {
  84             return super.newHandleItem(tag, owner, name, desc, itf);
  85         } finally {
  86             restoreItemHashTableFor(type);
  87         }
  88     }
  89 
  90     private void disableItemHashTableFor(int type) {
  91         for (Item i : items) {
  92             while (i != null) {
  93                 if (i.type == type) {
  94                     i.type = -type;
  95                 }
  96                 i = i.next;
  97             }
  98         }
  99     }
 100 
 101     private void restoreItemHashTableFor(int type) {
 102         for (Item i : items) {
 103             while (i != null) {
 104                 if (i.type == -type) {
 105                     i.type = type;
 106                 }
 107                 i = i.next;
 108             }
 109         }
 110     }
 111 
 112     public void setCacheInvokeDynamic(boolean value) {
 113         cacheInvokeDynamic = value;
 114     }
 115     public void setCacheMTypes(boolean value) {
 116         cacheMTypes = value;
 117     }
 118     public void setCacheMHandles(boolean value) {
 119         cacheMHandles = value;
 120     }
 121 
 122     public int getBytecodeLength(MethodVisitor mv) {
 123         ByteVector code;
 124         try {
 125             java.lang.reflect.Field field = mv.getClass().getDeclaredField("code");
 126             field.setAccessible(true);
 127             code = (ByteVector) field.get(mv);
 128         } catch (InaccessibleObjectException | SecurityException | ReflectiveOperationException e) {
 129             throw new Error("can not read field 'code' from class " + mv.getClass(), e);
 130         }
 131         try {
 132             java.lang.reflect.Field field = code.getClass().getDeclaredField("length");
 133             field.setAccessible(true);
 134             return field.getInt(code);
 135         } catch (InaccessibleObjectException | SecurityException | ReflectiveOperationException e) {
 136             throw new Error("can not read field 'length' from class " + code.getClass(), e);
 137         }
 138     }
 139 }
 140