Menu

Parameterized Types and TypeReference

Developers
2011-06-24
2012-10-08
  • Andrea Poli

    Andrea Poli - 2011-06-24

    Hi, I tried to create a TypeReference, using
    recoder.kit.TypeKit.createTypeReference, related to this type:

    LinkedHashMap<K, V>();
    

    K and V are mapped as
    recoder.service.DefaultProgramModelInfo.WrappedTypeArgument.
    The qualified names of K and V are:
    - net.sf.retrotranslator.transformer.ClassReplacement.<numeric id="">.K
    - net.sf.retrotranslator.transformer.ClassReplacement.<numeric id="">.V

    None of them are valid java identifiers so when I tried to create a type
    reference I received the following exception:

    java.lang.IllegalArgumentException: 29292008 is not a valid Java identifier
        at recoder.java.Identifier.<init>(Identifier.java:62)
        at recoder.java.JavaProgramFactory.createIdentifier(JavaProgramFactory.java:1218)
        at recoder.kit.MiscKit.createUncollatedReferenceQualifier(MiscKit.java:71)
        at recoder.kit.TypeKit.createTypeReference(TypeKit.java:123)
        at recoder.kit.TypeKit.createTypeReference(TypeKit.java:98)
        at recoder.kit.TypeKit.makeTypeArgRef(TypeKit.java:133)
        at recoder.kit.TypeKit.createTypeReference(TypeKit.java:166)
        at recoder.kit.TypeKit.createTypeReference(TypeKit.java:202)
    

    I patched this bug creating another constructor of Identifier which takes two
    arguments:

    public Identifier(UncollatedReferenceQualifier ref, String text)
    

    This constructor doesn't check if text is a valid java identifier.

    I saw a problem such like this when I tried to create type reference of an
    anonymous class. I solved that problem creating a type reference of the super
    class of the anonymous class.

    I'd like to know if there are other solutions related to these problems.

     
  • Tobias Gutzmann

    Tobias Gutzmann - 2011-06-26

    Hej,

    Well, first about anonymous classes: It's not possible to create type
    references to them. Creating references to the super class instead seems like
    a good idea. I don't see a problem with that.

    About the problem you describe above: Can you provide a minimal example
    (including your source code that makes use of Recoder)?

    Regards,
    Tobias

     
  • Andrea Poli

    Andrea Poli - 2011-06-27

    Hi,
    thank you for your reply :-) We are analyzing the code of RECODER (including
    retrotranslator) in order to test our tool. The class where the exceptions
    above arose is: net.sf.retrotranslator.transformer.ClassReplacement at line
    120.

    119:   private static <K, V> Map<K, V> createMap() {
    120:        return Collections.synchronizedMap(new LinkedHashMap<K, V>());
    121:   }
    

    I've just discovered that it was my fault. In the source code I try to make a
    type reference using the type of the parameter given by the method signature
    of the "synchronizedMap" method (which is java.util.Map<null, null="">) instead
    of the type of the expression (new LinkedHashMap<K,V>()) which is correctly
    java.util.LinkedHashMap<K,V>. Using the last type the method
    TypeKit.createTypeReference(...) works properly.

    Thanks a lot.

     

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.