Package javassist

Examples of javassist.CtBehavior


         {
            Object[] annotations = forceAnnotations ? member.getAnnotations() : member.getAvailableAnnotations();
            handleAnnotations(type, member, annotations, resource);
            if (member instanceof CtBehavior)
            {
               CtBehavior behavior = (CtBehavior)member;
               Object[][] paramAnnotations = forceAnnotations ? behavior.getParameterAnnotations() : behavior.getAvailableParameterAnnotations();
               for (int index = 0; index < paramAnnotations.length; index++)
               {
                  handleAnnotations(ElementType.PARAMETER, getBehaviorSignature(behavior, index), paramAnnotations[index], resource);                 
               }
            }
View Full Code Here


         {
            Object[] annotations = forceAnnotations ? member.getAnnotations() : member.getAvailableAnnotations();
            handleAnnotations(type, member, annotations, className, commit);
            if (member instanceof CtBehavior)
            {
               CtBehavior behavior = (CtBehavior)member;
               Object[][] paramAnnotations = forceAnnotations ? behavior.getParameterAnnotations() : behavior.getAvailableParameterAnnotations();
               for (int index = 0; index < paramAnnotations.length; index++)
               {
                  handleAnnotations(ElementType.PARAMETER, getBehaviorSignature(behavior, index), paramAnnotations[index], className, commit);
               }
            }
View Full Code Here

        ASTList mem = p.parseMember1(stable);
        try {
            if (mem instanceof FieldDecl)
                return compileField((FieldDecl)mem);
            else {
                CtBehavior cb = compileMethod(p, (MethodDecl)mem);
                CtClass decl = cb.getDeclaringClass();
                cb.getMethodInfo2()
                  .rebuildStackMapIf6(decl.getClassPool(),
                                      decl.getClassFile2());
                return cb;
            }
        }
View Full Code Here

         {
            Object[] annotations = forceAnnotations ? member.getAnnotations() : member.getAvailableAnnotations();
            handleAnnotations(type, member, annotations, className, commit);
            if (member instanceof CtBehavior)
            {
               CtBehavior behavior = (CtBehavior)member;
               Object[][] paramAnnotations = forceAnnotations ? behavior.getParameterAnnotations() : behavior.getAvailableParameterAnnotations();
               for (int index = 0; index < paramAnnotations.length; index++)
               {
                  handleAnnotations(ElementType.PARAMETER, getBehaviorSignature(behavior, index), paramAnnotations[index], className, commit);
               }
            }
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 = 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(JavassistHelper.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

                        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(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

                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

                continue;
            }
            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(JavassistHelper.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

                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 = 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(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 = 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, 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 = 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 think about null advice
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;
                                FieldMetaData fieldMetaData = JavassistMetaDataMaker.createFieldMetaData(
                                        fieldAccess.getField()
                                );

                                // handle GET
                                if (fieldAccess.isReader() &&
                                    !getFieldFilter(definition, classMetaData, fieldMetaData)) {
                                    // 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(m_joinPointIndex);
                                    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 fiel 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();

                                    m_joinPointIndex++;
                                }

                                // handle SET
                                if (fieldAccess.isWriter() &&
                                    !setFieldFilter(definition, classMetaData, fieldMetaData)) {
                                    // 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 think about null advice
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.