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());
}
}
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);
}
}
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());
}
}
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);
}
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);
}
Aggregations