View Javadoc
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.compress.harmony.unpack200.bytecode;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  /**
23   * Line number table
24   */
25  public class LineNumberTableAttribute extends BCIRenumberedAttribute {
26  
27      private static CPUTF8 attributeName;
28  
29      public static void setAttributeName(final CPUTF8 cpUTF8Value) {
30          attributeName = cpUTF8Value;
31      }
32  
33      private final int lineNumberTableLength;
34      private final int[] startPcs;
35      private final int[] lineNumbers;
36  
37      public LineNumberTableAttribute(final int lineNumberTableLength, final int[] startPcs, final int[] lineNumbers) {
38          super(attributeName);
39          this.lineNumberTableLength = lineNumberTableLength;
40          this.startPcs = startPcs;
41          this.lineNumbers = lineNumbers;
42      }
43  
44      @Override
45      public boolean equals(final Object obj) {
46          return this == obj;
47      }
48  
49      @Override
50      protected int getLength() {
51          return 2 + 4 * lineNumberTableLength;
52      }
53  
54      /*
55       * (non-Javadoc)
56       *
57       * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#getNestedClassFileEntries()
58       */
59      @Override
60      protected ClassFileEntry[] getNestedClassFileEntries() {
61          return new ClassFileEntry[] { getAttributeName() };
62      }
63  
64      @Override
65      protected int[] getStartPCs() {
66          return startPcs;
67      }
68  
69      /*
70       * (non-Javadoc)
71       *
72       * @see org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry#toString()
73       */
74      @Override
75      public String toString() {
76          return "LineNumberTable: " + lineNumberTableLength + " lines";
77      }
78  
79      @Override
80      protected void writeBody(final DataOutputStream dos) throws IOException {
81          dos.writeShort(lineNumberTableLength);
82          for (int i = 0; i < lineNumberTableLength; i++) {
83              dos.writeShort(startPcs[i]);
84              dos.writeShort(lineNumbers[i]);
85          }
86      }
87  }