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