All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.tools.reflect.Loader Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later,
 * or the Apache License Version 2.0.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

package org.testifyproject.testifyproject.tools.reflect;

import org.testifyproject.testifyproject.CannotCompileException;
import org.testifyproject.testifyproject.NotFoundException;
import org.testifyproject.testifyproject.ClassPool;

/**
 * A class loader for reflection.
 *
 * 

To run a program, say MyApp, * including a reflective class, * you must write a start-up program as follows: * *

     * public class Main {
     *   public static void main(String[] args) throws Throwable {
     *     org.testifyproject.testifyproject.tools.reflect.Loader cl
     *         = (org.testifyproject.testifyproject.tools.reflect.Loader)Main.class.getClassLoader();
     *     cl.makeReflective("Person", "MyMetaobject",
     *                       "org.testifyproject.testifyproject.tools.reflect.ClassMetaobject");
     *     cl.run("MyApp", args);
     *   }
     * }
     * 
* *

Then run this program as follows: * *

    % java org.testifyproject.testifyproject.tools.reflect.Loader Main arg1, ...
* *

This org.testifyproject.testifyprojectmand runs Main.main() with arg1, ... * and Main.main() runs MyApp.main() with * arg1, ... * The Person class is modified * to be a reflective class. Method calls on a Person * object are intercepted by an instance of MyMetaobject. * *

Also, you can run MyApp in a slightly different way: * *

     * public class Main2 {
     *   public static void main(String[] args) throws Throwable {
     *     org.testifyproject.testifyproject.tools.reflect.Loader cl = new org.testifyproject.testifyproject.tools.reflect.Loader();
     *     cl.makeReflective("Person", "MyMetaobject",
     *                       "org.testifyproject.testifyproject.tools.reflect.ClassMetaobject");
     *     cl.run("MyApp", args);
     *   }
     * }
     * 
* *

This program is run as follows: * *

    % java Main2 arg1, ...
* *

The difference from the former one is that the class Main * is loaded by org.testifyproject.testifyproject.tools.reflect.Loader whereas the class * Main2 is not. Thus, Main belongs * to the same name space (security domain) as MyApp * whereas Main2 does not; Main2 belongs * to the same name space as org.testifyproject.testifyproject.tools.reflect.Loader. * For more org.testifyproject.testifyprojecttails, * see the notes in the manual page of org.testifyproject.testifyproject.Loader. * *

The class Main2 is equivalent to this class: * *

     * public class Main3 {
     *   public static void main(String[] args) throws Throwable {
     *     Reflection reflection = new Reflection();
     *     org.testifyproject.testifyproject.Loader cl
     *         = new org.testifyproject.testifyproject.Loader(ClassPool.getDefault(reflection));
     *     reflection.makeReflective("Person", "MyMetaobject",
     *                               "org.testifyproject.testifyproject.tools.reflect.ClassMetaobject");
     *     cl.run("MyApp", args);
     *   }
     * }
     * 
* *

Note: * *

org.testifyproject.testifyproject.tools.reflect.Loader does not make a class reflective * if that class is in a java.* or * javax.* pacakge because of the specifications * on the class loading algorithm of Java. The JVM does not allow to * load such a system class with a user class loader. * *

To avoid this limitation, those classes should be statically * modified with org.testifyproject.testifyproject.tools.reflect.Compiler and the original * class files should be replaced. * * @see org.testifyproject.testifyproject.tools.reflect.Reflection * @see org.testifyproject.testifyproject.tools.reflect.Compiler * @see org.testifyproject.testifyproject.Loader */ public class Loader extends org.testifyproject.testifyproject.Loader { protected Reflection reflection; /** * Loads a class with an instance of Loader * and calls main() in that class. * * @param args org.testifyproject.testifyprojectmand line parameters. *

    * args[0] is the class name to be loaded. *
    args[1..n] are parameters passed * to the target main(). *
*/ public static void main(String[] args) throws Throwable { Loader cl = new Loader(); cl.run(args); } /** * Constructs a new class loader. */ public Loader() throws CannotCompileException, NotFoundException { super(); org.testifyproject.testifyprojectlegateLoadingOf("org.testifyproject.testifyproject.tools.reflect.Loader"); reflection = new Reflection(); ClassPool pool = ClassPool.getDefault(); addTranslator(pool, reflection); } /** * Produces a reflective class. * If the super class is also made reflective, it must be done * before the sub class. * * @param clazz the reflective class. * @param metaobject the class of metaobjects. * It must be a subclass of * Metaobject. * @param metaclass the class of the class metaobject. * It must be a subclass of * ClassMetaobject. * @return false if the class is already reflective. * * @see org.testifyproject.testifyproject.tools.reflect.Metaobject * @see org.testifyproject.testifyproject.tools.reflect.ClassMetaobject */ public boolean makeReflective(String clazz, String metaobject, String metaclass) throws CannotCompileException, NotFoundException { return reflection.makeReflective(clazz, metaobject, metaclass); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy