PageRenderTime 934ms CodeModel.GetById 548ms app.highlight 247ms RepoModel.GetById 135ms app.codeStats 0ms

/src/xmlvm2csharp/test/org/xmlvm/test/nativeImpl/ReflectionTest2.java

https://github.com/tisoft/xmlvm
Java | 353 lines | 305 code | 13 blank | 35 comment | 3 complexity | 018b580afd24525a8b746a68cfdcd601 MD5 | raw file
  1/* Copyright (c) 2002-2011 by XMLVM.org
  2 *
  3 * Project Info:  http://www.xmlvm.org
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms of the GNU Lesser General Public License as published by
  7 * the Free Software Foundation; either version 2.1 of the License, or
  8 * (at your option) any later version.
  9 *
 10 * This library is distributed in the hope that it will be useful, but
 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 13 * License for more details.
 14 *
 15 * You should have received a copy of the GNU Lesser General Public
 16 * License along with this library; if not, write to the Free Software
 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 18 * USA.
 19 */
 20package org.xmlvm.test.nativeImpl;
 21
 22import java.lang.reflect.Array;
 23
 24/*
 25 * tests native implementation of java.lang.Class,
 26 * mainly, getName(), getSimpleName() and getCanonicalName()
 27 */
 28class OuterClass {
 29    class InnerClass{}
 30    class InnerChild extends OuterClass {}
 31}
 32
 33public class ReflectionTest2 {
 34
 35    public static int localId = 1;
 36    public static int anonymousId = 0;
 37
 38    // this dummy method is only used to increment the localId
 39    public static String getLocalName() {
 40        class Local {} // increment localId
 41        Class<?> localClass = Local.class;
 42        return localClass.getName();
 43    }
 44
 45    public static void main(String[] args) {
 46        int numTests = 82;
 47        String[] description = new String[numTests];
 48        boolean[] result = new boolean[numTests];
 49        int i = -1;
 50        String thisPackage = "org.xmlvm.test.nativeImpl";
 51        String thisClass = thisPackage + ".ReflectionTest2";
 52
 53        // getSuperClass:
 54        i++;
 55        result[i] = java.lang.Object.class.getSuperclass() == null;
 56        description[i] = "java.lang.Object.class.getSuperclass() == null";
 57        i++;
 58        result[i] = int.class.getSuperclass() == null;
 59        description[i] = "int.class.getSuperclass() == null";
 60
 61        // getName, getSimpleName, getCanonicalName
 62        i++;
 63        result[i] = java.lang.Object.class.getName().equals("java.lang.Object");
 64        description[i] = "java.lang.Object.class.getName().equals(\"java.lang.Object\")";
 65        i++;
 66        result[i] = java.lang.Object.class.getCanonicalName().equals("java.lang.Object");
 67        description[i] = "java.lang.Object.class.getCanonicalName().equals(\"java.lang.Object\")";
 68        i++;
 69        result[i] = java.lang.Object.class.getSimpleName().equals("Object");
 70        description[i] = "java.lang.Object.class.getSimpleName().equals(\"Object\")";
 71        i++;
 72        result[i] = java.lang.String.class.getName().equals("java.lang.String");
 73        description[i] = "java.lang.String.class.getName().equals(\"java.lang.String\")";
 74        i++;
 75        result[i] = java.lang.String.class.getCanonicalName().equals("java.lang.String");
 76        description[i] = "java.lang.String.class.getCanonicalName().equals(\"java.lang.String\")";
 77        i++;
 78        result[i] = java.lang.String.class.getSimpleName().equals("String");
 79        description[i] = "java.lang.String.class.getSimpleName().equals(\"String\")";
 80        i++;
 81        result[i] = boolean.class.getName().equals("boolean");
 82        description[i] = "boolean.class.getName().equals(\"boolean\")";
 83        i++;
 84        result[i] = boolean.class.getCanonicalName().equals("boolean");
 85        description[i] = "boolean.class.getCanonicalName().equals(\"boolean\")";
 86        i++;
 87        result[i] = boolean.class.getSimpleName().equals("boolean");
 88        description[i] = "boolean.class.getSimpleName().equals(\"boolean\")";
 89        i++;
 90        result[i] = byte.class.getName().equals("byte");
 91        description[i] = "byte.class.getName().equals(\"byte\")";
 92        i++;
 93        result[i] = byte.class.getCanonicalName().equals("byte");
 94        description[i] = "byte.class.getCanonicalName().equals(\"byte\")";
 95        i++;
 96        result[i] = byte.class.getSimpleName().equals("byte");
 97        description[i] = "byte.class.getSimpleName().equals(\"byte\")";
 98        i++;
 99        result[i] = char.class.getName().equals("char");
100        description[i] = "char.class.getName().equals(\"char\")";
101        i++;
102        result[i] = char.class.getCanonicalName().equals("char");
103        description[i] = "char.class.getCanonicalName().equals(\"char\")";
104        i++;
105        result[i] = char.class.getSimpleName().equals("char");
106        description[i] = "char.class.getSimpleName().equals(\"char\")";
107        i++;
108        result[i] = short.class.getName().equals("short");
109        description[i] = "short.class.getName().equals(\"short\")";
110        i++;
111        result[i] = short.class.getCanonicalName().equals("short");
112        description[i] = "short.class.getCanonicalName().equals(\"short\")";
113        i++;
114        result[i] = short.class.getSimpleName().equals("short");
115        description[i] = "short.class.getSimpleName().equals(\"short\")";
116        i++;
117        result[i] = int.class.getName().equals("int");
118        description[i] = "int.class.getName().equals(\"int\")";
119        i++;
120        result[i] = int.class.getCanonicalName().equals("int");
121        description[i] = "int.class.getCanonicalName().equals(\"int\")";
122        i++;
123        result[i] = int.class.getSimpleName().equals("int");
124        description[i] = "int.class.getSimpleName().equals(\"int\")";
125        i++;
126        result[i] = long.class.getName().equals("long");
127        description[i] = "long.class.getName().equals(\"long\")";
128        i++;
129        result[i] = long.class.getCanonicalName().equals("long");
130        description[i] = "long.class.getCanonicalName().equals(\"long\")";
131        i++;
132        result[i] = long.class.getSimpleName().equals("long");
133        description[i] = "long.class.getSimpleName().equals(\"long\")";
134        i++;
135        result[i] = float.class.getName().equals("float");
136        description[i] = "float.class.getName().equals(\"float\")";
137        i++;
138        result[i] = float.class.getCanonicalName().equals("float");
139        description[i] = "float.class.getCanonicalName().equals(\"float\")";
140        i++;
141        result[i] = float.class.getSimpleName().equals("float");
142        description[i] = "float.class.getSimpleName().equals(\"float\")";
143        i++;
144        result[i] = double.class.getName().equals("double");
145        description[i] = "double.class.getName().equals(\"double\")";
146        i++;
147        result[i] = double.class.getCanonicalName().equals("double");
148        description[i] = "double.class.getCanonicalName().equals(\"double\")";
149        i++;
150        result[i] = double.class.getSimpleName().equals("double");
151        description[i] = "double.class.getSimpleName().equals(\"double\")";
152        // inner class:
153        Class<?> innerClass = OuterClass.InnerClass.class;
154        i++;
155        result[i] = innerClass.getName().equals(thisPackage
156                + ".OuterClass$InnerClass");
157        description[i] = "inner class name is: "
158                + thisPackage + "OuterClass$InnerClass";
159        i++;
160        result[i] = innerClass.getSimpleName().equals("InnerClass");
161        description[i] = "inner class simple name is: InnerClass";
162        i++;
163        result[i] = innerClass.getCanonicalName().equals(thisPackage
164                + ".OuterClass.InnerClass");
165        description[i] = "inner class canonical name is: "
166                + thisPackage + "OuterClass.InnerClass";
167        // inherited inner class:
168        innerClass = OuterClass.InnerChild.InnerChild.InnerClass.class;
169        i++;
170        result[i] = innerClass.getName().equals(thisPackage
171                + ".OuterClass$InnerClass");
172        description[i] = "inherited inner class name is: "
173                + thisPackage + "OuterClass$InnerClass";
174        i++;
175        result[i] = innerClass.getSimpleName().equals("InnerClass");
176        description[i] = "injerited inner class simple name is: InnerClass";
177        i++;
178        result[i] = innerClass.getCanonicalName().equals(thisPackage
179                + ".OuterClass.InnerClass");
180        description[i] = "inherited inner class canonical name is: "
181                + thisPackage + "OuterClass.InnerClass";
182        // local:
183        localId++;
184        class Local {}
185        Class<Local> localClass = Local.class;
186        i++;
187        result[i] = localClass.getName().equals(thisClass 
188                + "$" + localId + "Local");
189        description[i] = "local class name is: "
190                + thisClass + "$" + localId + "Local";
191        i++;
192        result[i] = localClass.getSimpleName().equals("Local");
193        description[i] = "local class simple name is: " + "Local";
194        i++;
195        result[i] = localClass.getCanonicalName()==null;
196        description[i] = "local class canonical name is null";
197        // anonymous:
198        anonymousId++;
199        Object anonymousInstance = new Object() {}; // increment the anonymousId
200        anonymousId++;
201        anonymousInstance = new Object() {}; // increment the anonymousId
202        anonymousId++;
203        anonymousInstance = new Object() {};
204        Class<?> anonymousClass = anonymousInstance.getClass();
205        i++;
206        result[i] = anonymousClass.getName().equals(thisClass + "$" + anonymousId);
207        description[i] = "anonymous class name is: " 
208                + thisClass + "$" + anonymousId;
209        i++;
210        result[i] = anonymousClass.getSimpleName().isEmpty();
211        description[i] = "anonymous class simple name is empty string";
212        i++;
213        result[i] = anonymousClass.getCanonicalName()==null;
214        description[i] = "anonymous class canonical name is null";
215
216        // arrays names:
217        i++;
218        result[i] = java.lang.Object[].class.getName().equals("[Ljava.lang.Object;");
219        description[i] = "java.lang.Object[].class.getName().equals(\"[Ljava.lang.Object;\")";
220        i++;
221        result[i] = java.lang.Object[].class.getCanonicalName().equals("java.lang.Object[]");
222        description[i] = "java.lang.Object[].class.getCanonicalName().equals(\"java.lang.Object[]\")";
223        i++;
224        result[i] = java.lang.Object[].class.getSimpleName().equals("Object[]");
225        description[i] = "java.lang.Object[].class.getSimpleName().equals(\"Object[]\")";
226        i++;
227        result[i] = java.lang.String[].class.getName().equals("[Ljava.lang.String;");
228        description[i] = "java.lang.String[].class.getName().equals(\"[Ljava.lang.String;\")";
229        i++;
230        result[i] = java.lang.String[].class.getCanonicalName().equals("java.lang.String[]");
231        description[i] = "java.lang.String[].class.getCanonicalName().equals(\"java.lang.String[]\")";
232        i++;
233        result[i] = java.lang.String[].class.getSimpleName().equals("String[]");
234        description[i] = "java.lang.String[].class.getSimpleName().equals(\"String[]\")";
235        i++;
236        result[i] = boolean[].class.getName().equals("[Z");
237        description[i] = "boolean[].class.getName().equals(\"[Z\")";
238        i++;
239        result[i] = boolean[].class.getCanonicalName().equals("boolean[]");
240        description[i] = "boolean[].class.getCanonicalName().equals(\"boolean[]\")";
241        i++;
242        result[i] = boolean[].class.getSimpleName().equals("boolean[]");
243        description[i] = "boolean[].class.getSimpleName().equals(\"boolean[]\")";
244        i++;
245        result[i] = byte[].class.getName().equals("[B");
246        description[i] = "byte[].class.getName().equals(\"[B\")";
247        i++;
248        result[i] = byte[].class.getCanonicalName().equals("byte[]");
249        description[i] = "byte[].class.getCanonicalName().equals(\"byte[]\")";
250        i++;
251        result[i] = byte[].class.getSimpleName().equals("byte[]");
252        description[i] = "byte[].class.getSimpleName().equals(\"byte[]\")";
253        i++;
254        result[i] = char[].class.getName().equals("[C");
255        description[i] = "char[].class.getName().equals(\"[C\")";
256        i++;
257        result[i] = char[].class.getCanonicalName().equals("char[]");
258        description[i] = "char[].class.getCanonicalName().equals(\"char[]\")";
259        i++;
260        result[i] = char[].class.getSimpleName().equals("char[]");
261        description[i] = "char[].class.getSimpleName().equals(\"char[]\")";
262        i++;
263        result[i] = short[].class.getName().equals("[S");
264        description[i] = "short[].class.getName().equals(\"[S\")";
265        i++;
266        result[i] = short[].class.getCanonicalName().equals("short[]");
267        description[i] = "short[].class.getCanonicalName().equals(\"short[]\")";
268        i++;
269        result[i] = short[].class.getSimpleName().equals("short[]");
270        description[i] = "short[].class.getSimpleName().equals(\"short[]\")";
271        i++;
272        result[i] = int[].class.getName().equals("[I");
273        description[i] = "int[].class.getName().equals(\"[I\")";
274        i++;
275        result[i] = int[].class.getCanonicalName().equals("int[]");
276        description[i] = "int[].class.getCanonicalName().equals(\"int[]\")";
277        i++;
278        result[i] = int[].class.getSimpleName().equals("int[]");
279        description[i] = "int[].class.getSimpleName().equals(\"int[]\")";
280        i++;
281        result[i] = long[].class.getName().equals("[J");
282        description[i] = "long[].class.getName().equals(\"[J\")";
283        i++;
284        result[i] = long[].class.getCanonicalName().equals("long[]");
285        description[i] = "long[].class.getCanonicalName().equals(\"long[]\")";
286        i++;
287        result[i] = long[].class.getSimpleName().equals("long[]");
288        description[i] = "long[].class.getSimpleName().equals(\"long[]\")";
289        i++;
290        result[i] = float[].class.getName().equals("[F");
291        description[i] = "float[].class.getName().equals(\"[F\")";
292        i++;
293        result[i] = float[].class.getCanonicalName().equals("float[]");
294        description[i] = "float[].class.getCanonicalName().equals(\"float[]\")";
295        i++;
296        result[i] = float[].class.getSimpleName().equals("float[]");
297        description[i] = "float[].class.getSimpleName().equals(\"float[]\")";
298        i++;
299        result[i] = double[].class.getName().equals("[D");
300        description[i] = "double[].class.getName().equals(\"[D\")";
301        i++;
302        result[i] = double[].class.getCanonicalName().equals("double[]");
303        description[i] = "double[].class.getCanonicalName().equals(\"double[]\")";
304        i++;
305        result[i] = double[].class.getSimpleName().equals("double[]");
306        description[i] = "double[].class.getSimpleName().equals(\"double[]\")";
307        // annonymous arrays
308        i++;
309        Object localArray = Array.newInstance(localClass, 42);
310        result[i] = localArray.getClass().getName().equals("[L"
311                + thisClass + "$" + localId + "Local;");
312        description[i] = "local class array's class name is: "
313                + "[L" + thisClass + "$" + localId + "Local;";
314        i++;
315        result[i] = localArray.getClass().getSimpleName().equals("Local[]");
316        description[i] = "local class array's class simple name is \"Local[]\"";
317        i++;
318        result[i] = localArray.getClass().getCanonicalName()==null;
319        description[i] = "local class array's class canonical name is null";
320        // anonymous arrays
321        i++;
322        Object anonymousArray = Array.newInstance(anonymousClass, 42);
323        result[i] = anonymousArray.getClass().getName().equals("[L" 
324                + thisClass + "$" + anonymousId + ";");
325        description[i] = "anonymous class array's class name is: "
326                + "[L" + thisClass + "$" + anonymousId + ";";
327        i++;
328        result[i] = anonymousArray.getClass().getSimpleName().equals("[]");
329        description[i] = "anonymous class array's class simple name is \"[]\"";
330        i++;
331        result[i] = anonymousArray.getClass().getCanonicalName()==null;
332        description[i] = "anonymous class array's class canonical name is null";
333        // multi-dim'l arrays
334        i++;
335        result[i] = int[][][][][].class.getName().equals("[[[[[I");
336        description[i] = "int[][][][][].class.getName().equals(\"[[[[[I\")";
337        i++;
338        result[i] = int[][][][][].class.getCanonicalName().equals("int[][][][][]");
339        description[i] = "int[][][][][].class.getCanonicalName().equals(\"int[][][][][]\")";
340        i++;
341        result[i] = int[][][][][].class.getSimpleName().equals("int[][][][][]");
342        description[i] = "int[][][][][].class.getSimpleName().equals(\"int[][][][][]\")";
343
344
345
346        // print results
347        for (int j=0; j<numTests; j++) {
348            System.out.println(j + ") "
349                    + (result[j] ? "passed" : "failed") + ": "+ description[j]);
350        }
351    }
352
353}