Skip to content

Instantly share code, notes, and snippets.

@melsener
Created May 31, 2017 19:03
Show Gist options
  • Save melsener/ed652c79b89a00c27faeb8f51bb9366e to your computer and use it in GitHub Desktop.
Save melsener/ed652c79b89a00c27faeb8f51bb9366e to your computer and use it in GitHub Desktop.
Playing with reflection.
import java.io.IOException;
import java.lang.reflect.*;
/**
* Created by melisa on 31/05/2017.
*/
class Person {
private String privateName;
public String publicName;
public Person friend;
public Person(String privateName , String publicName)
{
this.privateName = privateName;
this.publicName = publicName;
}
public void beFriend(Person friend)
{
this.friend = friend;
}
private void unfriend()
{
if(friend!=null)
friend = null;
}
public void exceptional() throws Exception
{
throw new IOException();
}
public String toString()
{
return new String(publicName + privateName);
}
}
public class PersonTest
{
public static void main(String [] args)
{
Class c1 = Person.class;
System.out.println("Class :" + c1);
System.out.println("Class Name :" + c1.getName());
System.out.println("Class Type :" + c1.getTypeName());
System.out.println("Super Class :" + c1.getSuperclass());
Method [] methods = c1.getMethods();
System.out.println("GetMethods");
for(Method m : methods)
{ System.out.println(m);
System.out.println(m.getName());
System.out.println(m.getModifiers());
System.out.println(m.getDeclaringClass());
System.out.println("----");
}
System.out.println();
Method [] declaredMethods = c1.getDeclaredMethods();
System.out.println("GetDeclaredMethods");
for(Method m : declaredMethods)
{ System.out.println(m);
System.out.println(m.getName());
System.out.println(m.getModifiers());
System.out.println(m.getDeclaringClass());
System.out.println("----");
}
System.out.println();
Field [] fields = c1.getFields();
System.out.println("GetFields");
for(Field m : fields)
{ System.out.println(m);
System.out.println(m.getModifiers());
System.out.println(m.getType());
}
System.out.println();
Field [] declaredFields = c1.getDeclaredFields();
System.out.println("GetDeclaredFields");
for(Field m : declaredFields)
System.out.println(m);
System.out.println();
Person[] personList = new Person[2];
personList[0] = new Person("Christ", "Jesus");
personList[1] = new Person("St.", "Joseph");
for(Person p : personList)
System.out.println(p);
System.out.println();
Person[] personList2= (Person[]) Array.newInstance(c1,5);
personList2[0] = new Person("Bach", "Sebastian");
personList2[1] = new Person("Mozart", "Amadeus");
personList2[2] = new Person("Bethooven", "Ludwig Van");
personList2[3] = new Person("Brahms", "Johannes");
Array.set(personList2,4,new Person("Vivaldi","Antonio"));
for(Person p : personList2)
System.out.println(p);
System.out.println();
Class[] paramTypes = new Class[]{String.class,String.class};
try {
//Throws Exception If the constructor isn't public.
Constructor cons = c1.getConstructor(paramTypes);
Object[] arguments = new Object[] {"Myung", "John"};
Person p = (Person) cons.newInstance(arguments);
System.out.println("Created Object With Reflection: " + p );
Method m = p.getClass().getDeclaredMethod("exceptional");
System.out.println(m.getName());
m.invoke(p);
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
catch (InvocationTargetException e)
{
System.out.println(e.getCause());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment