KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > java > parser > ASTreeNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * ASTree.java
21  *
22  * Created on February 4, 2002, 3:05 PM
23  */

24
25 package org.netbeans.lib.java.parser;
26
27 /**
28  *
29  * @author Tomas Hurka
30  */

31 class ASTreeNode extends ASTClass implements ASTreeTypes {
32
33     private int firstToken,lastToken;
34     ASTree subTrees[];
35
36     ASTreeNode(ASTContext context,int type, ASTree first, ASTree last, ASTree[] sub) {
37         super(context,type);
38         firstToken=first.getFirstToken();
39         lastToken=last.getLastToken();
40         subTrees=sub;
41     }
42
43     ASTreeNode(ASTContext context,int type, ASTree first, ASTree last, ASTree sub) {
44         this(context,type,first,last,new ASTree[]{sub});
45     }
46         
47     ASTreeNode(ASTContext context,int type, ASTree token) {
48         super(context,type);
49         subTrees=new ASTree[]{token};
50         firstToken=token.getFirstToken();
51         lastToken=token.getLastToken();
52     }
53
54     ASTreeNode(ASTContext context,int type, ASTree[] sub) {
55         super(context,type);
56         subTrees=sub;
57         computeBounds();
58     }
59     
60     ASTreeNode(ASTContext context,int type, ASTree leftAST, ASTree rightAST) {
61         super(context,type);
62         subTrees=new ASTree[]{leftAST,rightAST};
63         computeBounds();
64     }
65
66     private void computeBounds() {
67         int i,j;
68         
69         for (i=0;i<subTrees.length;i++) {
70             if (subTrees[i]!=null) {
71                 firstToken=subTrees[i].getFirstToken();
72                 break;
73             }
74         }
75         for (j=subTrees.length-1;j>=i;j--) {
76             if (subTrees[j]!=null) {
77                 lastToken=subTrees[j].getLastToken();
78                 break;
79             }
80         }
81     }
82
83     final ASTClass setLastToken(ASTree token) {
84         lastToken = token.getLastToken();
85         return this;
86     }
87     
88     final ASTClass setLastToken(int tokenIndex) {
89         lastToken = tokenIndex;
90         return this;
91     }
92
93     /** Getter for property lastToken.
94      * @return Value of property lastToken.
95      */

96     public final int getLastToken() {
97         return lastToken;
98     }
99     
100     final ASTClass setFirstToken(ASTree token) {
101         firstToken = token.getFirstToken();
102         return this;
103     }
104     
105     final ASTClass setFirstToken(int tokenIndex) {
106         firstToken = tokenIndex;
107         return this;
108     }
109
110     public final int getFirstToken() {
111         return firstToken;
112     }
113     
114     final ASTClass addSubTree(ASTree sub) {
115         if (subTrees!=null) {
116             int size=subTrees.length;
117             ASTree trees[]=new ASTree[size+1];
118
119             System.arraycopy(subTrees,0,trees,0,size);
120             trees[size]=sub;
121             subTrees=trees;
122         } else
123             subTrees=new ASTree[]{sub};
124         if (sub!=null)
125             lastToken=sub.getLastToken();
126         return this;
127     }
128   
129     final ASTClass setFirstAndLastToken(ASTree first, ASTree last) {
130         firstToken = first.getFirstToken();
131         lastToken=last.getLastToken();
132         return this;
133     }
134         
135     public final String JavaDoc toString() {
136         String JavaDoc text="ASTreeNode Type:"+getType()+"\n{\n";
137         int i;
138         
139         if (subTrees!=null) {
140             for (i=0;i<subTrees.length;i++) {
141                 String JavaDoc subText;
142                 ASTree t=subTrees[i];
143
144                 if (t!=null)
145                     subText=t.toString();
146                 else
147                     subText="NULL";
148                 text+=subText+"\n";
149             }
150         }
151         return text+"}\n";
152     }
153
154     public final ASTree[] getSubTrees() {
155         return subTrees;
156     }
157     
158 }
159
Popular Tags