1 19 20 package org.openide.src; 21 22 import java.util.StringTokenizer ; 23 24 28 public final class MethodParameter extends Object implements java.io.Serializable { 29 30 private String name; 31 32 33 private Type type; 34 35 36 private boolean fin; 37 38 static final long serialVersionUID =-6158959006278766562L; 39 44 public MethodParameter(String name, Type type, boolean fin) { 45 this.name = name; 46 this.type = type; 47 this.fin = fin; 48 } 49 50 55 public static MethodParameter parse(String text) throws IllegalArgumentException { 56 StringTokenizer tok = new StringTokenizer (text, " []", true); boolean rightBracketExpected = false; 58 59 boolean fin = false; 60 Type type = null; 61 String name = null; 62 63 while (tok.hasMoreTokens()) { 64 String token = tok.nextToken(); 65 if (token.equals(" ")) continue; 67 68 if (type == null) { if (token.equals("final")) { if (fin) throw new IllegalArgumentException (); 72 fin = true; 73 } 74 else { type = Type.parse(token); 76 } 77 } 78 else { if (token.equals("[")) { if (rightBracketExpected) 81 throw new IllegalArgumentException (); 82 rightBracketExpected = true; 83 } 84 else if (token.equals("]")) { if (!rightBracketExpected) 86 throw new IllegalArgumentException (); 87 type = Type.createArray(type); 88 rightBracketExpected = false; 89 } 90 else { if (name != null) throw new IllegalArgumentException (); 93 name = token; 94 } 95 } 96 } 97 if ((type == null) || (name == null) || rightBracketExpected) 98 throw new IllegalArgumentException (); 99 Type t = Type.parse(name); 101 if (!t.isClass()) 102 throw new IllegalArgumentException (); 103 return new MethodParameter(name, type, fin); 104 } 105 106 109 public Type getType() { 110 return type; 111 } 112 113 116 public void setType(Type type) { 117 this.type = type; 118 } 119 120 123 public String getName() { 124 return name; 125 } 126 127 130 public void setName(String name) { 131 this.name = name; 132 } 133 134 137 public void setFinal(boolean fin) { 138 this.fin = fin; 139 } 140 141 144 public boolean isFinal() { 145 return fin; 146 } 147 148 153 StringBuffer getAsString(StringBuffer appendTo, boolean source) { 154 if (fin) 155 appendTo.append("final "); type.getAsString(appendTo, source); 157 appendTo.append(" "); appendTo.append(name); 159 return appendTo; 160 } 161 162 164 public String getFullString() { 165 return getAsString(new StringBuffer (), false).toString(); 166 } 167 168 171 public String getSourceString() { 172 return getAsString(new StringBuffer (), true).toString(); 173 } 174 175 177 public String toString() { 178 return getAsString(new StringBuffer (), false).toString(); 179 } 180 181 190 public boolean compareTo(MethodParameter param, boolean onlyType, boolean source) { 191 if (!type.compareTo(param.type, source)) 192 return false; 193 194 return onlyType ? true : ((param.fin == fin) && (param.name.equals(name))); 195 } 196 197 198 201 public boolean equals(Object o) { 202 return (o instanceof MethodParameter) ? 203 compareTo((MethodParameter)o, true, false) : 204 false; 205 } 206 207 209 public int hashCode() { 210 return name.hashCode() + type.hashCode(); 211 } 212 } 213 | Popular Tags |