Package javassist

Examples of javassist.CtBehavior


                return;
            }
            ctClass.instrument(new ExprEditor() {
                public void edit(MethodCall methodCall) throws CannotCompileException {
                    try {
                        CtBehavior where = null;
                        try {
                            where = methodCall.where();
                        } catch (RuntimeException e) {
                            // <clinit> access leads to a bug in Javassist
                            where = ctClass.getClassInitializer();
                        }

                        // filter caller methods
                        if (methodFilterCaller(where)) {
                            return;
                        }

                        // get the callee method name, signature and class name
                        CtMethod calleeMethod = methodCall.getMethod();
                        String calleeClassName = methodCall.getClassName();
                        if (!(calleeMethod.getName().equals("proceedWithCallJoinPoint") && calleeClassName
                                .equals("org.codehaus.aspectwerkz.joinpoint.management.JoinPointManager"))) {
                            return;
                        }
                        System.out.println("found smtg to REMOVE");
                        System.out.println("calleeMethod = " + calleeMethod.getName());
                        System.out.println("calleeClassName = " + calleeClassName);
                        System.out.println("methodCall = " + methodCall.indexOfBytecode());
                        methodCall.replace("{java.lang.System.out.println($args[0]); $_=null;}");
                        try {
                            CodeIterator it = where.getMethodInfo().getCodeAttribute().iterator();
                            it.move(methodCall.indexOfBytecode() - 5);
                            System.out.println("it.get() = " + it.get());
                            it.next();
                            System.out.println("it.get() = " + it.get());
                            it.next();
View Full Code Here


            }
            ctClass.instrument(
                    new ExprEditor() {
                        public void edit(FieldAccess fieldAccess) throws CannotCompileException {
                            try {
                                CtBehavior where = null;
                                try {
                                    where = fieldAccess.where();
                                } catch (RuntimeException e) {
                                    // <clinit> access leads to a bug in Javassist
                                    where = ctClass.getClassInitializer();
                                }

                                // filter caller context
                                if (methodFilter(where)) {
                                    return;
                                }

                                // get field accessed information
                                final String fieldName = fieldAccess.getFieldName();
                                final String fieldSignature = fieldAccess.getField().getType().getName().replace(
                                        '/', '.'
                                )
                                                              + ' ' + fieldName;
                                FieldInfo fieldInfo = JavassistFieldInfo.getFieldInfo(
                                        fieldAccess.getField(),
                                        context.getLoader()
                                );
                                if (fieldInfo == null) {
                                    // when re-weaving is done, due to Javassist CtClass behavior,
                                    // the fieldInfo for __AW_Clazz addded field can be null
                                    // then we skip it silently
                                    return;
                                }
                                if (fieldAccess.isReader()
                                    && !getFieldFilter(
                                            definition,
                                            new ExpressionContext(PointcutType.GET, fieldInfo, classInfo),
                                            fieldInfo
                                    )) {
                                    // check the declaring class for the field is not the same as target class,
                                    // if that is the case then we have have class loaded and set in the ___AW_clazz already
                                    String declaringClassFieldName = TransformationUtil.STATIC_CLASS_FIELD;
                                    CtClass declaringClass = fieldAccess.getField().getDeclaringClass();
                                    if (!declaringClass.getName().replace('/', '.').equals(
                                            where.getDeclaringClass()
                                            .getName()
                                            .replace('/', '.')
                                    )) {
                                        declaringClassFieldName =
                                        addFieldAccessDeclaringClassField(
                                                declaringClass,
                                                fieldAccess.getField()
                                        );
                                    }

                                    //TODO ALEX might need to review since SET is not handled gracefully that way
                                    StringBuffer body = new StringBuffer();
                                    StringBuffer callBody = new StringBuffer();
                                    callBody.append(TransformationUtil.JOIN_POINT_MANAGER_FIELD);
                                    callBody.append('.');
                                    callBody.append(TransformationUtil.PROCEED_WITH_GET_JOIN_POINT_METHOD);
                                    callBody.append('(');
                                    callBody.append(TransformationUtil.calculateHash(fieldAccess.getField()));
                                    callBody.append(',');
                                    callBody.append(klass.getJoinPointIndex());
                                    if (Modifier.isStatic(fieldAccess.getField().getModifiers())) {
                                        callBody.append(", (Object)null, ");
                                    } else {
                                        callBody.append(", $0, ");
                                    }
                                    callBody.append(declaringClassFieldName);
                                    callBody.append(",\"");
                                    callBody.append(fieldSignature);
                                    callBody.append("\");");

                                    // handles advice returns null and field is primitive type
                                    if (!fieldAccess.getField().getType().isPrimitive()) {
                                        body.append("$_ = ($r)");
                                        body.append(callBody.toString());
                                    } else {
                                        String localResult = TransformationUtil.ASPECTWERKZ_PREFIX + "res";
                                        body.append("{ Object ").append(localResult).append(" = ");
                                        body.append(callBody.toString());
                                        body.append("if (").append(localResult).append(" != null)");
                                        body.append("$_ = ($r) ").append(localResult).append("; else ");
                                        body.append("$_ = ");
                                        body.append(
                                                JavassistHelper.getDefaultPrimitiveValue(
                                                        fieldAccess.getField().getType()
                                                )
                                        );
                                        body.append("; }");
                                    }
                                    fieldAccess.replace(body.toString());
                                    context.markAsAdvised();
                                    klass.incrementJoinPointIndex();
                                }
                                if (fieldAccess.isWriter()
                                    && !setFieldFilter(
                                            definition,
                                            new ExpressionContext(PointcutType.SET, fieldInfo, classInfo),
                                            fieldInfo
                                    )) {
                                    // check the declaring class for the field is not the same as target class,
                                    // if that is the case then we have have class loaded and set in the ___AW_clazz already
                                    String declaringClassFieldName = TransformationUtil.STATIC_CLASS_FIELD;
                                    CtClass declaringClass = fieldAccess.getField().getDeclaringClass();
                                    if (!declaringClass.getName().replace('/', '.').equals(
                                            where.getDeclaringClass()
                                            .getName()
                                            .replace('/', '.')
                                    )) {
                                        declaringClassFieldName =
                                        addFieldAccessDeclaringClassField(
View Full Code Here

            }
            ctClass.instrument(
                    new ExprEditor() {
                        public void edit(MethodCall methodCall) throws CannotCompileException {
                            try {
                                CtBehavior where = null;
                                try {
                                    where = methodCall.where();
                                } catch (RuntimeException e) {
                                    // <clinit> access leads to a bug in Javassist
                                    where = ctClass.getClassInitializer();
                                }

                                // filter caller methods
                                if (methodFilterCaller(where)) {
                                    return;
                                }

                                // get the callee method name, signature and class name
                                CtMethod calleeMethod = methodCall.getMethod();
                                String calleeClassName = methodCall.getClassName();
                                if (!(
                                         calleeMethod.getName().equals("proceedWithCallJoinPoint")
                                         &&
                                         calleeClassName.equals(
                                                 "org.codehaus.aspectwerkz.joinpoint.management.JoinPointManager"
                                         )
                                     )) {
                                    return;
                                }
                                System.out.println("found smtg to REMOVE");
                                System.out.println("calleeMethod = " + calleeMethod.getName());
                                System.out.println("calleeClassName = " + calleeClassName);
                                System.out.println("methodCall = " + methodCall.indexOfBytecode());
                                methodCall.replace("{java.lang.System.out.println($args[0]); $_=null;}");
                                try {
                                    CodeIterator it = where.getMethodInfo().getCodeAttribute().iterator();
                                    it.move(methodCall.indexOfBytecode() - 5);
                                    System.out.println("it.get() = " + it.get());
                                    it.next();
                                    System.out.println("it.get() = " + it.get());
                                    it.next();
View Full Code Here

                                try {
                                    exceptionClass = handlerExpr.getType();
                                } catch (NullPointerException e) {
                                    return;
                                }
                                CtBehavior where = null;
                                try {
                                    where = handlerExpr.where();
                                } catch (RuntimeException e) {
                                    // <clinit> access leads to a bug in Javassist
                                    where = ctClass.getClassInitializer();
                                }
                                MemberInfo withinMethodInfo = null;
                                if (where instanceof CtMethod) {
                                    withinMethodInfo = JavassistMethodInfo.getMethodInfo(
                                            (CtMethod)where,
                                            context.getLoader()
                                    );
                                } else if (where instanceof CtConstructor) {
                                    withinMethodInfo =
                                    JavassistConstructorInfo.getConstructorInfo(
                                            (CtConstructor)where,
                                            context.getLoader()
                                    );
                                }
                                ClassInfo exceptionClassInfo = JavassistClassInfo.getClassInfo(
                                        exceptionClass,
                                        context.getLoader()
                                );
                                ExpressionContext ctx = new ExpressionContext(
                                        PointcutType.HANDLER, exceptionClassInfo,
                                        withinMethodInfo
                                );
                                if (definition.hasPointcut(ctx)) {
                                    // call the wrapper method instead of the callee method
                                    StringBuffer body = new StringBuffer();
                                    body.append(TransformationUtil.JOIN_POINT_MANAGER_FIELD);
                                    body.append('.');
                                    body.append(TransformationUtil.PROCEED_WITH_HANDLER_JOIN_POINT_METHOD);
                                    body.append('(');

                                    // TODO: unique hash is needed, based on: executing class, executing method, catch clause (and sequence number?)
                                    body.append(TransformationUtil.calculateHash(exceptionClass));
                                    body.append(',');
                                    body.append(klass.getJoinPointIndex());
                                    if (Modifier.isStatic(where.getModifiers())) {
                                        body.append(", $1, (Object)null, \"");
                                    } else {
                                        body.append(", $1, this, \"");
                                    }
View Full Code Here

            }
            ctClass.instrument(
                    new ExprEditor() {
                        public void edit(MethodCall methodCall) throws CannotCompileException {
                            try {
                                CtBehavior where;
                                try {
                                    where = methodCall.where();
                                } catch (RuntimeException e) {
                                    // <clinit> access leads to a bug in Javassist
                                    where = ctClass.getClassInitializer();
                                }

                                // filter caller methods
                                if (methodFilterCaller(where)) {
                                    return;
                                }

                                // get the callee method name, signature and class name
                                CtMethod calleeMethod = methodCall.getMethod();
                                String calleeClassName = methodCall.getClassName();

                                // filter callee classes
                                if (!definition.inIncludePackage(calleeClassName)) {
                                    return;
                                }

                                // filter callee methods
                                if (methodFilterCallee(calleeMethod)) {
                                    return;
                                }
                                JavassistClassInfoRepository classInfoRepository = JavassistClassInfoRepository
                                        .getRepository(context.getLoader());

                                // TODO: callee side class info is NOT used, make use of it
                                ClassInfo calleeSideClassInfo = classInfoRepository.getClassInfo(calleeClassName);
                                if (calleeSideClassInfo == null) {
                                    calleeSideClassInfo = JavassistClassInfo.getClassInfo(
                                            ctClass.getClassPool().get(calleeClassName),
                                            context.getLoader()
                                    );
                                }

                                // create the caller method info, used for 'within' and 'withincode'
                                MemberInfo withinMemberInfo = null;
                                if (where instanceof CtMethod) {
                                    withinMemberInfo = JavassistMethodInfo.getMethodInfo(
                                            (CtMethod)where,
                                            context.getLoader()
                                    );
                                } else if (where instanceof CtConstructor) {
                                    withinMemberInfo =
                                    JavassistConstructorInfo.getConstructorInfo(
                                            (CtConstructor)where,
                                            context.getLoader()
                                    );
                                }

                                // create the callee method info
                                MethodInfo calleeSideMethodInfo = JavassistMethodInfo.getMethodInfo(
                                        methodCall.getMethod(),
                                        context.getLoader()
                                );
                                ExpressionContext ctx = new ExpressionContext(
                                        PointcutType.CALL, calleeSideMethodInfo,
                                        withinMemberInfo
                                );
                                if (definition.hasPointcut(ctx) || definition.hasCflowPointcut(ctx)) {
                                    // check the callee class is not the same as target class, if that is the case
                                    // then we have have class loaded and set in the ___AW_clazz already
                                    String declaringClassMethodName = TransformationUtil.STATIC_CLASS_FIELD;
                                    CtMethod method = methodCall.getMethod();
                                    CtClass declaringClass = method.getDeclaringClass();
                                    if (!declaringClass.getName().replace('/', '.').equals(
                                            where.getDeclaringClass()
                                            .getName()
                                            .replace('/', '.')
                                    )) {
                                        declaringClassMethodName = addCalleeMethodDeclaringClassField(ctClass, method);
                                    }

                                    // call the wrapper method instead of the callee method
                                    StringBuffer body = new StringBuffer();
                                    StringBuffer callBody = new StringBuffer();
                                    callBody.append(TransformationUtil.JOIN_POINT_MANAGER_FIELD);
                                    callBody.append('.');
                                    callBody.append(TransformationUtil.PROCEED_WITH_CALL_JOIN_POINT_METHOD);
                                    callBody.append('(');
                                    callBody.append(TransformationUtil.calculateHash(method));
                                    callBody.append(',');
                                    callBody.append(klass.getJoinPointIndex());
                                    callBody.append(", args, ");
                                    callBody.append(TransformationUtil.STATIC_CLASS_FIELD);
                                    if (Modifier.isStatic(where.getModifiers())) {
                                        callBody.append(", nullObject, ");
                                    } else {
                                        callBody.append(", this, ");
                                    }
                                    callBody.append("declaringClass, $0, \"");
                                    callBody.append(where.getName());
                                    callBody.append("\",\"");
                                    callBody.append(where.getSignature());
                                    callBody.append("\",");
                                    callBody.append(TransformationUtil.JOIN_POINT_TYPE_METHOD_CALL);
                                    callBody.append(");");
                                    body.append('{');
                                    if (method.getParameterTypes().length > 0) {
                                        body.append("Object[] args = $args; ");
                                    } else {
                                        body.append("Object[] args = null; ");
                                    }
                                    body.append("Class declaringClass = ");
                                    body.append(declaringClassMethodName);
                                    body.append("; ");
                                    if (Modifier.isStatic(where.getModifiers())) {
                                        body.append("Object nullObject = null;");
                                    }
                                    if (methodCall.getMethod().getReturnType() == CtClass.voidType) {
                                        body.append("$_ = ").append(callBody.toString()).append("}");
                                    } else if (!methodCall.getMethod().getReturnType().isPrimitive()) {
View Full Code Here

            }
            ctClass.instrument(
                    new ExprEditor() {
                        public void edit(NewExpr newExpr) throws CannotCompileException {
                            try {
                                CtBehavior where = null;
                                try {
                                    where = newExpr.where();
                                } catch (RuntimeException e) {
                                    // <clinit> access leads to a bug in Javassist
                                    where = ctClass.getClassInitializer();
                                }

                                // filter caller methods
                                if (methodFilterCaller(where)) {
                                    return;
                                }
                                CtConstructor ctConstructor = newExpr.getConstructor();
                                String calleeClassName = newExpr.getClassName();

                                // filter callee classes
                                if (!definition.inIncludePackage(calleeClassName)) {
                                    return;
                                }

                                // filter the constructors
                                if (constructorFilter(ctConstructor)) {
                                    return;
                                }

                                // create the caller method info
                                MemberInfo withinMethodInfo = null;
                                boolean isWithinInfoAMethod = true;
                                if (where instanceof CtMethod) {
                                    withinMethodInfo = JavassistMethodInfo.getMethodInfo(
                                            (CtMethod)where,
                                            context.getLoader()
                                    );
                                } else if (where instanceof CtConstructor) {
                                    withinMethodInfo =
                                    JavassistConstructorInfo.getConstructorInfo(
                                            (CtConstructor)where,
                                            context.getLoader()
                                    );
                                    isWithinInfoAMethod = false;
                                }

                                // create the constructor info
                                CtConstructor constructor = newExpr.getConstructor();
                                ConstructorInfo calleeSideConstructorInfo = JavassistConstructorInfo.getConstructorInfo(
                                        constructor,
                                        context
                                        .getLoader()
                                );
                                ExpressionContext ctx = new ExpressionContext(
                                        PointcutType.CALL, calleeSideConstructorInfo,
                                        withinMethodInfo
                                );

                                // is this a caller side method pointcut?
                                if (definition.hasPointcut(ctx)) {
                                    // check the callee class is not the same as target class, if that is the case
                                    // then we have have class loaded and set in the ___AW_clazz already
                                    String declaringClassMethodName = TransformationUtil.STATIC_CLASS_FIELD;
                                    CtClass declaringClass = ctConstructor.getDeclaringClass();
                                    if (!declaringClass.getName().replace('/', '.').equals(
                                            where.getDeclaringClass()
                                            .getName()
                                            .replace('/', '.')
                                    )) {
                                        declaringClassMethodName =
                                        addCalleeMethodDeclaringClassField(ctClass, ctConstructor);
                                    }

                                    // call the wrapper method instead of the callee method
                                    StringBuffer body = new StringBuffer();
                                    body.append('{');
                                    if (ctConstructor.getParameterTypes().length > 0) {
                                        body.append("Object[] args = $args; ");
                                    } else {
                                        body.append("Object[] args = null; ");
                                    }
                                    body.append("Class declaringClass = ");
                                    body.append(declaringClassMethodName);
                                    body.append("; ");
                                    if (Modifier.isStatic(where.getModifiers())) {
                                        body.append("Object nullObject = null;");
                                    }
                                    body.append("$_ = ($r)");
                                    body.append(TransformationUtil.JOIN_POINT_MANAGER_FIELD);
                                    body.append('.');
                                    body.append(TransformationUtil.PROCEED_WITH_CALL_JOIN_POINT_METHOD);
                                    body.append('(');
                                    body.append(TransformationUtil.calculateHash(ctConstructor));
                                    body.append(',');
                                    body.append(klass.getJoinPointIndex());
                                    body.append(", args, ");
                                    body.append(TransformationUtil.STATIC_CLASS_FIELD);
                                    if (Modifier.isStatic(where.getModifiers())) {
                                        body.append(", nullObject, ");
                                    } else {
                                        body.append(", this, ");
                                    }
                                    body.append("declaringClass, $0, \"");
                                    body.append(where.getName());
                                    body.append("\",\"");
                                    body.append(where.getSignature());
                                    body.append("\",");
                                    body.append(TransformationUtil.JOIN_POINT_TYPE_CONSTRUCTOR_CALL);
                                    body.append("); }");
                                    newExpr.replace(body.toString());
                                    context.markAsAdvised();
View Full Code Here

    final String newInitialiser = code == null ? "$_ = new " + clazz + "();" : code;
    final Set<CtBehavior> allBehaviours = new HashSet<CtBehavior>();
    if (o instanceof CtClass) {
      CtClass ctClass = (CtClass) o;
      Collections.addAll(allBehaviours, ctClass.getDeclaredConstructors());
      final CtBehavior initialiser = ctClass.getClassInitializer();
      if (initialiser != null) {
        allBehaviours.add(initialiser);
      }
    } else {
      allBehaviours.add((CtBehavior) o);
View Full Code Here

        }
      } catch (NotFoundException ignored) {
        ctClass.addField(new CtField(ctField, ctClass));
      }
      if (expectStatic) {
        CtBehavior initializer = ctClass.getClassInitializer();
        if (initializer != null) {
          removeInitializers(initializer, ctField);
        }
      }
    }
    for (CtMethod newMethod : from.getDeclaredMethods()) {
      if ((newMethod.getName().startsWith("construct") || newMethod.getName().startsWith("staticConstruct"))) {
        try {
          ctClass.getDeclaredMethod(newMethod.getName());
          boolean found = true;
          int i = 0;
          String name = newMethod.getName();
          while (found) {
            i++;
            try {
              ctClass.getDeclaredMethod(name + i);
            } catch (NotFoundException e2) {
              found = false;
            }
          }
          newMethod.setName(name + i);
        } catch (NotFoundException ignored) {
          // Not found - no need to change the name
        }
      }
    }
    for (CtMethod newMethod : from.getDeclaredMethods()) {
      try {
        CtMethod oldMethod = ctClass.getDeclaredMethod(newMethod.getName(), newMethod.getParameterTypes());
        replaceMethod(oldMethod, newMethod);
        if (Modifier.isSynchronized(newMethod.getModifiers())) {
          oldMethod.setModifiers(oldMethod.getModifiers() | Modifier.SYNCHRONIZED);
        }
      } catch (NotFoundException ignored) {
        CtMethod added = CtNewMethod.copy(newMethod, ctClass, classMap);
        ctClass.addMethod(added);
        MethodInfo addedMethodInfo = added.getMethodInfo2();
        String addedDescriptor = addedMethodInfo.getDescriptor();
        String newDescriptor = newMethod.getMethodInfo2().getDescriptor();
        if (!newDescriptor.equals(addedDescriptor)) {
          addedMethodInfo.setDescriptor(newDescriptor);
        }
        replaceMethod(added, newMethod);
        if (added.getName().startsWith("construct")) {
          try {
            insertSuper(added);
          } catch (CannotCompileException ignore) {
          }
          CtMethod runConstructors;
          try {
            runConstructors = ctClass.getMethod("runConstructors", "()V");
          } catch (NotFoundException e) {
            runConstructors = CtNewMethod.make("public void runConstructors() { }", ctClass);
            ctClass.addMethod(runConstructors);
            try {
              ctClass.getField("isConstructed");
            } catch (NotFoundException ignore) {
              ctClass.addField(new CtField(classPool.get("boolean"), "isConstructed", ctClass));
            }
            for (CtBehavior ctBehavior : ctClass.getDeclaredConstructors()) {
              ctBehavior.insertAfter("{ if(!this.isConstructed) { this.isConstructed = true; this.runConstructors(); } }");
            }
          }
          try {
            ctClass.getSuperclass().getMethod(added.getName(), "()V");
          } catch (NotFoundException ignore) {
            runConstructors.insertAfter(added.getName() + "();");
          }
        }
        if (added.getName().startsWith("staticConstruct")) {
          ctClass.makeClassInitializer().insertAfter("{ " + added.getName() + "(); }");
        }
      }
    }
    for (CtClass CtInterface : from.getInterfaces()) {
      ctClass.addInterface(CtInterface);
    }
    CtConstructor initializer = from.getClassInitializer();
    if (initializer != null) {
      ctClass.addMethod(initializer.toMethod("patchStaticInitializer", ctClass));
      ctClass.makeClassInitializer().insertAfter("patchStaticInitializer();");
    }
  }
View Full Code Here

      }
    } else if (o instanceof CtField) {
      CtField ctField = (CtField) o;
      ctField.setModifiers(Modifier.setPublic(ctField.getModifiers()));
    } else {
      CtBehavior ctBehavior = (CtBehavior) o;
      ctBehavior.setModifiers(Modifier.setPublic(ctBehavior.getModifiers()));
    }
  }
View Full Code Here

      ctClass.setModifiers(Modifier.setPublic(ctClass.getModifiers()));
      for (CtConstructor ctConstructor : ctClass.getDeclaredConstructors()) {
        public_(ctConstructor, Collections.<String, String>emptyMap());
      }
    } else {
      CtBehavior ctBehavior = (CtBehavior) o;
      ctBehavior.setModifiers(Modifier.clear(ctBehavior.getModifiers(), Modifier.FINAL));
    }
  }
View Full Code Here

TOP

Related Classes of javassist.CtBehavior

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.