View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.unpack200.bytecode;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * Line number table
26   */
27  public class LineNumberTableAttribute extends BCIRenumberedAttribute {
28  
29      private static CPUTF8 attributeName;
30  
31      public static void setAttributeName(final CPUTF8 cpUTF8Value) {
32          attributeName = cpUTF8Value;
33      }
34  
35      private final int lineNumberTableLength;
36      private final int[] startPcs;
37      private final int[] lineNumbers;
38  
39      public LineNumberTableAttribute(final int lineNumberTableLength, final int[] startPcs, final int[] lineNumbers) {
40          super(attributeName);
41          this.lineNumberTableLength = lineNumberTableLength;
42          this.startPcs = startPcs;
43          this.lineNumbers = lineNumbers;
44      }
45  
46      @Override
47      public boolean equals(final Object obj) {
48          return this == obj;
49      }
50  
51      @Override
52      protected int getLength() {
53          return 2 + 4 * lineNumberTableLength;
54      }
55  
56      /*
57       * (non-Javadoc)
58       *
59       * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#getNestedClassFileEntries()
60       */
61      @Override
62      protected ClassFileEntry[] getNestedClassFileEntries() {
63          return new ClassFileEntry[] { getAttributeName() };
64      }
65  
66      @Override
67      protected int[] getStartPCs() {
68          return startPcs;
69      }
70  
71      /*
72       * (non-Javadoc)
73       *
74       * @see org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry#toString()
75       */
76      @Override
77      public String toString() {
78          return "LineNumberTable: " + lineNumberTableLength + " lines";
79      }
80  
81      @Override
82      protected void writeBody(final DataOutputStream dos) throws IOException {
83          dos.writeShort(lineNumberTableLength);
84          for (int i = 0; i < lineNumberTableLength; i++) {
85              dos.writeShort(startPcs[i]);
86              dos.writeShort(lineNumbers[i]);
87          }
88      }
89  }