File: InlineJavaProtocol.java

package info (click to toggle)
libinline-java-perl 0.67-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: perl: 3,817; java: 2,621; makefile: 35; sh: 1
file content (880 lines) | stat: -rw-r--r-- 24,194 bytes parent folder | download | duplicates (3)
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
package org.perl.inline.java ;

import java.util.* ;
import java.io.* ;
import java.lang.reflect.* ;


/*
	This is where most of the work of Inline Java is done. Here determine
	the request type and then we proceed to serve it.
*/
class InlineJavaProtocol {
	private InlineJavaServer ijs ;
	private InlineJavaClass ijc ;
	private InlineJavaArray ija ;
	private String cmd ;
	private String response = null ;

	private final String encoding = "UTF-8" ;

	static private Map<String, Member> member_cache = Collections.synchronizedMap(new HashMap<String, Member>()) ;
	static private final String report_version = "V2" ;

	InlineJavaProtocol(InlineJavaServer _ijs, String _cmd) {
		ijs = _ijs ;
		ijc = new InlineJavaClass(ijs, this) ;
		ija = new InlineJavaArray(ijc) ;

		cmd = _cmd ;	
	}


	/*
		Starts the analysis of the command line
	*/
	void Do() throws InlineJavaException {
		StringTokenizer st = new StringTokenizer(cmd, " ") ;
		String c = st.nextToken() ;

		if (c.equals("call_method")){
			CallJavaMethod(st) ;
		}		
		else if (c.equals("set_member")){
			SetJavaMember(st) ;
		}		
		else if (c.equals("get_member")){
			GetJavaMember(st) ;
		}		
		else if (c.equals("add_classpath")){
			AddClassPath(st) ;
		}
		else if (c.equals("server_type")){
			ServerType(st) ;
		}
		else if (c.equals("report")){
			Report(st) ;
		}
		else if (c.equals("isa")){
			IsA(st) ;
		}
		else if (c.equals("create_object")){
			CreateJavaObject(st) ;
		}
		else if (c.equals("delete_object")){
			DeleteJavaObject(st) ;
		}
		else if (c.equals("obj_cnt")){
			ObjectCount(st) ;
		}
		else if (c.equals("cast")){
			Cast(st) ;
		}
		else if (c.equals("read")){
			Read(st) ;
		}
		else if (c.equals("make_buffered")){
			MakeBuffered(st) ;
		}
		else if (c.equals("readline")){
			ReadLine(st) ;
		}
		else if (c.equals("write")){
			Write(st) ;
		}
		else if (c.equals("close")){
			Close(st) ;
		}
		else if (c.equals("die")){
			InlineJavaUtils.debug(1, "received a request to die...") ;
			ijs.Shutdown() ;
		}
		else {
			throw new InlineJavaException("Unknown command " + c) ;
		}
	}

