Package com.github.antlrjavaparser.api.body

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


                }
            }

            for (final BodyDeclaration member : members) {
                if (member instanceof FieldDeclaration) {
                    final FieldDeclaration castMember = (FieldDeclaration) member;
                    for (final VariableDeclarator var : castMember
                            .getVariables()) {
                        final FieldMetadata field = JavaParserFieldMetadataBuilder
                                .getInstance(declaredByMetadataId, castMember,
                                        var, compilationUnitServices,
                                        typeParameterNames).build();

                        final CommentStructure commentStructure = new CommentStructure();
                        JavaParserCommentMetadataBuilder.updateCommentsToRoo(
                                commentStructure, member);
                        field.setCommentStructure(commentStructure);

                        cidBuilder.addField(field);
                    }
                }
                if (member instanceof MethodDeclaration) {
                    final MethodDeclaration castMember = (MethodDeclaration) member;
                    final MethodMetadata method = JavaParserMethodMetadataBuilder
                            .getInstance(declaredByMetadataId, castMember,
                                    compilationUnitServices, typeParameterNames)
                            .build();

                    final CommentStructure commentStructure = new CommentStructure();
                    JavaParserCommentMetadataBuilder.updateCommentsToRoo(
                            commentStructure, member);
                    method.setCommentStructure(commentStructure);

                    cidBuilder.addMethod(method);
                }
                if (member instanceof ConstructorDeclaration) {
                    final ConstructorDeclaration castMember = (ConstructorDeclaration) member;
                    final ConstructorMetadata constructor = JavaParserConstructorMetadataBuilder
                            .getInstance(declaredByMetadataId, castMember,
                                    compilationUnitServices, typeParameterNames)
                            .build();

                    final CommentStructure commentStructure = new CommentStructure();
                    JavaParserCommentMetadataBuilder.updateCommentsToRoo(
                            commentStructure, member);
                    constructor.setCommentStructure(commentStructure);

                    cidBuilder.addConstructor(constructor);
                }
                if (member instanceof TypeDeclaration) {
                    final TypeDeclaration castMember = (TypeDeclaration) member;
                    final JavaType innerType = new JavaType(
                            castMember.getName(), name);
                    final String innerTypeMetadataId = PhysicalTypeIdentifier
                            .createIdentifier(innerType, PhysicalTypeIdentifier
                                    .getPath(declaredByMetadataId));
                    final ClassOrInterfaceTypeDetails cid = new JavaParserClassOrInterfaceTypeDetailsBuilder(
                            compilationUnit, compilationUnitServices,
View Full Code Here


                compilationUnitServices.getEnclosingTypeName(),
                field.getFieldType(), compilationUnitServices);
        final ClassOrInterfaceType finalType = JavaParserUtils
                .getClassOrInterfaceType(initType);

        final FieldDeclaration newField = ASTHelper.createFieldDeclaration(
                JavaParserUtils.getJavaParserModifier(field.getModifier()),
                initType, field.getFieldName().getSymbolName());

        // Add parameterized types for the field type (not initializer)
        if (field.getFieldType().getParameters().size() > 0) {
            final List<Type> fieldTypeArgs = new ArrayList<Type>();
            finalType.setTypeArgs(fieldTypeArgs);
            for (final JavaType parameter : field.getFieldType()
                    .getParameters()) {
                fieldTypeArgs.add(JavaParserUtils.importParametersForType(
                        compilationUnitServices.getEnclosingTypeName(),
                        compilationUnitServices.getImports(), parameter));
            }
        }

        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);
        for (final AnnotationMetadata annotation : field.getAnnotations()) {
            JavaParserAnnotationMetadataBuilder.addAnnotationToList(
                    compilationUnitServices, annotations, annotation);
        }

        // Locate where to add this field; also verify if this field already
        // exists
        int nextFieldIndex = 0;
        int i = -1;
        for (final BodyDeclaration bd : members) {
            i++;
            if (bd instanceof FieldDeclaration) {
                // Next field should appear after this current field
                nextFieldIndex = i + 1;
                final FieldDeclaration bdf = (FieldDeclaration) bd;
                for (final VariableDeclarator v : bdf.getVariables()) {
                    Validate.isTrue(!field.getFieldName().getSymbolName()
                            .equals(v.getId().getName()),
                            "A field with name '%s' already exists", field
                                    .getFieldName().getSymbolName());
                }
View Full Code Here

        int i = -1;
        int toDelete = -1;
        for (final BodyDeclaration bd : members) {
            i++;
            if (bd instanceof FieldDeclaration) {
                final FieldDeclaration fieldDeclaration = (FieldDeclaration) bd;
                for (final VariableDeclarator var : fieldDeclaration
                        .getVariables()) {
                    if (var.getId().getName().equals(fieldName.getSymbolName())) {
                        toDelete = i;
                        break;
                    }
View Full Code Here

        // Get a map of all fields (as FieldDeclaration could contain more than
        // one field)
        final Map<String, FieldEntry> cidFields = new HashMap<String, FieldEntry>();
        String fieldName;
        FieldDeclaration field;
        if (newType.getMembers() != null) {
            for (final BodyDeclaration element : newType.getMembers()) {
                if (element instanceof FieldDeclaration) {
                    field = (FieldDeclaration) element;
                    for (final VariableDeclarator variable : field
                            .getVariables()) {
                        fieldName = variable.getId().getName();
                        cidFields.put(fieldName,
                                new FieldEntry(field, variable));
                    }
                }
            }
        }

        // Iterate over every field definition
        if (originalType.getMembers() != null) {
            for (final Iterator<BodyDeclaration> originalMemberstIter = originalType
                    .getMembers().iterator(); originalMemberstIter.hasNext();) {
                final BodyDeclaration originalMember = originalMemberstIter
                        .next();
                if (!(originalMember instanceof FieldDeclaration)) {
                    // this is not a field definition
                    continue;
                }
                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
                        // unit
                        variablesIter.remove();
                        continue;
                    }

                    // Check modifiers, type and annotations
                    if (equalFieldTypeModifiersAnnotations(field,
                            entry.fieldDeclaration)) {
                        // Variable declaration is equals:
                        // remove from cid map as already exists
                        cidFields.remove(fieldName);
                    }
                    else {
                        // as there are more variable definition remove it
                        // from original. At the end, process will create it
                        // again
                        // using new modifiers and type
                        variablesIter.remove();
                        // Modifiers changed
                        if (field.getVariables().size() == 1) {
                            // if no more variables update all field definition
                            field.setModifiers(entry.fieldDeclaration
                                    .getModifiers());
                            field.setType(entry.fieldDeclaration.getType());
                            if (field.getAnnotations() == null
                                    && entry.fieldDeclaration.getAnnotations() != null) {
                                field.setAnnotations(new ArrayList<AnnotationExpr>());
                            }
                            updateAnnotations(field.getAnnotations(),
                                    entry.fieldDeclaration.getAnnotations());

                            // remove processed field of cid
                            cidFields.remove(fieldName);
                            continue;
                        }
                    }

                }
                if (field.getVariables().isEmpty()) {
                    originalMemberstIter.remove();
                }
            }
        }

        if (cidFields.isEmpty()) {
            // Done it
            return;
        }

        if (originalType.getMembers() == null) {
            originalType.setMembers(new ArrayList<BodyDeclaration>());
        }

        // Add new fields
        List<VariableDeclarator> variables;
        for (final FieldEntry entry : cidFields.values()) {
            variables = new ArrayList<VariableDeclarator>(1);
            variables.add(entry.variableDeclarator);
            field = new FieldDeclaration(entry.fieldDeclaration.getModifiers(),
                    entry.fieldDeclaration.getType(), variables);
            field.setAnnotations(entry.fieldDeclaration.getAnnotations());
            field.setBeginComments(entry.fieldDeclaration.getBeginComments());
            field.setInternalComments(entry.fieldDeclaration
                    .getInternalComments());
            field.setEndComments(entry.fieldDeclaration.getEndComments());
            originalType.getMembers().add(field);
        }
    }
View Full Code Here

TOP

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

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.