Java Reference
In-Depth Information
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new RuntimeException(e.getMessage());
}
}
public String toString() {
return "Person: id=" + this.id + ", name=" + this.name;
}
}
Listing 3-4 illustrates how to get the description of a class. It lists the class access modifiers, its name, its
superclass name, and all interfaces implemented by it.
Listing 3-4. Reflecting on a Class
// ClassReflection.java
package com.jdojo.reflection;
import java.lang.reflect.Modifier;
import java.lang.reflect.TypeVariable;
public class ClassReflection {
public static void main(String[] args) {
// Print the class declaration for the Person class
String classDesciption = getClassDescription(Person.class);
System.out.println(classDesciption);
}
public static String getClassDescription(Class c) {
StringBuilder classDesc = new StringBuilder();
// Prepare the modifiers and construct keyword (class, enum, interface etc.)
int modifierBits = 0;
String keyword = "";
// Add keyword @interface, interface or class
if (c.isPrimitive()) {
// We do not want to add anything
}
 
Search WWH ::




Custom Search