1a198e05cbc3c30491ba4f1ef3b549fd8184dfc3
[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    $Id$
31
32 */
33
34 import java.lang.reflect.Method;
35 import java.lang.reflect.Constructor;
36
37 public class MinimalClassReflection {
38         public static void printClassInfo(String header, Class<?> cls) {
39                 Class<?>       clazz;
40                 Constructor<?> constr;
41                 Method         method;
42                 
43                 p("---------------------- %s ----------------------", header);
44                 p("is local:              %s", cls.isLocalClass());
45                 p("is anonymous:          %s", cls.isAnonymousClass());
46                 p("is member:             %s", cls.isMemberClass());
47                 p("name:                  %s", cls.getName());
48                 p("simple name:           %s", cls.getSimpleName());
49                 p("canonical name:        %s", cls.getCanonicalName());
50                 
51                 clazz = cls.getSuperclass();
52                 p("super class:           %s",
53                                 (clazz == null ? "null" : clazz.getName()));
54                 
55                 clazz = cls.getDeclaringClass();
56                 p("declaring class:       %s",
57                                 (clazz == null ? "null" : clazz.getName()));
58                 
59                 clazz = cls.getEnclosingClass();
60                 p("enclosing class:       %s",
61                                 (clazz == null ? "null" : clazz.getName()));
62                 
63                 constr = cls.getEnclosingConstructor();
64                 p("enclosing constructor: %s",
65                                 (constr == null ? "null" :
66                                         constr.getDeclaringClass().getName() +
67                                         "." + constr.getName()));
68                 
69                 method = cls.getEnclosingMethod();
70                 p("enclosing method:      %s",
71                                 (method == null ? "null" :
72                                         method.getDeclaringClass().getName() +
73                                         "." + method.getName()));
74                 
75                 p();
76         }
77         
78         public static void main(String[] args) {
79                 class ALocalClass {
80                         class AMemberClass {
81                         }
82                         
83                         public ALocalClass() {
84                                 class AnotherLocalClass {
85                                 }
86
87                                 printClassInfo(
88                                                 "test a member class",
89                                                 AMemberClass.class);
90                                 
91                                 printClassInfo(
92                                                 "test a anonymous class derived from a member class",
93                                                 new AMemberClass() {}.getClass());
94                                 
95                                 printClassInfo(
96                                                 "test a local class (local to a constructor)",
97                                                 AnotherLocalClass.class);
98                                 
99                                 printClassInfo(
100                                                 "test a anonymous class derived from a local " +
101                                                 "class (local to a constructor)",
102                                                 new AnotherLocalClass() {}.getClass());
103                         }
104                 }
105                 
106                 printClassInfo(
107                                 "test a normal class",
108                                 MinimalClassReflection.class);
109                 
110                 printClassInfo(
111                                 "test a local class (local to a method)",
112                                 ALocalClass.class);
113                 
114                 printClassInfo(
115                                 "test a anonymous class",
116                                 new Object() {}.getClass());
117                 
118                 printClassInfo(
119                                 "test a anonymous class derived from a local class" +
120                                 " (local to a method)",
121                                 new ALocalClass() {}.getClass());
122                 
123                 new ALocalClass();
124         }
125         
126         public static void p(String fmt, Object... args) {
127                 System.out.printf(fmt + "\n", args);
128         }
129         
130         public static <T> void p(T o) {
131                 System.out.println(o);
132         }
133         
134         public static void p() {
135                 System.out.println();
136         }
137 }