Example usage for org.eclipse.jdt.core.dom AST newEnumConstantDeclaration

List of usage examples for org.eclipse.jdt.core.dom AST newEnumConstantDeclaration

Introduction

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

Prototype

public EnumConstantDeclaration newEnumConstantDeclaration() 

Source Link

Document

Creates an unparented enum constant declaration node owned by this AST.

Usage

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGenerator.java

License:Apache License

/**
 * Generate Enumeration constants./*w  w w .  jav  a  2 s  .  co  m*/
 * 
 * @param clazz
 *            the UML class
 * @param ast
 *            the JDT Java AST
 * @param ed
 *            Enumeration declaration for Java JDT
 */
@SuppressWarnings("unchecked")
public void generateConstants(Classifier clazz, AST ast, EnumDeclaration ed) {
    // Get all properties for this enumeration
    Enumeration enumeration = (Enumeration) clazz;
    EList<EnumerationLiteral> enumerationLiterals = enumeration.getOwnedLiterals();
    for (EnumerationLiteral enumLiteral : enumerationLiterals) {
        EnumConstantDeclaration ec = ast.newEnumConstantDeclaration();
        ec.setName(ast.newSimpleName(enumLiteral.getName().toUpperCase()));

        // We need to sort the arguments so that it match the
        // constructor arguments!
        if (!constructorParameterNames.isEmpty()) {
            for (String constructorParameterName : constructorParameterNames) {
                logger.log(Level.FINE, "constructorParameterName: " + constructorParameterNames.toString());

                Slot slot = findSlotByName(constructorParameterName, enumLiteral);
                if (slot != null) {
                    // We found a slot with the same name
                    Property property = (Property) slot.getDefiningFeature();
                    Type type = property.getType();
                    chooseLiteralTypeAndAddToEnumConstantArguments(ast, ec, slot, type);
                } else {
                    // We didn't find the slot with the same name as in the
                    // constructor
                    logger.log(Level.SEVERE,
                            "EnumGenerator: Error in Generating Enum: we cannot find the correct slot by its name as it was given by the constructor!");
                    // Doing something intelligent...
                    // So we are adding the literal to the EnumConstant
                    // arguments just as it is, so this is not intelligent
                    // at the moment...
                    getSlotsNotIntelligent(ast, enumLiteral, ec);
                }
            }
        } else {
            // Constructor parameter types is empty
            // So we are adding the literal to the EnumConstant arguments
            // just as it is
            getSlotsNotIntelligent(ast, enumLiteral, ec);
        }

        ed.enumConstants().add(ec);
    }
}

From source file:de.dentrassi.varlink.generator.JdtGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void createEnum(final TypeDeclaration td, final String name, final EList<String> literals) {
    final AST ast = td.getAST();

    final EnumDeclaration ed = ast.newEnumDeclaration();
    td.bodyDeclarations().add(ed);/*from  w w w  . java2  s  . c om*/
    ed.setName(ast.newSimpleName(name));
    make(ed, PUBLIC_KEYWORD, STATIC_KEYWORD);

    for (final String literal : literals) {
        final EnumConstantDeclaration ecd = ast.newEnumConstantDeclaration();
        ecd.setName(ast.newSimpleName(literal));
        ed.enumConstants().add(ecd);
    }
}

From source file:org.asup.dk.compiler.rpj.writer.JDTMessageFileWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
private EnumDeclaration buildEnumDeclaration(AST ast, String name,
        List<QMessageDescription> messageDescriptions) {

    // write AST//from  www.j a  va 2s .  c  o m
    EnumDeclaration enumDeclaration = ast.newEnumDeclaration();
    enumDeclaration.setName(ast.newSimpleName(name));

    enumDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));

    for (QMessageDescription messageDescription : messageDescriptions) {
        EnumConstantDeclaration constantDeclaration = ast.newEnumConstantDeclaration();
        constantDeclaration.setName(ast.newSimpleName(messageDescription.getName()));
        enumDeclaration.enumConstants().add(constantDeclaration);

        /*
         * Javadoc javadoc = ast.newJavadoc(); TagElement tag =
         * javadoc.getAST().newTagElement(); TextElement textElement =
         * javadoc.getAST().newTextElement();
         * textElement.setText(messageDescription.getMessageText());
         * tag.fragments().add(textElement); javadoc.tags().add(tag);
         * 
         * constantDeclaration.setJavadoc(javadoc);
         */
    }

    return enumDeclaration;
}