	/*
		Returns a report on the Java classes, listing all public methods
		and members
	*/
	void Report(StringTokenizer st) throws InlineJavaException {
		StringBuffer pw = new StringBuffer(report_version + "\n") ;

		StringTokenizer st2 = new StringTokenizer(st.nextToken(), ":") ;
		st2.nextToken() ;

		StringTokenizer st3 = new StringTokenizer(Decode(st2.nextToken()), " ") ;

		ArrayList<String> class_list = new ArrayList<>() ;
		while (st3.hasMoreTokens()){
			String c = st3.nextToken() ;
			class_list.add(class_list.size(), c) ;
		}

		for (int i = 0 ; i < class_list.size() ; i++){
			String name = (String)class_list.get(i) ;
			Class c = ijc.ValidateClass(name) ;

			InlineJavaUtils.debug(3, "reporting for " + c) ;

			Class parent = c.getSuperclass() ;
			String pname = (parent == null ? "null" : parent.getName()) ;
			pw.append("class " + c.getName() + " " + pname + "\n") ;
			Constructor constructors[] = c.getConstructors() ;
			Method methods[] = c.getMethods() ;
			Field fields[] = c.getFields() ;

			boolean pub = ijc.ClassIsPublic(c) ;
			if (pub){
				// If the class is public and has no constructors,
				// we provide a default no-arg constructors.
				if (c.getDeclaredConstructors().length == 0){
					String noarg_sign = InlineJavaUtils.CreateSignature(new Class [] {}) ;
					pw.append("constructor " + noarg_sign + "\n") ;	
				}
			}

			boolean pn = InlineJavaPerlNatives.class.isAssignableFrom(c) ;
			for (int j = 0 ; j < constructors.length ; j++){
				Constructor x = constructors[j] ;
				if ((pn)&&(Modifier.isNative(x.getModifiers()))){
					continue ;
				}
				Class params[] = x.getParameterTypes() ;
				String sign = InlineJavaUtils.CreateSignature(params) ;
				Class decl = x.getDeclaringClass() ;
				pw.append("constructor " + sign + "\n") ;
			}

			for (int j = 0 ; j < methods.length ; j++){
				Method x = methods[j] ;
				if ((pn)&&(Modifier.isNative(x.getModifiers()))){
					continue ;
				}
				String stat = (Modifier.isStatic(x.getModifiers()) ? " static " : " instance ") ;
				String sign = InlineJavaUtils.CreateSignature(x.getParameterTypes()) ;
				Class decl = x.getDeclaringClass() ;
				pw.append("method" + stat + decl.getName() + " " + x.getName() + sign + "\n") ;
			}

			for (int j = 0 ; j < fields.length ; j++){
				Field x = fields[(InlineJavaUtils.ReverseMembers() ? (fields.length - 1 - j) : j)] ;
				String stat = (Modifier.isStatic(x.getModifiers()) ? " static " : " instance ") ;
				Class decl = x.getDeclaringClass() ;
				Class type = x.getType() ;
				pw.append("field" + stat + decl.getName() + " " + x.getName() + " " + type.getName() + "\n") ;
			}
		}

		SetResponse(pw.toString()) ;
	}


	void AddClassPath(StringTokenizer st) throws InlineJavaException {
		while (st.hasMoreTokens()){
			String path = Decode(st.nextToken()) ;
			InlineJavaServer.GetInstance().GetUserClassLoader().AddClassPath(path) ;
		}
		SetResponse(null) ;
	}


	void ServerType(StringTokenizer st) throws InlineJavaException {
		SetResponse(ijs.GetType()) ;
	}


	void IsA(StringTokenizer st) throws InlineJavaException {
		String class_name = st.nextToken() ;
		Class c = ijc.ValidateClass(class_name) ;

		String is_it_a = st.nextToken() ;
		Class d = ijc.ValidateClass(is_it_a) ;

		SetResponse(Integer.valueOf(ijc.DoesExtend(c, d))) ;
	}


	void ObjectCount(StringTokenizer st) throws InlineJavaException {
		SetResponse(Integer.valueOf(ijs.ObjectCount())) ;
	}


	/*
		Creates a Java Object with the specified arguments.
	*/
	void CreateJavaObject(StringTokenizer st) throws InlineJavaException {
		String class_name = st.nextToken() ;
		Class c = ijc.ValidateClass(class_name) ;

		if (! ijc.ClassIsArray(c)){
			ArrayList<Object> f = ValidateMethod(true, c, class_name, st) ;
			Object p[] = (Object [])f.get(1) ;
			Class clist[] = (Class [])f.get(2) ;

			try {
				Object o = CreateObject(c, p, clist) ;
				SetResponse(o) ;
			}
			catch (InlineJavaInvocationTargetException ite){
				Throwable t = ite.GetThrowable() ;
				if (t instanceof InlineJavaException){
					InlineJavaException ije = (InlineJavaException)t ;
					throw ije ;
				}
				else{
					SetResponse(new InlineJavaThrown(t)) ;
				}
			}
		}
		else{
			// Here we send the type of array we want, but CreateArray
			// exception the element type.
			StringBuffer sb = new StringBuffer(class_name) ;
			// Remove the ['s
			while (sb.toString().startsWith("[")){
				sb.replace(0, 1, "") ;	
			}
			// remove the L and the ;
			if (sb.toString().startsWith("L")){
				sb.replace(0, 1, "") ;
				sb.replace(sb.length() - 1, sb.length(), "") ;
			}

			Class ec = ijc.ValidateClass(sb.toString()) ;

			InlineJavaUtils.debug(4, "array elements: " + ec.getName()) ;
			Object o = ija.CreateArray(ec, st) ;
			SetResponse(o) ;
		}
	}


