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
33 * Test following methos of java.lang.Class for proper function:
37 * getEnclosingConstructor()
38 * getEnclosingMethod()
40 * I wrote this because getSuperclass() sometimes made problems.
42 import java.lang.reflect.Method;
43 import java.lang.reflect.Constructor;
45 public class MinimalClassReflection {
46 public static void printClassInfo(String header, Class<?> cls) {
48 Constructor<?> constr;
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());
59 clazz = cls.getSuperclass();
61 (clazz == null ? "null" : clazz.getName()));
63 clazz = cls.getDeclaringClass();
64 p("declaring class: %s",
65 (clazz == null ? "null" : clazz.getName()));
67 clazz = cls.getEnclosingClass();
68 p("enclosing class: %s",
69 (clazz == null ? "null" : clazz.getName()));
71 constr = cls.getEnclosingConstructor();
72 p("enclosing constructor: %s",
73 (constr == null ? "null" :
74 constr.getDeclaringClass().getName() +
75 "." + constr.getName()));
77 method = cls.getEnclosingMethod();
78 p("enclosing method: %s",
79 (method == null ? "null" :
80 method.getDeclaringClass().getName() +
81 "." + method.getName()));
86 public static void main(String[] args) {
91 public ALocalClass() {
92 class AnotherLocalClass {
96 "test a member class",
100 "test a anonymous class derived from a member class",
101 new AMemberClass() {}.getClass());
104 "test a local class (local to a constructor)",
105 AnotherLocalClass.class);
108 "test a anonymous class derived from a local " +
109 "class (local to a constructor)",
110 new AnotherLocalClass() {}.getClass());
115 "test a normal class",
116 MinimalClassReflection.class);
119 "test a local class (local to a method)",
123 "test a anonymous class",
124 new Object() {}.getClass());
127 "test a anonymous class derived from a local class" +
128 " (local to a method)",
129 new ALocalClass() {}.getClass());
134 public static void p(String fmt, Object... args) {
135 System.out.printf(fmt + "\n", args);
138 public static <T> void p(T o) {
139 System.out.println(o);
142 public static void p() {
143 System.out.println();