* Removed all Id tags.
[cacao.git] / tests / regression / MinimalClassReflection.java
1 /* tests/regression/MinimalClassReflection.java - checks most some of the
2    reflective methods onto java.lang.Class
3
4    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
5    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
6    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
7    TU Wien
8
9    This file is part of CACAO.
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2, or (at
14    your option) any later version.
15
16    This program is distributed in the hope that it will be useful, but
17    WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24    02110-1301, USA.
25
26    Contact: cacao@cacaojvm.org
27
28    Authors: Mathias Panzenböck
29
30 */
31
32 import java.lang.reflect.Method;
33 import java.lang.reflect.Constructor;
34
35 public class MinimalClassReflection {
36         public static void printClassInfo(String header, Class<?> cls) {
37                 Class<?>       clazz;
38                 Constructor<?> constr;
39                 Method         method;
40                 
41                 p("---------------------- %s ----------------------", header);
42                 p("is local:              %s", cls.isLocalClass());
43                 p("is anonymous:          %s", cls.isAnonymousClass());
44                 p("is member:             %s", cls.isMemberClass());
45                 p("name:                  %s", cls.getName());
46                 p("simple name:           %s", cls.getSimpleName());
47                 p("canonical name:        %s", cls.getCanonicalName());
48                 
49                 clazz = cls.getSuperclass();
50                 p("super class:           %s",
51                                 (clazz == null ? "null" : clazz.getName()));
52                 
53                 clazz = cls.getDeclaringClass();
54                 p("declaring class:       %s",
55                                 (clazz == null ? "null" : clazz.getName()));
56                 
57                 clazz = cls.getEnclosingClass();
58                 p("enclosing class:       %s",
59                                 (clazz == null ? "null" : clazz.getName()));
60                 
61                 constr = cls.getEnclosingConstructor();
62                 p("enclosing constructor: %s",
63                                 (constr == null ? "null" :
64                                         constr.getDeclaringClass().getName() +
65                                         "." + constr.getName()));
66                 
67                 method = cls.getEnclosingMethod();
68                 p("enclosing method:      %s",
69                                 (method == null ? "null" :
70                                         method.getDeclaringClass().getName() +
71                                         "." + method.getName()));
72                 
73                 p();
74         }
75         
76         public static void main(String[] args) {
77                 class ALocalClass {
78                         class AMemberClass {
79                         }
80                         
81                         public ALocalClass() {
82                                 class AnotherLocalClass {
83                                 }
84
85                                 printClassInfo(
86                                                 "test a member class",
87                                                 AMemberClass.class);
88                                 
89                                 printClassInfo(
90                                                 "test a anonymous class derived from a member class",
91                                                 new AMemberClass() {}.getClass());
92                                 
93                                 printClassInfo(
94                                                 "test a local class (local to a constructor)",
95                                                 AnotherLocalClass.class);
96                                 
97                                 printClassInfo(
98                                                 "test a anonymous class derived from a local " +
99                                                 "class (local to a constructor)",
100                                                 new AnotherLocalClass() {}.getClass());
101                         }
102                 }
103                 
104                 printClassInfo(
105                                 "test a normal class",
106                                 MinimalClassReflection.class);
107                 
108                 printClassInfo(
109                                 "test a local class (local to a method)",
110                                 ALocalClass.class);
111                 
112                 printClassInfo(
113                                 "test a anonymous class",
114                                 new Object() {}.getClass());
115                 
116                 printClassInfo(
117                                 "test a anonymous class derived from a local class" +
118                                 " (local to a method)",
119                                 new ALocalClass() {}.getClass());
120                 
121                 new ALocalClass();
122         }
123         
124         public static void p(String fmt, Object... args) {
125                 System.out.printf(fmt + "\n", args);
126         }
127         
128         public static <T> void p(T o) {
129                 System.out.println(o);
130         }
131         
132         public static void p() {
133                 System.out.println();
134         }
135 }