Example usage for org.eclipse.jdt.core.dom EnumDeclaration BODY_DECLARATIONS_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom EnumDeclaration BODY_DECLARATIONS_PROPERTY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom EnumDeclaration BODY_DECLARATIONS_PROPERTY.

Prototype

ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY

To view the source code for org.eclipse.jdt.core.dom EnumDeclaration BODY_DECLARATIONS_PROPERTY.

Click Source Link

Document

The "bodyDeclarations" structural property of this node type (element type: BodyDeclaration ).

Usage

From source file:com.github.parzonka.ccms.engine.SortElementsOperation.java

License:Open Source License

private ASTRewrite sortCompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit ast,
        final TextEditGroup group) {
    ast.accept(new ASTVisitor() {
        @Override// w  w w .  j av a2 s  .c  om
        public boolean visit(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit) {
            final List<AbstractTypeDeclaration> types = compilationUnit.types();
            for (final Iterator<AbstractTypeDeclaration> iter = types.iterator(); iter.hasNext();) {
                final AbstractTypeDeclaration typeDeclaration = iter.next();
                typeDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER,
                        new Integer(typeDeclaration.getStartPosition()));
                compilationUnit.setProperty(CONTAINS_MALFORMED_NODES,
                        Boolean.valueOf(isMalformed(typeDeclaration)));
            }
            return true;
        }

        @Override
        public boolean visit(AnnotationTypeDeclaration annotationTypeDeclaration) {
            final List bodyDeclarations = annotationTypeDeclaration.bodyDeclarations();
            for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) {
                final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next();
                bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER,
                        new Integer(bodyDeclaration.getStartPosition()));
                annotationTypeDeclaration.setProperty(CONTAINS_MALFORMED_NODES,
                        Boolean.valueOf(isMalformed(bodyDeclaration)));
            }
            return true;
        }

        @Override
        public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) {
            final List bodyDeclarations = anonymousClassDeclaration.bodyDeclarations();
            for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) {
                final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next();
                bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER,
                        new Integer(bodyDeclaration.getStartPosition()));
                anonymousClassDeclaration.setProperty(CONTAINS_MALFORMED_NODES,
                        Boolean.valueOf(isMalformed(bodyDeclaration)));
            }
            return true;
        }

        @Override
        public boolean visit(TypeDeclaration typeDeclaration) {
            final List bodyDeclarations = typeDeclaration.bodyDeclarations();
            for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) {
                final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next();
                bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER,
                        new Integer(bodyDeclaration.getStartPosition()));
                typeDeclaration.setProperty(CONTAINS_MALFORMED_NODES,
                        Boolean.valueOf(isMalformed(bodyDeclaration)));
            }
            return true;
        }

        @Override
        public boolean visit(EnumDeclaration enumDeclaration) {
            final List bodyDeclarations = enumDeclaration.bodyDeclarations();
            for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) {
                final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next();
                bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER,
                        new Integer(bodyDeclaration.getStartPosition()));
                enumDeclaration.setProperty(CONTAINS_MALFORMED_NODES,
                        Boolean.valueOf(isMalformed(bodyDeclaration)));
            }
            final List enumConstants = enumDeclaration.enumConstants();
            for (final Iterator iter = enumConstants.iterator(); iter.hasNext();) {
                final EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) iter.next();
                enumConstantDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER,
                        new Integer(enumConstantDeclaration.getStartPosition()));
                enumDeclaration.setProperty(CONTAINS_MALFORMED_NODES,
                        Boolean.valueOf(isMalformed(enumConstantDeclaration)));
            }
            return true;
        }
    });

    final ASTRewrite rewriter = ASTRewrite.create(ast.getAST());
    final boolean[] hasChanges = new boolean[] { false };

    ast.accept(new ASTVisitor() {

        /**
         * This
         *
         * @param elements
         * @param listRewrite
         */
        private void sortElements(List<BodyDeclaration> elements, ListRewrite listRewrite) {
            if (elements.size() == 0)
                return;

            final List<BodyDeclaration> myCopy = new ArrayList<BodyDeclaration>();
            myCopy.addAll(elements);

            // orderexperiment
            final List<Signature> methods = new ArrayList<Signature>();
            for (final BodyDeclaration bd : myCopy) {
                if (bd.getNodeType() == ASTNode.METHOD_DECLARATION) {
                    methods.add(new Signature((MethodDeclaration) bd));
                }
            }
            Collections.sort(methods, ((BodyDeclarationComparator) SortElementsOperation.this.comparator)
                    .getMethodDeclarationComparator());
            logger.info("Sorting of methods based on appearance:");
            int j = 0;
            for (final Signature sig : methods) {
                logger.info("Method [{}] : {}", ++j, sig);
            }

            Collections.sort(myCopy, SortElementsOperation.this.comparator);

            logger.info("Final sorting order just before the AST-Rewrite:");
            for (final BodyDeclaration bd : myCopy) {
                if (bd.getNodeType() == ASTNode.METHOD_DECLARATION) {
                    logger.info("{}", new Signature((MethodDeclaration) bd));
                } else {
                    logger.info("{}", bd.toString());
                }
            }

            for (int i = 0; i < elements.size(); i++) {
                final BodyDeclaration oldNode = elements.get(i);

                final BodyDeclaration newNode = myCopy.get(i);

                if (oldNode != newNode) {
                    if (oldNode.getNodeType() == ASTNode.METHOD_DECLARATION
                            && newNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
                        final Signature oldMethodSignature = new Signature((MethodDeclaration) oldNode);
                        final Signature newMethodSignature = new Signature((MethodDeclaration) newNode);
                        logger.trace("Swapping [{}]for [{}]", oldMethodSignature, newMethodSignature);
                    } else {
                        logger.trace("Swapping [{}]for [{}]", oldNode.getNodeType(), newNode.getNodeType());
                    }
                    listRewrite.replace(oldNode, rewriter.createMoveTarget(newNode), group);
                    hasChanges[0] = true;
                }
            }
        }

        @Override
        public boolean visit(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit) {
            if (checkMalformedNodes(compilationUnit)) {
                logger.warn("Malformed nodes. Aborting sorting of current element.");
                return true;
            }

            sortElements(compilationUnit.types(), rewriter.getListRewrite(compilationUnit,
                    org.eclipse.jdt.core.dom.CompilationUnit.TYPES_PROPERTY));
            return true;
        }

        @Override
        public boolean visit(AnnotationTypeDeclaration annotationTypeDeclaration) {
            if (checkMalformedNodes(annotationTypeDeclaration)) {
                logger.warn("Malformed nodes. Aborting sorting of current element.");
                return true;
            }

            sortElements(annotationTypeDeclaration.bodyDeclarations(), rewriter.getListRewrite(
                    annotationTypeDeclaration, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY));
            return true;
        }

        @Override
        public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) {
            if (checkMalformedNodes(anonymousClassDeclaration)) {
                logger.warn("Malformed nodes. Aborting sorting of current element.");
                return true;
            }

            sortElements(anonymousClassDeclaration.bodyDeclarations(), rewriter.getListRewrite(
                    anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY));
            return true;
        }

        @Override
        public boolean visit(TypeDeclaration typeDeclaration) {
            if (checkMalformedNodes(typeDeclaration)) {
                logger.warn("Malformed nodes. Aborting sorting of current element.");
                return true;
            }

            sortElements(typeDeclaration.bodyDeclarations(),
                    rewriter.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY));
            return true;
        }

        @Override
        public boolean visit(EnumDeclaration enumDeclaration) {
            if (checkMalformedNodes(enumDeclaration)) {
                return true; // abort sorting of current element
            }

            sortElements(enumDeclaration.bodyDeclarations(),
                    rewriter.getListRewrite(enumDeclaration, EnumDeclaration.BODY_DECLARATIONS_PROPERTY));
            sortElements(enumDeclaration.enumConstants(),
                    rewriter.getListRewrite(enumDeclaration, EnumDeclaration.ENUM_CONSTANTS_PROPERTY));
            return true;
        }
    });

    if (!hasChanges[0])
        return null;

    return rewriter;
}

From source file:net.atos.optimus.common.tools.jdt.JavaCodeHelper.java

License:Open Source License

/**
 * Return a Body Declaration Property according to the {@code type} type.
 * //from w w w .  j  av a 2  s  .  c om
 * @param type
 *            A Body Declaration type
 * @return a Body Declaration Property according to the {@code type} type.
 */
public static ChildListPropertyDescriptor getBodyDeclarationProperty(Class<?> type) {
    if (type.equals(TypeDeclaration.class)) {
        return TypeDeclaration.BODY_DECLARATIONS_PROPERTY;
    } else if (type.equals(EnumDeclaration.class)) {
        return EnumDeclaration.BODY_DECLARATIONS_PROPERTY;
    } else if (type.equals(AnnotationTypeDeclaration.class)) {
        return AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY;
    }

    return null;
}