Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package reflectionlab;
- import java.io.EOFException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.ObjectInputStream;
- import java.lang.reflect.Field;
- /**
- *
- * @author theInterloper
- */
- public class ReflectionLab {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
- readObjects();
- }
- private static void readObjects() {
- Object o = new Object();
- boolean data = true;
- try{
- File file = new File("Objects2.dat");
- if(!file.exists()){
- System.out.println("no file found");
- }
- else{
- FileInputStream FIS = new FileInputStream(file);
- ObjectInputStream OIS = new ObjectInputStream(FIS);
- while(data){
- try{
- o = (Object) OIS.readObject();
- //System.out.println(o.getClass());
- //showClassInfo(o);
- getContents(o);
- }catch(EOFException end){
- data = false;
- }//catch
- }//while
- }//else
- }catch(Exception e){
- System.out.println("Problem reading from file: " + e);
- }
- }
- public static void showClassInfo(Object o) throws IllegalArgumentException,IllegalAccessException {
- Class c = o.getClass();
- Field[] publicFields = c.getFields();
- for(int i=0; i< publicFields.length; i++){
- Painting painting = new Painting();
- String fieldName = publicFields[i].getName();
- Class typeClass = publicFields[i].getType();
- String fieldType = typeClass.getName();
- System.out.println("Name: " + fieldName + ",Type: " + fieldType);
- }
- }
- public static void getContents(Object o) throws IllegalArgumentException, IllegalAccessException {
- Class c = o.getClass();
- Painting painting = new Painting();
- Field fieldlist[] = c.getDeclaredFields();
- for (int i = 0; i < fieldlist.length; i++) {
- Field fld = fieldlist[i];
- fld.setAccessible(true);
- System.out.println(fld.get(o).toString());
- }
- }
- }
Add Comment
Please, Sign In to add comment