Java Code Examples for org.codehaus.groovy.ast.FieldNode#getInitialExpression()

The following examples show how to use org.codehaus.groovy.ast.FieldNode#getInitialExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void visitField(FieldNode node) {
    if (sameVariableName(leaf, node)) {
        if (node.hasInitialExpression()){
            Expression expression = node.getInitialExpression();
            if (expression instanceof ConstantExpression
                    && !expression.getText().equals("null")) { // NOI18N
                guessedType = ((ConstantExpression) expression).getType();
            } else if (expression instanceof ConstructorCallExpression) {
                guessedType = ((ConstructorCallExpression) expression).getType();
            } else if (expression instanceof MethodCallExpression) {
                int newOffset = ASTUtils.getOffset(doc, expression.getLineNumber(), expression.getColumnNumber());
                AstPath newPath = new AstPath(path.root(), newOffset, doc);
                guessedType = MethodInference.findCallerType(expression, newPath, doc, newOffset);
            }
        }
    }
}
 
Example 2
Source File: TupleConstructorASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Parameter createParam(FieldNode fNode, String name, boolean defaults, AbstractASTTransformation xform, boolean makeImmutable) {
    Parameter param = new Parameter(fNode.getType(), name);
    if (defaults) {
        param.setInitialExpression(providedOrDefaultInitialValue(fNode));
    } else if (!makeImmutable) {
        // TODO we could support some default vals provided they were listed last
        if (fNode.getInitialExpression() != null) {
            xform.addError("Error during " + MY_TYPE_NAME + " processing, default value processing disabled but default value found for '" + fNode.getName() + "'", fNode);
        }
    }
    return param;
}
 
Example 3
Source File: TupleConstructorASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Expression providedOrDefaultInitialValue(FieldNode fNode) {
    Expression initialExp = fNode.getInitialExpression() != null ? fNode.getInitialExpression() : nullX();
    final ClassNode paramType = fNode.getType();
    if (ClassHelper.isPrimitiveType(paramType) && isNull(initialExp)) {
        initialExp = primitivesInitialValues.get(paramType.getTypeClass());
    }
    return initialExp;
}
 
Example 4
Source File: InnerClassVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitProperty(PropertyNode node) {
    final FieldNode field = node.getField();
    final Expression init = field.getInitialExpression();
    field.setInitialValueExpression(null);
    super.visitProperty(node);
    field.setInitialValueExpression(init);
}
 
Example 5
Source File: ImmutablePropertyHandler.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Statement createPropInit(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, PropertyNode pNode, Parameter namedArgsMap) {
    FieldNode fNode = pNode.getField();
    if (fNode.isFinal() && fNode.isStatic()) return null;
    if (fNode.isFinal() && fNode.getInitialExpression() != null) {
        return checkFinalArgNotOverridden(cNode, fNode);
    }
    return createConstructorStatement(xform, cNode, pNode, namedArgsMap);
}
 
Example 6
Source File: LegacyHashMapPropertyHandler.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Statement createPropInit(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, PropertyNode pNode, Parameter namedArgsMap) {
    FieldNode fNode = pNode.getField();
    if (fNode.isFinal() && fNode.isStatic()) return null;
    if (fNode.isFinal() && fNode.getInitialExpression() != null) {
        return checkFinalArgNotOverridden(cNode, fNode);
    }
    return createLegacyConstructorStatementMapSpecial(fNode);
}
 
Example 7
Source File: AutoFinalASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void processField(FieldNode fNode, ClassCodeVisitorSupport visitor) {
    if (!isEnabled(fNode)) return;
    if (fNode.hasInitialExpression() && fNode.getInitialExpression() instanceof ClosureExpression) {
        visitor.visitField(fNode);
    }
}
 
