/** Writes the class file to the classpath and returns a class object */ public Class<?> saveClass() { ClassFile classFile = ctClass.getClassFile(); try { if (!extraClassPath.exists()) { extraClassPath.mkdirs(); } FactoryHelper.writeFile(classFile, extraClassPath.getPath()); ClassLoader loader = getClass().getClassLoader(); Class<?> cls = FactoryHelper.toClass(classFile, loader); return cls; // load class } catch (Exception e) { throw new RuntimeException(e); } }
/** * Initializes the object. */ public void start(ClassPool pool) throws NotFoundException { classPool = pool; final String msg = "javassist.tools.reflect.Sample is not found or broken."; try { CtClass c = classPool.get("javassist.tools.reflect.Sample"); rebuildClassFile(c.getClassFile()); trapMethod = c.getDeclaredMethod("trap"); trapStaticMethod = c.getDeclaredMethod("trapStatic"); trapRead = c.getDeclaredMethod("trapRead"); trapWrite = c.getDeclaredMethod("trapWrite"); readParam = new CtClass[] { classPool.get("java.lang.Object") }; } catch (NotFoundException e) { throw new RuntimeException(msg); } catch (BadBytecode e) { throw new RuntimeException(msg); } }
private CtClass createPatchesInfoClass() { try { CtClass ctPatchesInfoImpl = classPool.makeClass(Config.patchPackageName + ".PatchesInfoImpl"); ctPatchesInfoImpl.getClassFile().setMajorVersion(ClassFile.JAVA_7); ctPatchesInfoImpl.setInterfaces(new CtClass[]{classPool.get("com.meituan.robust.PatchesInfo")}); StringBuilder methodBody = new StringBuilder(); methodBody.append("public java.util.List getPatchedClassesInfo() {"); methodBody.append(" java.util.List patchedClassesInfos = new java.util.ArrayList();"); for (int i = 0; i < Config.modifiedClassNameList.size(); i++) { if (Constants.OBSCURE) { methodBody.append("com.meituan.robust.PatchedClassInfo patchedClass" + i + " = new com.meituan.robust.PatchedClassInfo(\"" + ReadMapping.getInstance().getClassMappingOrDefault(Config.modifiedClassNameList.get(i)).getValueName() + "\",\"" + NameManger.getInstance().getPatchControlName(Config.modifiedClassNameList.get(i).substring(Config.modifiedClassNameList.get(i).lastIndexOf('.') + 1)) + "\");"); } else { methodBody.append("com.meituan.robust.PatchedClassInfo patchedClass" + i + " = new com.meituan.robust.PatchedClassInfo(\"" + Config.modifiedClassNameList.get(i) + "\",\"" + NameManger.getInstance().getPatchControlName(Config.modifiedClassNameList.get(i).substring(Config.modifiedClassNameList.get(i).lastIndexOf('.') + 1)) + "\");"); } methodBody.append("patchedClassesInfos.add(patchedClass" + i + ");"); } methodBody.append(Constants.ROBUST_UTILS_FULL_NAME + ".isThrowable=!" + Config.catchReflectException + ";"); methodBody.append("return patchedClassesInfos;\n" + " }"); CtMethod m = make(methodBody.toString(), ctPatchesInfoImpl); ctPatchesInfoImpl.addMethod(m); return ctPatchesInfoImpl; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
/** * * @param iface The interface the new class should implement * @param oldlassName The class to be extended * @param newClassName the name of the new class to be created */ public ClassCustomizer(Class<?> iface, String oldlassName, String newClassName) { try { ClassPool pool = ClassPool.getDefault(); ctClass = pool.makeClass(newClassName); classFile = ctClass.getClassFile(); classFile.setSuperclass(oldlassName); classFile.setName(newClassName); classFile.setInterfaces(new String[] { iface.getName() }); } catch (Exception e) { throw new RuntimeException(e); } }
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
/** * Initializes the object. */ @Override public void start(ClassPool pool) throws NotFoundException { classPool = pool; final String msg = "javassist.tools.reflect.Sample is not found or broken."; try { CtClass c = classPool.get("javassist.tools.reflect.Sample"); rebuildClassFile(c.getClassFile()); trapMethod = c.getDeclaredMethod("trap"); trapStaticMethod = c.getDeclaredMethod("trapStatic"); trapRead = c.getDeclaredMethod("trapRead"); trapWrite = c.getDeclaredMethod("trapWrite"); readParam = new CtClass[] { classPool.get("java.lang.Object") }; } catch (NotFoundException e) { throw new RuntimeException(msg); } catch (BadBytecode e) { throw new RuntimeException(msg); } }
@SneakyThrows public Proxy<I> addField(String code, Class<? extends java.lang.annotation.Annotation> annotation, Map<String, Object> annotationProperties) { return handleException(() -> { CtField ctField = CtField.make(code, ctClass); if (null != annotation) { ConstPool constPool = ctClass.getClassFile().getConstPool(); AnnotationsAttribute attributeInfo = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); Annotation ann = new javassist.bytecode.annotation.Annotation(annotation.getName(), constPool); if (null != annotationProperties) { annotationProperties.forEach((key, value) -> { MemberValue memberValue = createMemberValue(value, constPool); if (memberValue != null) { ann.addMemberValue(key, memberValue); } }); } attributeInfo.addAnnotation(ann); ctField.getFieldInfo().addAttribute(attributeInfo); } ctClass.addField(ctField); }); }
/** Adds a new field of type double to the customized class */ public void addDoubleField(String fieldName) { // FIXME: this should support default values but does not ClassFile classFile = ctClass.getClassFile(); ConstPool constPool = classFile.getConstPool(); try { // add field FieldInfo fieldInfo = new FieldInfo(constPool, fieldName, "D"); classFile.addField(fieldInfo); CtConstructor ctor = CtNewConstructor.defaultConstructor(ctClass); ctClass.addConstructor(ctor); addDoubleSetter(classFile, fieldName); addDoubleGetter(classFile, fieldName); } catch (Exception e) { throw new RuntimeException(e); } }
ClassFile ccFile = cl.getClassFile(); ConstPool constpool = ccFile.getConstPool();
ClassFile cf = clazz.getClassFile(); // turn on the modified flag. try { ConstPool cp = cf.getConstPool();
return (MethodInfo)res; // already exists. ClassFile cf = clazz.getClassFile(); // turn on the modified flag. String accName = findAccessorName(cf); try {
return accName; // already exists. ClassFile cf = clazz.getClassFile(); // turn on the modified flag. accName = findAccessorName(cf); try {
return (MethodInfo)res; // already exists. ClassFile cf = clazz.getClassFile(); // turn on the modified flag. String accName = findAccessorName(cf); try {
CannotCompileException thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos;
thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos;
thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos;
thisClass.getClassFile(); // to call checkModify().
thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos;
thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos;