Search in sources :

Example 6 with FieldNode

use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.

the class AsmClassGenerator method storeThisInstanceField.

private void storeThisInstanceField(FieldExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    boolean setReferenceFromReference = field.isHolder() && expression.isUseReferenceDirectly();
    String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner());
    OperandStack operandStack = controller.getOperandStack();
    if (setReferenceFromReference) {
        // rhs is ready to use reference, just put it in the field
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
    } else if (field.isHolder()) {
        // rhs is normal value, set the value in the Reference
        operandStack.doGroovyCast(field.getOriginType());
        operandStack.box();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
    } else {
        // rhs is normal value, set normal value
        operandStack.doGroovyCast(field.getOriginType());
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
    }
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 7 with FieldNode

use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.

the class EnumVisitor method completeEnum.

private void completeEnum(ClassNode enumClass) {
    boolean isAic = isAnonymousInnerClass(enumClass);
    // create MIN_VALUE and MAX_VALUE fields
    FieldNode minValue = null, maxValue = null, values = null;
    if (!isAic) {
        ClassNode enumRef = enumClass.getPlainNodeReference();
        // create values field
        values = new FieldNode("$VALUES", PRIVATE_FS | Opcodes.ACC_SYNTHETIC, enumRef.makeArray(), enumClass, null);
        values.setSynthetic(true);
        addMethods(enumClass, values);
        checkForAbstractMethods(enumClass);
        // create MIN_VALUE and MAX_VALUE fields
        minValue = new FieldNode("MIN_VALUE", PUBLIC_FS, enumRef, enumClass, null);
        maxValue = new FieldNode("MAX_VALUE", PUBLIC_FS, enumRef, enumClass, null);
    }
    addInit(enumClass, minValue, maxValue, values, isAic);
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode)

Example 8 with FieldNode

use of org.codehaus.groovy.ast.FieldNode in project grails-core by grails.

the class DefaultASTDatabindingHelper method addDefaultDatabindingWhitelistField.

private void addDefaultDatabindingWhitelistField(final SourceUnit sourceUnit, final ClassNode classNode) {
    final FieldNode defaultWhitelistField = classNode.getDeclaredField(DEFAULT_DATABINDING_WHITELIST);
    if (defaultWhitelistField != null) {
        return;
    }
    final Set<String> propertyNamesToIncludeInWhiteList = getPropertyNamesToIncludeInWhiteList(sourceUnit, classNode);
    final ListExpression listExpression = new ListExpression();
    if (propertyNamesToIncludeInWhiteList.size() > 0) {
        for (String propertyName : propertyNamesToIncludeInWhiteList) {
            listExpression.addExpression(new ConstantExpression(propertyName));
            final FieldNode declaredField = getDeclaredFieldInInheritanceHierarchy(classNode, propertyName);
            boolean isSimpleType = false;
            if (declaredField != null) {
                final ClassNode type = declaredField.getType();
                if (type != null) {
                    isSimpleType = SIMPLE_TYPES.contains(type);
                }
            }
            if (!isSimpleType) {
                listExpression.addExpression(new ConstantExpression(propertyName + "_*"));
                listExpression.addExpression(new ConstantExpression(propertyName + ".*"));
            }
        }
    } else {
        listExpression.addExpression(new ConstantExpression(NO_BINDABLE_PROPERTIES));
    }
    classNode.addField(DEFAULT_DATABINDING_WHITELIST, Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL, new ClassNode(List.class), listExpression);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with FieldNode

use of org.codehaus.groovy.ast.FieldNode in project grails-core by grails.

the class DefaultASTDatabindingHelper method getPropertyNamesToIncludeInWhiteList.

private Set<String> getPropertyNamesToIncludeInWhiteList(final SourceUnit sourceUnit, final ClassNode 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 = getPropertyNamesToIncludeInWhiteListForParentClass(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 {
                            GrailsASTUtils.warning(sourceUnit, valueExpression, "The bindable constraint for property [" + propertyName + "] in class [" + classNode.getName() + "] has a value which is not a boolean literal and will be ignored.");
                        }
                    }
                }
            }
        }
    }
    final Set<String> fieldsInTransientsList = getPropertyNamesExpressedInTransientsList(classNode);
    propertyNamesToIncludeInWhiteList.addAll(bindablePropertyNames);
    final List<FieldNode> fields = classNode.getFields();
    for (FieldNode fieldNode : fields) {
        final String fieldName = fieldNode.getName();
        final boolean isDomainClass = GrailsASTUtils.isDomainClass(classNode, sourceUnit);
        if ((!unbindablePropertyNames.contains(fieldName)) && (bindablePropertyNames.contains(fieldName) || shouldFieldBeInWhiteList(fieldNode, fieldsInTransientsList, isDomainClass))) {
            propertyNamesToIncludeInWhiteList.add(fieldName);
        }
    }
    final Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
    for (Entry<String, MethodNode> methodEntry : declaredMethodsMap.entrySet()) {
        final MethodNode value = methodEntry.getValue();
        if (classNode.equals(value.getDeclaringClass())) {
            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_NODE_TO_WHITE_LIST_PROPERTY_NAMES.put(classNode, propertyNamesToIncludeInWhiteList);
    Map<String, ClassNode> allAssociationMap = GrailsASTUtils.getAllAssociationMap(classNode);
    for (String associationName : allAssociationMap.keySet()) {
        if (!propertyNamesToIncludeInWhiteList.contains(associationName) && !unbindablePropertyNames.contains(associationName)) {
            propertyNamesToIncludeInWhiteList.add(associationName);
        }
    }
    return propertyNamesToIncludeInWhiteList;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 10 with FieldNode

use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.

the class OptimizerVisitor method setConstField.

private void setConstField(ConstantExpression constantExpression) {
    final Object n = constantExpression.getValue();
    if (!(n instanceof Number))
        return;
    if (n instanceof Integer || n instanceof Double)
        return;
    // LCONST_0, LCONST_1
    if (n instanceof Long && (0L == (Long) n || 1L == (Long) n))
        return;
    FieldNode field = (FieldNode) const2Var.get(n);
    if (field != null) {
        constantExpression.setConstantName(field.getName());
        return;
    }
    final String name = "$const$" + const2Var.size();
    //TODO: this part here needs a bit of rethinking. If it can happen that the field is defined already,
    //      then is this code still valid?
    field = currentClass.getDeclaredField(name);
    if (field == null) {
        field = new FieldNode(name, Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL, constantExpression.getType(), currentClass, constantExpression);
        field.setSynthetic(true);
        missingFields.add(field);
    }
    constantExpression.setConstantName(field.getName());
    const2Var.put(n, field);
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode)

Aggregations

FieldNode (org.codehaus.groovy.ast.FieldNode)204 ClassNode (org.codehaus.groovy.ast.ClassNode)126 Expression (org.codehaus.groovy.ast.expr.Expression)58 MethodNode (org.codehaus.groovy.ast.MethodNode)57 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)54 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)50 Parameter (org.codehaus.groovy.ast.Parameter)49 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)43 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)43 ArrayList (java.util.ArrayList)40 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)38 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)33 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)31 PropertyNode (org.codehaus.groovy.ast.PropertyNode)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)27 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)22 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)15