Example 8
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static List<PropertyNode> getAllProperties(final Set<String> names, final ClassNode origType, final ClassNode cNode, final boolean includeProperties,
                                                  final boolean includeFields, final boolean includePseudoGetters, final boolean includePseudoSetters,
                                                  final boolean traverseSuperClasses, final boolean skipReadonly, final boolean reverse, final boolean allNames, final boolean includeStatic) {
    List<PropertyNode> result = new ArrayList<>();
    if (cNode != ClassHelper.OBJECT_TYPE && traverseSuperClasses && !reverse) {
        result.addAll(getAllProperties(names, origType, cNode.getSuperClass(), includeProperties, includeFields, includePseudoGetters, includePseudoSetters, true, skipReadonly));
    }
    if (includeProperties) {
        for (PropertyNode pNode : cNode.getProperties()) {
            if ((!pNode.isStatic() || includeStatic) && !names.contains(pNode.getName())) {
                result.add(pNode);
                names.add(pNode.getName());
            }
        }
        if (includePseudoGetters || includePseudoSetters) {
            BeanUtils.addPseudoProperties(origType, cNode, result, names, includeStatic, includePseudoGetters, includePseudoSetters);
        }
    }
    if (includeFields) {
        for (FieldNode fNode : cNode.getFields()) {
            if ((fNode.isStatic() && !includeStatic) || fNode.isSynthetic() || cNode.getProperty(fNode.getName()) != null || names.contains(fNode.getName())) {
                continue;
            }

            // internal field
            if (fNode.getName().contains("$") && !allNames) {
                continue;
            }

            if (fNode.isPrivate() && !cNode.equals(origType)) {
                continue;
            }
            if (fNode.isFinal() && fNode.getInitialExpression() != null && skipReadonly) {
                continue;
            }
            result.add(new PropertyNode(fNode, fNode.getModifiers(), null, null));
            names.add(fNode.getName());
        }
    }
    if (cNode != ClassHelper.OBJECT_TYPE && traverseSuperClasses && reverse) {
        result.addAll(getAllProperties(names, origType, cNode.getSuperClass(), includeProperties, includeFields, includePseudoGetters, includePseudoSetters, true, skipReadonly));
    }
    return result;
}
 
Example 9
Source File: Verifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode,
                                      boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
    Expression expression = fieldNode.getInitialExpression();
    if (expression != null) {
        final FieldExpression fe = new FieldExpression(fieldNode);
        if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & ACC_SYNTHETIC) != 0)) {
            fe.setUseReferenceDirectly(true);
        }
        ExpressionStatement statement =
                new ExpressionStatement(
                        new BinaryExpression(
                                fe,
                                Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()),
                                expression));
        if (fieldNode.isStatic()) {
            // GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
            // initialized first so that code dependent on it does not see their values as empty
            Expression initialValueExpression = fieldNode.getInitialValueExpression();
            Expression transformed = transformInlineConstants(initialValueExpression, fieldNode.getType());
            if (transformed instanceof ConstantExpression) {
                ConstantExpression cexp = (ConstantExpression) transformed;
                cexp = transformToPrimitiveConstantIfPossible(cexp);
                if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
                    fieldNode.setInitialValueExpression(transformed);
                    return; // GROOVY-5150: primitive type constants will be initialized directly
                }
                staticList.add(0, statement);
            } else {
                staticList.add(statement);
            }
            fieldNode.setInitialValueExpression(null); // to avoid double initialization in case of several constructors
            /*
             * If it is a statement for an explicitly declared static field inside an enum, store its
             * reference. For enums, they need to be handled differently as such init statements should
             * come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
             */
            if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
                initStmtsAfterEnumValuesInit.add(statement);
            }
        } else {
            list.add(statement);
        }
    }
}
 
Example 10
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression expression) {
    boolean oldInSpecialCtorFlag = inSpecialConstructorCall;
    inSpecialConstructorCall |= expression.isSpecialCall();
    super.visitConstructorCallExpression(expression);
    inSpecialConstructorCall = oldInSpecialCtorFlag;

    if (!expression.isUsingAnonymousInnerClass()) return;

    pushState();
    InnerClassNode innerClass = (InnerClassNode) expression.getType();
    innerClass.setVariableScope(currentScope);
    currentScope.setClassScope(innerClass);
    currentScope.setInStaticContext(false);
    for (MethodNode method : innerClass.getMethods()) {
        Parameter[] parameters = method.getParameters();
        if (parameters.length == 0) {
            parameters = null; // null means no implicit "it"
        }
        visitClosureExpression(new ClosureExpression(parameters, method.getCode()));
    }

    for (FieldNode field : innerClass.getFields()) {
        Expression initExpression = field.getInitialExpression();
        pushState(field.isStatic());
        if (initExpression != null) {
            if (initExpression.isSynthetic() && initExpression instanceof VariableExpression
                    && ((VariableExpression) initExpression).getAccessedVariable() instanceof Parameter) {
                // GROOVY-6834: accessing a parameter which is not yet seen in scope
                popState();
                continue;
            }
            initExpression.visit(this);
        }
        popState();
    }

    for (Statement initStatement : innerClass.getObjectInitializerStatements()) {
        initStatement.visit(this);
    }
    markClosureSharedVariables();
    popState();
}