Package japa.parser.ast.expr

Examples of japa.parser.ast.expr.Expression


            if (fd.getVariables() == null || fd.getVariables().size() != 1) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not return a field declaration correctly");
            }

            final Expression init = fd.getVariables().get(0).getInit();

            // Resolve imports (ROO-1505)
            if (init instanceof ObjectCreationExpr) {
                final ObjectCreationExpr ocr = (ObjectCreationExpr) init;
                final JavaType typeToImport = JavaParserUtils.getJavaTypeNow(
View Full Code Here


        }

        fieldName = new JavaSymbolName(var.getId().getName());

        // Lookup initializer, if one was requested and easily determinable
        final Expression e = var.getInit();
        if (e != null) {
            fieldInitializer = e.toString();
        }

        final List<AnnotationExpr> annotations = fieldDeclaration
                .getAnnotations();
        if (annotations != null) {
View Full Code Here

            annotationExpression = new MarkerAnnotationExpr(nameToUse);
        }
        else if (memberValuePairs.size() == 1
                && (memberValuePairs.get(0).getName() == null || "value"
                        .equals(memberValuePairs.get(0).getName()))) {
            final Expression toUse = JavaParserUtils
                    .importExpressionIfRequired(
                            compilationUnitServices.getEnclosingTypeName(),
                            compilationUnitServices.getImports(),
                            memberValuePairs.get(0).getValue());
            annotationExpression = new SingleMemberAnnotationExpr(nameToUse,
                    toUse);
        }
        else {
            // We have a number of pairs being presented
            annotationExpression = new NormalAnnotationExpr(nameToUse,
                    new ArrayList<MemberValuePair>());
        }

        // Add our AnnotationExpr to the actual annotations that will eventually
        // be flushed through to the compilation unit
        annotations.add(annotationExpression);

        // Add member-value pairs to our AnnotationExpr
        if (!memberValuePairs.isEmpty()) {
            // Have to check here for cases where we need to change an existing
            // MarkerAnnotationExpr to a NormalAnnotationExpr or
            // SingleMemberAnnotationExpr
            if (annotationExpression instanceof MarkerAnnotationExpr) {
                final MarkerAnnotationExpr mae = (MarkerAnnotationExpr) annotationExpression;

                annotations.remove(mae);

                if (memberValuePairs.size() == 1
                        && (memberValuePairs.get(0).getName() == null || "value"
                                .equals(memberValuePairs.get(0).getName()))) {
                    final Expression toUse = JavaParserUtils
                            .importExpressionIfRequired(compilationUnitServices
                                    .getEnclosingTypeName(),
                                    compilationUnitServices.getImports(),
                                    memberValuePairs.get(0).getValue());
                    annotationExpression = new SingleMemberAnnotationExpr(
                            nameToUse, toUse);
                    annotations.add(annotationExpression);
                }
                else {
                    // We have a number of pairs being presented
                    annotationExpression = new NormalAnnotationExpr(nameToUse,
                            new ArrayList<MemberValuePair>());
                    annotations.add(annotationExpression);
                }
            }
            if (annotationExpression instanceof SingleMemberAnnotationExpr) {
                // Potentially upgrade this expression to a NormalAnnotationExpr
                final SingleMemberAnnotationExpr smae = (SingleMemberAnnotationExpr) annotationExpression;
                if (memberValuePairs.size() == 1
                        && memberValuePairs.get(0).getName() == null
                        || memberValuePairs.get(0).getName().equals("value")
                        || memberValuePairs.get(0).getName().equals("")) {
                    // They specified only a single member-value pair, and it is
                    // the default anyway, so we need not do anything except
                    // update the value
                    final Expression toUse = JavaParserUtils
                            .importExpressionIfRequired(compilationUnitServices
                                    .getEnclosingTypeName(),
                                    compilationUnitServices.getImports(),
                                    memberValuePairs.get(0).getValue());
                    smae.setMemberValue(toUse);
                    return;
                }

                // There is > 1 expression, or they have provided some sort of
                // non-default value, so it's time to upgrade the expression
                // (whilst retaining any potentially existing expression values)
                final Expression existingValue = smae.getMemberValue();
                annotationExpression = new NormalAnnotationExpr(smae.getName(),
                        new ArrayList<MemberValuePair>());
                ((NormalAnnotationExpr) annotationExpression).getPairs().add(
                        new MemberValuePair("value", existingValue));
            }
            Validate.isInstanceOf(
                    NormalAnnotationExpr.class,
                    annotationExpression,
                    "Attempting to add >1 annotation member-value pair requires an existing normal annotation expression");
            final List<MemberValuePair> annotationPairs = ((NormalAnnotationExpr) annotationExpression)
                    .getPairs();
            annotationPairs.clear();
            for (final MemberValuePair pair : memberValuePairs) {
                final Expression toUse = JavaParserUtils
                        .importExpressionIfRequired(
                                compilationUnitServices.getEnclosingTypeName(),
                                compilationUnitServices.getImports(),
                                pair.getValue());
                pair.setValue(toUse);
View Full Code Here

        if (expression instanceof FieldAccessExpr) {
            final FieldAccessExpr field = (FieldAccessExpr) expression;
            final String fieldName = field.getField();

            // Determine the type
            final Expression scope = field.getScope();
            NameExpr nameToFind = null;
            if (scope instanceof FieldAccessExpr) {
                final FieldAccessExpr fScope = (FieldAccessExpr) scope;
                nameToFind = JavaParserUtils.getNameExpr(fScope.toString());
            }
View Full Code Here

        Validate.notNull(targetType, "Target type required");
        Validate.notNull(imports, "Imports required");
        Validate.notNull(value, "Expression value required");

        if (value instanceof FieldAccessExpr) {
            final Expression scope = ((FieldAccessExpr) value).getScope();
            final String field = ((FieldAccessExpr) value).getField();
            if (scope instanceof QualifiedNameExpr) {
                final String packageName = ((QualifiedNameExpr) scope)
                        .getQualifier().getName();
                final String simpleName = ((QualifiedNameExpr) scope).getName();
View Full Code Here

  private void printArguments(List<Expression> args, Object arg) {
    printer.print("(");
    if (args != null) {
      for (Iterator<Expression> i = args.iterator(); i.hasNext();) {
        Expression e = i.next();
        e.accept(this, arg);
        if (i.hasNext()) {
          printer.print(", ");
        }
      }
    }
View Full Code Here

  public void visit(ArrayInitializerExpr n, Object arg) {
    printer.print("{");
    if (n.getValues() != null) {
      printer.print(" ");
      for (Iterator<Expression> i = n.getValues().iterator(); i.hasNext();) {
        Expression expr = i.next();
        expr.accept(this, arg);
        if (i.hasNext()) {
          printer.print(", ");
        }
      }
      printer.print(" ");
View Full Code Here

  public void visit(ForStmt n, Object arg) {
    printer.print("for (");
    if (n.getInit() != null) {
      for (Iterator<Expression> i = n.getInit().iterator(); i.hasNext();) {
        Expression e = i.next();
        e.accept(this, arg);
        if (i.hasNext()) {
          printer.print(", ");
        }
      }
    }
    printer.print("; ");
    if (n.getCompare() != null) {
      n.getCompare().accept(this, arg);
    }
    printer.print("; ");
    if (n.getUpdate() != null) {
      for (Iterator<Expression> i = n.getUpdate().iterator(); i.hasNext();) {
        Expression e = i.next();
        e.accept(this, arg);
        if (i.hasNext()) {
          printer.print(", ");
        }
      }
    }
View Full Code Here

    private void printArguments(List<Expression> args, Object arg) {
        printer.print("(");
        if (args != null) {
            for (Iterator<Expression> i = args.iterator(); i.hasNext();) {
                Expression e = i.next();
                e.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
View Full Code Here

    public void visit(ArrayInitializerExpr n, Object arg) {
        printer.print("{");
        if (n.getValues() != null) {
            printer.print(" ");
            for (Iterator<Expression> i = n.getValues().iterator(); i.hasNext();) {
                Expression expr = i.next();
                expr.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
            printer.print(" ");
View Full Code Here

TOP

Related Classes of japa.parser.ast.expr.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.