Search code examples
javajavassist

How to define a field with a parameterized List using Javassist


How can I define a parameterized List field using Javassist? I have tried the following code which does not complain with an unparameterized List class, but causes a CannotCompileException when given a parameter.

    ClassPool pool = ClassPool.getDefault();
    pool.importPackage("java.util.List");
    CtClass cc = pool.makeClass("Test");
    CtField f = CtField.make("public List<String> p;", cc);
    // throws javassist.CannotCompileException: [source error] syntax error near "lic List<String> p;"

Solution

  • I'd guess the language compliance level of the compiler javassist is using internally is set to Java 1.4, which would explain why it doesn't understand generics (they were introduced in 1.4)