KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > codegen > ExceptionLabel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.codegen;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
15
16 public class ExceptionLabel extends Label {
17     
18     public int ranges[] = {POS_NOT_SET,POS_NOT_SET};
19     public int count = 0; // incremented each time placeStart or placeEnd is called
20
public TypeBinding exceptionType;
21     
22 public ExceptionLabel(CodeStream codeStream, TypeBinding exceptionType) {
23     super(codeStream);
24     this.exceptionType = exceptionType;
25 }
26
27 public void place() {
28     // register the handler inside the codeStream then normal place
29
codeStream.registerExceptionHandler(this);
30     if (CodeStream.DEBUG) System.out.println("\t\t\t\t<place at: "+codeStream.position+" - "+ this); //$NON-NLS-1$ //$NON-NLS-2$
31
this.position = codeStream.getPosition();
32 }
33
34 public void placeEnd() {
35     int endPosition = codeStream.position;
36     if (this.ranges[this.count-1] == endPosition) { // start == end ?
37
// discard empty exception handler
38
this.count--;
39     } else {
40         this.ranges[this.count++] = endPosition;
41     }
42 }
43
44 public void placeStart() {
45     int startPosition = codeStream.position;
46     if (this.count > 0 && this.ranges[this.count-1] == startPosition) { // start == previous end ?
47
// reopen current handler
48
this.count--;
49         return;
50     }
51     // only need to grow on even additions (i.e. placeStart only)
52
int length;
53     if (this.count == (length = this.ranges.length)) {
54         System.arraycopy(this.ranges, 0, this.ranges = new int[length*2], 0, length);
55     }
56     this.ranges[this.count++] = startPosition;
57 }
58 public String JavaDoc toString() {
59     String JavaDoc basic = getClass().getName();
60     basic = basic.substring(basic.lastIndexOf('.')+1);
61     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(basic);
62     buffer.append('@').append(Integer.toHexString(hashCode()));
63     buffer.append("(type=").append(this.exceptionType == null ? CharOperation.NO_CHAR : this.exceptionType.readableName()); //$NON-NLS-1$
64
buffer.append(", position=").append(position); //$NON-NLS-1$
65
buffer.append(", ranges = "); //$NON-NLS-1$
66
if (this.count == 0) {
67         buffer.append("[]"); //$NON-NLS-1$
68
} else {
69         for (int i = 0; i < this.count; i++) {
70             if ((i & 1) == 0) {
71                 buffer.append("[").append(ranges[i]); //$NON-NLS-1$
72
} else {
73                 buffer.append(",").append(ranges[i]).append("]"); //$NON-NLS-1$ //$NON-NLS-2$
74
}
75         }
76         if ((this.count & 1) == 1) {
77             buffer.append(",?]"); //$NON-NLS-1$
78
}
79     }
80     buffer.append(')');
81     return buffer.toString();
82 }
83 }
84
Popular Tags