1 /* tests/regression/MinimalClassReflection.java - checks most some of the
2 reflective methods onto java.lang.Class
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,
9 This file is part of CACAO.
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.
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.
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
26 Contact: cacao@cacaojvm.org
28 Authors: Mathias Panzenböck
34 import java.lang.reflect.Method;
35 import java.lang.reflect.Constructor;
37 public class MinimalClassReflection {
38 public static void printClassInfo(String header, Class<?> cls) {
40 Constructor<?> constr;
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());
51 clazz = cls.getSuperclass();
53 (clazz == null ? "null" : clazz.getName()));
55 clazz = cls.getDeclaringClass();
56 p("declaring class: %s",
57 (clazz == null ? "null" : clazz.getName()));
59 clazz = cls.getEnclosingClass();
60 p("enclosing class: %s",
61 (clazz == null ? "null" : clazz.getName()));
63 constr = cls.getEnclosingConstructor();
64 p("enclosing constructor: %s",
65 (constr == null ? "null" :
66 constr.getDeclaringClass().getName() +
67 "." + constr.getName()));
69 method = cls.getEnclosingMethod();
70 p("enclosing method: %s",
71 (method == null ? "null" :
72 method.getDeclaringClass().getName() +
73 "." + method.getName()));
78 public static void main(String[] args) {
83 public ALocalClass() {
84 class AnotherLocalClass {
88 "test a member class",
92 "test a anonymous class derived from a member class",
93 new AMemberClass() {}.getClass());
96 "test a local class (local to a constructor)",
97 AnotherLocalClass.class);
100 "test a anonymous class derived from a local " +
101 "class (local to a constructor)",
102 new AnotherLocalClass() {}.getClass());
107 "test a normal class",
108 MinimalClassReflection.class);
111 "test a local class (local to a method)",
115 "test a anonymous class",
116 new Object() {}.getClass());
119 "test a anonymous class derived from a local class" +
120 " (local to a method)",
121 new ALocalClass() {}.getClass());
126 public static void p(String fmt, Object... args) {
127 System.out.printf(fmt + "\n", args);
130 public static <T> void p(T o) {
131 System.out.println(o);
134 public static void p() {
135 System.out.println();