Search in sources :

Example 51 with CtMethod

use of javassist.CtMethod 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 52 with CtMethod

use of javassist.CtMethod in project powermock by powermock.

the class ConcreteClassGenerator method createConcreteSubClass.

public Class<?> createConcreteSubClass(Class<?> clazz) {
    if (clazz == null) {
        throw new IllegalArgumentException("clazz cannot be null");
    }
    if (!java.lang.reflect.Modifier.isAbstract(clazz.getModifiers())) {
        throw new IllegalArgumentException("clazz must be abstract");
    }
    ClassPool classpool = ClassPool.getDefault();
    final String originalClassName = clazz.getName();
    final CtClass originalClassAsCtClass;
    final CtClass newClass = classpool.makeClass(generateClassName(clazz));
    try {
        newClass.setSuperclass(classpool.get(clazz.getName()));
    } catch (Exception e1) {
        throw new RuntimeException(e1);
    }
    try {
        originalClassAsCtClass = classpool.get(originalClassName);
        CtMethod[] declaredMethods = originalClassAsCtClass.getDeclaredMethods();
        for (CtMethod ctMethod : declaredMethods) {
            if (Modifier.isAbstract(ctMethod.getModifiers())) {
                final String code = getReturnCode(ctMethod.getReturnType());
                CtNewMethod.make(ctMethod.getReturnType(), ctMethod.getName(), ctMethod.getParameterTypes(), ctMethod.getExceptionTypes(), code, newClass);
            }
        }
        if (!hasInheritableConstructor(originalClassAsCtClass)) {
            return null;
        }
        return newClass.toClass(this.getClass().getClassLoader(), this.getClass().getProtectionDomain());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod)

Example 53 with CtMethod

use of javassist.CtMethod in project ratpack by ratpack.

the class ClosureBackedHandler method describeTo.

@Override
public void describeTo(StringBuilder stringBuilder) {
    ClosureUtil.SourceInfo sourceInfo = ClosureUtil.getSourceInfo(invoker.getClosure());
    if (sourceInfo == null) {
        ClassPool pool = ClassPool.getDefault();
        try {
            CtClass ctClass = pool.get(invoker.getClosure().getClass().getName());
            CtMethod ctMethod = ctClass.getDeclaredMethod("doCall");
            int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
            ClassFile classFile = ctClass.getClassFile();
            String sourceFile = classFile.getSourceFile();
            if (lineNumber != -1 && sourceFile != null) {
                stringBuilder.append("closure at line ").append(lineNumber).append(" of ").append(sourceFile);
            } else {
                stringBuilder.append("closure ").append(invoker.getClosure().getClass().getName());
            }
        } catch (NotFoundException e) {
            stringBuilder.append(invoker.getClosure().getClass().getName());
        }
    } else {
        stringBuilder.append("closure at line ").append(sourceInfo.getLineNumber()).append(" of ").append(sourceInfo.getUri());
    }
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) ClosureUtil(ratpack.groovy.internal.ClosureUtil) CtMethod(javassist.CtMethod)

Example 54 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class AfterBurnerTest method testCheckIfMethodIsInvoked_whenItIsInvoked.

@Test
public void testCheckIfMethodIsInvoked_whenItIsInvoked() throws Exception {
    // GIVEN
    target.addMethod(CtNewMethod.make("public void bar() { }", target));
    CtMethod withinMethod = CtNewMethod.make("public void foo() { bar(); }", target);
    target.addMethod(withinMethod);
    // WHEN
    boolean isInvoked = afterBurner.checkIfMethodIsInvoked(withinMethod, "bar");
    // THEN
    assertTrue(isInvoked);
}
Also used : CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 55 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class AfterBurnerTest method testCheckIfMethodIsInvoked_whenItIsNotInvoked.

@Test
public void testCheckIfMethodIsInvoked_whenItIsNotInvoked() throws Exception {
    // GIVEN
    target.addMethod(CtNewMethod.make("public void bar() { }", target));
    CtMethod withinMethod = CtNewMethod.make("public void foo() { }", target);
    target.addMethod(withinMethod);
    // WHEN
    boolean isInvoked = afterBurner.checkIfMethodIsInvoked(withinMethod, "bar");
    // THEN
    assertFalse(isInvoked);
}
Also used : CtMethod(javassist.CtMethod) Test(org.junit.Test)

Aggregations

CtMethod (javassist.CtMethod)76 CtClass (javassist.CtClass)46 NotFoundException (javassist.NotFoundException)23 CannotCompileException (javassist.CannotCompileException)20 ClassPool (javassist.ClassPool)18 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2