Menu

Generic Typing for Lists

Developers
2012-04-11
2012-10-08
  • Frederik Schmidt

    Hi Recoder Developers,

    sorry for bothering you with this. However, unfortunately im not able to find
    a way to implement the following scenario.
    I want to add generics to TypeReferences and New statements. So my code looks
    like

    HashMap map = new HashMap();

    I would like to transform it to
    HashMap<String, Constant=""> map = new HashMap<String, Constant="">

    Unfortunately, i am unable to find a way to add generics to the typeReference
    or New statement. Would be great if you could give me a hint how I could add
    generics to Statements.

    regards
    frederik

    I try to add a type annotation to a List HashMap.

     
  • Tobias Gutzmann

    Tobias Gutzmann - 2012-04-11

    Hi,

    You'll first have to get the type reference.
    For a "New" statement, that's just obtained through getTypeReference(). For a
    "VariableDeclaration" (covers fields, parameters, and local variables), it's
    also getTypeReference().

    If you're sure that a TypeReference has no TypeArguments (otherwise, you
    wouldn't want to add any anyway), the following approach should work:
    // preparation
    ASTList<TypeArgumentDeclaration> list = new
    ASTList<TypeArgumentDeclaration>();
    typeRef.setTypeArguments(list);

    ...
    code to create proper typeArgs.
    ...
    list.add(typeArg1);
    list.add(typeArg2);

    // don't forget the following:
    typeRef.makeParentRoleValid();
    changeHistory.attached(typeArg1);
    changeHistory.attached(typeArg2);

    Hope it helps, otherwise, don't hesitate to contact me!

    Regards,
    Tobias

     
  • Frederik Schmidt

    Hi Tobias,

    thanks for your immediate help. Works as anticipated!!!

    cheers
    fred

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.