Advertisement
retrodaredevil

ReflectedObject java class

May 24th, 2017
1,536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. package com.yoshiplex.util;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Modifier;
  6.  
  7. public class ReflectedObject { // class made by retrodaredevil. Feel free to use in your plugins without permission.
  8.  
  9.     private Object o;
  10.    
  11.     public ReflectedObject(Object o){
  12.         this.o = o;
  13.     }
  14.     public Object toObject(){
  15.         return o;
  16.     }
  17.     public Class<?> getObjectClass(){
  18.         return o.getClass();
  19.     }
  20.     public String getName(){
  21.         return o.getClass().getTypeName();
  22.     }
  23.     public String getFullName(){
  24.         return o.getClass().getName();
  25.     }
  26.     public String getPackageName(){
  27.         return o.getClass().getPackage().getName();
  28.     }
  29.     public void setField(String field, Object o){
  30.         Field f = null;
  31.         try{
  32.             f = this.getField(field);
  33.         } catch(NoSuchFieldException e){
  34.             e.printStackTrace();
  35.         } catch(SecurityException e){
  36.             e.printStackTrace();
  37.         }
  38.         if(f == null){
  39.             return;
  40.         }
  41.         f.setAccessible(true);
  42.         try {
  43.             f.set(this.o, o);
  44.         } catch (IllegalArgumentException | IllegalAccessException e) {
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.     public ReflectedObject get(String field){
  49.         Field f = null;
  50.         try{
  51.             f = this.getField(field);
  52.         } catch(NoSuchFieldException e){
  53.             return null;
  54.         } catch(SecurityException e){
  55.             e.printStackTrace();
  56.         }
  57.         f.setAccessible(true);
  58.         try {
  59.             return new ReflectedObject(f.get(this.o));
  60.         } catch (IllegalArgumentException | IllegalAccessException e) {
  61.             e.printStackTrace();
  62.         }
  63.         return null;
  64.     }
  65.     public boolean hasField(String field){
  66.         try{
  67.             this.getField(field);
  68.         }catch(NoSuchFieldException | SecurityException e){
  69.             return false;
  70.         }
  71.         return true;
  72.     }
  73.     private Field getField(String field) throws NoSuchFieldException, SecurityException{
  74.         return o.getClass().getDeclaredField(field);
  75.     }
  76.    
  77.     public Method getMethod(String name, boolean ignoreCase){
  78.         return new Method(name, o, ignoreCase, o.getClass());
  79.     }
  80.     public Method getMethod(String name){
  81.         return this.getMethod(name, false);
  82.     }
  83.    
  84.    
  85.    
  86.     public static class Method{
  87.        
  88.         private String name;
  89.         private Object instance;
  90.         private boolean ignoreCase;
  91.         private Class<?> c;
  92.        
  93.         private Method(String name, Object instance, boolean ignoreCase, Class<?> c){
  94.             this.name = name;
  95.             this.instance = instance;
  96.             this.ignoreCase = ignoreCase;
  97.             this.c = c;
  98.         }
  99.         public boolean isValid(){
  100.             return this.getMethod() != null;
  101.         }
  102.         public ReflectedObject invoke(Object... variables){
  103.             java.lang.reflect.Method m = this.getMethod();
  104.             m.setAccessible(true);
  105.             try {
  106.                 return new ReflectedObject(m.invoke(instance, variables));
  107.             } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  108.                 return null;
  109.             }
  110.         }
  111.         public String getName(){
  112.             return name;
  113.         }
  114.         public Object getInstance(){
  115.             return instance;
  116.         }
  117.         private java.lang.reflect.Method getMethod(){
  118.             for(java.lang.reflect.Method m : c.getMethods()){
  119.                 if(this.ignoreCase){
  120.                     if(m.getName().equalsIgnoreCase(name)){
  121.                         return m;
  122.                     }
  123.                 } else {
  124.                     if(m.getName().equals(name)){
  125.                         return m;
  126.                     }
  127.                    
  128.                 }
  129.             }
  130.             return null;
  131.         }
  132.     }
  133.     public static void setStaticField(Class<?> c, String name, Object set){
  134.         Field f = null;
  135.         try{
  136.             f = c.getDeclaredField(name);
  137.         } catch(NoSuchFieldException e){
  138.             e.printStackTrace();
  139.         } catch(SecurityException e){
  140.             e.printStackTrace();
  141.         }
  142.         if(f == null){
  143.             throw new IllegalArgumentException("Error while getting the field '" + name + "'");
  144.         }
  145.         f.setAccessible(true);
  146.        
  147.         try{
  148.             Field modifiersField = Field.class.getDeclaredField("modifiers");
  149.             modifiersField.setAccessible(true);
  150.             modifiersField.setInt(f, modifiersField.getInt(f) - Modifier.FINAL);
  151.         } catch(SecurityException e){
  152.             System.out.println("A security manager may be preventing you from setting this field.");
  153.             e.printStackTrace();
  154.         } catch(IllegalAccessException | IllegalArgumentException | NoSuchFieldException e){
  155.             e.printStackTrace();
  156.         }
  157.        
  158.         try {
  159.             f.set(null, set);
  160.         } catch (IllegalArgumentException | IllegalAccessException e) {
  161.             e.printStackTrace();
  162.         }
  163.     }
  164.     public static Object getFromStaticField(Class<?> c, String name){
  165.         Field f = null;
  166.         try{
  167.             f = c.getDeclaredField(name);
  168.         } catch(NoSuchFieldException e){
  169.             e.printStackTrace();
  170.         } catch(SecurityException e){
  171.             e.printStackTrace();
  172.         }
  173.         if(f == null){
  174.             return null;
  175.         }
  176.         f.setAccessible(true);
  177.        
  178.         try{
  179.             Field modifiersField = Field.class.getDeclaredField("modifiers");
  180.             modifiersField.setAccessible(true);
  181.             modifiersField.setInt(f, modifiersField.getInt(f) - Modifier.FINAL);
  182.         } catch(SecurityException e){
  183.             System.out.println("A security manager may be preventing you from setting this field.");
  184.             e.printStackTrace();
  185.         } catch(IllegalAccessException | IllegalArgumentException | NoSuchFieldException e){
  186.             e.printStackTrace();
  187.         }
  188.        
  189.         try {
  190.             return f.get(null);
  191.         } catch (IllegalArgumentException | IllegalAccessException e) {
  192.             e.printStackTrace();
  193.         }
  194.         return null;
  195.     }
  196.  
  197.    
  198.    
  199.    
  200. }
  201.  
  202.  
  203. //  public static Method getMethodFromStaticField(Class<?> c, String name, boolean ignoreCase){
  204. //      return new Method("name", null, ignoreCase, c);
  205. //  }
  206. //  public static Method getMethodFromStaticField(Class<?> c, String name){
  207. //      return getMethodFromStaticField(c, name, false);
  208. //  }
  209. //  public static Class<?> getClassFromString(String pack, String name){
  210. //      return getClassFromString(pack + "." + name);
  211. //  }
  212. //  public static Class<?> getClassFromString(String whole){
  213. //      try {
  214. //          return Class.forName(whole);
  215. //      } catch (ClassNotFoundException e) {
  216. //          e.printStackTrace();
  217. //      }
  218. //      return null;
  219. //  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement