File: 07_polymorph.t

package info (click to toggle)
libinline-java-perl 0.58~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 964 kB
  • ctags: 684
  • sloc: perl: 4,717; java: 2,844; makefile: 35
file content (161 lines) | stat: -rw-r--r-- 2,632 bytes parent folder | download
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
use strict ;
use Test ;

use Inline Config => 
           DIRECTORY => './_Inline_test';

use Inline(
	Java => 'DATA',
	STUDY => ['java.util.HashMap', 'java.lang.String'],
	AUTOSTUDY => 1,
) ;

use Inline::Java qw(cast coerce) ;


BEGIN {
	plan(tests => 24) ;
}


my $t = new types7() ;

{
	my $t1 = new t17() ;
	
	ok($t->func(5), "int") ;
	ok($t->func(coerce("char", 5)), "char") ;
	ok($t->func(55), "int") ;
	ok($t->func("str"), "string") ;
	ok($t->func(coerce("java.lang.StringBuffer", "str")), "stringbuffer") ;
	
	ok($t->f($t->{hm}), "hashmap") ;
	ok($t->f(cast("java.lang.Object", $t->{hm})), "object") ;
	
	ok($t->f(["a", "b", "c"]), "string[]") ;
	
	ok($t->f(["12.34", "45.67"]), "double[]") ;
	ok($t->f(coerce("java.lang.Object", ['a'], "[Ljava.lang.String;")), "object") ;
	
	eval {$t->func($t1)} ; ok($@, qr/Can't find any signature/) ;
	eval {$t->func(cast("int", $t1))} ; ok($@, qr/Can't cast (.*) to a int/) ;
	
	my $t2 = new t27() ;
	ok($t2->f($t2), "t1") ;
	ok($t1->f($t2), "t1") ;
	ok($t2->f($t1), "t2") ;
	ok($t2->f(cast("t17", $t2)), "t2") ;

	ok($t2->f($t1), "t2") ;

	# Here we should always get the more specific stuff
	ok($t2->{i}, 7) ;
	ok($t2->{j}, 3.1416) ;

	# So this should fail
	eval {$t2->{j} = "string"} ; ok($@, qr/Can't convert/) ;

	# Interfaces
	my $al = $t1->get_al() ;
	ok(0, $t1->count($al)) ;

	my $hm = new java::util::HashMap() ;
	$hm->put('key', 'value') ;
	my $a = $hm->entrySet()->toArray() ;
	foreach my $e (@{$a}){
		ok(cast('java.util.Map$Entry', $e)->getKey(), 'key') ;
	}

	my $str = new java::lang::String('test') ;
	ok($str, 'test') ;
}

ok($t->__get_private()->{proto}->ObjectCount(), 1) ;


__END__

__Java__


import java.util.* ;

class t17 {
	public int i = 5 ;
	public String j = "toto" ;

	public t17(){
	}

	public String f(t27 o){
		return "t1" ;
	}

	public void n(){
	}

	public ArrayList get_al(){
		return new ArrayList() ;
	}

	public int count(Collection c){
		return c.size() ;
	}
}


class t27 extends t17 {
	public int i = 7 ;
	public double j = 3.1416 ;

	public t27(){
	}

	public String f(t17 o){
		return "t2" ;
	}

	public void n(){
	}
}


class types7 {
	public HashMap hm = new HashMap() ;

	public types7(){
	}

	public String func(String o){
		return "string" ;
	}

	public String func(StringBuffer o){
		return "stringbuffer" ;
	}

	public String func(int o){
		return "int" ;
	}

	public String func(char o){
		return "char" ;
	}

	public	String f(HashMap o){
		return "hashmap" ;
	}

	public String f(Object o){
		return "object" ;
	}

	public String f(String o[]){
		return "string[]" ;
	}

	public String f(double o[]){
		return "double[]" ;
	}
}