1 /*
   2  * Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
   3  * 
   4  * This file is part of the Lock Contention Tracing Subsystem for the HotSpot
   5  * Virtual Machine, which is developed at Christian Doppler Laboratory on
   6  * Monitoring and Evolution of Very-Large-Scale Software Systems. Please
   7  * contact us at <http://mevss.jku.at/> if you need additional information
   8  * or have any questions.
   9  *
  10  * This code is free software; you can redistribute it and/or modify it
  11  * under the terms of the GNU General Public License version 2 only, as
  12  * published by the Free Software Foundation.
  13  *
  14  * This code is distributed in the hope that it will be useful, but WITHOUT
  15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17  * version 2 for more details (a copy is included in the LICENSE file that
  18  * accompanied this code).
  19  *
  20  * You should have received a copy of the GNU General Public License version
  21  * 2 along with this work. If not, see <http://www.gnu.org/licenses/>.
  22  *
  23  */ 
  24 package sun.evtracing.parser.metadata;
  25 
  26 import java.util.Arrays;
  27 import java.util.ListIterator;
  28 import java.util.NoSuchElementException;
  29 
  30 import sun.evtracing.processing.MetadataStore;
  31 
  32 class JavaStackData implements Iterable<JavaStackFrame> {
  33         public static final JavaStackData UNKNOWN = new JavaStackData(new long[0], new int[0], null);
  34 
  35         private final JavaMethod[] methods;
  36         private final int[] bcis;
  37 
  38         public JavaStackData(long[] methods, int[] bcis, MetadataStore metadataStore) {
  39                 assert (methods.length == bcis.length);
  40 
  41                 this.methods = new JavaMethod[methods.length];
  42                 for (int i = 0; i < methods.length; i++) {
  43                         this.methods[i] = metadataStore.getOrAddMethod(methods[i]);
  44                 }
  45                 this.bcis = Arrays.copyOf(bcis, bcis.length);
  46         }
  47 
  48         public int frameCount() {
  49                 return methods.length;
  50         }
  51 
  52         public boolean hasMethodFromClassLoader(long classLoader) {
  53                 return Arrays.stream(methods).anyMatch(m -> m.clazz().classLoader() == classLoader);
  54         }
  55 
  56         @Override
  57         public String toString() {
  58                 StringBuilder sb = new StringBuilder();
  59                 for (JavaStackFrame frame : this) {
  60                         sb.append(frame.toString());
  61                         sb.append(System.lineSeparator());
  62                 }
  63                 return sb.toString();
  64         }
  65 
  66         @Override
  67         public ListIterator<JavaStackFrame> iterator() {
  68                 return new ListItr(0);
  69         }
  70 
  71         public ListIterator<JavaStackFrame> lastIterator() {
  72                 return new ListItr(methods.length);
  73         }
  74 
  75         private class ListItr implements ListIterator<JavaStackFrame> {
  76 
  77                 private int index;
  78 
  79                 public ListItr(int index) {
  80                         this.index = index;
  81                 }
  82 
  83                 @Override
  84                 public boolean hasNext() {
  85                         return (index < methods.length);
  86                 }
  87 
  88                 @Override
  89                 public JavaStackFrame next() {
  90                         if (!hasNext()) {
  91                                 throw new NoSuchElementException();
  92                         }
  93                         JavaStackFrame jsf = new JavaStackFrame(methods[index], bcis[index]);
  94                         index++;
  95                         return jsf;
  96                 }
  97 
  98                 @Override
  99                 public boolean hasPrevious() {
 100                         return (index > 0);
 101                 }
 102 
 103                 @Override
 104                 public JavaStackFrame previous() {
 105                         if (!hasPrevious()) {
 106                                 throw new NoSuchElementException();
 107                         }
 108                         index--;
 109                         return new JavaStackFrame(methods[index], bcis[index]);
 110                 }
 111 
 112                 @Override
 113                 public int nextIndex() {
 114                         return index + 1;
 115                 }
 116 
 117                 @Override
 118                 public int previousIndex() {
 119                         return index - 1;
 120                 }
 121 
 122                 @Override
 123                 public void remove() {
 124                         throw new UnsupportedOperationException();
 125                 }
 126 
 127                 @Override
 128                 public void set(JavaStackFrame e) {
 129                         throw new UnsupportedOperationException();
 130                 }
 131 
 132                 @Override
 133                 public void add(JavaStackFrame e) {
 134                         throw new UnsupportedOperationException();
 135                 }
 136         }
 137 
 138 }