1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
package org.perl.inline.java ;
import java.util.* ;
import java.lang.reflect.* ;
class InlineJavaClass {
private InlineJavaServer ijs ;
private InlineJavaProtocol ijp ;
static private HashMap<Class, String> class2jni_code = new HashMap<>() ;
static {
class2jni_code.put(byte.class, "B") ;
class2jni_code.put(short.class, "S") ;
class2jni_code.put(int.class, "I") ;
class2jni_code.put(long.class, "J") ;
class2jni_code.put(float.class, "F") ;
class2jni_code.put(double.class, "D") ;
class2jni_code.put(boolean.class, "Z") ;
class2jni_code.put(char.class, "C") ;
class2jni_code.put(void.class, "V") ;
} ;
static private HashMap<Class, Class> class2wrapper = new HashMap<>() ;
static {
class2wrapper.put(byte.class, java.lang.Byte.class) ;
class2wrapper.put(short.class, java.lang.Short.class) ;
class2wrapper.put(int.class, java.lang.Integer.class) ;
class2wrapper.put(long.class, java.lang.Long.class) ;
class2wrapper.put(float.class, java.lang.Float.class) ;
class2wrapper.put(double.class, java.lang.Double.class) ;
class2wrapper.put(boolean.class, java.lang.Boolean.class) ;
class2wrapper.put(char.class, java.lang.Character.class) ;
class2wrapper.put(void.class, java.lang.Void.class) ;
} ;
static private HashMap<String, Class> name2class = new HashMap<>() ;
static {
name2class.put("byte", byte.class) ;
name2class.put("short", short.class) ;
name2class.put("int", int.class) ;
name2class.put("long", long.class) ;
name2class.put("float", float.class) ;
name2class.put("double", double.class) ;
name2class.put("boolean", boolean.class) ;
name2class.put("char", char.class) ;
name2class.put("void", void.class) ;
name2class.put("B", byte.class) ;
name2class.put("S", short.class) ;
name2class.put("I", int.class) ;
name2class.put("J", long.class) ;
name2class.put("F", float.class) ;
name2class.put("D", double.class) ;
name2class.put("Z", boolean.class) ;
name2class.put("C", char.class) ;
name2class.put("V", void.class) ;
} ;
InlineJavaClass(InlineJavaServer _ijs, InlineJavaProtocol _ijp){
ijs = _ijs ;
ijp = _ijp ;
}
/*
Makes sure a class exists
*/
static Class ValidateClass(String name) throws InlineJavaException {
Class pc = FindType(name) ;
if (pc != null){
return pc ;
}
try {
Class c = Class.forName(name, true, InlineJavaServer.GetInstance().GetUserClassLoader()) ;
return c ;
}
catch (ClassNotFoundException e){
throw new InlineJavaException("Class " + name + " not found") ;
}
}
/*
Remove L...; from a class name if it has been extracted from an Array class name.
*/
static String CleanClassName(String name){
if (name != null){
int l = name.length() ;
if ((l > 2)&&(name.charAt(0) == 'L')&&(name.charAt(l - 1) == ';')){
name = name.substring(1, l - 1) ;
}
}
return name ;
}
static private Class ValidateClassQuiet(String name){
try {
return ValidateClass(name) ;
}
catch (InlineJavaException ije){
return null ;
}
}
/*
This is the monster method that determines how to cast arguments
*/
Object [] CastArguments(Class [] params, ArrayList args) throws InlineJavaException {
Object ret[] = new Object [params.length] ;
for (int i = 0 ; i < params.length ; i++){
// Here the args are all strings or objects (or undef)
// we need to match them to the prototype.
Class p = params[i] ;
InlineJavaUtils.debug(4, "arg " + String.valueOf(i) + " of signature is " + p.getName()) ;
ret[i] = CastArgument(p, (String)args.get(i)) ;
}
return ret ;
}
/*
This is the monster method that determines how to cast arguments
*/
Object CastArgument(Class p, String argument) throws InlineJavaException {
Object ret = null ;
ArrayList<String> tokens = new ArrayList<>() ;
StringTokenizer st = new StringTokenizer(argument, ":") ;
for (int j = 0 ; st.hasMoreTokens() ; j++){
tokens.add(j, st.nextToken()) ;
}
if (tokens.size() == 1){
tokens.add(1, "") ;
}
String type = (String)tokens.get(0) ;
// We need to separate the primitive types from the
// reference types.
boolean num = ClassIsNumeric(p) ;
if ((num)||(ClassIsString(p))){
Class ap = p ;
if (ap == java.lang.Number.class){
InlineJavaUtils.debug(4, "specializing java.lang.Number to java.lang.Double") ;
ap = java.lang.Double.class ;
}
else if (ap.getName().equals("java.lang.CharSequence")){
InlineJavaUtils.debug(4, "specializing java.lang.CharSequence to java.lang.String") ;
ap = java.lang.String.class ;
}
if (type.equals("undef")){
if (num){
InlineJavaUtils.debug(4, "args is undef -> forcing to " + ap.getName() + " 0") ;
ret = ijp.CreateObject(ap, new Object [] {"0"}, new Class [] {String.class}) ;
InlineJavaUtils.debug(4, " result is " + ret.toString()) ;
}
else{
ret = null ;
InlineJavaUtils.debug(4, "args is undef -> forcing to " + ap.getName() + " " + ret) ;
InlineJavaUtils.debug(4, " result is " + ret) ;
}
}
else if (type.equals("scalar")){
String arg = ijp.Decode((String)tokens.get(1)) ;
InlineJavaUtils.debug(4, "args is scalar (" + arg + ") -> forcing to " + ap.getName()) ;
try {
ret = ijp.CreateObject(ap, new Object [] {arg}, new Class [] {String.class}) ;
InlineJavaUtils.debug(4, " result is " + ret.toString()) ;
}
catch (NumberFormatException e){
throw new InlineJavaCastException("Can't convert " + arg + " to " + ap.getName()) ;
}
}
else if (type.equals("double")){
String arg = ijp.Decode((String)tokens.get(1)) ;
// We have native double bytes in arg.
long l = 0 ;
char c[] = arg.toCharArray() ;
for (int i = 0 ; i < 8 ; i++){
l += (((long)c[i]) << (8 * i)) ;
}
double d = Double.longBitsToDouble(l) ;
ret = Double.valueOf(d) ;
}
else {
throw new InlineJavaCastException("Can't convert reference to " + p.getName()) ;
}
}
else if (ClassIsBool(p)){
if (type.equals("undef")){
InlineJavaUtils.debug(4, "args is undef -> forcing to bool false") ;
ret = Boolean.FALSE;
InlineJavaUtils.debug(4, " result is " + ret.toString()) ;
}
else if (type.equals("scalar")){
String arg = ijp.Decode((String)tokens.get(1)) ;
InlineJavaUtils.debug(4, "args is scalar (" + arg + ") -> forcing to bool") ;
if ((arg.equals(""))||(arg.equals("0"))){
arg = "false" ;
}
else{
arg = "true" ;
}
ret = Boolean.valueOf(arg) ;
InlineJavaUtils.debug(4, " result is " + ret.toString()) ;
}
else{
throw new InlineJavaCastException("Can't convert reference to " + p.getName()) ;
}
}
else if (ClassIsChar(p)){
if (type.equals("undef")){
InlineJavaUtils.debug(4, "args is undef -> forcing to char '\0'") ;
ret = Character.valueOf('\0') ;
InlineJavaUtils.debug(4, " result is " + ret.toString()) ;
}
else if (type.equals("scalar")){
String arg = ijp.Decode((String)tokens.get(1)) ;
InlineJavaUtils.debug(4, "args is scalar -> forcing to char") ;
char c = '\0' ;
if (arg.length() == 1){
c = arg.toCharArray()[0] ;
}
else if (arg.length() > 1){
throw new InlineJavaCastException("Can't convert " + arg + " to " + p.getName()) ;
}
ret = Character.valueOf(c) ;
InlineJavaUtils.debug(4, " result is " + ret.toString()) ;
}
else{
throw new InlineJavaCastException("Can't convert reference to " + p.getName()) ;
}
}
else {
InlineJavaUtils.debug(4, "class " + p.getName() + " is reference") ;
// We know that what we expect here is a real object
if (type.equals("undef")){
InlineJavaUtils.debug(4, "args is undef -> forcing to null") ;
ret = null ;
}
else if (type.equals("scalar")){
// Here if we need a java.lang.Object.class, it's probably
// because we can store anything, so we use a String object.
if (p == java.lang.Object.class){
String arg = ijp.Decode((String)tokens.get(1)) ;
ret = arg ;
}
else{
throw new InlineJavaCastException("Can't convert primitive type to " + p.getName()) ;
}
}
else if (type.equals("java_object")){
// We need an object and we got an object...
InlineJavaUtils.debug(4, "class " + p.getName() + " is reference") ;
String c_name = (String)tokens.get(1) ;
String objid = (String)tokens.get(2) ;
Class c = ValidateClass(c_name) ;
if (DoesExtend(c, p) > -1){
InlineJavaUtils.debug(4, " " + c.getName() + " is a kind of " + p.getName()) ;
// get the object from the hash table
int id = Integer.parseInt(objid) ;
Object o = ijs.GetObject(id) ;
ret = o ;
}
else{
throw new InlineJavaCastException("Can't cast a " + c.getName() + " to a " + p.getName()) ;
}
}
else{
InlineJavaUtils.debug(4, "class " + p.getName() + " is reference") ;
String pkg = (String)tokens.get(1) ;
pkg = pkg.replace('/', ':') ;
String objid = (String)tokens.get(2) ;
if (DoesExtend(p, org.perl.inline.java.InlineJavaPerlObject.class) > -1){
InlineJavaUtils.debug(4, " Perl object is a kind of " + p.getName()) ;
int id = Integer.parseInt(objid) ;
ret = new InlineJavaPerlObject(pkg, id) ;
}
else{
throw new InlineJavaCastException("Can't cast a Perl object to a " + p.getName()) ;
}
}
}
return ret ;
}
/*
Returns the number of levels that separate a from b
*/
static int DoesExtend(Class a, Class b){
return DoesExtend(a, b, 0) ;
}
static int DoesExtend(Class a, Class b, int level){
InlineJavaUtils.debug(4, "checking if " + a.getName() + " extends " + b.getName()) ;
if (a == b){
return level ;
}
Class parent = a.getSuperclass() ;
if (parent != null){
InlineJavaUtils.debug(4, " parent is " + parent.getName()) ;
int ret = DoesExtend(parent, b, level + 1) ;
if (ret != -1){
return ret ;
}
}
// Maybe b is an interface a implements it?
Class inter[] = a.getInterfaces() ;
for (int i = 0 ; i < inter.length ; i++){
InlineJavaUtils.debug(4, " interface is " + inter[i].getName()) ;
int ret = DoesExtend(inter[i], b, level + 1) ;
if (ret != -1){
return ret ;
}
}
return -1 ;
}
/*
Finds the wrapper class for the passed primitive type.
*/
static Class FindWrapper(Class p){
Class w = (Class)class2wrapper.get(p) ;
if (w == null){
w = p ;
}
return w ;
}
/*
Finds the primitive type class for the passed primitive type name.
*/
static Class FindType (String name){
return (Class)name2class.get(name) ;
}
static String FindJNICode(Class p){
if (! Object.class.isAssignableFrom(p)){
return (String)class2jni_code.get(p) ;
}
else {
String name = p.getName().replace('.', '/') ;
if (p.isArray()){
return name ;
}
else{
return "L" + name + ";" ;
}
}
}
static boolean ClassIsPrimitive(Class p){
String name = p.getName() ;
if ((ClassIsNumeric(p))||(ClassIsString(p))||(ClassIsChar(p))||(ClassIsBool(p))){
InlineJavaUtils.debug(4, "class " + name + " is primitive") ;
return true ;
}
return false ;
}
/*
Determines if class is of numerical type.
*/
static private HashMap<Class, Boolean> numeric_classes = new HashMap<>() ;
static {
Class [] list = {
java.lang.Byte.class,
java.lang.Short.class,
java.lang.Integer.class,
java.lang.Long.class,
java.lang.Float.class,
java.lang.Double.class,
java.lang.Number.class,
byte.class,
short.class,
int.class,
long.class,
float.class,
double.class,
} ;
for (int i = 0 ; i < list.length ; i++){
numeric_classes.put(list[i], Boolean.TRUE) ;
}
}
static boolean ClassIsNumeric (Class p){
return (numeric_classes.get(p) != null) ;
}
static private HashMap<Class, Boolean> double_classes = new HashMap<>();
static {
Class [] list = {
java.lang.Double.class,
double.class,
} ;
for (int i = 0 ; i < list.length ; i++){
double_classes.put(list[i], Boolean.TRUE) ;
}
}
static boolean ClassIsDouble (Class p){
return (double_classes.get(p) != null) ;
}
/*
Class is String or StringBuffer
*/
static private HashMap<Class, Boolean> string_classes = new HashMap<>();
static {
Class csq = ValidateClassQuiet("java.lang.CharSequence") ;
Class [] list = {
java.lang.String.class,
java.lang.StringBuffer.class,
csq
} ;
for (int i = 0 ; i < list.length ; i++){
string_classes.put(list[i], Boolean.TRUE) ;
}
}
static boolean ClassIsString (Class p){
return (string_classes.get(p) != null) ;
}
/*
Class is Char
*/
static private HashMap<Class, Boolean> char_classes = new HashMap<>() ;
static {
Class [] list = {
java.lang.Character.class,
char.class,
} ;
for (int i = 0 ; i < list.length ; i++){
char_classes.put(list[i], Boolean.TRUE) ;
}
}
static boolean ClassIsChar (Class p){
return (char_classes.get(p) != null) ;
}
/*
Class is Bool
*/
static private HashMap<Class, Boolean> bool_classes = new HashMap<>() ;
static {
Class [] list = {
java.lang.Boolean.class,
boolean.class,
} ;
for (int i = 0 ; i < list.length ; i++){
bool_classes.put(list[i], Boolean.TRUE) ;
}
}
static boolean ClassIsBool (Class p){
return (bool_classes.get(p) != null) ;
}
/*
Determines if a class is not of a primitive type or of a
wrapper class.
*/
static boolean ClassIsReference (Class p){
String name = p.getName() ;
if (ClassIsPrimitive(p)){
return false ;
}
InlineJavaUtils.debug(4, "class " + name + " is reference") ;
return true ;
}
static boolean ClassIsArray (Class p){
String name = p.getName() ;
if ((name.startsWith("["))&&(ClassIsReference(p))){
InlineJavaUtils.debug(4, "class " + name + " is array") ;
return true ;
}
return false ;
}
static boolean ClassIsPublic (Class p){
int pub = p.getModifiers() & Modifier.PUBLIC ;
if (pub != 0){
return true ;
}
return false ;
}
static boolean ClassIsHandle (Class p){
if ((ClassIsReadHandle(p))||(ClassIsWriteHandle(p))){
return true ;
}
return false ;
}
static boolean ClassIsReadHandle (Class p){
if ((java.io.Reader.class.isAssignableFrom(p))||
(java.io.InputStream.class.isAssignableFrom(p))){
return true ;
}
return false ;
}
static boolean ClassIsWriteHandle (Class p){
if ((java.io.Writer.class.isAssignableFrom(p))||
(java.io.OutputStream.class.isAssignableFrom(p))){
return true ;
}
return false ;
}
}
|