1 16 17 package de.gulden.util.javasource; 18 19 import de.gulden.util.javasource.jjt.Node; 20 import de.gulden.util.javasource.jjt.*; 21 import javax.xml.parsers.*; 22 import org.w3c.dom.*; 23 import java.io.*; 24 import java.util.*; 25 26 32 public class Type implements ParserTreeConstants, Serializable { 33 34 38 41 protected String typeName; 42 43 46 protected int dimension; 47 48 51 protected Member myMember; 52 53 54 58 61 public Type(Member member) { 62 myMember=member; 63 } 64 65 66 70 73 public String getUnqualifiedTypeName() { 74 return SourceObject.unqualify(typeName, countOuterClasses(typeName)); 75 } 76 77 80 public String getTypeName() { 81 return typeName; 82 } 83 84 87 public String getFullTypeName() { 88 return getTypeName() + SourceParser.repeat("[]",dimension); 89 } 90 91 94 public String getFullUnqualifiedTypeName() { 95 return getUnqualifiedTypeName() + SourceParser.repeat("[]",dimension); 96 } 97 98 101 public int getDimension() { 102 return dimension; 103 } 104 105 108 public void setTypeName(String n) { 109 typeName=n; 110 } 111 112 115 public void setDimension(int d) { 116 dimension=d; 117 } 118 119 125 public Element buildXML(Document d) { 126 Element e=d.createElement("type"); 127 String typeName=getTypeName(); 128 e.setAttribute("name",typeName); 129 e.setAttribute("unqualifiedName",SourceObject.unqualify(typeName)); 130 e.setAttribute("dimension",String.valueOf(getDimension())); 131 e.setAttribute("fullName",String.valueOf(getFullTypeName())); 132 return e; 133 } 134 135 141 public void initFromXML(Element element) throws IOException { 142 String n=element.getAttribute("name"); 143 String d=element.getAttribute("dimension"); 144 if ((n!=null)&&(d!=null)) { 145 try { 146 typeName=n; 147 dimension=Integer.parseInt(d); 148 } 149 catch (NumberFormatException nfe) { 150 throw new IOException("'<type>' tag must have numeric attribute 'dimension'"); 151 } 152 } 153 else { 154 throw new IOException("'<type>' tag must have attribute 'qualifiedName'"); 155 } 156 } 157 158 164 void initFromAST(Node fathernode) { 165 initFromAST(fathernode, null); 166 } 167 168 175 void initFromAST(Node fathernode, Node varnode) { 176 Node typenode=fathernode.getChild(JJT_TYPE); 177 if (typenode.getChild(JJT_NAME)!=null) { 178 String n=typenode.getName(); 179 typeName=myMember.getDeclaringClass().qualify(n); 180 } else { 181 typeName="void"; 182 } 183 Node searchNode; 184 if (varnode != null) { 185 searchNode = typenode; 186 } else { 187 searchNode=fathernode; 188 } 189 dimension = countArrayDimension(searchNode); 190 if (varnode != null) { 191 dimension += countArrayDimension(varnode); 192 } 193 } 194 195 private int countOuterClasses(String classname) { 196 Package basePackage = myMember.getPackage().getBasePackage(); 198 Class clazz = basePackage.findClass(classname); 199 if (clazz != null) { int cnt = 0; 201 while (clazz instanceof ClassInner) { 202 cnt++; 203 clazz = ((ClassInner)clazz).getDeclaringClass(); 204 } 205 return cnt; 206 } else { int cnt = 0; 208 boolean found = false; 209 do { 210 try { 211 java.lang.Class.forName(classname); found = true; 214 } catch (ClassNotFoundException cnfe) { 215 cnt++; 216 int pos = classname.lastIndexOf('.'); 218 if (pos == -1) { return 0; 220 } 221 classname = classname.substring(0, pos); 222 } 223 } while(!found); 224 return cnt; 225 } 226 } 227 228 229 233 236 private static int countArrayDimension(Node n) { 237 if (n.getId()==JJT_ISARRAY) { 238 return 1; 239 } else if (n.getId()==JJT_CODE) { 240 return 0; } else { 242 int sum=0; 243 Node[] children=n.getAllChildren(); 244 for (int i=0;i<children.length;i++) { 245 Node child = children[i]; 246 if ( child.getId()!=JJT_PARAMETER ) { sum+=countArrayDimension(children[i]); } 249 } 250 return sum; 251 } 252 } 253 254 } | Popular Tags |