/** * Writes a class file represented by this <code>CtClass</code> * object in the current directory. * Once this method is called, further modifications are not * possible any more. * * @see #debugWriteFile() */ public void writeFile() throws NotFoundException, IOException, CannotCompileException { writeFile("."); }
/** * Writes a class file represented by this <code>CtClass</code> * object in the current directory. * Once this method is called, further modifications are not * possible any more. * * @see #debugWriteFile() */ public void writeFile() throws NotFoundException, IOException, CannotCompileException { writeFile("."); }
pool.get(entries[i].classname).writeFile();
private boolean letUsersSendClassfile(OutputStream out, String filename, int length) throws IOException, BadHttpRequest { if (classPool == null) return false; byte[] classfile; String classname = filename.substring(0, length - 6).replace('/', '.'); try { if (translator != null) translator.onLoad(classPool, classname); CtClass c = classPool.get(classname); classfile = c.toBytecode(); if (debugDir != null) c.writeFile(debugDir); } catch (Exception e) { throw new BadHttpRequest(e); } sendHeader(out, classfile.length, typeClass); out.write(classfile); return true; }
/** * Writes a class file as <code>writeFile()</code> does although this * method does not prune or freeze the class after writing the class * file. Note that, once <code>writeFile()</code> or <code>toBytecode()</code> * is called, it cannot be called again since the class is pruned and frozen. * This method would be useful for debugging. * * @param directoryName it must end without a directory separator. */ public void debugWriteFile(String directoryName) { try { boolean p = stopPruning(true); writeFile(directoryName); defrost(); stopPruning(p); } catch (Exception e) { throw new RuntimeException(e); } }
pool.get(entries[i].classname).writeFile();
private boolean letUsersSendClassfile(OutputStream out, String filename, int length) throws IOException, BadHttpRequest { if (classPool == null) return false; byte[] classfile; String classname = filename.substring(0, length - 6).replace('/', '.'); try { if (translator != null) translator.onLoad(classPool, classname); CtClass c = classPool.get(classname); classfile = c.toBytecode(); if (debugDir != null) c.writeFile(debugDir); } catch (Exception e) { throw new BadHttpRequest(e); } sendHeader(out, classfile.length, typeClass); out.write(classfile); return true; }
/** * Writes a class file as <code>writeFile()</code> does although this * method does not prune or freeze the class after writing the class * file. Note that, once <code>writeFile()</code> or <code>toBytecode()</code> * is called, it cannot be called again since the class is pruned and frozen. * This method would be useful for debugging. * * @param directoryName it must end without a directory separator. */ public void debugWriteFile(String directoryName) { try { boolean p = stopPruning(true); writeFile(directoryName); defrost(); stopPruning(p); } catch (Exception e) { throw new RuntimeException(e); } }
private static void modifyProxyFactory() throws NotFoundException, CannotCompileException, IOException { System.out.println("Generating method bodies for com.zaxxer.hikari.proxy.ProxyFactory"); String packageName = ProxyConnection.class.getPackage().getName(); CtClass proxyCt = classPool.getCtClass("com.zaxxer.hikari.pool.ProxyFactory"); for (CtMethod method : proxyCt.getMethods()) { switch (method.getName()) { case "getProxyConnection": method.setBody("{return new " + packageName + ".HikariProxyConnection($$);}"); break; case "getProxyStatement": method.setBody("{return new " + packageName + ".HikariProxyStatement($$);}"); break; case "getProxyPreparedStatement": method.setBody("{return new " + packageName + ".HikariProxyPreparedStatement($$);}"); break; case "getProxyCallableStatement": method.setBody("{return new " + packageName + ".HikariProxyCallableStatement($$);}"); break; case "getProxyResultSet": method.setBody("{return new " + packageName + ".HikariProxyResultSet($$);}"); break; default: // unhandled method break; } } proxyCt.writeFile(genDirectory + "target/classes"); }
targetCt.writeFile(genDirectory + "target/classes");
/** * Writes a class file represented by this <code>CtClass</code> * object in the current directory. * Once this method is called, further modifications are not * possible any more. */ public void writeFile() throws NotFoundException, IOException, CannotCompileException { writeFile("."); }
ClassPool pool = ClassPool.getDefault(); CtClass source = pool.get("MySourceClass"); CtMethod sourceMethod = source.getDeclaredMethod("myMethod"); CtClass dest = pool.get("MyDestClass"); dest.addMethod(sourceMethod); dest.writeFile();
private void saveClass(CtClass cc) { if (directory != null) { try { cc.writeFile(directory.getAbsolutePath()); } catch (CannotCompileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("Random"); // maybe java.util.Random CtMethod m = cc.getDeclaredMethod("nextLong"); m.setBody("my.super.duper.RandomClass.nextLong()"); cc.writeFile();
ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("TestInstr"); CtMethod m = cc.getDeclaredMethod("sayHello"); m.insertBefore("{ System.out.println(\"method sayHello has been called\"); }"); cc.writeFile();
CtClass etype = ClassPool.getDefault().get("com.original.library.OriginalLibrary"); // get method from class CtMethod cm = etype.getDeclaredMethod("getValue"); // change the method bosy cm.setBody("return 3;"); etype.rebuildClassFile(); // give the path where classes is placed, In my eclipse it is bin etype.writeFile("bin"); OriginalLibrary originalLibraryObject; originalLibraryObject = new OriginalLibrary(); System.out.println(originalLibraryObject.getValue()); System.out.println(originalLibraryObject.getAnotherValue());
ClassPool pool = new ClassPool(); pool.appendClassPath("pathToYourJar"); CtClass clazz = pool.get("Your class"); CtMethod throwingMethod = CtMethod.make("throw new RuntimeException();",clazz); for(CtMethod method : clazz.getDeclaredMethods()){ method.setBody(throwingMethod,null); } clazz.writeFile("pathToYourNewClassDirectory"); //zip the classes in your new class directory into a jar, // add a manifest if you need to, deploy to where you want it
ClassPool classpool = ClassPool.getDefault(); CtClass ctClass = classpool.get(Class1.class.getName()); CtField field = ctClass.getField("strCompany"); CodeConverter codeConverter = new CodeConverter(); codeConverter.redirectFieldAccess(field, ctClass, "strCompany2"); ctClass.instrument(codeConverter); field.setName("strCompany2"); ctClass.writeFile("./injectedClasses");
public static void crackOtherMethod(ClassPool pool, String classname, String methodName, String source) throws NotFoundException, CannotCompileException, IOException { // 获取需要修改的类 CtClass javaServiceClass = pool.getCtClass(classname); // 获取类中的stop方法 CtMethod stopMethod = javaServiceClass.getDeclaredMethod(methodName); // 修改该方法的内容 stopMethod.setBody(source); javaServiceClass.writeFile(); } }
private void processAndWriteClass(Directory baseDir, File classfile) throws Exception { CtClass clazz = classPool.get(ClassNames.getClassName(baseDir, classfile)); classInstrumenter.instrumentClass(clazz); if (clazz.isModified()) clazz.writeFile(processDirectory.getAbsolutePath()); clazz.detach(); }