package sds.classfile.attributes;

import java.io.IOException;
import sds.classfile.ClassFileStream;
import sds.classfile.ConstantPool;

/**
 * This class is for
 * <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.12">
 * LineNumberTable Attribute</a>.
 * @author inagaki
 */
public class LineNumberTable extends AttributeInfo {
	private LNTable[] lineNumberTable;

	/**
	 * 
	 * @param nameIndex
	 * @param length 
	 */
	public LineNumberTable(int nameIndex, int length) {
		super(AttributeType.LineNumberTable, nameIndex, length);
	}

	/**
	 * returns line number table.
	 * @return line number table
	 */
	public LNTable[] getLineNumberTable() {
		return lineNumberTable;
	}

	@Override
	public void read(ClassFileStream data, ConstantPool pool) throws IOException {
		this.lineNumberTable = new LNTable[data.readShort()];
		for(int i = 0; i < lineNumberTable.length; i++) {
			lineNumberTable[i] = new LNTable(data);
			if(i == lineNumberTable.length-1) {
				lineNumberTable[i].endPc = lineNumberTable[i].getStartPc();
				if(lineNumberTable.length > 1) {
					lineNumberTable[i-1].endPc = lineNumberTable[i].getStartPc();
				}
			} else if(i > 0) {
				lineNumberTable[i-1].endPc = lineNumberTable[i].startPc;
			}
		}
	}

	/**
	 * This class is for line number table.
	 */
	public class LNTable {
		private int startPc;
		private int endPc;
		private int lineNumber;

		LNTable(ClassFileStream data) throws IOException {
			this.startPc = data.readShort();
			this.lineNumber = data.readShort();
		}

		/**
		 * returns start number of opcode.
		 * @return start number
		 */
		public int getStartPc() {
			return startPc;
		}

		/**
		 * returns line number in source file.
		 * @return line number
		 */
		public int getLineNumber() {
			return lineNumber;
		}

		/**
		 * returns end number of opcode.
		 * @return end number
		 */
		public int getEndPc() {
			return endPc;
		}
	}
}
@@ -949,20 +949,83 @@
 ta);%0A%09%09%09
-if(i
+setEndPc(i);%0A%09%09%7D%0A%09%7D%0A%0A%09private void setEndPc(int index) %7B%0A%09%09if(index
  == line
@@ -1047,25 +1047,24 @@
 gth-1) %7B%0A%09%09%09
-%09
 lineNumberTa
@@ -1068,16 +1068,20 @@
 rTable%5Bi
+ndex
 %5D.endPc 
@@ -1091,32 +1091,36 @@
 ineNumberTable%5Bi
+ndex
 %5D.getStartPc();%0A
@@ -1125,45 +1125,60 @@
 ;%0A%09%09
+%09setEndPc2(index);%0A%09%09%09return;%0A%09%09%7D%0A
 %09%09if(
-l
 in
-eNumberTable.length %3E 1
+dex %3E 0
 ) %7B%0A
-%09%09
 %09%09%09l
@@ -1185,32 +1185,36 @@
 ineNumberTable%5Bi
+ndex
 -1%5D.endPc = line
@@ -1230,56 +1230,106 @@
 le%5Bi
-%5D.getS
+ndex%5D.s
 tartPc
-()
 ;%0A%09%09
-%09%09%7D%0A%09%09%09%7D else if(i %3E 0
+%7D%0A%09%7D%0A%0A%09private void setEndPc2(int index) %7B%0A%09%09if(lineNumberTable.length %3E 1
 ) %7B%0A%09%09%09
-%09
 line
@@ -1341,16 +1341,20 @@
 rTable%5Bi
+ndex
 -1%5D.endP
@@ -1378,23 +1378,27 @@
 le%5Bi
-%5D.startPc;%0A%09%09%09%7D
+ndex%5D.getStartPc();
 %0A%09%09%7D

add methods for setting end pc.