Cannot move method body, if type arguments are involved: "Cannot make a static reference to the non-static type T"

64 views
Skip to first unread message

stefan...@gmx.de

unread,
Jan 14, 2016, 12:34:16 PM1/14/16
to Xtend Programming Language
Hello @all,

currently, I have an issue during active annotation processing and copying methods. I know that this is a well discussed topic already.

In a simple example I created, I copy a method (head), rename it and move the body of another method to the created one (active annotation processing).
The problem does only exist if using type arguments.

Some code:




 
static def copyMethod(MutableClassDeclaration clazz, MethodDeclaration method) {

  val methodDeclaration
= clazz.addMethod(method.simpleName) [
    it
.abstract = method.abstract
    it
.docComment = method.docComment
    it
.visibility = method.visibility
    it
.synchronized = method.synchronized
    it
.deprecated = method.deprecated
    it
.static = method.static
    it
.exceptions = method.exceptions
    it
.varArgs = method.varArgs
    it
.strictFloatingPoint = method.strictFloatingPoint
   
]

 
for (t : method.typeParameters)
    methodDeclaration
.addTypeParameter(t.simpleName, t.upperBounds)

  methodDeclaration
.returnType = method.returnType

 
for (p : method.parameters) {
    methodDeclaration
.addParameter(p.simpleName, p.type)
   
}

 
return methodDeclaration
 
}
 
 
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
   
   
for (m : annotatedClass.declaredMethods) {
   
    val newMethod
= annotatedClass.copyMethod(m)
    newMethod
.simpleName = newMethod.simpleName + "_copiedMethod"
    newMethod
.body = m.body
    m
.body = '''// new method'''
   
   
}
   
 
}
 


If using the annotation in the following example, you will get the error "Cannot make a static reference to the non-static type T" in the line with "var T myObj = null". I also saw errors like "Cannot cast T to T" already. Somehow, when the body is in the copied method, the "T" from the new method does not match to the "T" from the original method.


  @SomeAnnotation
   
class SomeAnnotationTest {
 
   
def <T> void myMethod() {
     
var T myObj = null;
   
}

 
}


As an additional information: the generated code has no problem at all. The problem is just that xtend reports an error.

You might ask, of course, why I want that annotation. Of course, it is just an easy example. I found many tricks and workarounds in this forum which require you to move the body and also I would need this. Actually, my real use case perfectly works already, but it struggles, if type parameters are involved. Therefore, I've created the easy-to-understand example. I will attach the whole project in one ZIP and attach. There you can easily see and reproduce the issue.

A probably related question ("how to copy type arguments") was already asked in https://groups.google.com/forum/#!msg/xtend-lang/X8uYmJOk4hA/lVXZjjjnrr0J.

example.zip

Norbert Sándor

unread,
Jan 24, 2016, 8:11:08 AM1/24/16
to Xtend Programming Language, stefan...@gmx.de
Now I cannot check your example code throughly - sorry.

Just one question: have you tried the method de.oehme.xtend.contrib.SignatureHelper.copySignatureFrom() in xtend-contrib?
It is used for example by @Cached which seems to work for complex generic method signatures as well.
Maybe check CachedProcessor, its implementation is very similar to what you would like to achieve...

BR,
Norbi

stefan...@gmx.de

unread,
Jan 25, 2016, 11:57:27 AM1/25/16
to Xtend Programming Language, stefan...@gmx.de
Hello Norbert,

thanks a lot for this hint. I already found lots of interesting things happening in this class.


Unfortunately, also with SignatureHelper the problem remains. You cannot use it for a method like "def <T> void myMethod()" where you actually use T inside.


I've used "SignatureHelper.addIndirection", which does exactly what I need. Internally, "copySignatureFrom" is used. However, if dealing with type parameters, it won't work.


BR

Sven Efftinge (sven@efftinge.de)

unread,
Jan 26, 2016, 3:25:26 AM1/26/16
to xtend...@googlegroups.com
Hi Stefan,

you have found a bug. 
Thanks, for reporting.

Sven

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

stefan...@gmx.de

unread,
Feb 5, 2016, 8:42:37 AM2/5/16
to Xtend Programming Language

With the newest nightly the issue is really fixed.

Thanks a lot!


Reply all
Reply to author
Forward
0 new messages