0

I tried with active annotation of xtend, and I want to create a live annotation which can generate a String[] field to record the names of method parameters.

@Target(ElementType::TYPE)
@Active(typeof(ParameterRecorderProcessor))
annotation ParameterRecorder {
}

class ParameterRecorderProcessor extends AbstractClassProcessor {

    override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {

        var iii = 0;

        // add the public methods to the interface
        for (method : annotatedClass.declaredMethods) {
            if (method.visibility == Visibility::PUBLIC) {
                iii = iii + 1
                annotatedClass.addField(method.simpleName + "_" + iii) [
                    type = typeof(String[]).newTypeReference // String[] doesn't work

                    var s = ""
                    for (p : method.parameters) {
                        if(s.length > 0) s = s + ","
                        s = s + "\"" + p.simpleName + "\""
                    }
                    val ss = s

                    initializer = [
                        '''[«ss»]'''
                    ]
                ]
            }
        }
    }
}

You can see I use typeof(String[]).newTypeReference to define the type of new created field, but it doesn't work. The generated java code is looking like:

private Object index_1;

It uses Object and the initializer part has be empty.

How to fix it?

Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

1

This looks like a bug to me. As a workaround, you may want to use typeof(String).newTypeReference.newArrayTypeReference or more concise string.newArrayTypeReference

Sebastian Zarnekow
  • 6,609
  • 20
  • 23