77 lines
2.1 KiB
Java
77 lines
2.1 KiB
Java
// typeinfo/toys/ToyTest.java
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
// Testing class Class
|
|
// {java typeinfo.toys.ToyTest}
|
|
package typeinfo.toys;
|
|
|
|
interface HasBatteries {}
|
|
interface Waterproof {}
|
|
interface Shoots {}
|
|
|
|
class Toy {
|
|
// Comment out the following no-arg
|
|
// constructor to see NoSuchMethodError
|
|
Toy() {}
|
|
Toy(int i) {}
|
|
}
|
|
|
|
class FancyToy extends Toy
|
|
implements HasBatteries, Waterproof, Shoots {
|
|
FancyToy() { super(1); }
|
|
}
|
|
|
|
public class ToyTest {
|
|
static void printInfo(Class cc) {
|
|
System.out.println("Class name: " + cc.getName() +
|
|
" is interface? [" + cc.isInterface() + "]");
|
|
System.out.println(
|
|
"Simple name: " + cc.getSimpleName());
|
|
System.out.println(
|
|
"Canonical name : " + cc.getCanonicalName());
|
|
}
|
|
public static void main(String[] args) {
|
|
Class c = null;
|
|
try {
|
|
c = Class.forName("typeinfo.toys.FancyToy");
|
|
} catch(ClassNotFoundException e) {
|
|
System.out.println("Can't find FancyToy");
|
|
System.exit(1);
|
|
}
|
|
printInfo(c);
|
|
for(Class face : c.getInterfaces())
|
|
printInfo(face);
|
|
Class up = c.getSuperclass();
|
|
Object obj = null;
|
|
try {
|
|
// Requires no-arg constructor:
|
|
obj = up.newInstance();
|
|
} catch(InstantiationException e) {
|
|
System.out.println("Cannot instantiate");
|
|
System.exit(1);
|
|
} catch(IllegalAccessException e) {
|
|
System.out.println("Cannot access");
|
|
System.exit(1);
|
|
}
|
|
printInfo(obj.getClass());
|
|
}
|
|
}
|
|
/* Output:
|
|
Class name: typeinfo.toys.FancyToy is interface? [false]
|
|
Simple name: FancyToy
|
|
Canonical name : typeinfo.toys.FancyToy
|
|
Class name: typeinfo.toys.HasBatteries is interface? [true]
|
|
Simple name: HasBatteries
|
|
Canonical name : typeinfo.toys.HasBatteries
|
|
Class name: typeinfo.toys.Waterproof is interface? [true]
|
|
Simple name: Waterproof
|
|
Canonical name : typeinfo.toys.Waterproof
|
|
Class name: typeinfo.toys.Shoots is interface? [true]
|
|
Simple name: Shoots
|
|
Canonical name : typeinfo.toys.Shoots
|
|
Class name: typeinfo.toys.Toy is interface? [false]
|
|
Simple name: Toy
|
|
Canonical name : typeinfo.toys.Toy
|
|
*/
|