private String[] ensureInitialized(ClassNode domainClass) {
   FieldNode field = domainClass.getField("namedQueries");
   if (field != null && field.isStatic()) {
     Expression initialExpression = field.getInitialExpression();
     if (initialExpression instanceof ClosureExpression) {
       Statement code = ((ClosureExpression) initialExpression).getCode();
       if (code instanceof BlockStatement) {
         List<Statement> statements = ((BlockStatement) code).getStatements();
         if (statements != null) {
           List<String> namedQueries = new ArrayList<String>(statements.size());
           for (Statement s : statements) {
             if (s instanceof ExpressionStatement) {
               Expression expr = ((ExpressionStatement) s).getExpression();
               if (expr instanceof MethodCallExpression) {
                 MethodCallExpression call = (MethodCallExpression) expr;
                 namedQueries.add(call.getMethodAsString());
               }
             }
           }
           return namedQueries.toArray(NO_QUERIES);
         }
       }
     }
   }
   return NO_QUERIES;
 }
Beispiel #2
0
  /**
   * Evaluates a constraints closure and returns metadata about the constraints configured in the
   * closure. The Map returned has property names as keys and the value associated with each of
   * those property names is a Map<String, Expression> which has constraint names as keys and the
   * Expression associated with that constraint as values
   *
   * @param closureExpression the closure expression to evaluate
   * @return the Map as described above
   */
  public static Map<String, Map<String, Expression>> getConstraintMetadata(
      final ClosureExpression closureExpression) {

    final List<MethodCallExpression> methodExpressions = new ArrayList<MethodCallExpression>();

    final Map<String, Map<String, Expression>> results =
        new LinkedHashMap<String, Map<String, Expression>>();
    final Statement closureCode = closureExpression.getCode();
    if (closureCode instanceof BlockStatement) {
      final List<Statement> closureStatements = ((BlockStatement) closureCode).getStatements();
      for (final Statement closureStatement : closureStatements) {
        if (closureStatement instanceof ExpressionStatement) {
          final Expression expression = ((ExpressionStatement) closureStatement).getExpression();
          if (expression instanceof MethodCallExpression) {
            methodExpressions.add((MethodCallExpression) expression);
          }
        } else if (closureStatement instanceof ReturnStatement) {
          final ReturnStatement returnStatement = (ReturnStatement) closureStatement;
          Expression expression = returnStatement.getExpression();
          if (expression instanceof MethodCallExpression) {
            methodExpressions.add((MethodCallExpression) expression);
          }
        }

        for (final MethodCallExpression methodCallExpression : methodExpressions) {
          final Expression objectExpression = methodCallExpression.getObjectExpression();
          if (objectExpression instanceof VariableExpression
              && "this".equals(((VariableExpression) objectExpression).getName())) {
            final Expression methodCallArguments = methodCallExpression.getArguments();
            if (methodCallArguments instanceof TupleExpression) {
              final List<Expression> methodCallArgumentExpressions =
                  ((TupleExpression) methodCallArguments).getExpressions();
              if (methodCallArgumentExpressions != null
                  && methodCallArgumentExpressions.size() == 1
                  && methodCallArgumentExpressions.get(0) instanceof NamedArgumentListExpression) {
                final Map<String, Expression> constraintNameToExpression =
                    new LinkedHashMap<String, Expression>();
                final List<MapEntryExpression> mapEntryExpressions =
                    ((NamedArgumentListExpression) methodCallArgumentExpressions.get(0))
                        .getMapEntryExpressions();
                for (final MapEntryExpression mapEntryExpression : mapEntryExpressions) {
                  final Expression keyExpression = mapEntryExpression.getKeyExpression();
                  if (keyExpression instanceof ConstantExpression) {
                    final Object value = ((ConstantExpression) keyExpression).getValue();
                    if (value instanceof String) {
                      constraintNameToExpression.put(
                          (String) value, mapEntryExpression.getValueExpression());
                    }
                  }
                }
                results.put(methodCallExpression.getMethodAsString(), constraintNameToExpression);
              }
            }
          }
        }
      }
    }
    return results;
  }
 @Override
 public void visitMethodCallExpression(MethodCallExpression call) {
   String name =
       call.getMethodAsString(); // Could be null (for 'funny' calls where target name is dynamic)
   SearchingCodeVisitor.MethodCallAction action = getMethodCallAction(name);
   if (action != null) {
     Assert.isLegal(action.methodName.equals(name));
     if (!isVisited(call)) {
       action.doit(this, call);
     }
   }
   super.visitMethodCallExpression(call);
 }
 Expression transformMethodCallExpression(final MethodCallExpression expr) {
   Expression objectExpression = expr.getObjectExpression();
   if (expr.isSafe()) {
     MethodCallExpression notSafe =
         new MethodCallExpression(objectExpression, expr.getMethod(), expr.getArguments());
     notSafe.copyNodeMetaData(expr);
     notSafe.setSpreadSafe(expr.isSpreadSafe());
     notSafe.setSourcePosition(expr);
     notSafe.setMethodTarget(expr.getMethodTarget());
     TernaryExpression texpr =
         new TernaryExpression(
             new BooleanExpression(
                 new BinaryExpression(
                     objectExpression,
                     Token.newSymbol(
                         "!=",
                         objectExpression.getLineNumber(),
                         objectExpression.getColumnNumber()),
                     ConstantExpression.NULL)),
             notSafe,
             ConstantExpression.NULL);
     return staticCompilationTransformer.transform(texpr);
   }
   ClassNode type =
       staticCompilationTransformer
           .getTypeChooser()
           .resolveType(objectExpression, staticCompilationTransformer.getClassNode());
   if (type != null && type.isArray()) {
     String method = expr.getMethodAsString();
     ClassNode componentType = type.getComponentType();
     if ("getAt".equals(method)) {
       Expression arguments = expr.getArguments();
       if (arguments instanceof TupleExpression) {
         List<Expression> argList = ((TupleExpression) arguments).getExpressions();
         if (argList.size() == 1) {
           Expression indexExpr = argList.get(0);
           ClassNode argType =
               staticCompilationTransformer
                   .getTypeChooser()
                   .resolveType(indexExpr, staticCompilationTransformer.getClassNode());
           ClassNode indexType = ClassHelper.getWrapper(argType);
           if (componentType.isEnum() && ClassHelper.Number_TYPE == indexType) {
             // workaround for generated code in enums which use .next() returning a Number
             indexType = ClassHelper.Integer_TYPE;
           }
           if (argType != null && ClassHelper.Integer_TYPE == indexType) {
             BinaryExpression binaryExpression =
                 new BinaryExpression(
                     objectExpression,
                     Token.newSymbol("[", indexExpr.getLineNumber(), indexExpr.getColumnNumber()),
                     indexExpr);
             binaryExpression.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, componentType);
             return staticCompilationTransformer.transform(binaryExpression);
           }
         }
       }
     }
     if ("putAt".equals(method)) {
       Expression arguments = expr.getArguments();
       if (arguments instanceof TupleExpression) {
         List<Expression> argList = ((TupleExpression) arguments).getExpressions();
         if (argList.size() == 2) {
           Expression indexExpr = argList.get(0);
           Expression objExpr = argList.get(1);
           ClassNode argType =
               staticCompilationTransformer
                   .getTypeChooser()
                   .resolveType(indexExpr, staticCompilationTransformer.getClassNode());
           if (argType != null && ClassHelper.Integer_TYPE == ClassHelper.getWrapper(argType)) {
             BinaryExpression arrayGet =
                 new BinaryExpression(
                     objectExpression,
                     Token.newSymbol("[", indexExpr.getLineNumber(), indexExpr.getColumnNumber()),
                     indexExpr);
             arrayGet.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, componentType);
             BinaryExpression assignment =
                 new BinaryExpression(
                     arrayGet,
                     Token.newSymbol("=", objExpr.getLineNumber(), objExpr.getColumnNumber()),
                     objExpr);
             return staticCompilationTransformer.transform(assignment);
           }
         }
       }
     }
   }
   return staticCompilationTransformer.superTransform(expr);
 }