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

The following examples show how to use org.codehaus.groovy.ast.FieldNode#setInitialValueExpression() . 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: SourceURIASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void setScriptURIOnField(final FieldNode fieldNode, final AnnotationNode node) {
    if (fieldNode.hasInitialExpression()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", fieldNode);
        return;
    }

    URI uri = getSourceURI(node);

    if (uri == null) {
        addError("Unable to get the URI for the source of this class!", fieldNode);
    } else {
        // Set the RHS to '= URI.create("string for this URI")'.
        // That may throw an IllegalArgumentExpression wrapping the URISyntaxException.
        fieldNode.setInitialValueExpression(getExpression(uri));
    }
}
 
Example 2
Source File: GeneralUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static Statement createConstructorStatementDefault(final FieldNode fNode) {
    final String name = fNode.getName();
    final ClassNode fType = fNode.getType();
    final Expression fieldExpr = propX(varX("this"), name);
    Expression initExpr = fNode.getInitialValueExpression();
    Statement assignInit;
    if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
        if (ClassHelper.isPrimitiveType(fType)) {
            assignInit = EmptyStatement.INSTANCE;
        } else {
            assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
        }
    } else {
        assignInit = assignS(fieldExpr, initExpr);
    }
    fNode.setInitialValueExpression(null);
    Expression value = findArg(name);
    return ifElseS(equalsNullX(value), assignInit, assignS(fieldExpr, castX(fType, value)));
}
 
Example 3
Source File: DetectorTransform.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void visit(ASTNode[] nodes, SourceUnit source) {
  if (nodes.length == 0 || !(nodes[0] instanceof ModuleNode)) {
    source.getErrorCollector().addError(new SimpleMessage(
      "internal error in DetectorTransform", source));
    return;
  }
  ModuleNode module = (ModuleNode)nodes[0];
  for (ClassNode clazz : (List<ClassNode>)module.getClasses()) {
    FieldNode field = clazz.getField(VERSION_FIELD_NAME);
    if (field != null) {
      field.setInitialValueExpression(new ConstantExpression(ReleaseInfo.getVersion()));
      break;
    }
  }
}
 
Example 4
Source File: DetectorTransform.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void visit(ASTNode[] nodes, SourceUnit source) {
  if (nodes.length == 0 || !(nodes[0] instanceof ModuleNode)) {
    source.getErrorCollector().addError(new SimpleMessage(
      "internal error in DetectorTransform", source));
    return;
  }
  ModuleNode module = (ModuleNode)nodes[0];
  for (ClassNode clazz : (List<ClassNode>)module.getClasses()) {
    FieldNode field = clazz.getField(VERSION_FIELD_NAME);
    if (field != null) {
      field.setInitialValueExpression(new ConstantExpression(ReleaseInfo.getVersion()));
      break;
    }
  }
}
 
Example 5
Source File: LazyASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Expression getInitExpr(ErrorCollecting xform, FieldNode fieldNode) {
    Expression initExpr = fieldNode.getInitialValueExpression();
    fieldNode.setInitialValueExpression(null);

    if (initExpr == null || initExpr instanceof EmptyExpression) {
        if (fieldNode.getType().isAbstract()) {
            xform.addError("You cannot lazily initialize '" + fieldNode.getName() + "' from the abstract class '" +
                    fieldNode.getType().getName() + "'", fieldNode);
        }
        initExpr = ctorX(fieldNode.getType());
    }

    return initExpr;
}
 
Example 6
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 7
Source File: DetectorTransform.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void visit(ASTNode[] nodes, SourceUnit source) {
  if (nodes.length == 0 || !(nodes[0] instanceof ModuleNode)) {
    source.getErrorCollector().addError(new SimpleMessage(
      "internal error in DetectorTransform", source));
    return;
  }
  ModuleNode module = (ModuleNode)nodes[0];
  for (ClassNode clazz : (List<ClassNode>)module.getClasses()) {
    FieldNode field = clazz.getField(VERSION_FIELD_NAME);
    if (field != null) {
      field.setInitialValueExpression(new ConstantExpression(ReleaseInfo.getVersion()));
      break;
    }
  }
}
 
Example 8
Source File: DetectorTransform.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void visit(ASTNode[] nodes, SourceUnit source) {
  if (nodes.length == 0 || !(nodes[0] instanceof ModuleNode)) {
    source.getErrorCollector().addError(new SimpleMessage(
      "internal error in DetectorTransform", source));
    return;
  }
  ModuleNode module = (ModuleNode)nodes[0];
  for (ClassNode clazz : (List<ClassNode>)module.getClasses()) {
    FieldNode field = clazz.getField(VERSION_FIELD_NAME);
    if (field != null) {
      field.setInitialValueExpression(new ConstantExpression(ReleaseInfo.getVersion()));
      break;
    }
  }
}
 
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);
        }
    }
}