Package org.eclipse.jdt.core.dom

Examples of org.eclipse.jdt.core.dom.EnumConstantDeclaration


     * @param value
     * @param doc documentation text, or <code>null</code> if none
     */
    public void addEnumConstant(String value, String doc) {
        if (m_class instanceof EnumDeclaration) {
            EnumConstantDeclaration enumdecl = getAST().newEnumConstantDeclaration();
            enumdecl.setName(getAST().newSimpleName(value));
            addJavaDoc(doc, enumdecl);
            ((EnumDeclaration)m_class).enumConstants().add(enumdecl);
        } else {
            // should not be possible, but just in case of added types in future
            throw new IllegalStateException("Internal error - cannot add constant to class type");
View Full Code Here


     * @param doc documentation text, or <code>null</code> if none
     * @param value
     */
    public void addEnumConstant(String name, String doc, String value) {
        if (m_class instanceof EnumDeclaration) {
            EnumConstantDeclaration enumdecl = getAST().newEnumConstantDeclaration();
            enumdecl.setName(getAST().newSimpleName(name));
            StringLiteral strlit = getAST().newStringLiteral();
            strlit.setLiteralValue(value);
            enumdecl.arguments().add(strlit);
            addJavaDoc(doc, enumdecl);
            ((EnumDeclaration)m_class).enumConstants().add(enumdecl);
        } else {
            // should not be possible, but just in case of added types in future
            throw new IllegalStateException("Internal error - cannot add constant to class type");
View Full Code Here

      }
    }
 
    IValueList enumConstants = new IValueList(values);
    for (Iterator it = node.enumConstants().iterator(); it.hasNext();) {
      EnumConstantDeclaration d = (EnumConstantDeclaration) it.next();
      enumConstants.add(visitChild(d));
    }
 
    IValueList bodyDeclarations = new IValueList(values);
    if (!node.bodyDeclarations().isEmpty()) {
View Full Code Here

      }
      this.buffer.append(" ");//$NON-NLS-1$
    }
    this.buffer.append("{");//$NON-NLS-1$
    for (Iterator it = node.enumConstants().iterator(); it.hasNext(); ) {
      EnumConstantDeclaration d = (EnumConstantDeclaration) it.next();
      d.accept(this);
      // enum constant declarations do not include punctuation
      if (it.hasNext()) {
        // enum constant declarations are separated by commas
        this.buffer.append(", ");//$NON-NLS-1$
      }
    }
    if (!node.bodyDeclarations().isEmpty()) {
      this.buffer.append("; ");//$NON-NLS-1$
      for (Iterator it = node.bodyDeclarations().iterator(); it.hasNext(); ) {
        BodyDeclaration d = (BodyDeclaration) it.next();
        d.accept(this);
        // other body declarations include trailing punctuation
      }
    }
    this.buffer.append("}\n");//$NON-NLS-1$
    return false;
View Full Code Here

                    return compareNames(bodyDeclaration1, bodyDeclaration2,
                            typeName1, typeName2);

                case ASTNode.ENUM_CONSTANT_DECLARATION:
                    EnumConstantDeclaration enum1 = (EnumConstantDeclaration) bodyDeclaration1;
                    EnumConstantDeclaration enum2 = (EnumConstantDeclaration) bodyDeclaration2;

                    String enumName1 = enum1.getName().getIdentifier();
                    String enumName2 = enum2.getName().getIdentifier();

                    return compareNames(bodyDeclaration1, bodyDeclaration2,
                            enumName1, enumName2);

                case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
View Full Code Here

  }

  private static EnumConstantDeclaration createNewEnumConstantDeclarataion(
      AST ast, SimpleName constantName, Javadoc docComment,
      Collection annotationCollection) {
    final EnumConstantDeclaration enumConstDecl = ast
        .newEnumConstantDeclaration();
    enumConstDecl.setJavadoc(docComment);
    enumConstDecl.setName(constantName);
    enumConstDecl.modifiers().addAll(annotationCollection);
    return enumConstDecl;
  }