	/*
		Calls a Java method
	*/
	void CallJavaMethod(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		String class_name = st.nextToken() ;
		Object o = null ;
		if (id > 0){
			o = ijs.GetObject(id) ;

			// Use the class sent by Perl (it might be casted)
			// class_name = o.getClass().getName() ;
		}

		Class c = ijc.ValidateClass(class_name) ;
		String method = st.nextToken() ;

		if ((ijc.ClassIsArray(c))&&(method.equals("getLength"))){
			int length = Array.getLength(o) ;
			SetResponse(Integer.valueOf(length)) ;
		}
		else{
			ArrayList<Object> f = ValidateMethod(false, c, method, st) ;
			Method m = (Method)f.get(0) ;
			String name = m.getName() ;	
			Object p[] = (Object [])f.get(1) ;

			try {
				Object ret = InlineJavaServer.GetInstance().GetUserClassLoader().invoke(m, o, p) ;
				SetResponse(ret, AutoCast(ret, m.getReturnType())) ;
			}
			catch (IllegalAccessException e){
				throw new InlineJavaException("You are not allowed to invoke method " + name + " in class " + class_name + ": " + e.getMessage()) ;
			}
			catch (IllegalArgumentException e){
				throw new InlineJavaException("Arguments for method " + name + " in class " + class_name + " are incompatible: " + e.getMessage()) ;
			}
			catch (InvocationTargetException e){
				Throwable t = e.getTargetException() ;
				String type = t.getClass().getName() ;
				String msg = t.getMessage() ;
				InlineJavaUtils.debug(1, "method " + name + " in class " + class_name + " threw exception " + type + ": " + msg) ;
				if (t instanceof InlineJavaException){
					InlineJavaException ije = (InlineJavaException)t ;
					throw ije ;
				}
				Throwable retval = t;
				if (t instanceof InlineJavaPerlException){
					InlineJavaPerlException ijpe = (InlineJavaPerlException)t;
					Object eo = ijpe.GetObject();
                                        if (eo instanceof Throwable) {
                                          retval = (Throwable)eo;
                                        }
					InlineJavaUtils.debug(2, "InlineJavaPerlException " + retval.toString()) ;
				}
				SetResponse(new InlineJavaThrown(retval)) ;
			}
		}
	}


	/*
	*/  
	Class AutoCast(Object o, Class want){
		if (o == null){
			return null ;
		}
		Class got = o.getClass() ;
		if (got.equals(want)){
			return null ;
		}
		boolean _public = (got.getModifiers() & Modifier.PUBLIC) != 0 ;
		if ((_public)||(got.getPackage() == null)){
			return null ;
		}
		InlineJavaUtils.debug(3, "AutoCast: " + got.getName() + " -> " + want.getName()) ;
		return want ;
	}


	/*
		Returns a new reference to the current object, using the provided subtype
	*/
	void Cast(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		String class_name = st.nextToken() ;
		Object o = ijs.GetObject(id) ;
		Class c = ijc.ValidateClass(class_name) ;

		SetResponse(o, c) ;
	}


	/*
	*/
	void Read(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;
		int len = Integer.parseInt(st.nextToken()) ;

		Object o = ijs.GetObject(id) ;
		Object ret = null ;
		try {
			ret = InlineJavaHandle.read(o, len) ;
		}
		catch (java.io.IOException e){
			ret = new InlineJavaThrown(e) ;
		}

		SetResponse(ret) ;
	}


