Search in sources :

Example 11 with CtConstructor

use of javassist.CtConstructor in project gwt-test-utils by gwt-test-utils.

the class GwtPatcher method init.

@InitMethod
static void init(CtClass ctClass) throws CannotCompileException {
    CtConstructor staticInitializer = ctClass.makeClassInitializer();
    staticInitializer.insertAfter("setBridge(" + GwtTestGWTBridge.class.getName() + ".get());");
}
Also used : GwtTestGWTBridge(com.googlecode.gwt.test.internal.handlers.GwtTestGWTBridge) CtConstructor(javassist.CtConstructor) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Example 12 with CtConstructor

use of javassist.CtConstructor in project atlas by alibaba.

the class CodeInjectByJavassist method inject.

public static synchronized byte[] inject(ClassPool pool, String className, InjectParam injectParam) throws Exception {
    try {
        CtClass cc = pool.get(className);
        cc.defrost();
        if (className.equalsIgnoreCase("android.taobao.atlas.framework.FrameworkProperties") || className.equalsIgnoreCase("android.taobao.atlas.version.VersionKernal")) {
            if (StringUtils.isNotBlank(injectParam.version)) {
                CtClass ctClass = pool.get("java.lang.String");
                CtField ctField = new CtField(ctClass, "version", cc);
                ctField.setModifiers(Modifier.PRIVATE);
                CtMethod ctMethod = CtNewMethod.getter("getVersion", ctField);
                ctMethod.setModifiers(Modifier.PUBLIC);
                CtField.Initializer initializer = CtField.Initializer.constant(injectParam.version);
                cc.addField(ctField, initializer);
                cc.addMethod(ctMethod);
                logger.info("[android.taobao.atlas.framework.FrameworkProperties] inject version " + injectParam.version);
            }
            addField(pool, cc, "bundleInfo", injectParam.bundleInfo);
            addField(pool, cc, "autoStartBundles", injectParam.autoStartBundles);
            addField(pool, cc, "preLaunch", injectParam.preLaunch);
            addField(pool, cc, "group", injectParam.group);
            addField(pool, cc, "outApp", String.valueOf(injectParam.outApp));
        }
        ClazzInjecter clazzInjecter = sInjecterMap.get(className);
        if (null != clazzInjecter) {
            Map<String, String> stringMap = clazzInjecter.getInjectFields();
            if (!stringMap.isEmpty()) {
                for (String key : stringMap.keySet()) {
                    addField(pool, cc, key, stringMap.get(key));
                }
            }
        }
        if (!injectParam.removePreverify) {
            Collection<String> refClasses = cc.getRefClasses();
            if (refClasses.contains("com.taobao.verify.Verifier")) {
                return cc.toBytecode();
            }
            boolean flag = false;
            if (className.equalsIgnoreCase("com.ut.mini.crashhandler.IUTCrashCaughtListner")) {
                flag = true;
            }
            if (cc.isInterface()) {
                final CtClass defClass = pool.get(Class.class.getName());
                CtField defField = new CtField(defClass, "_inject_field__", cc);
                defField.setModifiers(Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL);
                cc.addField(defField, CtField.Initializer.byExpr("Boolean.TRUE.booleanValue()?java.lang.String.class:com.taobao.verify.Verifier.class;"));
            } else {
                CtConstructor[] ctConstructors = cc.getDeclaredConstructors();
                if (null != ctConstructors && ctConstructors.length > 0) {
                    CtConstructor ctConstructor = ctConstructors[0];
                    ctConstructor.insertBeforeBody("if(Boolean.FALSE.booleanValue()){java.lang.String.valueOf(com.taobao.verify.Verifier.class);}");
                } else {
                    final CtClass defClass = pool.get(Class.class.getName());
                    CtField defField = new CtField(defClass, "_inject_field__", cc);
                    defField.setModifiers(Modifier.STATIC);
                    cc.addField(defField, CtField.Initializer.byExpr("Boolean.TRUE.booleanValue()?java.lang.String.class:com.taobao.verify.Verifier.class;"));
                }
            }
        }
        return cc.toBytecode();
    } catch (Throwable e) {
        throw new Exception("[InjectError]:" + className + ",reason:" + e.getMessage());
    }
}
Also used : NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 13 with CtConstructor

