Example usage for org.eclipse.jdt.core.dom EnumConstantDeclaration setName

List of usage examples for org.eclipse.jdt.core.dom EnumConstantDeclaration setName

Introduction

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

Prototype

public void setName(SimpleName constantName) 

Source Link

Document

Sets the name of the constant declared in this enum declaration to the given name.

Usage

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaEnumConstantDeclarationModelAdapter.java

License:Open Source License

@Override
public void setValueFeatureValue(Object element, Object feature, Object value) {
    if (CodeSyncPackage.eINSTANCE.getCodeSyncElement_Name().equals(feature)) {
        EnumConstantDeclaration enumConstant = getEnumConstant(element);
        enumConstant.setName(enumConstant.getAST().newSimpleName((String) value));
    }//w  ww.  j  av  a2s  .  co  m
    super.setValueFeatureValue(element, feature, value);
}

From source file:com.ecfeed.ui.common.EclipseModelImplementer.java

License:Open Source License

@SuppressWarnings("unchecked")
private void addEnumItems(CompilationUnit unit, String typeName, List<ChoiceNode> nodes, IType enumType)
        throws CoreException {

    EnumDeclaration enumDeclaration = getEnumDeclaration(unit, typeName);
    if (enumDeclaration == null) {
        return;/* ww w.ja  v a 2  s.  c  om*/
    }

    List<String> enumItemNames = new ArrayList<String>();

    for (ChoiceNode node : nodes) {
        EnumConstantDeclaration constant = unit.getAST().newEnumConstantDeclaration();
        String enumItemName = node.getValueString();

        if (enumItemNames.contains(enumItemName)) {
            continue;
        }
        constant.setName(unit.getAST().newSimpleName(enumItemName));
        enumDeclaration.enumConstants().add(constant);
        enumItemNames.add(enumItemName);
    }

    saveChanges(unit, enumType.getResource().getLocation());
}

From source file:com.testify.ecfeed.ui.common.EclipseModelImplementer.java

License:Open Source License

@SuppressWarnings("unchecked")
protected void implementChoicesDefinitions(List<ChoiceNode> nodes) throws CoreException {
    refreshWorkspace();/* www .  j a  va 2 s . c o m*/
    AbstractParameterNode parent = getParameter(nodes);
    if (parent == null) {
        return;
    }
    String typeName = parent.getType();
    if (parameterDefinitionImplemented(parent) == false) {
        implementParameterDefinition(parent);
    }
    IType enumType = getJavaProject().findType(typeName);
    ICompilationUnit iUnit = enumType.getCompilationUnit();
    CompilationUnit unit = getCompilationUnit(enumType);
    EnumDeclaration enumDeclaration = getEnumDeclaration(unit, typeName);
    if (enumDeclaration != null) {
        for (ChoiceNode node : nodes) {
            EnumConstantDeclaration constant = unit.getAST().newEnumConstantDeclaration();
            constant.setName(unit.getAST().newSimpleName(node.getValueString()));
            enumDeclaration.enumConstants().add(constant);
        }
        saveChanges(unit, enumType.getResource().getLocation());
    }
    enumType.getResource().refreshLocal(IResource.DEPTH_ONE, null);
    iUnit.becomeWorkingCopy(null);
    iUnit.commitWorkingCopy(true, null);
    refreshWorkspace();
}

From source file:com.worldline.awltech.i18ntools.editor.data.model.I18NResourceBundle.java

License:Open Source License

@SuppressWarnings("unchecked")
private boolean addNewLiterals() {
    if (this.newMessages.size() == 0)
        return false;

    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setSource(enumeration);//from  w ww. j av a  2  s .c  o m
    CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
    compilationUnit.recordModifications();

    EnumDeclaration enumDeclaration = null;
    for (Iterator<?> iterator = compilationUnit.types().iterator(); iterator.hasNext()
            && enumDeclaration == null;) {
        Object next = iterator.next();
        if (next instanceof EnumDeclaration)
            enumDeclaration = (EnumDeclaration) next;
    }

    if (enumDeclaration == null)
        return false;

    // Check if entry exists !!!

    AST ast = enumDeclaration.getAST();
    for (I18NEntry entry : newMessages) {
        String entryName = entry.getName();
        boolean nameExists = false;
        for (Object o : enumDeclaration.enumConstants()) {
            EnumConstantDeclaration ecd = (EnumConstantDeclaration) o;
            if (entryName.equals(ecd.getName().getFullyQualifiedName()))
                nameExists = true;
        }
        if (!nameExists) {
            EnumConstantDeclaration newEnumConstantDeclaration = enumDeclaration.getAST()
                    .newEnumConstantDeclaration();
            newEnumConstantDeclaration.setName(ast.newSimpleName(entryName));

            StringLiteral literal = ast.newStringLiteral();
            literal.setLiteralValue(entryName);
            newEnumConstantDeclaration.arguments().add(literal);
            enumDeclaration.enumConstants().add(newEnumConstantDeclaration);
        }
    }

    try {
        IDocument document = new Document(enumeration.getSource());
        final TextEdit rewrite = compilationUnit.rewrite(document, null);
        rewrite.apply(document);
        final String newSource = document.get();
        enumeration.getBuffer().setContents(newSource);
        enumeration.getResource().refreshLocal(IResource.DEPTH_ZERO, null);
        enumeration.save(null, false);

        defaultMessages.addAll(newMessages);
        newMessages.clear();

    } catch (MalformedTreeException | BadLocationException | CoreException e) {
        LogMessage.error().message("An error occurred while saving file.").throwable(e).log();
    }

    return false;
}