	void MakeBuffered(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		Object o = ijs.GetObject(id) ;
		Object ret = null ;
		try {
			ret = InlineJavaHandle.makeBuffered(o) ;
			if (ret != o){
				int buf_id = ijs.PutObject(ret) ;
				ret = Integer.valueOf(buf_id) ;
			}
			else {
				ret = Integer.valueOf(id) ;
			}
		}
		catch (java.io.IOException e){
			ret = new InlineJavaThrown(e) ;
		}

		SetResponse(ret) ;
	}


	void ReadLine(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		Object o = ijs.GetObject(id) ;
		Object ret = null ;
		try {
			ret = InlineJavaHandle.readLine(o) ;
		}
		catch (java.io.IOException e){
			ret = new InlineJavaThrown(e) ;
		}

		SetResponse(ret) ;
	}


	void Write(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;
		Object arg = ijc.CastArgument(Object.class, st.nextToken()) ;

		Object o = ijs.GetObject(id) ;
		Object ret = null ;
		try {
			int len = InlineJavaHandle.write(o, arg.toString()) ;
			ret = Integer.valueOf(len) ;
		}
		catch (java.io.IOException e){
			ret = new InlineJavaThrown(e) ;
		}

		SetResponse(ret) ;
	}


	void Close(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		Object o = ijs.GetObject(id) ;
		Object ret = null ;
		try {
			InlineJavaHandle.close(o) ;
		}
		catch (java.io.IOException e){
			ret = new InlineJavaThrown(e) ;
		}

		SetResponse(ret) ;
	}


	/*
		Sets a Java member variable
	*/
	void SetJavaMember(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		String class_name = st.nextToken() ;
		Object o = null ;
		if (id > 0){
			o = ijs.GetObject(id) ;

			// Use the class sent by Perl (it might be casted)
			// class_name = o.getClass().getName() ;
		}

		Class c = ijc.ValidateClass(class_name) ;
		String member = st.nextToken() ;

		if (ijc.ClassIsArray(c)){
			int idx = Integer.parseInt(member) ;
			Class type = ijc.ValidateClass(st.nextToken()) ;
			String arg = st.nextToken() ;

			String msg = "For array of type " + c.getName() + ", element " + member + ": " ;
			try {
				Object elem = ijc.CastArgument(type, arg) ;
				InlineJavaServer.GetInstance().GetUserClassLoader().array_set(o, idx, elem) ;
				SetResponse(null) ;
			}
			catch (InlineJavaCastException e){
				throw new InlineJavaCastException(msg + e.getMessage()) ;
			}
			catch (InlineJavaException e){
				throw new InlineJavaException(msg + e.getMessage()) ;
			}
		}
		else{
			ArrayList<Object> fl = ValidateMember(c, member, st) ;
			Field f = (Field)fl.get(0) ;
			String name = f.getName() ;
			Object p = (Object)fl.get(1) ;

			try {
				InlineJavaServer.GetInstance().GetUserClassLoader().set(f, o, p) ;
				SetResponse(null) ;
			}
			catch (IllegalAccessException e){
				throw new InlineJavaException("You are not allowed to set member " + name + " in class " + class_name + ": " + e.getMessage()) ;
			}
			catch (IllegalArgumentException e){
				throw new InlineJavaException("Argument for member " + name + " in class " + class_name + " is incompatible: " + e.getMessage()) ;
			}
		}
	}


