Domanda

I'm tring to generate, using the Xtend Xbase API, a simple Java mathod. This is the Xtend code.

        var jvmMethod = element.toMethod("fromSap",element.newTypeRef(element.dtoFullName))[
    var jvmTypeReference= fromSap.sapType;
    var param = toParameter("sapOb",jvmTypeReference)
    parameters+=param
    body = ['''....''')]

everything works fine except the parameter that has the final modifier. This is the generated code:

  public class DTOTest {
     public DTOTest fromSap(final String sapOb) {
    DTOTest result = new DTOTest();
    return result;   
  }
}

I tried to add:

param.setFinal(false);

but I got

This expression is not allowed in this context, since it doesn't cause any side effects.

at compile time

Can someone help me to remove the final modifier from the generated code?

Davide

È stato utile?

Soluzione

Method parameters are considered to be final in the linking / scoping of the expression that is contained in your method. If parameters weren't final, none of them could be used from within lambda expressions. Unfortunately there is no means of 'final' in the reflective API (java.lang.reflect) for parameters. Therefore no property 'isFinal' can be exposed but we simply mark them as such internally.

The error message 'This expression is not allowed in this context, since it doesn't cause any side effects.' is misleading though. It likely shadows the real error that there is no such method JvmFormalParameter.setFinal(boolean).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top