1 33 34 35 package bsh; 36 37 import java.lang.reflect.Array ; 38 39 class BSHType extends SimpleNode 40 implements BshClassManager.Listener 41 { 42 47 private Class baseType; 48 52 private int arrayDims; 53 54 57 private Class type; 58 59 String descriptor; 60 61 BSHType(int id) { 62 super(id); 63 } 64 65 69 public void addArrayDimension() { 70 arrayDims++; 71 } 72 73 SimpleNode getTypeNode() { 74 return (SimpleNode)jjtGetChild(0); 75 } 76 77 84 public String getTypeDescriptor( 85 CallStack callstack, Interpreter interpreter, String defaultPackage ) 86 { 87 if ( descriptor != null ) 89 return descriptor; 90 91 String descriptor; 92 SimpleNode node = getTypeNode(); 94 if ( node instanceof BSHPrimitiveType ) 95 descriptor = getTypeDescriptor( ((BSHPrimitiveType)node).type ); 96 else 97 { 98 String clasName = ((BSHAmbiguousName)node).text; 99 BshClassManager bcm = interpreter.getClassManager(); 100 String definingClass = bcm.getClassBeingDefined( clasName ); 105 106 Class clas = null; 107 if ( definingClass == null ) 108 { 109 try { 110 clas = ((BSHAmbiguousName)node).toClass( 111 callstack, interpreter ); 112 } catch ( EvalError e ) { 113 } 117 } else 118 clasName = definingClass; 119 120 if ( clas != null ) 121 { 122 descriptor = getTypeDescriptor( clas ); 124 }else 125 { 126 if ( defaultPackage == null || Name.isCompound( clasName ) ) 127 descriptor = "L" + clasName.replace('.','/') + ";"; 128 else 129 descriptor = 130 "L"+defaultPackage.replace('.','/')+"/"+clasName + ";"; 131 } 132 } 133 134 for(int i=0; i<arrayDims; i++) 135 descriptor = "["+descriptor; 136 137 this.descriptor = descriptor; 138 return descriptor; 140 } 141 142 public Class getType( CallStack callstack, Interpreter interpreter ) 143 throws EvalError 144 { 145 if ( type != null ) 147 return type; 148 149 SimpleNode node = getTypeNode(); 151 if ( node instanceof BSHPrimitiveType ) 152 baseType = ((BSHPrimitiveType)node).getType(); 153 else 154 baseType = ((BSHAmbiguousName)node).toClass( 155 callstack, interpreter ); 156 157 if ( arrayDims > 0 ) { 158 try { 159 int[] dims = new int[arrayDims]; Object obj = Array.newInstance(baseType, dims); 163 type = obj.getClass(); 164 } catch(Exception e) { 165 throw new EvalError("Couldn't construct array type", 166 this, callstack ); 167 } 168 } else 169 type = baseType; 170 171 interpreter.getClassManager().addListener(this); 174 175 return type; 176 } 177 178 183 public Class getBaseType() { 184 return baseType; 185 } 186 190 public int getArrayDims() { 191 return arrayDims; 192 } 193 194 public void classLoaderChanged() { 195 type = null; 196 baseType = null; 197 } 198 199 public static String getTypeDescriptor( Class clas ) 200 { 201 if ( clas == Boolean.TYPE ) return "Z"; 202 if ( clas == Character.TYPE ) return "C"; 203 if ( clas == Byte.TYPE ) return "B"; 204 if ( clas == Short.TYPE ) return "S"; 205 if ( clas == Integer.TYPE ) return "I"; 206 if ( clas == Long.TYPE ) return "J"; 207 if ( clas == Float.TYPE ) return "F"; 208 if ( clas == Double.TYPE ) return "D"; 209 if ( clas == Void.TYPE ) return "V"; 210 String name = clas.getName().replace('.','/'); 212 213 if ( name.startsWith("[") || name.endsWith(";") ) 214 return name; 215 else 216 return "L"+ name.replace('.','/') +";"; 217 } 218 } 219 | Popular Tags |