KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LocalTypes


1 /* LocalTypes Copyright (C) 1999 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: LocalTypes.java,v 1.6 1999/08/19 15:16:59 jochen Exp $
18  */

19
20
21 class A {
22 }
23
24 interface I1 {
25 }
26
27 interface I2 {
28 }
29
30 interface I12 extends I1, I2 {
31 }
32
33 class B extends A implements I1 {
34 }
35
36 class C extends B implements I2, I12 {
37 }
38
39 class D extends A implements I12 {
40 }
41
42 class E extends A implements I2 {
43 }
44
45 public class LocalTypes {
46     A a;
47     B b;
48     C c;
49     D d;
50     E e;
51
52     I1 i1;
53     I2 i2;
54     I12 i12;
55
56     boolean g_bo;
57     byte g_by;
58     short g_sh;
59     int g_in;
60
61     int z;
62     int[]ain;
63
64     public void arithTest() {
65         int a=1,b=2;
66         boolean xb = true,yb = false;
67     int xi = 1,yi = 0;
68         int c=0;
69         arithTest();
70         if ((xb & yb) || (xi & yi) != 0) {
71             c = 5;
72             arithTest();
73             xb &= yb;
74             xi &= yi;
75             arithTest();
76             xb = xb | yb;
77             xi = xi | yi;
78             arithTest();
79             xb ^= yb;
80             xi ^= yi;
81             arithTest();
82             xb = xb && yb;
83             xi = (xi != 0) && (yi != 0) ? 1 : 0;
84             arithTest();
85             b <<= a;
86             b <<= c;
87         }
88     xi++;
89         a&=b;
90     }
91     
92     public void intTypeTest() {
93         boolean b = false;
94         boolean abo[] = null;
95         byte aby[] = null;
96         byte by;
97         int in;
98         short sh;
99         b = g_bo;
100         in = g_sh;
101         sh = (short)g_in;
102         by = (byte)sh;
103         sh = by;
104         in = by;
105         abo[0] = g_bo;
106         abo[1] = false;
107         abo[2] = true;
108         aby[0] = g_by;
109         aby[1] = 0;
110         aby[2] = 1;
111     }
112
113     native void arrFunc(B[] b);
114     
115     /**
116      * This is an example where it is really hard to know, which type
117      * each local has.
118      */

119     void DifficultType () {
120         B myB = c;
121     C myC = c;
122         I2 myI2 = c;
123         I12 myI12 = c;
124     boolean bool = true;
125         B[] aB = new C[3];
126         arrFunc(new C[3]);
127
128     while (bool) {
129             if (bool) {
130                 i1 = myB;
131                 i2 = myC;
132                 i1 = aB[0];
133             } else if (bool) {
134                 i1 = myI12;
135                 i2 = myI2;
136             } else {
137                 i1 = myC;
138                 i2 = myI12;
139             }
140         myB = b;
141             if (bool)
142                 myI2 = i12;
143             else {
144                 myI12 = d;
145                 myI2 = e;
146             }
147     }
148     }
149
150     /**
151      * This is an example where it is really hard to know, which type
152      * each local has.
153      */

154     void DifficultArrayType () {
155         boolean bool = true;
156         B[] aB = new C[3];
157         arrFunc(new C[3]);
158         C[][][][][] x = new C[4][3][4][][];
159         int[][][][][] y = new int[1][2][][][];
160
161     while (bool) {
162             if (bool) {
163                 i1 = aB[0];
164                 aB[0] = b;
165             }
166     }
167     }
168
169     public void castTests() {
170     System.err.println(null instanceof int[]);
171     System.err.println(((Object JavaDoc)new int[4]) instanceof byte[]);
172     System.err.println(((Object JavaDoc)new byte[4]) instanceof int[]);
173     System.err.println(((Object JavaDoc)new int[4]) instanceof LocalTypes);
174     System.err.println(((Object JavaDoc)new int[4]) instanceof int[][]);
175     System.err.println(new Object JavaDoc[4] instanceof int[][]);
176     System.err.println(new int[5][4] instanceof java.io.Serializable JavaDoc[]);
177     System.err.println(((Object JavaDoc)this) instanceof RuntimeException JavaDoc);
178     System.err.println(((RuntimeException JavaDoc)(Object JavaDoc)this).getMessage());
179     ((java.io.PrintStream JavaDoc)null).println("Hallo");
180     ((LocalTypes) null).a = ((LocalTypes) null).b;
181     }
182 }
183
184
Popular Tags