View Full Code Here

        final Javadoc originalJavadoc = originalFieldDeclaration
            .getJavadoc();
        final Javadoc newJavadoc = (Javadoc) ASTNode.copySubtree(ast,
            originalJavadoc);

        final EnumConstantDeclaration constDecl = createNewEnumConstantDeclarataion(
            ast, ast.newSimpleName(constantField.getElementName()),
            newJavadoc, annotationCollection);

        newEnumConstantToOldConstantFieldMap.put(constDecl, constantField);
        enumConstantDeclarationCollection.add(constDecl);
      }

      // Get the consistent access modifier of the enum constants.
      final int flag = Util.getConsistentVisibility(constants);
      /*******************************************************************
       * * TODO: Need condition check here: 1. If enum is in its own file
       * then only public and package default are allowed. 2. Else, if
       * enum is embedded in another type, then all visibilities are
       * allowed, but may need more checking here for private!.
       ******************************************************************/
      if (!(Flags.isPublic(flag) || Flags.isPackageDefault(flag)))
        status
            .addFatalError(Messages.ConvertConstantsToEnumRefactoring_EnumTypeMustHaveCorrectVisibility);

      EnumDeclaration newEnumDeclaration = null;
      // only add modifier if it is not package default.
      if (!Flags.isPackageDefault(flag)) {
        final Modifier newModifier = ast
            .newModifier(Modifier.ModifierKeyword
                .fromFlagValue(flag));

        newEnumDeclaration = createNewEnumDeclaration(ast, ast
            .newSimpleName((String) this.simpleTypeNames.get(col)),
            enumConstantDeclarationCollection,
            new Object[] { newModifier });
      } else
        newEnumDeclaration = createNewEnumDeclaration(ast, ast
            .newSimpleName((String) this.simpleTypeNames.get(col)),
            enumConstantDeclarationCollection, new Object[] {});

      // TODO [bm] pretty dirty hack to workaround 16: Refactoring should not use UI components for code changes
      //       http://code.google.com/p/constants-to-enum-eclipse-plugin/issues/detail?id=16
      final NewEnumWizardPage[] result = new NewEnumWizardPage[1];
      Display.getDefault().syncExec(new Runnable() {
        public void run() {
          result[0]= new NewEnumWizardPage();
        }
      });
      NewEnumWizardPage page = result[0];
      page.setTypeName((String) this.simpleTypeNames.get(col), false);

      final IPackageFragmentRoot root = this.getPackageFragmentRoot();
      page.setPackageFragmentRoot(root, false);

      final IPackageFragment pack = this.getPackageFragment(
          (String) this.packageNames.get(col), monitor);
      page.setPackageFragment(pack, false);

      /*******************************************************************
       * * TODO: This is somewhat of a dirty workaround. Basically, I am
       * creating a new Enum type only to replace the root AST node. If
       * you have any better ideas please let me know!
       ******************************************************************/
      /*******************************************************************
       * TODO: Need a way of inserting this new type such that it appears
       * in the text changes for rollback purposes, etc.
       ******************************************************************/
      try {
        page.createType(monitor);
      } catch (final InterruptedException E) {
        status.addFatalError(E.getMessage());
      }

      // Modify the newly created enum type.
      final IType newEnumType = page.getCreatedType();
      final CompilationUnit node = (CompilationUnit) Util.getASTNode(
          newEnumType, monitor);

      final ASTRewrite astRewrite = ASTRewrite.create(node.getAST());
      final ImportRewrite importRewrite = ImportRewrite
          .create(node, true);

      final EnumDeclaration oldEnumDeclaration = (EnumDeclaration) node
          .types().get(0);

      // Add imports for annotations to the enum constants.
      for (final Iterator eit = newEnumDeclaration.enumConstants()
          .iterator(); eit.hasNext();) {
        final Object obj = eit.next();
        final EnumConstantDeclaration ecd = (EnumConstantDeclaration) obj;
        for (final Iterator emit = ecd.modifiers().iterator(); emit
            .hasNext();) {
          final Object o = emit.next();
          if (o instanceof Annotation) {
            final Annotation anno = (Annotation) o;
            final String newName = importRewrite
View Full Code Here

   {
      List<EnumConstant<JavaEnum>> result = new ArrayList<EnumConstant<JavaEnum>>();

      for (Object o : (((EnumDeclaration) getBodyDeclaration()).enumConstants()))
      {
         EnumConstantDeclaration field = (EnumConstantDeclaration) o;
         result.add(new EnumConstantImpl<JavaEnum>((JavaEnum) this, field));
      }

      return Collections.unmodifiableList(result);
   }
View Full Code Here

      init(parent);

      String stub = "public enum Stub { " + declaration + " }";
      JavaEnum temp = (JavaEnum) JavaParser.parse(stub);
      List<Member<JavaEnum, ?>> fields = temp.getMembers();
      EnumConstantDeclaration newField = (EnumConstantDeclaration) fields.get(0).getInternal();
      EnumConstantDeclaration subtree = (EnumConstantDeclaration) ASTNode.copySubtree(ast, newField);
      this.enumConstant = subtree;
   }
View Full Code Here

      init(parent);

      String stub = "public enum Stub { " + declaration + " }";
      JavaEnum temp = (JavaEnum) JavaParser.parse(stub);
      List<EnumConstant<JavaEnum>> constants = temp.getEnumConstants();
      EnumConstantDeclaration newField = (EnumConstantDeclaration) constants.get(0).getInternal();
      EnumConstantDeclaration subtree = (EnumConstantDeclaration) ASTNode.copySubtree(ast, newField);
      this.enumConstant = subtree;
   }
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.core.dom.EnumConstantDeclaration

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.