public String[] findNamedQueries(DomainClass domainClass) {
   if (domainClass.getGroovyClass() == null) {
     return NO_QUERIES;
   }
   String name = domainClass.getGroovyClass().getName();
   String[] queries = domainClassToQueries.get(name);
   if (queries == null) {
     queries = ensureInitialized(domainClass.getGroovyClass());
     synchronized (GrailsCore.get().getLockForProject(project)) {
       domainClassToQueries.put(name, queries);
     }
   }
   return queries;
 }
Ejemplo n.º 2
0
  private AnnotatedNode createMethodDeclaration(
      String finderName,
      String[] props,
      String[] comparators,
      String newPropName,
      String newComparator) {
    // need to determine number of parameters

    // first figure out how many properties we need to look at
    int numArgs = -1;
    if (newPropName != null) {
      for (int i = 0; i < props.length; i++) {
        if (props[i] == null) {
          props[i] = newPropName;
          numArgs = i + 1;
          break;
        }
      }
    }
    if (newComparator != null) {
      for (int i = 0; i < comparators.length; i++) {
        if (comparators[i] == null) {
          comparators[i] = newComparator;
          numArgs = i + 1;
          break;
        }
      }
    }

    List<Parameter> params = new ArrayList<Parameter>(2);
    // now go through each component and comparator.
    // determine the kind of parameters they require
    for (int i = 0; i < numArgs; i++) {
      ClassNode[] classNodes = COMPARATOR_ARGUMENT_MAP.get(comparators[i]);
      if (classNodes.length > 0) {
        String uncapitalized = uncapitalize(props[i]);
        params.add(new Parameter(classNodes[0], uncapitalized));
        if (classNodes.length == 2) {
          params.add(new Parameter(classNodes[1], uncapitalized + "1"));
        }
      }
    }

    MethodNode method =
        new MethodNode(
            finderName,
            Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC,
            createReturnType(finderName),
            params.toArray(new Parameter[params.size()]),
            NO_EXCEPTIONS,
            EMPTY_BLOCK);
    if (domain != null) {
      method.setDeclaringClass(domain.getGroovyClass());
    } else {
      method.setDeclaringClass(VariableScope.OBJECT_CLASS_NODE);
    }
    return method;
  }
Ejemplo n.º 3
0
 private Set<String> generateDomainProperties(DomainClass domain) {
   Assert.isNotNull(domain, "Domain class should not be null");
   List<PropertyNode> domainPropertiesNodes = domain.getDomainProperties();
   Set<String> properties = new HashSet<String>(domainPropertiesNodes.size() * 2);
   for (PropertyNode property : domainPropertiesNodes) {
     properties.add(capitalize(property.getName()));
   }
   return properties;
 }
Ejemplo n.º 4
0
 /**
  * Creates a valid declaration based on the name passed in
  *
  * @param finderName the name of the dynamic finder
  * @return A field declaration corresponding to the dynamic finder return a field and not a method
  *     so that components can be built up more easily
  */
 public FieldNode createFieldDeclaration(String finderName) {
   ClassNode declaring = domain != null ? domain.getGroovyClass() : null;
   if (declaring == null) {
     declaring = VariableScope.OBJECT_CLASS_NODE;
   }
   FieldNode field =
       new FieldNode(
           finderName,
           Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
           createReturnType(finderName),
           declaring,
           null);
   field.setDeclaringClass(declaring);
   return field;
 }
Ejemplo n.º 5
0
 /**
  * @param finderName
  * @return
  */
 private ClassNode createReturnType(String finderName) {
   if (finderName.startsWith("countBy")) {
     return VariableScope.INTEGER_CLASS_NODE;
   } else {
     ClassNode groovyClass = domain != null ? domain.getGroovyClass() : null;
     if (groovyClass == null) {
       groovyClass = VariableScope.OBJECT_CLASS_NODE;
     }
     if (finderName.startsWith("findAllBy") || finderName.startsWith("listOrderBy")) {
       ClassNode list = VariableScope.clonedList();
       ClassNode thisClass = groovyClass;
       list.getGenericsTypes()[0].setType(thisClass);
       list.getGenericsTypes()[0].setName(thisClass.getName());
       list.getGenericsTypes()[0].setUpperBounds(null);
       return list;
     } else {
       return groovyClass;
     }
   }
 }