Package javassist

Examples of javassist.CtBehavior


                  }
               }
            }
            if (hasPointcut)
            {
               CtBehavior behavior = call.where();
               if (behavior instanceof CtMethod)
                  modifyMethod(call, classname);
               else if (behavior instanceof CtConstructor) modifyConstructor(call, classname);
            }
         }
View Full Code Here


/*  89 */     ASTList mem = p.parseMember1(this.stable);
/*     */     try {
/*  91 */       if ((mem instanceof FieldDecl)) {
/*  92 */         return compileField((FieldDecl)mem);
/*     */       }
/*  94 */       CtBehavior cb = compileMethod(p, (MethodDecl)mem);
/*  95 */       CtClass decl = cb.getDeclaringClass();
/*  96 */       cb.getMethodInfo2().rebuildStackMapIf6(decl.getClassPool(), decl.getClassFile2());
/*     */
/*  99 */       return cb;
/*     */     }
/*     */     catch (BadBytecode bb)
/*     */     {
View Full Code Here

        LinkedList<CtBehavior> withLineNumber = Lists.newLinkedList(withLineNumberIterable);
        Collections.sort(withLineNumber, (o1, o2) -> Integer.valueOf(o1.getMethodInfo().getLineNumber(0)).compareTo(o2.getMethodInfo().getLineNumber(0)));

        if (!withLineNumber.isEmpty()) {
          CtBehavior method = withLineNumber.get(0);
          int lineNumber = method.getMethodInfo().getLineNumber(0);

          ClassFile classFile = ctClass.getClassFile();
          String sourceFile = classFile.getSourceFile();

          if (lineNumber != -1 && sourceFile != null) {
View Full Code Here

            {
               //System.out.println("XXX RETURNING");
               return;
            }

            CtBehavior behavior = call.where();

            boolean hasPointcut = false;

            DeclareChecker.checkDeclares(manager, call, advisor);
           
View Full Code Here

                  break;
               }
            }
            if (hasPointcut)
            {
               CtBehavior behavior = call.where();
               if (behavior instanceof CtMethod)
                  modifyMethod(call, classname);
               else if (behavior instanceof CtConstructor) modifyConstructor(call, classname);
            }
         }
View Full Code Here

         if ((newcall != null && declare.matchesCall(advisor, newcall) || (mcall != null && declare.matchesCall(advisor, mcall))))
         {
            if (mcall != null)
            {
               sb.append("method call:");
               CtBehavior caller = mcall.where();
               if (caller instanceof CtConstructor)
               {
                  CtConstructor con = (CtConstructor)caller;
                  addConstructor(sb, con);
                  sb.append(" calls ");
                  addMethod(sb, mcall.getMethod());
               }
               else if (caller instanceof CtMethod)
               {
                  CtMethod met = (CtMethod)caller;
                  addMethod(sb, met);
                  sb.append(" calls ");
                  addMethod(sb, mcall.getMethod());
               }
            }
            else if (newcall != null)
            {
               sb.append("constructor call: ");
               CtBehavior caller = newcall.where();
               if (caller instanceof CtConstructor)
               {
                  CtConstructor con = (CtConstructor)caller;
                  addConstructor(sb, con);
                  sb.append(" calls ");
View Full Code Here

                continue;
            }
            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;
                        if (where instanceof CtMethod) {
                            withinMethodInfo = JavassistMethodInfo.getMethodInfo((CtMethod) where, context.getLoader());
                        } else if (where instanceof CtConstructor) {
                            withinMethodInfo = JavassistConstructorInfo.getConstructorInfo(
                                (CtConstructor) where,
                                context.getLoader());
                        }

                        // 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 = TransformationConstants.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(TransformationConstants.JOIN_POINT_MANAGER_FIELD);
                            body.append('.');
                            body.append(TransformationConstants.PROCEED_WITH_CALL_JOIN_POINT_METHOD);
                            body.append('(');
                            body.append(JavassistHelper.calculateHash(ctConstructor));
                            body.append(',');
                            body.append(klass.getJoinPointIndex());
                            body.append(", args, ");
                            body.append(TransformationConstants.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(TransformationConstants.JOIN_POINT_TYPE_CONSTRUCTOR_CALL);
                            body.append("); }");
                            newExpr.replace(body.toString());
                            context.markAsAdvised();
View Full Code Here

                    // AW-228, super.callSomething(..) is not a valid join point
                    if (methodCall.isSuper()) {
                        return;
                    }
                    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 = TransformationConstants.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(TransformationConstants.JOIN_POINT_MANAGER_FIELD);
                            callBody.append('.');
                            callBody.append(TransformationConstants.PROCEED_WITH_CALL_JOIN_POINT_METHOD);
                            callBody.append('(');
                            callBody.append(JavassistHelper.calculateHash(method));
                            callBody.append(',');
                            callBody.append(klass.getJoinPointIndex());
                            callBody.append(", args, ");
                            callBody.append(TransformationConstants.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(TransformationConstants.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

                continue;
            }
            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;
                        }

                        // create the caller method info
                        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());
                        }

                        if (fieldAccess.isReader()
                            && !getFieldFilter(
                                definition,
                                new ExpressionContext(PointcutType.GET, fieldInfo, withinMethodInfo),
                                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 = TransformationConstants.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(TransformationConstants.JOIN_POINT_MANAGER_FIELD);
                            callBody.append('.');
                            callBody.append(TransformationConstants.PROCEED_WITH_GET_JOIN_POINT_METHOD);
                            callBody.append('(');
                            callBody.append(JavassistHelper.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 = TransformationConstants.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, withinMethodInfo),
                                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 = TransformationConstants.STATIC_CLASS_FIELD;
                            CtClass declaringClass = fieldAccess.getField().getDeclaringClass();
                            if (!declaringClass.getName().replace('/', '.').equals(
                                where.getDeclaringClass().getName().replace('/', '.'))) {
                                declaringClassFieldName = addFieldAccessDeclaringClassField(declaringClass, fieldAccess
                                        .getField());
                            }

                            //TODO ALEX think about null advice
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(TransformationConstants.JOIN_POINT_MANAGER_FIELD);
                            body.append('.');
                            body.append(TransformationConstants.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(JavassistHelper.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

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.