Coverage Report - org.apache.commons.javaflow.bytecode.Stack
 
Classes in this File Line Coverage Branch Coverage Complexity
Stack
0%
0/204
0%
0/92
2.556
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.javaflow.bytecode;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.ObjectInputStream;
 21  
 import java.io.ObjectOutputStream;
 22  
 import java.io.Serializable;
 23  
 import org.apache.commons.javaflow.utils.ReflectionUtils;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 /**
 28  
  * Stack to store the frame information along the invocation trace.
 29  
  *
 30  
  * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
 31  
  * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
 32  
  * @version CVS $Id: Stack.java 480487 2006-11-29 08:54:42Z bayard $
 33  
  */
 34  
 public class Stack implements Serializable {
 35  
 
 36  0
     private static final Log log = LogFactory.getLog(Stack.class);
 37  
     private static final long serialVersionUID = 2L;
 38  
     
 39  
     private int[] istack;
 40  
     private float[] fstack;
 41  
     private double[] dstack;
 42  
     private long[] lstack;
 43  
     private Object[] ostack;
 44  
     private Object[] rstack;
 45  
     private int iTop, fTop, dTop, lTop, oTop, rTop;
 46  
     protected Runnable runnable;
 47  
 
 48  0
     public Stack(Runnable pRunnable) {
 49  0
         istack = new int[10];
 50  0
         lstack = new long[5];
 51  0
         dstack = new double[5];
 52  0
         fstack = new float[5];
 53  0
         ostack = new Object[10];
 54  0
         rstack = new Object[5];
 55  0
         runnable = pRunnable;
 56  0
     }
 57  
 
 58  0
     public Stack(final Stack pParent) {
 59  0
         istack = new int[pParent.istack.length];
 60  0
         lstack = new long[pParent.lstack.length];
 61  0
         dstack = new double[pParent.dstack.length];
 62  0
         fstack = new float[pParent.fstack.length];
 63  0
         ostack = new Object[pParent.ostack.length];
 64  0
         rstack = new Object[pParent.rstack.length];
 65  0
         iTop = pParent.iTop;
 66  0
         fTop = pParent.fTop;
 67  0
         dTop = pParent.dTop;
 68  0
         lTop = pParent.lTop;
 69  0
         oTop = pParent.oTop;
 70  0
         rTop = pParent.rTop;
 71  0
         System.arraycopy(pParent.istack, 0, istack, 0, iTop);
 72  0
         System.arraycopy(pParent.fstack, 0, fstack, 0, fTop);
 73  0
         System.arraycopy(pParent.dstack, 0, dstack, 0, dTop);
 74  0
         System.arraycopy(pParent.lstack, 0, lstack, 0, lTop);
 75  0
         System.arraycopy(pParent.ostack, 0, ostack, 0, oTop);
 76  0
         System.arraycopy(pParent.rstack, 0, rstack, 0, rTop);
 77  0
         runnable = pParent.runnable;
 78  0
     }
 79  
 
 80  
     public boolean hasDouble() {
 81  0
         return dTop > 0;
 82  
     }
 83  
     
 84  
     public double popDouble() {
 85  0
         if (dTop == 0) {
 86  0
             throw new EmptyStackException("pop double");
 87  
         }
 88  
         
 89  0
         final double d = dstack[--dTop];
 90  0
         log.debug("pop double " + d + " " + getStats());
 91  0
         return d;
 92  
     }
 93  
 
 94  
     public boolean hasFloat() {
 95  0
         return fTop > 0;
 96  
     }
 97  
 
 98  
     public float popFloat() {
 99  0
         if (fTop == 0) {
 100  0
             throw new EmptyStackException("pop float");
 101  
         }
 102  
         
 103  0
         final float f = fstack[--fTop];
 104  0
         log.debug("pop float " + f + " " + getStats());
 105  0
         return f;
 106  
     }
 107  
 
 108  
     public boolean hasInt() {
 109  0
         return iTop > 0;
 110  
     }
 111  
 
 112  
     public int popInt() {
 113  0
         if (iTop == 0) {
 114  0
             throw new EmptyStackException("pop int");
 115  
         }
 116  
         
 117  0
         final int i = istack[--iTop];
 118  0
         log.debug("pop int " + i + " " + getStats());
 119  0
         return i;
 120  
     }
 121  
 
 122  
     public boolean hasLong() {
 123  0
         return lTop > 0;
 124  
     }
 125  
 
 126  
     public long popLong() {
 127  0
         if (lTop == 0) {
 128  0
             throw new EmptyStackException("pop long");
 129  
         }
 130  
         
 131  0
         final long l = lstack[--lTop];
 132  0
         log.debug("pop long " + l + " " + getStats());
 133  0
         return l;
 134  
     }
 135  
 
 136  
     public boolean hasObject() {
 137  0
         return oTop > 0;
 138  
     }
 139  
 
 140  
     public Object popObject() {
 141  0
         if (oTop == 0) {
 142  0
             throw new EmptyStackException("pop object");
 143  
         }
 144  
         
 145  0
         final Object o = ostack[--oTop];
 146  0
         ostack[oTop] = null;  // avoid unnecessary reference to object
 147  
 
 148  0
         if(log.isDebugEnabled()) {
 149  0
             final String clazz = ReflectionUtils.getClassName(o);
 150  0
             final String clazzLoader = ReflectionUtils.getClassLoaderName(o);
 151  
 
 152  0
             log.debug("pop object "+ clazz + "/" + clazzLoader + " [" + o + "] ");
 153  
         }
 154  
         
 155  0
         return o;
 156  
     }
 157  
 
 158  
     public boolean hasReference() {
 159  0
         return rTop > 0;
 160  
     }
 161  
 
 162  
     public Object popReference() {
 163  0
         if (rTop == 0) {
 164  0
             throw new EmptyStackException("pop reference");
 165  
         }
 166  
         
 167  0
         final Object o = rstack[--rTop];
 168  0
         rstack[rTop] = null;  // avoid unnecessary reference to object
 169  
 
 170  0
         if(log.isDebugEnabled()) {
 171  0
             final String clazz = ReflectionUtils.getClassName(o);
 172  0
             final String clazzLoader = ReflectionUtils.getClassLoaderName(o);
 173  
 
 174  0
             log.debug("pop reference "+ clazz + "/" + clazzLoader + " [" + o + "] " + getStats());
 175  
         }
 176  
         
 177  0
         return o;
 178  
     }
 179  
 
 180  
     public void pushDouble(double d) {
 181  0
         log.debug("push double " + d + " " + getStats());
 182  
 
 183  0
         if (dTop == dstack.length) {
 184  0
             double[] hlp = new double[Math.max(8,dstack.length*2)];
 185  0
             System.arraycopy(dstack, 0, hlp, 0, dstack.length);
 186  0
             dstack = hlp;
 187  
         }
 188  0
         dstack[dTop++] = d;
 189  0
     }
 190  
 
 191  
     public void pushFloat(float f) {
 192  0
         log.debug("push float " + f + " " + getStats());
 193  
         
 194  0
         if (fTop == fstack.length) {
 195  0
             float[] hlp = new float[Math.max(8,fstack.length*2)];
 196  0
             System.arraycopy(fstack, 0, hlp, 0, fstack.length);
 197  0
             fstack = hlp;
 198  
         }
 199  0
         fstack[fTop++] = f;
 200  0
     }
 201  
 
 202  
     public void pushInt(int i) {
 203  0
         log.debug("push int " + i + " " + getStats());
 204  
 
 205  0
         if (iTop == istack.length) {
 206  0
             int[] hlp = new int[Math.max(8,istack.length*2)];
 207  0
             System.arraycopy(istack, 0, hlp, 0, istack.length);
 208  0
             istack = hlp;
 209  
         }
 210  0
         istack[iTop++] = i;
 211  0
     }
 212  
 
 213  
     public void pushLong(long l) {
 214  0
         log.debug("push long " + l + " " + getStats());
 215  
         
 216  0
         if (lTop == lstack.length) {
 217  0
             long[] hlp = new long[Math.max(8,lstack.length*2)];
 218  0
             System.arraycopy(lstack, 0, hlp, 0, lstack.length);
 219  0
             lstack = hlp;
 220  
         }
 221  0
         lstack[lTop++] = l;
 222  0
     }
 223  
 
 224  
     public void pushObject(Object o) {
 225  
 
 226  0
         if (log.isDebugEnabled()) {
 227  0
             final String clazz = ReflectionUtils.getClassName(o);
 228  0
             final String clazzLoader = ReflectionUtils.getClassLoaderName(o);            
 229  0
             log.debug("push object " + clazz + "/" + clazzLoader + " [" + o + "] " + getStats());
 230  
         }
 231  
         
 232  0
         if (oTop == ostack.length) {
 233  0
             Object[] hlp = new Object[Math.max(8,ostack.length*2)];
 234  0
             System.arraycopy(ostack, 0, hlp, 0, ostack.length);
 235  0
             ostack = hlp;
 236  
         }
 237  0
         ostack[oTop++] = o;
 238  0
     }
 239  
 
 240  
     public void pushReference(Object o) {
 241  
 
 242  0
         if (log.isDebugEnabled()) {
 243  0
             final String clazz = ReflectionUtils.getClassName(o);
 244  0
             final String clazzLoader = ReflectionUtils.getClassLoaderName(o);
 245  
     
 246  0
             log.debug("push reference " + clazz + "/" + clazzLoader + " [" + o + "] " + getStats());
 247  
         }
 248  
         
 249  0
         if (rTop == rstack.length) {
 250  0
             Object[] hlp = new Object[Math.max(8,rstack.length*2)];
 251  0
             System.arraycopy(rstack, 0, hlp, 0, rstack.length);
 252  0
             rstack = hlp;
 253  
         }
 254  0
         rstack[rTop++] = o;
 255  0
     }
 256  
 
 257  
     public boolean isSerializable() {
 258  0
         for (int i = 0; i < rTop; i++) {
 259  0
             final Object r = rstack[i];
 260  0
             if (!(r instanceof Serializable)) {
 261  0
                 return false;
 262  
             }
 263  
         }
 264  0
         for (int i = 0; i < oTop; i++) {
 265  0
             final Object o = ostack[i];
 266  0
             if (!(o instanceof Serializable)) {
 267  0
                 return false;
 268  
             }
 269  
         }
 270  0
         return true;
 271  
     }
 272  
 
 273  
     public boolean isEmpty() {
 274  0
         return iTop==0 && lTop==0 && dTop==0 && fTop==0 && oTop==0 && rTop==0;
 275  
     }
 276  
 
 277  
     private String getStats() {
 278  0
         final StringBuffer sb = new StringBuffer();
 279  0
         sb.append("i[").append(iTop).append("],");
 280  0
         sb.append("l[").append(lTop).append("],");
 281  0
         sb.append("d[").append(dTop).append("],");
 282  0
         sb.append("f[").append(fTop).append("],");
 283  0
         sb.append("o[").append(oTop).append("],");
 284  0
         sb.append("r[").append(rTop).append("]");
 285  0
         return sb.toString();
 286  
     }
 287  
 
 288  
     private String getContent() {
 289  0
         final StringBuffer sb = new StringBuffer();
 290  0
         sb.append("i[").append(iTop).append("]\n");
 291  0
         sb.append("l[").append(lTop).append("]\n");
 292  0
         sb.append("d[").append(dTop).append("]\n");
 293  0
         sb.append("f[").append(fTop).append("]\n");
 294  0
         sb.append("o[").append(oTop).append("]\n");
 295  0
         for(int i=0; i<oTop;i++) {
 296  0
             sb.append(' ').append(i).append(": ");
 297  0
             sb.append(ReflectionUtils.getClassName(ostack[i])).append('/').append(ReflectionUtils.getClassLoaderName(ostack[i]));
 298  0
             sb.append('\n');
 299  
         }
 300  0
         sb.append("r[").append(rTop).append("]\n");
 301  0
         for(int i=0; i<rTop;i++) {
 302  0
             sb.append(' ').append(i).append(": ");
 303  0
             sb.append(ReflectionUtils.getClassName(rstack[i])).append('/').append(ReflectionUtils.getClassLoaderName(rstack[i]));
 304  0
             sb.append('\n');
 305  
         }
 306  
         
 307  0
         return sb.toString();
 308  
     }
 309  
     
 310  
     public String toString() {
 311  0
         return getContent();
 312  
     }
 313  
 
 314  
 
 315  
     private void writeObject(ObjectOutputStream s) throws IOException {
 316  0
         s.writeInt(iTop);
 317  0
         for( int i=0; i<iTop; i++ ) {
 318  0
             s.writeInt(istack[i]);
 319  
         }
 320  
 
 321  0
         s.writeInt(lTop);
 322  0
         for( int i=0; i<lTop; i++ ) {
 323  0
             s.writeLong(lstack[i]);
 324  
         }
 325  
 
 326  0
         s.writeInt(dTop);
 327  0
         for( int i=0; i<dTop; i++ ) {
 328  0
             s.writeDouble(dstack[i]);
 329  
         }
 330  
 
 331  0
         s.writeInt(fTop);
 332  0
         for( int i=0; i<fTop; i++ ) {
 333  0
             s.writeDouble(fstack[i]);
 334  
         }
 335  
 
 336  0
         s.writeInt(oTop);
 337  0
         for( int i=0; i<oTop; i++ ) {
 338  0
             s.writeObject(ostack[i]);
 339  
         }
 340  
 
 341  0
         s.writeInt(rTop);
 342  0
         for( int i=0; i<rTop; i++ ) {
 343  0
             s.writeObject(rstack[i]);
 344  
         }
 345  
 
 346  0
         s.writeObject(runnable);
 347  0
     }
 348  
 
 349  
     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
 350  0
         iTop = s.readInt();
 351  0
         istack = new int[iTop];
 352  0
         for( int i=0; i<iTop; i++ ) {
 353  0
             istack[i] = s.readInt();
 354  
         }
 355  
 
 356  0
         lTop = s.readInt();
 357  0
         lstack = new long[lTop];
 358  0
         for( int i=0; i<lTop; i++ ) {
 359  0
             lstack[i] = s.readLong();
 360  
         }
 361  
 
 362  0
         dTop = s.readInt();
 363  0
         dstack = new double[dTop];
 364  0
         for( int i=0; i<dTop; i++ ) {
 365  0
             dstack[i] = s.readDouble();
 366  
         }
 367  
 
 368  0
         fTop = s.readInt();
 369  0
         fstack = new float[fTop];
 370  0
         for( int i=0; i<fTop; i++ ) {
 371  0
             fstack[i] = s.readFloat();
 372  
         }
 373  
 
 374  0
         oTop = s.readInt();
 375  0
         ostack = new Object[oTop];
 376  0
         for( int i=0; i<oTop; i++ ) {
 377  0
             ostack[i] = s.readObject();
 378  
         }
 379  
 
 380  0
         rTop = s.readInt();
 381  0
         rstack = new Object[rTop];
 382  0
         for( int i=0; i<rTop; i++ ) {
 383  0
             rstack[i] = s.readObject();
 384  
         }
 385  
 
 386  0
         runnable = (Runnable)s.readObject();
 387  0
     }
 388  
 
 389  
 }