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());");
}
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());
}
}
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("{}");
}
}
}
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 */
}
}
}
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.");
}
Aggregations