use of javassist.CtConstructor in project powermock by powermock.

the class AbstractMainMockTransformer method suppressStaticInitializerIfRequested.

protected void suppressStaticInitializerIfRequested(final CtClass clazz, final String name) throws CannotCompileException {
    if (strategy == CLASSLOADER) {
        if (MockGateway.staticConstructorCall(name) != MockGateway.PROCEED) {
            CtConstructor classInitializer = clazz.makeClassInitializer();
            classInitializer.setBody("{}");
        }
    }
}
Also used : CtConstructor(javassist.CtConstructor)

Example 14 with CtConstructor

use of javassist.CtConstructor in project powermock by powermock.

the class TestClassTransformer method restoreOriginalConstructorsAccesses.

private void restoreOriginalConstructorsAccesses(CtClass clazz) throws Exception {
    Class<?> originalClass = testClass.getName().equals(clazz.getName()) ? testClass : Class.forName(clazz.getName(), true, testClass.getClassLoader());
    for (final CtConstructor ctConstr : clazz.getConstructors()) {
        int ctModifiers = ctConstr.getModifiers();
        if (!Modifier.isPublic(ctModifiers)) {
            /* Probably a defer-constructor */
            continue;
        }
        int desiredAccessModifiers = originalClass.getDeclaredConstructor(asOriginalClassParams(ctConstr.getParameterTypes())).getModifiers();
        if (Modifier.isPrivate(desiredAccessModifiers)) {
            ctConstr.setModifiers(Modifier.setPrivate(ctModifiers));
        } else if (Modifier.isProtected(desiredAccessModifiers)) {
            ctConstr.setModifiers(Modifier.setProtected(ctModifiers));
        } else if (!Modifier.isPublic(desiredAccessModifiers)) {
            ctConstr.setModifiers(Modifier.setPackage(ctModifiers));
        } else {
        /* ctConstr remains public */
        }
    }
}
Also used : CtConstructor(javassist.CtConstructor)

Example 15 with CtConstructor

use of javassist.CtConstructor in project drill by apache.

the class GuavaPatcher method patchStopwatch.

/**
   * Makes Guava stopwatch look like the old version for compatibility with hbase-server (for test purposes).
   */
private static void patchStopwatch() throws Exception {
    ClassPool cp = ClassPool.getDefault();
    CtClass cc = cp.get("com.google.common.base.Stopwatch");
    // Expose the constructor for Stopwatch for old libraries who use the pattern new Stopwatch().start().
    for (CtConstructor c : cc.getConstructors()) {
        if (!Modifier.isStatic(c.getModifiers())) {
            c.setModifiers(Modifier.PUBLIC);
        }
    }
    // Add back the Stopwatch.elapsedMillis() method for old consumers.
    CtMethod newmethod = CtNewMethod.make("public long elapsedMillis() { return elapsed(java.util.concurrent.TimeUnit.MILLISECONDS); }", cc);
    cc.addMethod(newmethod);
    // Load the modified class instead of the original.
    cc.toClass();
    logger.info("Google's Stopwatch patched for old HBase Guava version.");
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod) CtConstructor(javassist.CtConstructor)

Aggregations

CtConstructor (javassist.CtConstructor)15 CtClass (javassist.CtClass)8 ClassPool (javassist.ClassPool)6 CtMethod (javassist.CtMethod)5 IOException (java.io.IOException)4 CannotCompileException (javassist.CannotCompileException)4 CtField (javassist.CtField)4 NotFoundException (javassist.NotFoundException)3 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)2 AfterBurnerImpossibleException (com.github.stephanenicolas.afterburner.exception.AfterBurnerImpossibleException)1 GwtTestGWTBridge (com.googlecode.gwt.test.internal.handlers.GwtTestGWTBridge)1 FileNotFoundException (java.io.FileNotFoundException)1 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ZipException (java.util.zip.ZipException)1 CtNewMethod (javassist.CtNewMethod)1 DMLScript (org.apache.sysml.api.DMLScript)1 Script (org.apache.sysml.api.mlcontext.Script)1 DataIdentifier (org.apache.sysml.parser.DataIdentifier)1