KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > elements > AstElement


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 package org.netbeans.modules.ruby.elements;
21
22 import org.jruby.ast.Node;
23 import java.util.Set JavaDoc;
24 import org.netbeans.api.gsf.Element;
25 import org.netbeans.api.gsf.ElementKind;
26 import org.netbeans.api.gsf.Modifier;
27 import java.awt.Image JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.EnumSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35 import org.jruby.ast.ArgsNode;
36 import org.jruby.ast.ArgumentNode;
37 import org.jruby.ast.ArgumentNode;
38 import org.jruby.ast.ClassNode;
39 import org.jruby.ast.ClassVarDeclNode;
40 import org.jruby.ast.ClassVarNode;
41 import org.jruby.ast.Colon2Node;
42 import org.jruby.ast.ConstDeclNode;
43 import org.jruby.ast.ConstNode;
44 import org.jruby.ast.DefnNode;
45 import org.jruby.ast.DefsNode;
46 import org.jruby.ast.InstAsgnNode;
47 import org.jruby.ast.InstVarNode;
48 import org.jruby.ast.ListNode;
49 import org.jruby.ast.ModuleNode;
50 import org.jruby.ast.Node;
51 import org.jruby.ast.SymbolNode;
52 import org.jruby.ast.types.INameNode;
53 import org.jruby.parser.RubyParserResult;
54 import org.netbeans.api.gsf.Element;
55 import org.netbeans.api.gsf.ElementKind;
56 import org.netbeans.api.gsf.Modifier;
57 import org.openide.filesystems.FileObject;
58
59 /**
60  * A Ruby element coming from a JRuby parse tree
61  *
62  * @author Tor Norbye
63  */

64 public abstract class AstElement extends RubyElement {
65     protected Node node;
66     protected ArrayList JavaDoc<AstElement> children;
67     protected String JavaDoc name;
68     private String JavaDoc in;
69     protected Set JavaDoc<Modifier> modifiers;
70
71     public AstElement(Node node) {
72         super();
73         this.node = node;
74     }
75
76     public Node getNode() {
77         return node;
78     }
79
80     public abstract String JavaDoc getName();
81
82     // public String getName() {
83
// if (name == null) {
84
// name = node.toString();
85
// }
86
//
87
// return name;
88
// }
89
public String JavaDoc getDisplayName() {
90         return getName();
91     }
92
93     public String JavaDoc getDescription() {
94         // XXX TODO
95
return getName();
96     }
97
98     @SuppressWarnings JavaDoc("unchecked")
99     public List JavaDoc<AstElement> getChildren() {
100 // if (children == null) {
101
// children = new ArrayList<AstElement>();
102
//
103
// for (Node child : (List<Node>)node.childNodes()) {
104
// addInterestingChildren(this, children, child);
105
// }
106
// }
107
//
108
if (children == null) {
109             return Collections.emptyList();
110         }
111
112         return children;
113     }
114     
115     public void addChild(AstElement child) {
116         if (children == null) {
117             children = new ArrayList JavaDoc<AstElement>();
118         }
119         children.add(child);
120     }
121     
122     public static AstElement create(Node node) {
123         if (node instanceof DefnNode || node instanceof DefsNode) {
124             return new AstMethodElement(node);
125         } else if (node instanceof ClassNode) {
126             return new AstClassElement(node);
127         } else if (node instanceof ModuleNode) {
128             return new AstModuleElement(node);
129         } else if (node instanceof ConstDeclNode || node instanceof ClassVarDeclNode) {
130             return new AstFieldElement(node);
131         } else if (node instanceof ConstNode) {
132             return new AstVariableElement(node, ((ConstNode)node).getName());
133         } else if (node instanceof ClassVarNode || node instanceof ClassVarDeclNode ||
134                 node instanceof InstVarNode || node instanceof InstAsgnNode) {
135             return new AstFieldElement(node);
136         } else if (node instanceof ConstDeclNode) {
137             return new AstConstantElement((ConstDeclNode)node);
138         } else if (node instanceof SymbolNode) {
139             return new AstAttributeElement((SymbolNode)node);
140         } else {
141             return null;
142         }
143     }
144
145     public String JavaDoc toString() {
146         String JavaDoc clz = getClass().getName();
147
148         return clz.substring(0, clz.lastIndexOf('.')) + ":" + node.toString();
149     }
150
151     public Image JavaDoc getIcon() {
152         return null;
153     }
154
155     public String JavaDoc getIn() {
156         // TODO - compute signature via AstUtilities
157
return in;
158     }
159
160     public void setIn(String JavaDoc in) {
161         this.in = in;
162     }
163
164     public ElementKind getKind() {
165         return ElementKind.OTHER;
166     }
167
168     public Set JavaDoc<Modifier> getModifiers() {
169         return Collections.emptySet();
170     }
171
172 }
173
Popular Tags