Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package reflectionlab;
  6.  
  7. import java.io.EOFException;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.ObjectInputStream;
  11. import java.lang.reflect.Field;
  12.  
  13. /**
  14.  *
  15.  * @author theInterloper
  16.  */
  17. public class ReflectionLab {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
  23.         readObjects();  
  24.     }
  25.  
  26.     private static void readObjects() {
  27.         Object o = new Object();
  28.         boolean data = true;
  29.         try{
  30.             File file = new File("Objects2.dat");
  31.             if(!file.exists()){
  32.                 System.out.println("no file found");
  33.             }
  34.             else{
  35.                 FileInputStream FIS = new FileInputStream(file);
  36.                 ObjectInputStream OIS = new ObjectInputStream(FIS);
  37.                
  38.                 while(data){
  39.                     try{
  40.                         o = (Object) OIS.readObject();
  41.                         //System.out.println(o.getClass());
  42.                         //showClassInfo(o);
  43.                         getContents(o);
  44.                     }catch(EOFException end){
  45.                         data = false;
  46.                     }//catch
  47.                 }//while
  48.             }//else
  49.         }catch(Exception e){
  50.             System.out.println("Problem reading from file: " + e);
  51.         }
  52.     }
  53.  
  54.     public static void showClassInfo(Object o) throws IllegalArgumentException,IllegalAccessException {
  55.         Class c = o.getClass();
  56.         Field[] publicFields = c.getFields();
  57.         for(int i=0; i< publicFields.length; i++){
  58.             Painting painting = new Painting();
  59.             String fieldName = publicFields[i].getName();
  60.             Class typeClass = publicFields[i].getType();
  61.             String fieldType = typeClass.getName();
  62.             System.out.println("Name: " + fieldName + ",Type: " + fieldType);  
  63.         }
  64.        
  65.     }
  66.  
  67.     public static void getContents(Object o) throws IllegalArgumentException, IllegalAccessException {
  68.         Class c = o.getClass();
  69.         Painting painting = new Painting();
  70.         Field fieldlist[] = c.getDeclaredFields();
  71.        
  72.         for (int i = 0; i < fieldlist.length; i++) {
  73.                 Field fld = fieldlist[i];
  74.                 fld.setAccessible(true);
  75.                  System.out.println(fld.get(o).toString());
  76.                  
  77.                
  78.             }
  79.        
  80.     }
  81.  
  82.    
  83. }
Add Comment
Please, Sign In to add comment