From source file:org.asup.dk.compiler.rpj.writer.JDTNamedNodeWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void writeEnum(EnumDeclaration target, QDataTerm<?> dataTerm) {
    AST ast = target.getAST();

    QSpecial special = dataTerm.getFacet(QSpecial.class);

    target.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    target.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));

    // elements/*from  w w  w .j  a  v  a  2s. c  o  m*/
    int num = 0;

    if (special != null) {
        for (QSpecialElement element : special.getElements()) {

            EnumConstantDeclaration constantDeclaration = ast.newEnumConstantDeclaration();
            constantDeclaration.setName(ast.newSimpleName(normalizeEnumName(element.getName())));
            writeEnumField(constantDeclaration, element);
            target.enumConstants().add(num, constantDeclaration);
            num++;
        }
    }

    // restricted
    if (!dataTerm.isRestricted()) {
        EnumConstantDeclaration constantDeclaration = ast.newEnumConstantDeclaration();
        constantDeclaration.setName(ast.newSimpleName("OTHER"));
        // QSpecialElement elemDef =
        // QIntegratedLanguageCoreFactory.eINSTANCE.createSpecialElement();
        // writeEnumField(constantDeclaration, elemDef);
        target.enumConstants().add(num, constantDeclaration);
    }

}

From source file:org.decojer.cavaj.transformers.TrOutline.java

License:Open Source License

private static void decompileField(@Nonnull final F f, @Nonnull final CU cu) {
    if (checkFieldIgnore(f, cu)) {
        return;/*  w  w  w .  j  a  v a 2 s. c o m*/
    }
    final String name = f.getName();
    final T t = f.getT();
    final AST ast = cu.getAst();

    final boolean isEnum = f.isEnum();

    // decompile BodyDeclaration, possible subtypes:
    // FieldDeclaration, EnumConstantDeclaration
    final BodyDeclaration fieldDeclaration;
    if (isEnum) {
        fieldDeclaration = ast.newEnumConstantDeclaration();
        ((EnumConstantDeclaration) fieldDeclaration).setName(newSimpleName(name, ast));
    } else {
        final VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
        variableDeclarationFragment.setName(newSimpleName(name, ast));
        fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
    }
    f.setAstNode(fieldDeclaration);

    // decompile synthetic Javadoc-comment if no annotation set
    if (f.isSynthetic()) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("is synthetic");
        javadoc.tags().add(tagElement);
        fieldDeclaration.setJavadoc(javadoc);
    }
    // decompile deprecated Javadoc-tag if no annotation set
    if (f.getAf(AF.DEPRECATED) && !Annotations.isDeprecatedAnnotation(f.getAs())) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("@deprecated");
        javadoc.tags().add(tagElement);
        fieldDeclaration.setJavadoc(javadoc);
    }

    final List<IExtendedModifier> modifiers = fieldDeclaration.modifiers();
    assert modifiers != null;

    // decompile annotations, add annotation modifiers before other modifiers, order preserved
    // in source code generation through Eclipse JDT
    if (f.getAs() != null) {
        Annotations.decompileAnnotations(f.getAs(), modifiers, t);
    }

    final boolean isInterfaceMember = t.isInterface();

    // decompile modifier flags, public is default for enum and interface
    if (f.getAf(AF.PUBLIC) && !isEnum && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    }
    if (f.getAf(AF.PRIVATE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
    }
    if (f.getAf(AF.PROTECTED)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD));
    }
    // static is default for enum and interface
    if (f.isStatic() && !isEnum && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    }
    // final is default for enum and interface
    if (f.getAf(AF.FINAL) && !isEnum && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
    }
    if (f.getAf(AF.VOLATILE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.VOLATILE_KEYWORD));
    }
    if (f.getAf(AF.TRANSIENT)) {
        modifiers.add(ast.newModifier(ModifierKeyword.TRANSIENT_KEYWORD));
    }
    // not for enum constant declaration
    if (fieldDeclaration instanceof FieldDeclaration) {
        ((FieldDeclaration) fieldDeclaration).setType(newType(f.getValueT(), t));
        final Object value = f.getValue();
        if (value != null) {
            // only final, non static - no arrays, class types
            final Expression expr = newLiteral(f.getValueT(), value, t, null);
            final VariableDeclarationFragment variableDeclarationFragment = (VariableDeclarationFragment) ((FieldDeclaration) fieldDeclaration)
                    .fragments().get(0);
            variableDeclarationFragment.setInitializer(expr);
        }
    }
}

From source file:org.eclipse.babel.tapiji.tools.java.ui.refactoring.Cal10nEnumRefactoringVisitor.java

License:Open Source License

/**
 * Modifies the enum file. It replaces the old key with the new one.
 *///w w  w  . j  a va 2 s  . com
