Biblioteca Java - Rev 32
Subversion Repositories:
(root)/Courses and labs samples/ISP/Exemple_ISP_Cluj_2015/ExempleReflexie/src/exemple/reflexie/clase/InspectClass.java @ 34
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exemple.reflexie.clase;
import exemple.reflexie.app.Car;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
*
* @author Mihai Hulea mihai.hulea@aut.utcluj.ro
*/
public class InspectClass {
public static void describeInstance(Object object) {
Class<?> clazz = object.getClass();
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
Field[] fields = clazz.getDeclaredFields();
Method[] methods = clazz.getDeclaredMethods();
System.out.println("Description for class: " + clazz.getName());
System.out.println();
System.out.println("Summary");
System.out.println("-----------------------------------------");
System.out.println("Constructors: " + (constructors.length));
System.out.println("Fields: " + (fields.length));
System.out.println("Methods: " + (methods.length));
System.out.println();
System.out.println();
System.out.println("Details");
System.out.println("-----------------------------------------");
if (constructors.length > 0) {
System.out.println();
System.out.println("Constructors:");
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
}
if (fields.length > 0) {
System.out.println();
System.out.println("Fields:");
for (Field field : fields) {
System.out.println(field);
}
}
if (methods.length > 0) {
System.out.println();
System.out.println("Methods:");
for (Method method : methods) {
System.out.println(method);
}
}
}
static boolean[] checkModifiers(int modifiers){
boolean[] b = new boolean[11];
b[0] = Modifier.isAbstract(modifiers);
b[1] = Modifier.isFinal(modifiers);
b[2] = Modifier.isInterface(modifiers);
b[3] = Modifier.isNative(modifiers);
b[4] = Modifier.isPrivate(modifiers);
b[5] = Modifier.isProtected(modifiers);
b[6] = Modifier.isPublic(modifiers);
b[6] = Modifier.isStatic(modifiers);
b[7] = Modifier.isStrict(modifiers);
b[8] = Modifier.isSynchronized(modifiers);
b[9] = Modifier.isTransient(modifiers);
b[10] = Modifier.isVolatile(modifiers);
return b;
}
public static void main(String[] args) throws Exception{
Car myCar = new Car();
// Class c = Car.class;
// describeInstance(myCar);
/* System.out.println("Class name:"+c.getName());
Class s = c.getSuperclass();
System.out.println("Superclass name:"+s.getName());
*/
System.out.println("My car is:"+myCar);
Method privateMethod = Car.class.
getDeclaredMethod("setSpeedLimit", Integer.class);
privateMethod.setAccessible(true);
privateMethod.invoke(myCar, 300);
System.out.println("My car is:"+myCar);
Field privateField = Car.class.
getDeclaredField("maxSpeed");
privateField.setAccessible(true);
Integer fieldValue = (Integer) privateField.get(myCar);
System.out.println("Max car speed is = " + fieldValue);
Car o = (Car)Car.class.getConstructor().newInstance();
describeInstance(o);
o.accelerate();
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exemple.reflexie.clase;
import exemple.reflexie.app.Car;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
*
* @author Mihai Hulea mihai.hulea@aut.utcluj.ro
*/
public class InspectClass {
public static void describeInstance(Object object) {
Class<?> clazz = object.getClass();
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
Field[] fields = clazz.getDeclaredFields();
Method[] methods = clazz.getDeclaredMethods();
System.out.println("Description for class: " + clazz.getName());
System.out.println();
System.out.println("Summary");
System.out.println("-----------------------------------------");
System.out.println("Constructors: " + (constructors.length));
System.out.println("Fields: " + (fields.length));
System.out.println("Methods: " + (methods.length));
System.out.println();
System.out.println();
System.out.println("Details");
System.out.println("-----------------------------------------");
if (constructors.length > 0) {
System.out.println();
System.out.println("Constructors:");
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
}
if (fields.length > 0) {
System.out.println();
System.out.println("Fields:");
for (Field field : fields) {
System.out.println(field);
}
}
if (methods.length > 0) {
System.out.println();
System.out.println("Methods:");
for (Method method : methods) {
System.out.println(method);
}
}
}
static boolean[] checkModifiers(int modifiers){
boolean[] b = new boolean[11];
b[0] = Modifier.isAbstract(modifiers);
b[1] = Modifier.isFinal(modifiers);
b[2] = Modifier.isInterface(modifiers);
b[3] = Modifier.isNative(modifiers);
b[4] = Modifier.isPrivate(modifiers);
b[5] = Modifier.isProtected(modifiers);
b[6] = Modifier.isPublic(modifiers);
b[6] = Modifier.isStatic(modifiers);
b[7] = Modifier.isStrict(modifiers);
b[8] = Modifier.isSynchronized(modifiers);
b[9] = Modifier.isTransient(modifiers);
b[10] = Modifier.isVolatile(modifiers);
return b;
}
public static void main(String[] args) throws Exception{
Car myCar = new Car();
// Class c = Car.class;
// describeInstance(myCar);
/* System.out.println("Class name:"+c.getName());
Class s = c.getSuperclass();
System.out.println("Superclass name:"+s.getName());
*/
System.out.println("My car is:"+myCar);
Method privateMethod = Car.class.
getDeclaredMethod("setSpeedLimit", Integer.class);
privateMethod.setAccessible(true);
privateMethod.invoke(myCar, 300);
System.out.println("My car is:"+myCar);
Field privateField = Car.class.
getDeclaredField("maxSpeed");
privateField.setAccessible(true);
Integer fieldValue = (Integer) privateField.get(myCar);
System.out.println("Max car speed is = " + fieldValue);
Car o = (Car)Car.class.getConstructor().newInstance();
describeInstance(o);
o.accelerate();
}
}