	/*
		Gets a Java member variable
	*/
	void GetJavaMember(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		String class_name = st.nextToken() ;
		Object o = null ;
		if (id > 0){
			o = ijs.GetObject(id) ;

			// Use the class sent by Perl (it might be casted)
			// class_name = o.getClass().getName() ;
		}

		Class c = ijc.ValidateClass(class_name) ;
		String member = st.nextToken() ;

		if (ijc.ClassIsArray(c)){
			int idx = Integer.parseInt(member) ;
			Object ret = InlineJavaServer.GetInstance().GetUserClassLoader().array_get(o, idx) ;
			Class eclass = ijc.ValidateClass(ijc.CleanClassName(class_name.substring(1))) ;
			SetResponse(ret, AutoCast(ret, eclass)) ;
		}
		else{
			ArrayList<Object> fl = ValidateMember(c, member, st) ;

			Field f = (Field)fl.get(0) ;
			String name = f.getName() ;
			try {
				Object ret = InlineJavaServer.GetInstance().GetUserClassLoader().get(f, o) ;
				SetResponse(ret, AutoCast(ret, f.getType())) ;
			}
			catch (IllegalAccessException e){
				throw new InlineJavaException("You are not allowed to set member " + name + " in class " + class_name + ": " + e.getMessage()) ;
			}
			catch (IllegalArgumentException e){
				throw new InlineJavaException("Argument for member " + name + " in class " + class_name + " is incompatible: " + e.getMessage()) ;
			}
		}
	}


	/*
		Deletes a Java object
	*/
	void DeleteJavaObject(StringTokenizer st) throws InlineJavaException {
		int id = Integer.parseInt(st.nextToken()) ;

		Object o = ijs.DeleteObject(id) ;

		SetResponse(null) ;
	}

	
	/*
		Creates a Java Object with the specified arguments.
	*/
	Object CreateObject(Class p, Object args[], Class proto[]) throws InlineJavaException {
		p = ijc.FindWrapper(p) ;

		String name = p.getName() ;
		Object ret = null ;
		try {
			ret = InlineJavaServer.GetInstance().GetUserClassLoader().create(p, args, proto) ;
		}
		catch (NoSuchMethodException e){
			throw new InlineJavaException("Constructor for class " + name + " with signature " + InlineJavaUtils.CreateSignature(proto) + " not found: " + e.getMessage()) ;
		}
		catch (InstantiationException e){
			throw new InlineJavaException("You are not allowed to instantiate object of class " + name + ": " + e.getMessage()) ;
		}
		catch (IllegalAccessException e){
			throw new InlineJavaException("You are not allowed to instantiate object of class " + name + " using the constructor with signature " + InlineJavaUtils.CreateSignature(proto) + ": " + e.getMessage()) ;
		}
		catch (IllegalArgumentException e){
			throw new InlineJavaException("Arguments to constructor for class " + name + " with signature " + InlineJavaUtils.CreateSignature(proto) + " are incompatible: " + e.getMessage()) ;
		}
		catch (InvocationTargetException e){
			Throwable t = e.getTargetException() ;
			String type = t.getClass().getName() ;
			String msg = t.getMessage() ;
			throw new InlineJavaInvocationTargetException(
				"Constructor for class " + name + " with signature " + InlineJavaUtils.CreateSignature(proto) + " threw exception " + type + ": " + msg,
				t) ;
		}

		return ret ;
	}


	/*
		Makes sure a method exists
	*/
	ArrayList<Object> ValidateMethod(boolean constructor, Class c, String name, StringTokenizer st) throws InlineJavaException {
		ArrayList<Object> ret = new ArrayList<>() ;

		// Extract signature
		String signature = st.nextToken() ;

		// Extract the arguments
		ArrayList<String> args = new ArrayList<>() ;
		while (st.hasMoreTokens()){
			args.add(args.size(), st.nextToken()) ;
		}

		String key = c.getName() + "." + name + signature ;
		ArrayList<Member> ml = new ArrayList<>() ;
		Class params[] = null ;

		Member cached = (Member)member_cache.get(key) ;
		if (cached != null){
				InlineJavaUtils.debug(3, "method was cached") ;
				ml.add(ml.size(), cached) ;
		}
		else{
			Member ma[] = (constructor ? (Member [])c.getConstructors() : (Member [])c.getMethods()) ;
			for (int i = 0 ; i < ma.length ; i++){
				Member m = ma[i] ;

				if (m.getName().equals(name)){
					InlineJavaUtils.debug(3, "found a " + name + (constructor ? " constructor" : " method")) ;
	
					if (constructor){
						params = ((Constructor)m).getParameterTypes() ;
					}
					else{
						params = ((Method)m).getParameterTypes() ;
					}

					// Now we check if the signatures match
					String sign = InlineJavaUtils.CreateSignature(params, ",") ;
					InlineJavaUtils.debug(3, sign + " = " + signature + "?") ;

					if (signature.equals(sign)){
						InlineJavaUtils.debug(3, "has matching signature " + sign) ;
						ml.add(ml.size(), m) ;
						member_cache.put(key, m) ;
						break ;
					}
				}
			}
		}

		// Now we got a list of matching methods (actually 0 or 1). 
		// We have to figure out which one we will call.
		if (ml.size() == 0){
			// Nothing matched. Maybe we got a default constructor
			if ((constructor)&&(signature.equals("()"))){
				ret.add(0, null) ;
				ret.add(1, new Object [] {}) ;
				ret.add(2, new Class [] {}) ;
			}
			else{
				throw new InlineJavaException(
					(constructor ? "Constructor " : "Method ") + 
					name + " for class " + c.getName() + " with signature " +
					signature + " not found") ;
			}
		}
		else if (ml.size() == 1){
			// Now we need to force the arguments received to match
			// the methods signature.
			Member m = (Member)ml.get(0) ;
			if (constructor){
				params = ((Constructor)m).getParameterTypes() ;
			}
			else{
				params = ((Method)m).getParameterTypes() ;
			}

			String msg = "In method " + name + " of class " + c.getName() + ": " ;
			try {
				ret.add(0, m) ;
				ret.add(1, ijc.CastArguments(params, args)) ;
				ret.add(2, params) ;
			}
			catch (InlineJavaCastException e){
				throw new InlineJavaCastException(msg + e.getMessage()) ;
			}
			catch (InlineJavaException e){
				throw new InlineJavaException(msg + e.getMessage()) ;
			}
		}

		return ret ;
	}


	/*
		Makes sure a member exists
	*/
	ArrayList<Object> ValidateMember(Class c, String name, StringTokenizer st) throws InlineJavaException {
		ArrayList<Object> ret = new ArrayList<>() ;

		// Extract member type
		String type = st.nextToken() ;

		// Extract the argument
		String arg = st.nextToken() ;

		String key = type + " " + c.getName() + "." + name ;
		ArrayList<Member> fl = new ArrayList<>() ;
		Class param = null ;

		Member cached = (Member)member_cache.get(key) ;
		if (cached != null){
			InlineJavaUtils.debug(3, "member was cached") ;
			fl.add(fl.size(), cached) ;
		}
		else {
			Field fa[] = c.getFields() ;
			for (int i = 0 ; i < fa.length ; i++){
				Field f = fa[(InlineJavaUtils.ReverseMembers() ? (fa.length - 1 - i) : i)] ;

				if (f.getName().equals(name)){
					InlineJavaUtils.debug(3, "found a " + name + " member") ;

					param = f.getType() ;
					String t = param.getName() ;
					if (type.equals(t)){
						InlineJavaUtils.debug(3, "has matching type " + t) ;
						fl.add(fl.size(), f) ;
					}
				}
			}
		}

		// Now we got a list of matching members. 
		// We have to figure out which one we will call.
		if (fl.size() == 0){
			throw new InlineJavaException(
				"Member " + name + " of type " + type + " for class " + c.getName() +
					" not found") ;
		}
		else {
			// Now we need to force the arguments received to match
			// the methods signature.

			// If we have more that one, we use the last one, which is the most
			// specialized
			Field f = (Field)fl.get(fl.size() - 1) ;
			member_cache.put(key, f) ;
			param = f.getType() ;

			String msg = "For member " + name + " of class " + c.getName() + ": " ;
			try {
				ret.add(0, f) ;
				ret.add(1, ijc.CastArgument(param, arg)) ;
				ret.add(2, param) ;
			}
			catch (InlineJavaCastException e){
				throw new InlineJavaCastException(msg + e.getMessage()) ;
			}
			catch (InlineJavaException e){
				throw new InlineJavaException(msg + e.getMessage()) ;
			}
		}

		return ret ;
	}