@Override
public boolean visit(EnumConstantDeclaration node) {

    if (node.resolveVariable().getName().equals(oldKey)) {

        // ASTRewrite
        AST ast = enumCu.getAST();
        ASTRewrite rewriter = ASTRewrite.create(ast);

        EnumConstantDeclaration newDeclaration = ast.newEnumConstantDeclaration();

        SimpleName newSimpleName = ast.newSimpleName(newKey);
        newDeclaration.setName(newSimpleName);

        rewriter.replace(node, newDeclaration, null);

        try {
            TextEdit textEdit = rewriter.rewriteAST();
            if (textEdit.hasChildren()) { // if the compilation unit has
                                          // been
                                          // changed
                ICompilationUnit icu = (ICompilationUnit) enumCu.getJavaElement();
                icu.applyTextEdit(textEdit, null);
                icu.getBuffer().save(null, true);

                // protocol
                int startPos = node.getStartPosition();
                changeSet.add(icu.getPath().toPortableString() + ": line " + enumCu.getLineNumber(startPos));
            }
        } catch (Exception e) {
            Logger.logError(e);
        }
    }

    return false;
}

From source file:org.eclipse.jpt.common.core.tests.internal.utility.jdt.AnnotationTestCase.java

License:Open Source License

protected EnumConstantDeclaration newEnumConstantDeclaration(AST ast, String enumConstantName) {
    EnumConstantDeclaration enumConstantDeclaration = ast.newEnumConstantDeclaration();
    enumConstantDeclaration.setName(ast.newSimpleName(enumConstantName));
    return enumConstantDeclaration;
}

From source file:org.flowerplatform.codesync.code.java.adapter.JavaTypeDeclarationModelAdapter.java

License:Open Source License

public static Object createCorrespondingModelElement(AST ast, Node node) {
    ASTNode child = null;//from  www .ja  va  2  s  .  co  m
    if (CodeSyncCodeJavaConstants.ATTRIBUTE.equals(node.getType())) {
        VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
        FieldDeclaration field = ast.newFieldDeclaration(fragment);
        child = field;
    } else if (CodeSyncCodeJavaConstants.OPERATION.equals(node.getType())) {
        child = ast.newMethodDeclaration();
    } else if (CodeSyncCodeJavaConstants.ENUM_CONSTANT.equals(node.getType())) {
        child = ast.newEnumConstantDeclaration();
    } else if (CodeSyncCodeJavaConstants.CLASS.equals(node.getType())) {
        child = ast.newTypeDeclaration();
    } else if (CodeSyncCodeJavaConstants.INTERFACE.equals(node.getType())) {
        TypeDeclaration type = ast.newTypeDeclaration();
        type.setInterface(true);
        child = type;
    } else if (CodeSyncCodeJavaConstants.ENUM.equals(node.getType())) {
        child = ast.newEnumDeclaration();
    } else if (CodeSyncCodeJavaConstants.ANNOTATION_TYPE.equals(node.getType())) {
        child = ast.newAnnotationTypeDeclaration();
    }
    return child;
}

From source file:org.smeup.sys.dk.compiler.rpj.writer.JDTNamedNodeWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void writeInnerEnum(EnumDeclaration target, QDataTerm<?> dataTerm, boolean statik) {
    AST ast = target.getAST();

    QSpecial special = dataTerm.getFacet(QSpecial.class);

    target.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));

    if (statik)/*w w  w . j av  a2s  .  c  o  m*/
        target.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));

    // elements
    int num = 0;

    if (special != null)
        for (QSpecialElement element : special.getElements()) {

            EnumConstantDeclaration constantDeclaration = ast.newEnumConstantDeclaration();
            constantDeclaration.setName(ast.newSimpleName(normalizeEnumName(element.getName())));
            writeEnumField(constantDeclaration, element);
            target.enumConstants().add(num, constantDeclaration);
            num++;
        }

    // restricted
    if (!dataTerm.isRestricted()) {
        EnumConstantDeclaration constantDeclaration = ast.newEnumConstantDeclaration();
        constantDeclaration.setName(ast.newSimpleName("OTHER"));
        // QSpecialElement elemDef =
        // QIntegratedLanguageCoreFactory.eINSTANCE.createSpecialElement();
        // writeEnumField(constantDeclaration, elemDef);
        target.enumConstants().add(num, constantDeclaration);
    }

}

From source file:org.smeup.sys.dk.compiler.rpj.writer.JDTNamedNodeWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void writeInnerEnum(EnumDeclaration target, QFileFormat<?> fileFormat, boolean statik) {
    AST ast = target.getAST();

    target.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));

    if (statik)//from w  ww.  j a  v a 2s.  c  om
        target.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));

    // elements
    int num = 0;

    for (QDataTerm<?> element : fileFormat.getDefinition().getElements()) {

        EnumConstantDeclaration constantDeclaration = ast.newEnumConstantDeclaration();
        constantDeclaration.setName(ast.newSimpleName(normalizeEnumName(element.getName())));
        target.enumConstants().add(num, constantDeclaration);
        num++;
    }
}