public Class<?> toClass(ClassLoader loader, ProtectionDomain pd) { if (mCtc != null) { mCtc.detach(); CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass); if (mClassName == null) { mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? ClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) { mCtc.setSuperclass(ctcs); mCtc.addInterface(mPool.get(DC.class.getName())); // add dynamic class tag. if (mInterfaces != null) { for (String cl : mInterfaces) { mCtc.addInterface(mPool.get(cl)); mCtc.addField(CtField.make(code, mCtc)); for (String code : mMethods) { if (code.charAt(0) == ':') { mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null)); } else { mCtc.addMethod(CtNewMethod.make(code, mCtc)); mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc)); mCtc.addConstructor(CtNewConstructor
int modify = orgclass.getModifiers(); if (Modifier.isAbstract(modify) || Modifier.isNative(modify) || !Modifier.isPublic(modify)) throw new CannotCompileException(orgclass.getName() + " must be public, non-native, and non-abstract."); CtClass proxy = classPool.makeClass(orgclass.getName(), orgclass.getSuperclass()); proxy.setInterfaces(interfacesForProxy); = new CtField(classPool.get("javassist.tools.rmi.ObjectImporter"), fieldImporter, proxy); f.setModifiers(Modifier.PRIVATE); proxy.addField(f, CtField.Initializer.byParameter(0)); f = new CtField(CtClass.intType, fieldObjectId, proxy); f.setModifiers(Modifier.PRIVATE); proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f)); proxy.addConstructor(CtNewConstructor.defaultConstructor(proxy)); CtConstructor cons = CtNewConstructor.skeleton(proxyConstructorParamTypes, null, proxy); proxy.addConstructor(cons);
private CtClass createControlClass(CtClass modifiedClass) throws Exception { CtClass patchClass = classPool.get(NameManger.getInstance().getPatchName(modifiedClass.getName())); patchClass.defrost(); CtClass controlClass = classPool.getAndRename(Constants.PATCH_TEMPLATE_FULL_NAME, NameManger.getInstance().getPatchControlName(modifiedClass.getSimpleName())); StringBuilder getRealParameterMethodBody = new StringBuilder(); getRealParameterMethodBody.append("public Object getRealParameter(Object parameter) {"); getRealParameterMethodBody.append("if(parameter instanceof " + modifiedClass.getName() + "){"); getRealParameterMethodBody. append("return new " + patchClass.getName() + "(parameter);"); getRealParameterMethodBody.append("}"); getRealParameterMethodBody.append("return parameter;}"); controlClass.addMethod(CtMethod.make(getRealParameterMethodBody.toString(), controlClass)); controlClass.getDeclaredMethod("accessDispatch").insertBefore(getAccessDispatchMethodBody(patchClass, modifiedClass.getName())); controlClass.getDeclaredMethod("isSupport").insertBefore(getIsSupportMethodBody(patchClass, modifiedClass.getName())); controlClass.defrost(); return controlClass; }
/** * Removes this <code>CtClass</code> object from the * <code>ClassPool</code>. * After this method is called, any method cannot be called on the * removed <code>CtClass</code> object. * * <p>If <code>get()</code> in <code>ClassPool</code> is called * with the name of the removed method, * the <code>ClassPool</code> will read the class file again * and constructs another <code>CtClass</code> object representing * the same class. */ public void detach() { ClassPool cp = getClassPool(); CtClass obj = cp.removeCached(getName()); if (obj != this) cp.cacheCtClass(getName(), obj, false); }
private CtField(String typeDesc, String name, CtClass clazz) throws CannotCompileException { super(clazz); ClassFile cf = clazz.getClassFile2(); if (cf == null) throw new CannotCompileException("bad declaring class: " + clazz.getName()); fieldInfo = new FieldInfo(cf.getConstPool(), name, typeDesc); }
/** * Adds a default constructor to the super classes. */ private void modifySuperclass(CtClass orgclass) throws CannotCompileException, NotFoundException { CtClass superclazz; for (;; orgclass = superclazz) { superclazz = orgclass.getSuperclass(); if (superclazz == null) break; try { superclazz.getDeclaredConstructor(null); break; // the constructor with no arguments is found. } catch (NotFoundException e) { } superclazz.addConstructor( CtNewConstructor.defaultConstructor(superclazz)); } } }
public Class<?> toClass() { if (mCtc != null) mCtc.detach(); long id = CLASS_NAME_COUNTER.getAndIncrement(); try { CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass); if (mClassName == null) mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? TccClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) mCtc.setSuperclass(ctcs); mCtc.addInterface(mPool.get(DC.class.getName())); // add dynamic class tag. if (mInterfaces != null) for (String cl : mInterfaces) mCtc.addInterface(mPool.get(cl)); if (mFields != null) for (String code : mFields) mCtc.addField(CtField.make(code, mCtc)); if (mMethods != null) { for (String code : mMethods) { if (code.charAt(0) == ':') mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null)); else { ConstPool constpool = mCtc.getClassFile().getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation("org.mengyun.tcctransaction.api.Compensable", constpool); mCtc.addMethod(ctMethod); mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc));
int i = name.lastIndexOf('.'); String className = i < 0 ? name : name.substring(i + 1); ClassPool pool = new ClassPool(true); pool.appendClassPath(new LoaderClassPath(ClassHelper.getCallerClassLoader(getClass()))); Matcher matcher = IMPORT_PATTERN.matcher(source); List<String> importPackages = new ArrayList<String>(); if (pkg.endsWith(".*")) { String pkgName = pkg.substring(0, pkg.length() - 2); pool.importPackage(pkgName); importPackages.add(pkgName); } else { ifaceClass = ClassUtils.forName(packages, iface).getName(); cls.addInterface(pool.get(ifaceClass)); if (method.length() > 0) { if (method.startsWith(className)) { cls.addConstructor(CtNewConstructor.make("public " + method, cls)); } else if (FIELD_PATTERN.matcher(method).matches()) { cls.addField(CtField.make("private " + method, cls)); } else { cls.addMethod(CtNewMethod.make("public " + method, cls)); return cls.toClass(ClassHelper.getCallerClassLoader(getClass()), JavassistCompiler.class.getProtectionDomain());
public static void enableStreamingSupport() throws Exception { CtClass ctClass = pool.makeClass("com.jsoniter.IterImpl"); ctClass.setSuperclass(pool.get(IterImplForStreaming.class.getName())); ctClass.toClass(); } }
} catch (ClassNotFoundException 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))); Class<?> type = parameterTypes[i]; Annotation[] annotations = parameterAnnotations[i]; AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(Constraint.class)) { javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation( classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName())); Method[] members = annotation.annotationType().getMethods(); for (Method member : members) { CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName)); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
ClassPool cp = ClassPool.getDefault(); cp.insertClassPath(new ClassClassPath(ConfigVariationsTestSuiteBuilder.class)); CtClass cl = cp.makeClass(pkg + "." + clsName); cl.setSuperclass(cp.get(cls.getName())); CtMethod mtd = CtNewMethod.make("public static void init() { " + "injectTestsConfiguration(" + ConfigVariationsTestSuiteBuilder.class.getName() ClassFile ccFile = cl.getClassFile(); ConstPool constpool = ccFile.getConstPool(); mtd.getMethodInfo().addAttribute(attr); cl.addMethod(mtd); return cl.toClass();
private static TemplateRenderer tryToCompile(String source, Map<String, String> expressions, Map<String, String> constants, Class<?> modelType) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException { ClassPool cp = ClassPool.getDefault(); CtClass sup = cp.get(Object.class.getCanonicalName()); CtClass cls = cp.makeClass("RapidoidTemplate" + ID_GEN.incrementAndGet(), sup); cls.addInterface(cp.get(TemplateRenderer.class.getCanonicalName())); cls.addConstructor(CtNewConstructor.defaultConstructor(cls)); addExpressions(expressions, cls); addConstants(constants, cls); CtClass[] params = {cp.get(RenderCtx.class.getCanonicalName())}; CtClass clsVoid = cp.get(void.class.getCanonicalName()); cls.addMethod(CtNewMethod.make(Modifier.PUBLIC, clsVoid, "render", params, new CtClass[0], source, cls)); return (TemplateRenderer) cls.toClass().newInstance(); }
throws CannotCompileException, NotFoundException if (clazz.getAttribute("Reflective") != null) return false; // this is already reflective. else clazz.setAttribute("Reflective", new byte[0]); CtClass mlevel = classPool.get("javassist.tools.reflect.Metalevel"); boolean addMeta = !clazz.subtypeOf(mlevel); if (addMeta) clazz.addInterface(mlevel); f = new CtField(classPool.get("javassist.tools.reflect.Metaobject"), metaobjectField, clazz); f.setModifiers(Modifier.PROTECTED); clazz.addField(f, CtField.Initializer.byNewWithParams(metaobject)); clazz.addMethod(CtNewMethod.getter(metaobjectGetter, f)); clazz.addMethod(CtNewMethod.setter(metaobjectSetter, f)); f = new CtField(classPool.get("javassist.tools.reflect.ClassMetaobject"), classobjectField, clazz); f.setModifiers(Modifier.PRIVATE | Modifier.STATIC); clazz.addField(f, CtField.Initializer.byNew(metaclass, new String[] { clazz.getName() })); clazz.addMethod(CtNewMethod.getter(classobjectAccessor, f)); return true;
ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bout); ClassFile classFile = clazz.getClassFile(); out.writeUTF(javaName); CtMethod[] methods = clazz.getDeclaredMethods(); int classMods = clazz.getModifiers(); if ((classMods & Modifier.INTERFACE) != 0) if (methods.length > 0) String[] interfaces = classFile.getInterfaces(); for (int i = 0; i < interfaces.length; i++) interfaces[i] = javaName(interfaces[i]); CtField[] fields = clazz.getDeclaredFields(); Arrays.sort(fields, new Comparator() { public int compare(Object o1, Object o2) { CtConstructor[] constructors = clazz.getDeclaredConstructors(); Arrays.sort(constructors, new Comparator() { public int compare(Object o1, Object o2) { throw new CannotCompileException(e); throw new CannotCompileException(e);
public static Decoder gen(String cacheKey, String source) throws Exception { Decoder decoder; CtClass ctClass = pool.makeClass(cacheKey); ctClass.setInterfaces(new CtClass[]{pool.get(Decoder.class.getName())}); CtMethod staticMethod = CtNewMethod.make(source, ctClass); ctClass.addMethod(staticMethod); CtMethod interfaceMethod = CtNewMethod.make("" + "public Object decode(com.jsoniter.JsonIterator iter) {" + "return decode_(iter);" + "}", ctClass); ctClass.addMethod(interfaceMethod); decoder = (Decoder) ctClass.toClass().newInstance(); return decoder; }
for (CtMethod ctMethod : invokeSuperMethodList) { if ((ctMethod.getName().replaceAll("\\.", "/") + ctMethod.getSignature().subSequence(0, ctMethod.getSignature().indexOf(")") + 1)).equals(getMethodSignureInSmaliLine(line))) { result = line.replace(Constants.SMALI_INVOKE_VIRTUAL_COMMAND, Constants.SMALI_INVOKE_SUPER_COMMAND); try { if (!ctMethod.getReturnType().isPrimitive()) { returnType = "L" + ctMethod.getReturnType().getName().replaceAll("\\.", "/"); } else { returnType = String.valueOf(((CtPrimitiveType) ctMethod.getReturnType()).getDescriptor()); result = result.replace("p0", "p1"); String fullClassNameInSmali = ctMethod.getDeclaringClass().getClassPool().get(fullClassName).getSuperclass().getName().replaceAll("\\.", "/"); result = result.replace(result.substring(result.indexOf(PACKNAME_START) + 1, result.indexOf(PACKNAME_END)), fullClassNameInSmali); result = result.substring(0, result.indexOf(")") + 1) + returnType; if (!ctMethod.getReturnType().isPrimitive()) { result += ";";
private void ensureType(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("frozenType", "()I", ccms) ) { // Build a simple field & method returning the type token cc.addField(new CtField(CtClass.intType, "_frozen$type", cc)); cc.addMethod(CtNewMethod.make("public int frozenType() {" + " return _frozen$type == 0 ? (_frozen$type=water.TypeMap.onIce(\""+cc.getName()+"\")) : _frozen$type;" + "}",cc)); } }
private void processFields(CtClass clazz) throws CannotCompileException, NotFoundException { CtField[] fs = clazz.getDeclaredFields(); for (int i = 0; i < fs.length; ++i) { CtField f = fs[i]; int mod = f.getModifiers(); if ((mod & Modifier.PUBLIC) != 0 && (mod & Modifier.FINAL) == 0) { mod |= Modifier.STATIC; String name = f.getName(); CtClass ftype = f.getType(); CtMethod wmethod = CtNewMethod.wrapped(ftype, readPrefix + name, readParam, null, trapRead, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); CtClass[] writeParam = new CtClass[2]; writeParam[0] = classPool.get("java.lang.Object"); writeParam[1] = ftype; wmethod = CtNewMethod.wrapped(CtClass.voidType, writePrefix + name, writeParam, null, trapWrite, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); } } }
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); } }
@SneakyThrows public Proxy(Class<I> superClass, String... classPathString) { if (superClass == null) { throw new NullPointerException("superClass can not be null"); } this.superClass = superClass; ClassPool classPool = new ClassPool(true); ClassPath classPath = new ClassClassPath(this.getClass()); classPool.insertClassPath(classPath); if (classPathString != null) { for (String path : classPathString) { classPool.insertClassPath(path); } } className = superClass.getSimpleName() + "FastBeanCopier" + counter.getAndAdd(1); classFullName = superClass.getPackage() + "." + className; ctClass = classPool.makeClass(classFullName); if (superClass != Object.class) { if (superClass.isInterface()) { ctClass.setInterfaces(new CtClass[]{classPool.get(superClass.getName())}); } else { ctClass.setSuperclass(classPool.get(superClass.getName())); } } addConstructor("public " + className + "(){}"); }