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     public ClassWriterExt(ClassReader cr, int flags) {
  34         super(cr, flags);
  35     }
  36 
  37     public ClassWriterExt(int flags) {
  38         super(flags);
  39     }
  40 /*
  41     @Override
  42     Item newInvokeDynamicItem(final String name, final String desc,
  43                     final Handle bsm, final Object... bsmArgs) {
  44         if (cacheInvokeDynamic) {
  45             return super.newInvokeDynamicItem(name, desc, bsm, bsmArgs);
  46         }
  47         int type = ClassWriter.INDY;
  48         disableItemHashTableFor(type);
  49         Item result;
  50         try {
  51             return super.newInvokeDynamicItem(name, desc, bsm, bsmArgs);
  52         } finally {
  53             restoreItemHashTableFor(type);
  54         }
  55     }
  56 
  57     @Override
  58     Item newStringishItem(final int type, final String value) {
  59         if (type != ClassWriter.MTYPE) {
  60             return super.newStringishItem(type, value);
  61         }
  62         if (cacheMTypes) {
  63             return super.newStringishItem(type, value);
  64         }
  65         disableItemHashTableFor(type);
  66         try {
  67             return super.newStringishItem(type, value);
  68         } finally {
  69             restoreItemHashTableFor(type);
  70         }
  71     }
  72 
  73     @Override
  74     Item newHandleItem(final int tag, final String owner, final String name,
  75             final String desc, final boolean itf) {
  76         if (cacheMHandles) {
  77             return super.newHandleItem(tag, owner, name, desc, itf);
  78         }
  79         int type = ClassWriter.HANDLE_BASE + tag;
  80         disableItemHashTableFor(type);
  81         try {
  82             return super.newHandleItem(tag, owner, name, desc, itf);
  83         } finally {
  84             restoreItemHashTableFor(type);
  85         }
  86     }
  87 
  88     private void disableItemHashTableFor(int type) {
  89         for (Item i : items) {
  90             while (i != null) {
  91                 if (i.type == type) {
  92                     i.type = -type;
  93                 }
  94                 i = i.next;
  95             }
  96         }
  97     }
  98 
  99     private void restoreItemHashTableFor(int type) {
 100         for (Item i : items) {
 101             while (i != null) {
 102                 if (i.type == -type) {
 103                     i.type = type;
 104                 }
 105                 i = i.next;
 106             }
 107         }
 108     }
 109 */
 110     public void setCacheInvokeDynamic(boolean value) {
 111         if (!value) throw new Error("method isn't implemented yet");
 112         cacheInvokeDynamic = value;
 113     }
 114     public void setCacheMTypes(boolean value) {
 115         if (!value) throw new Error("method isn't implemented yet");
 116         cacheMTypes = value;
 117     }
 118     public void setCacheMHandles(boolean value) {
 119         if (!value) throw new Error("method isn't implemented yet");
 120         cacheMHandles = value;
 121     }
 122 
 123     public int getBytecodeLength(MethodVisitor mv) {
 124         ByteVector code;
 125         try {
 126             java.lang.reflect.Field field = mv.getClass().getDeclaredField("code");
 127             field.setAccessible(true);
 128             code = (ByteVector) field.get(mv);
 129         } catch (InaccessibleObjectException | SecurityException | ReflectiveOperationException e) {
 130             throw new Error("can not read field 'code' from class " + mv.getClass(), e);
 131         }
 132         try {
 133             java.lang.reflect.Field field = code.getClass().getDeclaredField("length");
 134             field.setAccessible(true);
 135             return field.getInt(code);
 136         } catch (InaccessibleObjectException | SecurityException | ReflectiveOperationException e) {
 137             throw new Error("can not read field 'length' from class " + code.getClass(), e);
 138         }
 139     }
 140 }
 141