Package com.github.antlrjavaparser.api.body

Examples of com.github.antlrjavaparser.api.body.VariableDeclarator


        final List<VariableDeclarator> vars = newField.getVariables();
        Validate.notEmpty(vars,
                "Expected ASTHelper to have provided a single VariableDeclarator");
        Validate.isTrue(vars.size() == 1,
                "Expected ASTHelper to have provided a single VariableDeclarator");
        final VariableDeclarator vd = vars.iterator().next();

        if (StringUtils.isNotBlank(field.getFieldInitializer())) {
            // There is an initializer.
            // We need to make a fake field that we can have JavaParser parse.
            // Easiest way to do that is to build a simple source class
            // containing the required field and re-parse it.
            final StringBuilder sb = new StringBuilder();
            sb.append("class TemporaryClass {\n");
            sb.append("  private " + field.getFieldType() + " "
                    + field.getFieldName() + " = "
                    + field.getFieldInitializer() + ";\n");
            sb.append("}\n");
            final ByteArrayInputStream bais = new ByteArrayInputStream(sb
                    .toString().getBytes());
            CompilationUnit ci;
            try {
                ci = JavaParser.parse(bais);
            }
            catch (final IOException e) {
                throw new IllegalStateException(
                        "Illegal state: Unable to parse input stream", e);
            }
            catch (final ParseException pe) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not parse correctly", pe);
            }
            final List<TypeDeclaration> types = ci.getTypes();
            if (types == null || types.size() != 1) {
                throw new IllegalArgumentException("Field member invalid");
            }
            final TypeDeclaration td = types.get(0);
            final List<BodyDeclaration> bodyDeclarations = td.getMembers();
            if (bodyDeclarations == null || bodyDeclarations.size() != 1) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not return body declarations correctly");
            }
            final BodyDeclaration bd = bodyDeclarations.get(0);
            if (!(bd instanceof FieldDeclaration)) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not return a field declaration correctly");
            }
            final FieldDeclaration fd = (FieldDeclaration) bd;
            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(
                        compilationUnitServices, ocr.getType(), null);
                final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(
                        compilationUnitServices.getEnclosingTypeName(),
                        compilationUnitServices.getImports(), typeToImport);
                final ClassOrInterfaceType classOrInterfaceType = JavaParserUtils
                        .getClassOrInterfaceType(nameExpr);
                ocr.setType(classOrInterfaceType);

                if (typeToImport.getParameters().size() > 0) {
                    final List<Type> initTypeArgs = new ArrayList<Type>();
                    finalType.setTypeArgs(initTypeArgs);
                    for (final JavaType parameter : typeToImport
                            .getParameters()) {
                        initTypeArgs.add(JavaParserUtils
                                .importParametersForType(
                                        compilationUnitServices
                                                .getEnclosingTypeName(),
                                        compilationUnitServices.getImports(),
                                        parameter));
                    }
                    classOrInterfaceType.setTypeArgs(initTypeArgs);
                }
            }

            vd.setInit(init);
        }

        // Add annotations
        final List<AnnotationExpr> annotations = new ArrayList<AnnotationExpr>();
        newField.setAnnotations(annotations);
View Full Code Here


                field = (FieldDeclaration) originalMember;

                // Check every variable declared in definition
                for (final Iterator<VariableDeclarator> variablesIter = field
                        .getVariables().iterator(); variablesIter.hasNext();) {
                    final VariableDeclarator originalVariable = variablesIter
                            .next();
                    fieldName = originalVariable.getId().getName();

                    // look for field name in cid
                    final FieldEntry entry = cidFields.get(fieldName);
                    if (entry == null) {
                        // Not found: remove field from original compilation
View Full Code Here

TOP

Related Classes of com.github.antlrjavaparser.api.body.VariableDeclarator

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.