	/*
		This sets the response that will be returned to the Perl
		script
	*/
	void SetResponse(Object o) throws InlineJavaException {
		SetResponse(o, null) ;
	}


	void SetResponse(Object o, Class p) throws InlineJavaException {
		response = "ok " + SerializeObject(o, p) ;
	}


	String SerializeObject(Object o, Class p) throws InlineJavaException {
		Class c = (o == null ? null : o.getClass()) ;

		if ((c != null)&&(p != null)){
			if (ijc.DoesExtend(c, p) < 0){
				throw new InlineJavaException("Can't cast a " + c.getName() + " to a " + p.getName()) ;
			}
			else{
				c = p ;
			}
		}

		if (o == null){
			return "undef:" ;
		}
		else if ((ijc.ClassIsNumeric(c))||(ijc.ClassIsChar(c))||(ijc.ClassIsString(c))){
			if ((ijs.GetNativeDoubles())&&(ijc.ClassIsDouble(c))){
				Double d = (Double)o ;
				long l = Double.doubleToLongBits(d.doubleValue()) ;
				char ca[] = new char[8] ;
				for (int i = 0 ; i < 8 ; i++){
					ca[i] = (char)((l >> (8 * i)) & 0xFF) ;
				}
				return "double:" + Encode(new String(ca)) ;
			}
			else {
				return "scalar:" + Encode(o.toString()) ;
			}
		}
		else if (ijc.ClassIsBool(c)){
			String b = o.toString() ;
			return "scalar:" + Encode((b.equals("true") ? "1" : "0")) ;
		}
		else {
			if (! (o instanceof org.perl.inline.java.InlineJavaPerlObject)){
				// Here we need to register the object in order to send
				// it back to the Perl script.
				boolean thrown = false ;
				String type = "object" ;
				if (o instanceof InlineJavaThrown){ 
					thrown = true ;
					o = ((InlineJavaThrown)o).GetThrowable() ;
					c = o.getClass() ;
				}
				else if (ijc.ClassIsArray(c)){
					type = "array" ;
				}
				else if (ijc.ClassIsHandle(c)){
					type = "handle" ;
				}
				int id = ijs.PutObject(o) ;

				return "java_" + type + ":" + (thrown ? "1" : "0") + ":" + String.valueOf(id) +
					":" + c.getName() ;
			}
			else {
				return "perl_object:" + ((InlineJavaPerlObject)o).GetId() +
					":" + ((InlineJavaPerlObject)o).GetPkg() ;
			}
		}
	}


	byte[] DecodeToByteArray(String s){
		return InlineJavaUtils.DecodeBase64(s.toCharArray()) ;
	}


	String Decode(String s) throws InlineJavaException {
		try {
			if (encoding != null){
				return new String(DecodeToByteArray(s), encoding) ;
			}
			else {
				return new String(DecodeToByteArray(s)) ;
			}
		}
		catch (UnsupportedEncodingException e){
			throw new InlineJavaException("Unsupported encoding: " + e.getMessage()) ;
		}
	}


	String EncodeFromByteArray(byte bytes[]){
		return new String(InlineJavaUtils.EncodeBase64(bytes)) ;
	}


	String Encode(String s) throws InlineJavaException {
		try {
			if (encoding != null){
				return EncodeFromByteArray(s.getBytes(encoding)) ;
			}
			else {
				return EncodeFromByteArray(s.getBytes()) ;
			}
		}
		catch (UnsupportedEncodingException e){
			throw new InlineJavaException("Unsupported encoding: " + e.getMessage()) ;
		}
	}


	String GetResponse(){
		return response ;
	}
}