Java Code Examples for javassist.bytecode.AccessFlag#STATIC
The following examples show how to use
javassist.bytecode.AccessFlag#STATIC .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldTransformer.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
private void addReadWriteMethods(ClassFile classfile) throws CannotCompileException { List fields = classfile.getFields(); for (Iterator field_iter = fields.iterator(); field_iter.hasNext();) { FieldInfo finfo = (FieldInfo) field_iter.next(); if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0 && (!finfo.getName().equals(HANDLER_FIELD_NAME))) { // case of non-static field if (filter.handleRead(finfo.getDescriptor(), finfo .getName())) { addReadMethod(classfile, finfo); readableFields.put(finfo.getName(), finfo .getDescriptor()); } if (filter.handleWrite(finfo.getDescriptor(), finfo .getName())) { addWriteMethod(classfile, finfo); writableFields.put(finfo.getName(), finfo .getDescriptor()); } } } }
Example 2
Source File: JavassistDynamicProxyClass.java From code with Apache License 2.0 | 4 votes |
/** * 动态代理对象构建 */ public void newProxyInstance(ClassLoader loader, Class<?> proxyTarget, InvocationHandler invocationHandler) throws NotFoundException, CannotCompileException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { ClassPool pool = new ClassPool(); pool.insertClassPath(new LoaderClassPath(proxyTarget.getClassLoader())); CtClass targetClass = pool.get(proxyTarget.getName()); // 构建代理类,父类是targetClass CtClass proxyClass = pool.makeClass(proxyTarget.getName() + "$proxy", targetClass); // 构建代理类的成员变量,添加到代理类 CtField handlerField = new CtField(pool.get(InvocationHandler.class.getName()), "h", proxyClass); proxyClass.addField(handlerField); int methodIndex = 0; for (CtMethod ctMethod : targetClass.getDeclaredMethods()) { // 满足非public,native、static、final直接跳过 if (!AccessFlag.isPublic(ctMethod.getModifiers())) { continue; } else if ((ctMethod.getModifiers() & AccessFlag.NATIVE) != 0) { continue; } else if ((ctMethod.getModifiers() & AccessFlag.STATIC) != 0) { continue; } else if ((ctMethod.getModifiers() & AccessFlag.FINAL) != 0) { continue; } String methodName = ctMethod.getName() + methodIndex; CtField methodField = new CtField(pool.get(Method.class.getName()), methodName, proxyClass); StringBuilder paramTypeSrc = new StringBuilder("new Class[]{"); for (int i = 0; i < ctMethod.getParameterTypes().length; i++) { if (i != 0) { paramTypeSrc.append(","); } paramTypeSrc.append(ctMethod.getParameterTypes()[i].getName()).append(".class"); } paramTypeSrc.append("}"); String d = proxyTarget.getName() + ".class"; String initSrc = getClass().getName() + ".getMethod(" + d + ",\"" + ctMethod.getName() + "\"," + paramTypeSrc.toString() + ")"; proxyClass.addField(methodField, initSrc); CtMethod copyMethod = CtNewMethod.copy(ctMethod, proxyClass, null); String bodySrc = "{"; bodySrc += "Object result=h.invoke($0," + methodName + ",$args);"; if (!"void".equals(copyMethod.getReturnType().getName())) { bodySrc += "return ($r)result;"; } bodySrc += "}"; copyMethod.setBody(bodySrc); proxyClass.addMethod(copyMethod); methodIndex++; } // 构建代理类的构造器 CtConstructor constructor = new CtConstructor( new CtClass[]{pool.get(InvocationHandler.class.getName())}, proxyClass); constructor.setBody("h=$1;"); proxyClass.addConstructor(constructor); // 生成代理类字节码文件 proxyClass.debugWriteFile("D:\\code\\GitRepository\\Apm\\apm\\javassist\\target"); Class clazz = proxyClass.toClass(); UserServiceImpl userService = (UserServiceImpl) clazz.getConstructor( InvocationHandler.class).newInstance(invocationHandler); userService.getName("11"); }
Example 3
Source File: JavaAssistInsertImpl.java From Robust with Apache License 2.0 | 4 votes |
@Override protected void insertCode(List<CtClass> box, File jarFile) throws CannotCompileException, IOException, NotFoundException { ZipOutputStream outStream = new JarOutputStream(new FileOutputStream(jarFile)); // new ForkJoinPool().submit { for (CtClass ctClass : box) { if (isNeedInsertClass(ctClass.getName())) { //change class modifier ctClass.setModifiers(AccessFlag.setPublic(ctClass.getModifiers())); if (ctClass.isInterface() || ctClass.getDeclaredMethods().length < 1) { //skip the unsatisfied class zipFile(ctClass.toBytecode(), outStream, ctClass.getName().replaceAll("\\.", "/") + ".class"); continue; } boolean addIncrementalChange = false; for (CtBehavior ctBehavior : ctClass.getDeclaredBehaviors()) { if (!addIncrementalChange) { //insert the field addIncrementalChange = true; ClassPool classPool = ctBehavior.getDeclaringClass().getClassPool(); CtClass type = classPool.getOrNull(Constants.INTERFACE_NAME); CtField ctField = new CtField(type, Constants.INSERT_FIELD_NAME, ctClass); ctField.setModifiers(AccessFlag.PUBLIC | AccessFlag.STATIC); ctClass.addField(ctField); } if (!isQualifiedMethod(ctBehavior)) { continue; } //here comes the method will be inserted code methodMap.put(ctBehavior.getLongName(), insertMethodCount.incrementAndGet()); try { if (ctBehavior.getMethodInfo().isMethod()) { CtMethod ctMethod = (CtMethod) ctBehavior; boolean isStatic = (ctMethod.getModifiers() & AccessFlag.STATIC) != 0; CtClass returnType = ctMethod.getReturnType(); String returnTypeString = returnType.getName(); //construct the code will be inserted in string format String body = "Object argThis = null;"; if (!isStatic) { body += "argThis = $0;"; } String parametersClassType = getParametersClassType(ctMethod); // body += " if (com.meituan.robust.PatchProxy.isSupport(\$args, argThis, ${Constants.INSERT_FIELD_NAME}, $isStatic, " + methodMap.get(ctBehavior.longName) + ",${parametersClassType},${returnTypeString}.class)) {" body += " if (com.meituan.robust.PatchProxy.isSupport($args, argThis, " + Constants.INSERT_FIELD_NAME + ", " + isStatic + ", " + methodMap.get(ctBehavior.getLongName()) + "," + parametersClassType + "," + returnTypeString + ".class)) {"; body += getReturnStatement(returnTypeString, isStatic, methodMap.get(ctBehavior.getLongName()), parametersClassType, returnTypeString + ".class"); body += " }"; //finish the insert-code body ,let`s insert it ctBehavior.insertBefore(body); } } catch (Throwable t) { //here we ignore the error t.printStackTrace(); System.out.println("ctClass: " + ctClass.getName() + " error: " + t.getMessage()); } } } //zip the inserted-classes into output file zipFile(ctClass.toBytecode(), outStream, ctClass.getName().replaceAll("\\.", "/") + ".class"); } // }.get() outStream.close(); }