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

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

Introduction

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

Prototype

ChildListPropertyDescriptor ENUM_CONSTANTS_PROPERTY

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

Click Source Link

Document

The "enumConstants" structural property of this node type (element type: EnumConstantDeclaration ).

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 ww. j av a 2  s. c o  m*/
        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:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJEnum.java

License:Open Source License

@Override
public boolean insertSibling(ASTJNode<?> node, ASTJNode<?> newSibling, boolean before) {
    if (newSibling.getParent() != null || node.getParent() != this) {
        return false;
    }/*  ww  w .ja  v  a 2s. c  o m*/

    if (newSibling instanceof ASTJEnumConstant) {
        if (node instanceof ASTJEnumConstant) {
            insert(newSibling, EnumDeclaration.ENUM_CONSTANTS_PROPERTY, node, before);
        } else if (node instanceof ASTJAnnotation) {
            insertFirst(newSibling, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
        } else {
            insertLast(newSibling, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
        }

        return true;
    } else if (node instanceof ASTJEnumConstant) {
        if (newSibling instanceof ASTJAnnotation) {
            insertLastAnnotation((ASTJAnnotation) newSibling);
            return true;
        } else if (newSibling instanceof ASTJMember<?>) {
            insertFirst(newSibling, getASTNode().getBodyDeclarationsProperty());
            return true;
        }
    }

    return super.insertSibling(node, newSibling, before);
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJEnum.java

License:Open Source License

@Override
public boolean addChild(ASTJNode<?> child) {
    if (child.getParent() != null) {
        return false;
    }//w w  w  . ja  va 2  s  .  c o  m

    if (child instanceof ASTJEnumConstant) {
        insertLast(child, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
        return true;
    }

    return super.addChild(child);
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJEnum.java

License:Open Source License

@Override
public boolean remove(ASTJNode<?> node) {
    if (node.getParent() != this) {
        return false;
    }/*from   ww w . java 2  s . co  m*/

    if (node instanceof ASTJEnumConstant) {
        remove(node, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
        return true;
    }

    return super.remove(node);
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJEnum.java

License:Open Source License

protected List<JNode> getEnumConstants() {
    List<JNode> constants = new ArrayList<JNode>();
    ListRewrite listRewrite = rewriter.getListRewrite(getASTNode(), EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
    for (Object enumConstant : listRewrite.getRewrittenList()) {
        JNode node = getFacadeHelper().convertToNode(enumConstant);
        if (node != null) {
            constants.add(node);// w  w  w  . j  a  v a 2 s.co  m
        }
    }
    return constants;
}

From source file:org.eclipse.scout.sdk.saml.importer.internal.jdt.imports.OrganizeImportsHelper.java

License:Open Source License

/**
 * Returns the type binding of the node's type context or null if the node is inside
 * an annotation, type parameter, super type declaration, or Javadoc of a top level type.
 * The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the
 * type's body.//from w w w  . j  a  va2  s.  c o  m
 * 
 * @param node
 *          an AST node
 * @return the type binding of the node's parent type context, or <code>null</code>
 */
public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) {
    StructuralPropertyDescriptor lastLocation = null;

    while (node != null) {
        if (node instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
            if (lastLocation == decl.getBodyDeclarationsProperty()
                    || lastLocation == decl.getJavadocProperty()) {
                return decl.resolveBinding();
            } else if (decl instanceof EnumDeclaration
                    && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) {
                return decl.resolveBinding();
            }
        } else if (node instanceof AnonymousClassDeclaration) {
            return ((AnonymousClassDeclaration) node).resolveBinding();
        }
        lastLocation = node.getLocationInParent();
        node = node.getParent();
    }
    return null;
}

From source file:org.incha.core.jswingripples.parser.BindingSupport.java

License:Open Source License

/**
 * Returns the type binding of the node's type context or null if the node is an annotation, type parameter or super type declaration of a tope level type.
 * The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the type's body.
 * @param node/*w  w  w  .  j a v  a 2s . c o  m*/
 * @return the type binding of the node's parent type context
 */
public ITypeBinding getBindingOfParentTypeContext(ASTNode node) {
    StructuralPropertyDescriptor lastLocation = null;

    while (node != null) {
        if (node instanceof AbstractTypeDeclaration) {
            final AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
            if (lastLocation == decl.getBodyDeclarationsProperty()) {
                return decl.resolveBinding();
            } else if (decl instanceof EnumDeclaration
                    && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) {
                return decl.resolveBinding();
            }
        } else if (node instanceof AnonymousClassDeclaration) {
            return ((AnonymousClassDeclaration) node).resolveBinding();
        }
        lastLocation = node.getLocationInParent();
        node = node.getParent();
    }
    return null;
}