Package alt.jiapi.reflect

Examples of alt.jiapi.reflect.JiapiClass


    static Signature mainSig = new Signature("([Ljava/lang/String;)V");

    static Signature printlnSig = new Signature("(Ljava/lang/String;)V");

    static byte[] jiapiHelloWorld() throws MethodExistsException {
        JiapiClass c = JiapiClass.createClass("HelloWorld");

        // No API to set SourceFile!

        JiapiMethod method = c.addMethod(Modifier.PUBLIC, "<init>", emptySig);
        InstructionList il = method.getInstructionList();
        InstructionFactory iFactory = il.getInstructionFactory();
        il.add(iFactory.aload(0));
        il.add(iFactory.invoke(0, "java/lang/Object", "<init>", emptySig));
        il.add(iFactory.returnMethod(method));

        method = c.addMethod(Modifier.PUBLIC | Modifier.STATIC, "main", mainSig);
        il = method.getInstructionList();
        iFactory = il.getInstructionFactory();
        il.add(iFactory.getField(Modifier.STATIC,
                "java/lang/System",
                "out",
                "Ljava/io/PrintStream;"));
        il.add(iFactory.pushConstant("Hello world!"));
        il.add(iFactory.invoke(0, "java/io/PrintStream", "println", printlnSig));
        il.add(iFactory.returnMethod(method));

        return c.getByteCode();
    }
View Full Code Here


     *
     * @param il InstructionList to forward
     */
    protected void forward(InstructionList il) {
        if (child != null) {
            JiapiClass jc = getCurrentClass();
            child.setCurrentClass(jc);

            InstrumentorChain pre = child.preInstrument();
            if (pre != null) {
//                    if (preInstrumentations.get(jc.getName()) == null) {
View Full Code Here

      byte[] tempBuffer = new byte[classFileBuffer.length];
      System.arraycopy(classFileBuffer, 0, tempBuffer, 0,
           classFileBuffer.length);

      try {
    JiapiClass jc = JiapiClass.parseClass(tempBuffer);

    if (transformer.transform(jc)) {
        return jc.getByteCode();
    }
      }
      catch(Exception e) {
    e.printStackTrace();
      }
View Full Code Here

     * Instrument a JiapiClass.
     *
     * @param clazz A Class to be instrumented
     */
    public void instrument(InstructionList il) {
        JiapiClass clazz = getCurrentClass();

        // If the class is interface, then the modifiers must be public.
        // Interface can have only public methods.
        if (Modifier.isInterface(clazz.getModifiers())) {
            if (!Modifier.isPublic(modifiers)) {
                log.info("Cannot create non public method " +
                          methodName + " to interface " + clazz.getName());

                // NOTE! Throw an exception if mode is FORWARD_NEW (or BOTH)
                forward(il);
                return;
            }
        }

        log.info("Creating method " + clazz.getName() + "." + methodName);

        JiapiMethod method = null;
        try {
            Signature signature = new Signature(returnType, parameterNames);
            method = clazz.addMethod((short)modifiers, methodName, signature);
        }
        catch (MethodExistsException mee) {
            log.info("method " + clazz.getName() + "." + methodName +
                     " already exists");

            method = mee.getMethod();
        }

View Full Code Here

        Runtime.setFieldValue(fieldName, value);

        try {
            Loader l = new Loader();
            JiapiClass jc = l.loadClass("alt.jiapi.Runtime");
//             Class []params = new Class[] { String.class };
//             getFieldValue = Runtime.class.getMethod("getFieldValue", params);
            getFieldValue = jc.getDeclaredMethod("getFieldValue", new String[] {"java.lang.String"});
        }
        catch (Exception e) {
            // not possible, but just in case
            e.printStackTrace();
            throw new RuntimeException(e.toString());
View Full Code Here

        }

        InstructionFactory factory =
            il.getDeclaringMethod().getInstructionFactory();

        JiapiClass jc = getCurrentClass();

        JiapiField field = null;
        try {
            field = jc.getDeclaredField(fieldName);
        }
        catch (NoSuchFieldException e) {
            throw new JiapiRuntimeException("No such field: " + e.getMessage());
        }
View Full Code Here

    }

    public HelloWorldBuilder() throws Exception {
        // Load the target class:
        Loader loader = new Loader();
        JiapiClass clazz = loader.loadClass("samples.reflect.hello1.Test");
        System.out.println(clazz);

        // Add an empty method for a clazz. The method signature must
        // match the method from HelloWorld interface, the only difference
        // is that we want to implement the method now so it can't be abstract.
        Signature signature = new Signature("void", new String[] {} );
        JiapiMethod method = clazz.addMethod(Modifier.PUBLIC, "helloWorld",
                                             signature);
       
        // Then create the method body. The body will make a call to
        // System.out.println and then just return.
        InstructionList il = method.getInstructionList();
        InstructionFactory iFactory = method.getInstructionFactory();

        JiapiMethod println = clazz.getDeclaredMethod("println", new String[]
                                                      {"java.lang.Object"});

//         il.add(iFactory.getField(loader.loadClass("java.lang.System").getField("out")));
//         JiapiMethod println = loader.loadClass(System.out.getClass().getName()).getMethod("println", new String[] { "java.lang.String" } );

        il.add(iFactory.pushConstant("hello world!"));

        il.add(iFactory.invoke(println));
        il.add(iFactory.returnMethod(method));

        // Finalize the modified class and dump it to the file:

        clazz.dump(new FileOutputStream("Test.class"));
    }
View Full Code Here

    public CopyInstrumentor(Method sourceMethod, Loader loader)
            throws ClassNotFoundException, NoSuchMethodException {

        // Load sourceMethod's instructions.
        //        Loader loader = getContext().getLoader();
        JiapiClass clazz = null;
        try {
            clazz=loader.loadClass(sourceMethod.getDeclaringClass().getName());
        }
        catch(java.io.IOException ioe) {
            ioe.printStackTrace();
            throw new ClassNotFoundException(sourceMethod.getDeclaringClass().getName());
        }

        Class[] parameterTypes = sourceMethod.getParameterTypes();
        String[] parameterTypeNames = new String[parameterTypes.length];
        for (int i = 0; i < parameterTypes.length; i++) {
            parameterTypeNames[i] = parameterTypes[i].getName();
        }

        JiapiMethod method = clazz.getMethod(sourceMethod.getName(),
                                             parameterTypeNames);
        source = method.getInstructionList();
    }
View Full Code Here

     * Instrument a JiapiClass.
     *
     * @param clazz A Class to be instrumented
     */
    public void instrument(InstructionList il) {
        JiapiClass clazz = getCurrentClass();
        log.info("Adding field '" + modifiers + " " + type + " " + name +
                 "' to " + clazz.getName());

        // If the class is interface, then the modifiers must be public,
        // static and final.
        if (Modifier.isInterface(clazz.getModifiers())) {
            if (!(Modifier.isPublic(modifiers) &&
                  Modifier.isStatic(modifiers) &&
                  Modifier.isFinal(modifiers))) {

                log.info("Cannot add '" + modifiers + " " + type + " " + name +
                         "' to " + clazz.getName());

                forward(il);
                return;
            }
        }

        try {
            clazz.addField(modifiers, type, name);
        }
        catch (FieldExistsException fee) {
            log.info("field " + fee.getField() + " already exists in a class "+
                     clazz.getName());
        }

        forward(il);
    }
View Full Code Here

    }

    public void instrument(JiapiMethod jm) {
        InstructionList il = jm.getInstructionList();
        InstructionFactory factory = il.getInstructionFactory();
        JiapiClass fep = getEventProducer();
        JiapiMethod fieldGet = null;
        JiapiMethod fieldSet = null;
        JiapiField jiapiField = getEventProducerField();

        try {
            fieldGet =
                fep.getDeclaredMethod("fieldGet",
                                      new String[] { "java.lang.Object",
                                                     "java.lang.String" });
            fieldSet =
                fep.getDeclaredMethod("fieldSet",
                                      new String[] { "java.lang.Object",
                                                     "java.lang.String" });
        }
        catch(Exception e) {
            System.out.println("ERROR: " + e);
View Full Code Here

TOP

Related Classes of alt.jiapi.reflect.JiapiClass

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.