summaryrefslogtreecommitdiffstats
path: root/package.json
diff options
context:
space:
mode:
Diffstat (limited to 'package.json')
-rw-r--r--package.json2
1 files changed, 1 insertions, 1 deletions
diff --git a/package.json b/package.json
index 02c10e8..f0300ce 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@vaadin/vaadin-core",
- "version": "18.0.0-alpha1",
+ "version": "18.0.0-beta1",
"description": "Vaadin components is an evolving set of free, open sourced custom HTML elements for building mobile and desktop web applications in modern browsers.",
"author": "Vaadin Ltd",
"license": "Apache-2.0",
id='n151' href='#n151'>151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
package sample.evolve;

import javassist.*;

/**
 * Evolution provides a set of methods for instrumenting bytecodes.
 * 
 * For class evolution, updatable class A is renamed to B. Then an abstract
 * class named A is produced as the super class of B. If the original class A
 * has a public method m(), then the abstract class A has an abstract method
 * m().
 * 
 * abstract class A abstract m() _makeInstance() | class A --------> class B m()
 * m()
 * 
 * Also, all the other classes are translated so that "new A(i)" in the methods
 * is replaced with "_makeInstance(i)". This makes it possible to change the
 * behavior of the instantiation of the class A.
 */
public class Evolution implements Translator {
    public final static String handlerMethod = "_makeInstance";

    public final static String latestVersionField = VersionManager.latestVersionField;

    public final static String versionManagerMethod = "initialVersion";

    private static CtMethod trapMethod;

    private static final int initialVersion = 0;

    private ClassPool pool;

    private String updatableClassName = null;

    private CtClass updatableClass = null;

    public void start(ClassPool _pool) throws NotFoundException {
        pool = _pool;

        // Get the definition of Sample.make() and store it into trapMethod
        // for later use.
        trapMethod = _pool.getMethod("sample.evolve.Sample", "make");
    }

    public void onLoad(ClassPool _pool, String classname)
            throws NotFoundException, CannotCompileException {
        onLoadUpdatable(classname);

        /*
         * Replaces all the occurrences of the new operator with a call to
         * _makeInstance().
         */
        CtClass clazz = _pool.get(classname);
        CtClass absClass = updatableClass;
        CodeConverter converter = new CodeConverter();
        converter.replaceNew(absClass, absClass, handlerMethod);
        clazz.instrument(converter);
    }

    private void onLoadUpdatable(String classname) throws NotFoundException,
            CannotCompileException {
        // if the class is a concrete class,
        // classname is <updatableClassName>$$<version>.

        int i = classname.lastIndexOf("$$");
        if (i <= 0)
            return;

        String orgname = classname.substring(0, i);
        if (!orgname.equals(updatableClassName))
            return;

        int version;
        try {
            version = Integer.parseInt(classname.substring(i + 2));
        }
        catch (NumberFormatException e) {
            throw new NotFoundException(classname, e);
        }

        CtClass clazz = pool.getAndRename(orgname, classname);
        makeConcreteClass(clazz, updatableClass, version);
    }

    /*
     * Register an updatable class.
     */
    public void makeUpdatable(String classname) throws NotFoundException,
            CannotCompileException {
        if (pool == null)
            throw new RuntimeException(
                    "Evolution has not been linked to ClassPool.");

        CtClass c = pool.get(classname);
        updatableClassName = classname;
        updatableClass = makeAbstractClass(c);
    }

    /**
     * Produces an abstract class.
     */
    protected CtClass makeAbstractClass(CtClass clazz)
            throws CannotCompileException, NotFoundException {
        int i;

        CtClass absClass = pool.makeClass(clazz.getName());
        absClass.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
        absClass.setSuperclass(clazz.getSuperclass());
        absClass.setInterfaces(clazz.getInterfaces());

        // absClass.inheritAllConstructors();

        CtField fld = new CtField(pool.get("java.lang.Class"),
                latestVersionField, absClass);
        fld.setModifiers(Modifier.PUBLIC | Modifier.STATIC);

        CtField.Initializer finit = CtField.Initializer.byCall(pool
                .get("sample.evolve.VersionManager"), versionManagerMethod,
                new String[] { clazz.getName() });
        absClass.addField(fld, finit);

        CtField[] fs = clazz.getDeclaredFields();
        for (i = 0; i < fs.length; ++i) {
            CtField f = fs[i];
            if (Modifier.isPublic(f.getModifiers()))
                absClass.addField(new CtField(f.getType(), f.getName(),
                        absClass));
        }

        CtConstructor[] cs = clazz.getDeclaredConstructors();
        for (i = 0; i < cs.length; ++i) {
            CtConstructor c = cs[i];
            int mod = c.getModifiers();
            if (Modifier.isPublic(mod)) {
                CtMethod wm = CtNewMethod.wrapped(absClass, handlerMethod, c
                        .getParameterTypes(), c.getExceptionTypes(),
                        trapMethod, null, absClass);
                wm.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
                absClass.addMethod(wm);
            }
        }

        CtMethod[] ms = clazz.getDeclaredMethods();
        for (i = 0; i < ms.length; ++i) {
            CtMethod m = ms[i];
            int mod = m.getModifiers();
            if (Modifier.isPublic(mod))
                if (Modifier.isStatic(mod))
                    throw new CannotCompileException(
                            "static methods are not supported.");
                else {
                    CtMethod m2 = CtNewMethod.abstractMethod(m.getReturnType(),
                            m.getName(), m.getParameterTypes(), m
                                    .getExceptionTypes(), absClass);
                    absClass.addMethod(m2);
                }
        }

        return absClass;
    }

    /**
     * Modifies the given class file so that it is a subclass of the abstract
     * class produced by makeAbstractClass().
     * 
     * Note: the naming convention must be consistent with
     * VersionManager.update().
     */
    protected void makeConcreteClass(CtClass clazz, CtClass abstractClass,
            int version) throws CannotCompileException, NotFoundException {
        int i;
        clazz.setSuperclass(abstractClass);
        CodeConverter converter = new CodeConverter();
        CtField[] fs = clazz.getDeclaredFields();
        for (i = 0; i < fs.length; ++i) {
            CtField f = fs[i];
            if (Modifier.isPublic(f.getModifiers()))
                converter.redirectFieldAccess(f, abstractClass, f.getName());
        }

        CtConstructor[] cs = clazz.getDeclaredConstructors();
        for (i = 0; i < cs.length; ++i)
            cs[i].instrument(converter);

        CtMethod[] ms = clazz.getDeclaredMethods();
        for (i = 0; i < ms.length; ++i)
            ms[i].instrument(converter);
    }
}