1 package org.netbeans.modules.ruby.elements; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.EnumSet ; 6 import java.util.List ; 7 import java.util.Set ; 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 <String > parameters; 21 private Modifier access = Modifier.PUBLIC; 22 23 public AstMethodElement(Node node) { 24 super(node); 25 } 26 27 @SuppressWarnings ("unchecked") 28 public List <String > getParameters() { 29 if ((parameters == null) && node instanceof DefnNode) { 30 List <Node> nodes = (List <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 <Node> args = (List <Node>)an.childNodes(); 40 41 for (Node arg : args) { 42 if (arg instanceof ListNode) { 43 List <Node> args2 = (List <Node>)arg.childNodes(); 44 parameters = new ArrayList <String >(args2.size()); 45 46 for (Node arg2 : args2) { 47 if (arg2 instanceof ArgumentNode) { 48 String name = ((ArgumentNode)arg2).getName(); 49 parameters.add(name); 50 } 51 } 52 } else if (arg instanceof ArgumentNode) { 53 throw new RuntimeException ( 54 "Unexpected argument location in the AST"); 55 56 } 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 return false; 79 } 80 81 @Override 82 public String 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 <Modifier> modifiers) { 99 this.modifiers = modifiers; 100 } 101 102 public Set <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 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 |