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; }
public void visitConstructorCallExpression(ConstructorCallExpression call) { isSpecialConstructorCall = call.isSpecialCall(); super.visitConstructorCallExpression(call); isSpecialConstructorCall = false; if (!call.isUsingAnonymousInnerClass()) return; pushState(); InnerClassNode innerClass = (InnerClassNode) call.getType(); innerClass.setVariableScope(currentScope); for (MethodNode method : innerClass.getMethods()) { Parameter[] parameters = method.getParameters(); if (parameters.length == 0) parameters = null; // null means no implicit "it" ClosureExpression cl = new ClosureExpression(parameters, method.getCode()); visitClosureExpression(cl); } for (FieldNode field : innerClass.getFields()) { final Expression expression = field.getInitialExpression(); if (expression != null) { expression.visit(this); } } for (Statement statement : innerClass.getObjectInitializerStatements()) { statement.visit(this); } markClosureSharedVariables(); popState(); }
private void createConstructorMapCommon(ClassNode cNode, BlockStatement body) { final List<FieldNode> fList = cNode.getFields(); for (FieldNode fNode : fList) { if (fNode.isPublic()) continue; // public fields will be rejected elsewhere if (cNode.getProperty(fNode.getName()) != null) continue; // a property if (fNode.isFinal() && fNode.isStatic()) continue; if (fNode.getName().contains("$")) continue; // internal field if (fNode.isFinal() && fNode.getInitialExpression() != null) body.addStatement(checkFinalArgNotOverridden(cNode, fNode)); body.addStatement(createConstructorStatementDefault(fNode)); } final Parameter[] params = new Parameter[] {new Parameter(HASHMAP_TYPE, "args")}; doAddConstructor( cNode, new ConstructorNode( ACC_PUBLIC, params, ClassNode.EMPTY_ARRAY, new IfStatement( equalsNullExpr(new VariableExpression("args")), new EmptyStatement(), body))); }
private Set<String> getPropertyNamesToIncludeInWhiteList( final SourceUnit sourceUnit, final ClassNode classNode) { if (CLASS_NAME_TO_WHITE_LIST_PROPERTY_NAMES.containsKey(classNode)) { return CLASS_NAME_TO_WHITE_LIST_PROPERTY_NAMES.get(classNode); } final Set<String> propertyNamesToIncludeInWhiteList = new HashSet<String>(); final Set<String> unbindablePropertyNames = new HashSet<String>(); final Set<String> bindablePropertyNames = new HashSet<String>(); if (!classNode.getSuperClass().equals(new ClassNode(Object.class))) { final Set<String> parentClassPropertyNames = getPropertyNamesToIncludeInWhiteList(sourceUnit, classNode.getSuperClass()); bindablePropertyNames.addAll(parentClassPropertyNames); } final FieldNode constraintsFieldNode = classNode.getDeclaredField(CONSTRAINTS_FIELD_NAME); if (constraintsFieldNode != null && constraintsFieldNode.hasInitialExpression()) { final Expression constraintsInitialExpression = constraintsFieldNode.getInitialExpression(); if (constraintsInitialExpression instanceof ClosureExpression) { final Map<String, Map<String, Expression>> constraintsInfo = GrailsASTUtils.getConstraintMetadata((ClosureExpression) constraintsInitialExpression); for (Entry<String, Map<String, Expression>> constraintConfig : constraintsInfo.entrySet()) { final String propertyName = constraintConfig.getKey(); final Map<String, Expression> mapEntryExpressions = constraintConfig.getValue(); for (Entry<String, Expression> entry : mapEntryExpressions.entrySet()) { final String constraintName = entry.getKey(); if (BINDABLE_CONSTRAINT_NAME.equals(constraintName)) { final Expression valueExpression = entry.getValue(); Boolean bindableValue = null; if (valueExpression instanceof ConstantExpression) { final Object constantValue = ((ConstantExpression) valueExpression).getValue(); if (constantValue instanceof Boolean) { bindableValue = (Boolean) constantValue; } } if (bindableValue != null) { if (Boolean.TRUE.equals(bindableValue)) { unbindablePropertyNames.remove(propertyName); bindablePropertyNames.add(propertyName); } else { bindablePropertyNames.remove(propertyName); unbindablePropertyNames.add(propertyName); } } else { final String message = "The bindable constraint for property [" + propertyName + "] in class [" + classNode.getName() + "] has a value which is not a boolean literal and will be ignored."; GrailsASTUtils.warning(sourceUnit, valueExpression, message); } } } } } } final Set<String> fieldsInTransientsList = getPropertyNamesExpressedInTransientsList(classNode); propertyNamesToIncludeInWhiteList.addAll(bindablePropertyNames); final List<FieldNode> fields = classNode.getFields(); for (FieldNode fieldNode : fields) { final String fieldName = fieldNode.getName(); if ((!unbindablePropertyNames.contains(fieldName)) && (bindablePropertyNames.contains(fieldName) || shouldFieldBeInWhiteList(fieldNode, fieldsInTransientsList))) { propertyNamesToIncludeInWhiteList.add(fieldName); } } final Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap(); for (Entry<String, MethodNode> methodEntry : declaredMethodsMap.entrySet()) { final MethodNode value = methodEntry.getValue(); if (value.getDeclaringClass() == classNode) { Parameter[] parameters = value.getParameters(); if (parameters != null && parameters.length == 1) { final String methodName = value.getName(); if (methodName.startsWith("set")) { final Parameter parameter = parameters[0]; final ClassNode paramType = parameter.getType(); if (!paramType.equals(new ClassNode(Object.class))) { final String restOfMethodName = methodName.substring(3); final String propertyName = GrailsNameUtils.getPropertyName(restOfMethodName); if (!unbindablePropertyNames.contains(propertyName)) { propertyNamesToIncludeInWhiteList.add(propertyName); } } } } } } CLASS_NAME_TO_WHITE_LIST_PROPERTY_NAMES.put(classNode, propertyNamesToIncludeInWhiteList); Map<String, ClassNode> allAssociationMap = GrailsASTUtils.getAllAssociationMap(classNode); for (String associationName : allAssociationMap.keySet()) { if (!propertyNamesToIncludeInWhiteList.contains(associationName)) { propertyNamesToIncludeInWhiteList.add(associationName); } } return propertyNamesToIncludeInWhiteList; }