View Javadoc

    1   /***************************************************************************************
    2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
    3    * http://aspectwerkz.codehaus.org                                                    *
    4    * ---------------------------------------------------------------------------------- *
    5    * The software in this package is published under the terms of the LGPL license      *
    6    * a copy of which has been included with this distribution in the license.txt file.  *
    7    **************************************************************************************/
    8   package org.codehaus.aspectwerkz.expression;
    9
    10  import org.codehaus.aspectwerkz.annotation.AnnotationInfo;
    11  import org.codehaus.aspectwerkz.expression.ast.ASTAnd;
    12  import org.codehaus.aspectwerkz.expression.ast.ASTAttribute;
    13  import org.codehaus.aspectwerkz.expression.ast.ASTCall;
    14  import org.codehaus.aspectwerkz.expression.ast.ASTCflow;
    15  import org.codehaus.aspectwerkz.expression.ast.ASTCflowBelow;
    16  import org.codehaus.aspectwerkz.expression.ast.ASTClassPattern;
    17  import org.codehaus.aspectwerkz.expression.ast.ASTConstructorPattern;
    18  import org.codehaus.aspectwerkz.expression.ast.ASTExecution;
    19  import org.codehaus.aspectwerkz.expression.ast.ASTExpression;
    20  import org.codehaus.aspectwerkz.expression.ast.ASTFieldPattern;
    21  import org.codehaus.aspectwerkz.expression.ast.ASTGet;
    22  import org.codehaus.aspectwerkz.expression.ast.ASTHandler;
    23  import org.codehaus.aspectwerkz.expression.ast.ASTMethodPattern;
    24  import org.codehaus.aspectwerkz.expression.ast.ASTModifier;
    25  import org.codehaus.aspectwerkz.expression.ast.ASTNot;
    26  import org.codehaus.aspectwerkz.expression.ast.ASTOr;
    27  import org.codehaus.aspectwerkz.expression.ast.ASTParameter;
    28  import org.codehaus.aspectwerkz.expression.ast.ASTPointcutReference;
    29  import org.codehaus.aspectwerkz.expression.ast.ASTRoot;
    30  import org.codehaus.aspectwerkz.expression.ast.ASTSet;
    31  import org.codehaus.aspectwerkz.expression.ast.ASTStaticInitialization;
    32  import org.codehaus.aspectwerkz.expression.ast.ASTWithin;
    33  import org.codehaus.aspectwerkz.expression.ast.ASTWithinCode;
    34  import org.codehaus.aspectwerkz.expression.ast.ExpressionParserVisitor;
    35  import org.codehaus.aspectwerkz.expression.ast.Node;
    36  import org.codehaus.aspectwerkz.expression.ast.SimpleNode;
    37  import org.codehaus.aspectwerkz.expression.ast.ASTArgs;
    38  import org.codehaus.aspectwerkz.expression.ast.ASTArgParameter;
    39  import org.codehaus.aspectwerkz.expression.regexp.TypePattern;
    40  import org.codehaus.aspectwerkz.reflect.ClassInfo;
    41  import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
    42  import org.codehaus.aspectwerkz.reflect.FieldInfo;
    43  import org.codehaus.aspectwerkz.reflect.MemberInfo;
    44  import org.codehaus.aspectwerkz.reflect.MethodInfo;
    45  import org.codehaus.aspectwerkz.reflect.ReflectionInfo;
    46
    47  import java.lang.reflect.Modifier;
    48  import java.util.ArrayList;
    49  import java.util.Iterator;
    50  import java.util.List;
    51
    52  import org.codehaus.aspectwerkz.expression.ast.ASTHasField;
    53  import org.codehaus.aspectwerkz.expression.ast.ASTHasMethod;
    54  import org.codehaus.aspectwerkz.expression.ast.ASTTarget;
    55  import org.codehaus.aspectwerkz.expression.ast.ASTThis;
    56  import org.codehaus.aspectwerkz.util.Strings;
    57
    58  /***
    59   * The visitor that extract all possible arguments referenced by the expression.
    60   * <p/>
    61   * TODO handle pointcut reference and handle parameter transition
    62   * + checks as done in the ArgIndexVisitor for this / target compliance.
    63   *
    64   * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur </a>
    65   */
    66  public class ExpressionValidateVisitor implements ExpressionParserVisitor {
    67
    68      protected Node m_root;
    69      protected String m_expression;
    70      protected String m_namespace;
    71
    72      /***
    73       * Creates a new expression.
    74       *
    75       * @param expression the expression as a string
    76       * @param namespace  the namespace
    77       * @param root       the AST root
    78       */
    79      public ExpressionValidateVisitor(final String expression,
    80                                       final String namespace,
    81                                       final Node root) {
    82          m_expression = expression;
    83          m_namespace = namespace;
    84          m_root = root;
    85      }
    86
    87      /***
    88       * Populate data with the possible arguments
    89       *
    90       * @param data a list to feed with Strings
    91       */
    92      public void populate(List data) {
    93          visit(m_root, data);
    94      }
    95
    96      // ============ Boot strap =============
    97      public Object visit(Node node, Object data) {
    98          return node.jjtGetChild(0).jjtAccept(this, data);
    99      }
    100
    101     public Object visit(SimpleNode node, Object data) {
    102         return node.jjtGetChild(0).jjtAccept(this, data);
    103     }
    104
    105     public Object visit(ASTRoot node, Object data) {
    106         return node.jjtGetChild(0).jjtAccept(this, data);
    107     }
    108
    109     public Object visit(ASTExpression node, Object data) {
    110         return node.jjtGetChild(0).jjtAccept(this, data);
    111     }
    112
    113     // ============ Logical operators =============
    114     public Object visit(ASTOr node, Object data) {
    115         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    116             List args = (List) node.jjtGetChild(i).jjtAccept(this, data);
    117             //((List) data).addAll(args);
    118         }
    119         return data;
    120     }
    121
    122     public Object visit(ASTAnd node, Object data) {
    123         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    124             node.jjtGetChild(i).jjtAccept(this, data);
    125         }
    126         return data;
    127     }
    128
    129     public Object visit(ASTNot node, Object data) {
    130         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    131             node.jjtGetChild(i).jjtAccept(this, data);
    132         }
    133         return data;
    134     }
    135
    136     // ============ Pointcut types =============
    137     public Object visit(ASTPointcutReference node, Object data) {
    138         // visit the args - if any
    139         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    140             node.jjtGetChild(i).jjtAccept(this, data);
    141         }
    142         return data;
    143     }
    144
    145     public Object visit(ASTExecution node, Object data) {
    146         return data;
    147     }
    148
    149     public Object visit(ASTCall node, Object data) {
    150         return data;
    151     }
    152
    153     public Object visit(ASTSet node, Object data) {
    154         return data;
    155     }
    156
    157     public Object visit(ASTGet node, Object data) {
    158         return data;
    159     }
    160
    161     public Object visit(ASTHandler node, Object data) {
    162         return data;
    163     }
    164
    165     public Object visit(ASTStaticInitialization node, Object data) {
    166         return data;
    167     }
    168
    169     public Object visit(ASTWithin node, Object data) {
    170         return data;
    171     }
    172
    173     public Object visit(ASTWithinCode node, Object data) {
    174         return data;
    175     }
    176
    177
    178     public Object visit(ASTHasMethod node, Object data) {
    179         return data;
    180     }
    181
    182     public Object visit(ASTHasField node, Object data) {
    183         return data;
    184     }
    185
    186     public Object visit(ASTCflow node, Object data) {
    187         // visit the sub expression
    188         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    189             node.jjtGetChild(i).jjtAccept(this, data);
    190         }
    191         return data;
    192     }
    193
    194     public Object visit(ASTCflowBelow node, Object data) {
    195         // visit the sub expression
    196         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    197             node.jjtGetChild(i).jjtAccept(this, data);
    198         }
    199         return data;
    200     }
    201
    202     public Object visit(ASTTarget node, Object data) {
    203         ((List) data).add(node.getIdentifier());
    204         return data;
    205     }
    206
    207     public Object visit(ASTThis node, Object data) {
    208         ((List) data).add(node.getIdentifier());
    209         return data;
    210     }
    211
    212     // ============ Patterns =============
    213     public Object visit(ASTClassPattern node, Object data) {
    214         return data;
    215     }
    216
    217     public Object visit(ASTMethodPattern node, Object data) {
    218         return data;
    219     }
    220
    221     public Object visit(ASTConstructorPattern node, Object data) {
    222         return data;
    223     }
    224
    225     public Object visit(ASTFieldPattern node, Object data) {
    226         return data;
    227     }
    228
    229     public Object visit(ASTParameter node, Object data) {
    230         return data;
    231     }
    232
    233     public Object visit(ASTArgs node, Object data) {
    234         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    235             List args = (List) node.jjtGetChild(i).jjtAccept(this, data);
    236             ((List) data).addAll(args);
    237         }
    238         return data;
    239     }
    240
    241     public Object visit(ASTArgParameter node, Object data) {
    242         TypePattern typePattern = node.getTypePattern();
    243         ((List) data).add(typePattern.getPattern());
    244         return data;
    245     }
    246
    247     public Object visit(ASTAttribute node, Object data) {
    248         return data;
    249     }
    250
    251     public Object visit(ASTModifier node, Object data) {
    252         return data;
    253     }
    254
    255     /***
    256      * Returns the string representation of the expression.
    257      *
    258      * @return
    259      */
    260     public String toString() {
    261         return m_expression;
    262     }
    263
    264 }