KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.netbeans.modules.ruby.elements;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.EnumSet JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import org.jruby.ast.ArgsNode;
10 import org.jruby.ast.ArgumentNode;
11 import org.jruby.ast.DefnNode;
12 import org.jruby.ast.DefsNode;
13 import org.jruby.ast.ListNode;
14 import org.jruby.ast.Node;
15 import org.netbeans.api.gsf.ElementKind;
16 import org.netbeans.api.gsf.Modifier;
17
18
19 public class AstMethodElement extends AstElement implements MethodElement {
20     private List JavaDoc<String JavaDoc> parameters;
21     private Modifier access = Modifier.PUBLIC;
22
23     public AstMethodElement(Node node) {
24         super(node);
25     }
26
27     @SuppressWarnings JavaDoc("unchecked")
28     public List JavaDoc<String JavaDoc> getParameters() {
29         if ((parameters == null) && node instanceof DefnNode) {
30             // Find the arguments -- node that DefnNode.getArgsNode is NOT what we want, that
31
// is simply a singleton node with the name of the function itself
32
List JavaDoc<Node> nodes = (List JavaDoc<Node>)node.childNodes();
33
34             for (Node c : nodes) {
35                 if (c instanceof ArgsNode) {
36                     ArgsNode an = (ArgsNode)c;
37
38                     if (an.getArgsCount() > 0) {
39                         List JavaDoc<Node> args = (List JavaDoc<Node>)an.childNodes();
40
41                         for (Node arg : args) {
42                             if (arg instanceof ListNode) {
43                                 List JavaDoc<Node> args2 = (List JavaDoc<Node>)arg.childNodes();
44                                 parameters = new ArrayList JavaDoc<String JavaDoc>(args2.size());
45
46                                 for (Node arg2 : args2) {
47                                     if (arg2 instanceof ArgumentNode) {
48                                         String JavaDoc name = ((ArgumentNode)arg2).getName();
49                                         parameters.add(name);
50                                     }
51                                 }
52                             } else if (arg instanceof ArgumentNode) {
53                                 throw new RuntimeException JavaDoc(
54                                     "Unexpected argument location in the AST");
55
56                                 //String name = ((ArgumentNode)arg).getName();
57
//ISourcePosition pos = arg.getPosition();
58
//int start = pos.getStartOffset();
59
//int end = pos.getEndOffset();
60
//ComVariable parameter = new DefaultComVariable(name, true, start, end);
61
//parameters.add(parameter);
62
}
63                         }
64                     }
65                 }
66             }
67
68             if (parameters == null) {
69                 parameters = Collections.emptyList();
70             }
71         }
72
73         return parameters;
74     }
75
76     public boolean isDeprecated() {
77         // XXX TODO: When wrapping java objects I guess these functions -could- be deprecated, right?
78
return false;
79     }
80
81     @Override JavaDoc
82     public String JavaDoc getName() {
83         if (name == null) {
84             if (node instanceof DefnNode) {
85                 name = ((DefnNode)node).getName();
86             } else if (node instanceof DefsNode) {
87                 name = ((DefsNode)node).getName();
88             }
89
90             if (name == null) {
91                 name = node.toString();
92             }
93         }
94
95         return name;
96     }
97     
98     public void setModifiers(Set JavaDoc<Modifier> modifiers) {
99         this.modifiers = modifiers;
100     }
101
102     public Set JavaDoc<Modifier> getModifiers() {
103         if (modifiers == null) {
104             if (node instanceof DefsNode) {
105                 modifiers = EnumSet.of(Modifier.STATIC, access);
106             } else {
107                 modifiers = EnumSet.of(access);
108             }
109         }
110
111         return modifiers;
112     }
113
114     public void setAccess(Modifier access) {
115         this.access = access;
116     }
117
118     @Override JavaDoc
119     public ElementKind getKind() {
120         if ("initialize".equals(getName()) || "new".equals(getName())) {
121             return ElementKind.CONSTRUCTOR;
122         } else {
123             return ElementKind.METHOD;
124         }
125     }
126 }
127
Popular Tags