package coreJava_5_13;
import java.lang.reflect.*;
import java.util.*;
public class ReflectionTest {
public static void main( String args[] ) {
Scanner in = new Scanner(System.in);
System.out.println("Enter class name (e.g. java.lang.Double):");
String name = in.next();
try {
Class cl = Class.forName(name);
Class supercl = cl.getSuperclass();
String modifiers = Modifier.toString(cl.getModifiers());
if( modifiers.length() > 0 ) System.out.print(modifiers + " ");
System.out.print("Class " + cl.getName() );
if( supercl != null && supercl != Object.class ) System.out.print( " extends " + supercl.getName() );
System.out.print("\n{\n");
printConstructors(cl);
System.out.println();
printMethods(cl);
System.out.println();
printFields(cl);
System.out.println("}");
}catch( Exception e ) {
e.printStackTrace();
}
System.exit(0);
}
public static void printConstructors( Class cl ) {
Constructor[] constructors = cl.getDeclaredConstructors();
for( Constructor c : constructors ) {
System.out.print(" ");
String name = c.getName();
String modifiers = Modifier.toString(c.getModifiers());
if( modifiers.length() > 0 ) System.out.print( modifiers + " " );
System.out.print(name+"(");
Class[] params = c.getParameterTypes();
for( int i = 0; i < params.length; i++ ) {
if( i > 0 ) System.out.print(", ");
System.out.print(params[i].getName());
}
System.out.println(");");
}
}
public static void printMethods( Class cl ) {
Method[] methods = cl.getDeclaredMethods();
for( Method m : methods ) {
System.out.print(" ");
String modifiers = Modifier.toString(m.getModifiers());
if( modifiers.length() > 0 ) System.out.print( modifiers + " " );
String ret = m.getReturnType().getName();
System.out.print(ret + " ");
System.out.print( m.getName() + "(");
Class[] params = m.getParameterTypes();
for( int i = 0; i < params.length; i++ ) {
if( i > 0 ) System.out.print(", ");
System.out.print(params[i].getName());
}
System.out.println(");");
}
}
public static void printFields( Class cl ) {
Field[] fields = cl.getDeclaredFields();
for( Field f : fields ) {
System.out.print(" ");
String modifiers = Modifier.toString(f.getModifiers());
if( modifiers.length() > 0 ) System.out.print(modifiers + " ");
Class type = f.getType();
System.out.println( type.getName() + " " + f.getName() + ";" );
}
}
}
package coreJava_5_13;
import java.util.*;
public class ObjectAnalyzerTest {
public static void main( String args[] ) {
ArrayList squares = new ArrayList<>();
for( int i = 1; i <= 5; i++ )
squares.add(i*i);
int[] arrInt = new int[5];
String[] arrStr = new String[3];
arrStr[0] = "I";
arrStr[1] = "love";
arrStr[2] = "u";
for( int i = 0; i < arrInt.length; i++ )
arrInt[i] = i + 1;
System.out.println( new ObjectAnalyzer().toString(squares) );
System.out.println( new ObjectAnalyzer().toString( arrInt ) );
System.out.println( new ObjectAnalyzer().toString( arrStr ) );
}
}
package coreJava_5_13;
import java.util.*;
import java.lang.reflect.*;
public class ObjectAnalyzer {
private ArrayList
package coreJava_5_13;
import java.lang.reflect.*;
import java.util.*;
public class CopyOfTest {
public static void main( String args[] ) {
int[] a = { 1, 2, 3 };
String[] b = { "I", "love", "u" };
a = (int[]) goodCopyOf( a, 10 );
b = ( String[] ) goodCopyOf( b, 10 );
System.out.println( Arrays.toString(a) );
System.out.println( Arrays.toString(b) );
//double[] c = { 1.0, 1.1, 1.2 };
//the following call will generate an exception
//b = (String[]) badCopyOf( b, 10 );
}
public static Object goodCopyOf( Object obj, int len ) {
Class cl = obj.getClass();
if( !cl.isArray() ) return null;
Class type = cl.getComponentType();
int length = Array.getLength(obj);
Object newArray = Array.newInstance(type, len);
System.arraycopy(obj, 0, newArray, 0, Math.min(length, len) );
return newArray;
}
public static Object[] badCopyOf( Object[] obj, int len ) {
Object[] newArray = new Object[len];
System.arraycopy(obj, 0, newArray, 0, Math.min(obj.length, len) );
return newArray;
}
}
Method m1 = Employee.class.getMethod("getName");
Method m2 = Employee.class.getMethod("raiseSalary", double.class);
package coreJava_5_13;
import java.lang.reflect.*;
public class MethodTableTest {
public static void main( String args[] ) {
try {
Method sqrt = Math.class.getMethod("sqrt", double.class);
Method square = MethodTableTest.class.getMethod("square", double.class);
printTable( 1, 10, 10, square );
printTable( 1, 10, 10, sqrt );
}catch( Exception e ) {
e.printStackTrace();
}
}
public static double square( double x ) {
return x*x;
}
public static void printTable( double from, double to, int n, Method f ) {
System.out.println( f );
double dx = ( to - from ) / ( n - 1 );
for( double x = from; x <= to; x += dx ) {
try {
double y = (Double) f.invoke(null, x);
System.out.printf("%10.4f | %10.4f%n", x, y );
}catch( Exception e ) {
e.printStackTrace();
}
}
}
}