0

What should be the value value to add a new enum constant to the type type?

public void addEnumConstant(IType type, String value){
    if(type.isEnum()){
        type.createField(value, null, false, null);
    }
}

Edit: I guess I was too brief, so I add more information about my problem

I am using the JavaModel framework and want to add an enum constant to IType object. I want to avoid explicit use of AST.

It is easy to add a new method to a type, by following code:

IType type = [...];
type.createMethod("public void method(){}", null, false, null);

Also I can add a variable field:

type.createField("public int var", null, false, null);

but I have no idea how to add an enum constant.

Patryk
  • 1,421
  • 8
  • 21
  • What's IType ? is it enum? – SMA Oct 16 '14 at 13:43
  • I guess you are using the Eclipse JDT Abstract Syntax Tree code to try and add a new field to a type. You are getting down votes because you have not made this clear enough and people don't understand what you are doing. – greg-449 Oct 16 '14 at 14:00
  • org.eclipse.jdt.core.IType – Patryk Oct 16 '14 at 14:01

1 Answers1

0

IType offers a method to create a type with the given contents. This may be used for Enum as well.

createType(String value, IJavaElement sibling, boolean force, IProgressMonitor monitor)

UPDATE

If you have access to the compliation unit, then you can create an EnumConstantDeclaration AST node in your IType.

ICompilationUnit iCUnit = type.getCompilationUnit()

// convert iCUnit to CompilationUnit

EnumConstantDeclaration constant = compilationUnit.getAST().newEnumConstantDeclaration();
constant.setName(parse.getAST().newSimpleName("constant name"));

Code for converting ICompilationUnit to CompilationUnit :

//Get the compilation unit for traversing AST
final ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(javaElement.getCompilationUnit());
parser.setResolveBindings(true);

final CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
Unni Kris
  • 3,081
  • 4
  • 35
  • 57
  • I want to add a constant to existing enum declaration, so I cannot use createType of IPackageFragment, but rather createField from IType – Patryk Oct 17 '14 at 05:26
  • After your update: This is what I will eventually have to do, if there is no other way. The point is that it is not that simple as in your snippet. First I need to convert ICompilationUnit to CompilationUnit and then apply the changes using TextEdits. I did it in about 20 lines of code, so the alternative of using createField() in one line was really tempting, especially that I do not use AST explicitly anywhere else in my code. – Patryk Oct 17 '14 at 08:31
  • Agree that the `createField()` is a much elegant option and uses JDT alone. But not sure of any another alternative. – Unni Kris Oct 17 '14 at 09:41
  • I was just traversing an Enum and can find that the `IType.getFields()` gives the Enum constant list, which may infer that the Enum constant is also a IField. Have you tried creating the constant with the `createField()` method? – Unni Kris Oct 17 '14 at 10:05
  • Unfortunately the answer does not describe how the AST can be converted back into Java source code. This seems to be possible (https://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html), but looks quite complicated. – FelixM Dec 19 '19 at 02:02
  • @FelixM Refer https://stackoverflow.com/questions/12878710/saving-modified-ast-in-a-new-file-with-eclipse-plugin/12879941#12879941 – Unni Kris Dec 20 '19 at 13:59