From source file:com.worldline.awltech.i18ntools.wizard.core.modules.ResourceBundleWrapper.java

License:Open Source License

/**
 * Refactors the source code to replace selected source by literal.
 * /*from   w w  w. j  a  v  a2s .co m*/
 * @param nodeFinder
 * @param literalName
 * @param resolver
 * @return
 */
@SuppressWarnings("unchecked")
private boolean effectiveAddLiteral(final ASTNodeFinder nodeFinder, final String literalName,
        final ASTExpressionResolver resolver) {

    final ASTNode parent = nodeFinder.getParentNode();
    final AST ast = parent.getAST();

    final MethodInvocation replacement = ast.newMethodInvocation();
    replacement.setExpression(ast.newName(this.resourceBundleName + "." + literalName));
    replacement.setName(ast.newSimpleName("value"));

    final EnumDeclaration enumDeclaration = (EnumDeclaration) this.enumDomCompilationUnit.types().get(0);

    final EnumConstantDeclaration enumConstantDeclaration = enumDeclaration.getAST()
            .newEnumConstantDeclaration();
    enumConstantDeclaration.setName(enumDeclaration.getAST().newSimpleName(literalName));

    enumDeclaration.enumConstants().add(enumConstantDeclaration);

    boolean hasMessageKeyConstructor = false;
    for (final Iterator<Object> iterator = enumDeclaration.bodyDeclarations().iterator(); iterator.hasNext()
            && !hasMessageKeyConstructor;) {
        final Object next = iterator.next();
        if (next instanceof MethodDeclaration) {
            final MethodDeclaration methodDeclaration = (MethodDeclaration) next;
            if (methodDeclaration.isConstructor() && methodDeclaration.parameters().size() > 0) {
                hasMessageKeyConstructor = true;
            }
        }
    }

    if (hasMessageKeyConstructor) {
        final StringLiteral literal = enumDeclaration.getAST().newStringLiteral();
        literal.setLiteralValue(literalName);
        enumConstantDeclaration.arguments().add(literal);
    }

    StructuralPropertyDescriptor locationInParent = null;
    if (nodeFinder.getFoundNode() != null) {
        locationInParent = nodeFinder.getFoundNode().getLocationInParent();
    } else {
        // TODO
        return false;
    }

    ResourceBundleWrapper.addImportToCompilationUnitIfMissing(nodeFinder.getParentNode(),
            this.packageName + "." + this.resourceBundleName);

    if (locationInParent.isChildListProperty()) {
        final List<Object> list = (List<Object>) parent.getStructuralProperty(locationInParent);
        final int index = list.indexOf(nodeFinder.getFoundNode());
        list.remove(nodeFinder.getFoundNode());
        list.add(index, replacement);
    } else {
        parent.setStructuralProperty(locationInParent, replacement);
    }

    for (final Expression parameter : resolver.getMessageParameters()) {
        final Expression newParameter = ASTTreeCloner.clone(parameter);
        replacement.arguments().add(newParameter);
    }

    String messagePattern = resolver.getMessagePattern();
    if (this.prefixMessageByKey) {
        messagePattern = String.format("[%s] %s", literalName, messagePattern);
    }

    this.properties.put(literalName, messagePattern);
    return true;
}

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

License:Apache License

/**
 * Generate Enumeration constants.//w w  w . j a va2s  . c  o 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  ww  w .  jav a2 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:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.EnumConstantDeclaration node) {
    EnumConstantDeclaration element = (EnumConstantDeclaration) this.binding.get(node);
    this.initializeNode(element, node);

    element.setName(node.getName().getIdentifier());
    if (this.binding.get(node.getAnonymousClassDeclaration()) != null)
        element.setAnonymousClassDeclaration(
                (AnonymousClassDeclaration) this.binding.get(node.getAnonymousClassDeclaration()));
    for (Iterator<?> i = node.arguments().iterator(); i.hasNext();) {
        Expression itElement = (Expression) this.binding.get(i.next());
        if (itElement != null)
            element.getArguments().add(itElement);
    }/* w w w .  java 2  s.c o  m*/

    endVisitBD(node, element);
    JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public void writeLabels(Collection<String> labels) {

    if (labels.isEmpty())
        return;/*from w ww  . j a v  a  2s .c o m*/

    EnumDeclaration enumType = getAST().newEnumDeclaration();
    enumType.setName(getAST().newSimpleName("TAG"));
    enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));

    // elements
    int num = 0;

    for (String label : labels) {

        EnumConstantDeclaration constantDeclaration = getAST().newEnumConstantDeclaration();
        constantDeclaration.setName(getAST().newSimpleName(normalizeEnumName(label)));

        enumType.enumConstants().add(num, constantDeclaration);
        num++;
    }

    getTarget().bodyDeclarations().add(enumType);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public void writeMessages(Collection<String> messages) {

    EnumDeclaration enumType = getAST().newEnumDeclaration();
    enumType.setName(getAST().newSimpleName("QCPFMSG"));
    enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));

    // elements//from  w ww .jav a  2  s  . c o m
    int num = 0;

    for (String message : messages) {
        if (message.equalsIgnoreCase("CPF0000"))
            continue;
        EnumConstantDeclaration constantDeclaration = getAST().newEnumConstantDeclaration();
        constantDeclaration.setName(getAST().newSimpleName(normalizeEnumName(message)));

        enumType.enumConstants().add(num, constantDeclaration);
        num++;
    }

    getTarget().bodyDeclarations().add(enumType);
}