-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parameter expressions for executable methods (#10469)
* Fix parameter expressions for executable methods * extract private method (#10473) * More tests --------- Co-authored-by: Sergio del Amo <[email protected]>
- Loading branch information
Showing
3 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
inject-java/src/test/groovy/io/micronaut/expressions/ParameterExpressionsSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.micronaut.expressions | ||
|
||
import io.micronaut.annotation.processing.test.AbstractEvaluatedExpressionsSpec | ||
import io.micronaut.context.annotation.Value | ||
import io.micronaut.inject.annotation.EvaluatedAnnotationValue | ||
|
||
class ParameterExpressionsSpec extends AbstractEvaluatedExpressionsSpec { | ||
|
||
void "test evaluating method parameter expression"() { | ||
given: | ||
def ctx = buildContext('tst.MyBean', """ | ||
package tst; | ||
import io.micronaut.context.annotation.Executable; | ||
import io.micronaut.context.annotation.Value; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
@Singleton | ||
class MyBean { | ||
String someParam; | ||
@Executable | ||
void doStuff(@Value("#{1 + 1 + this.myNumber()}") String someParam) { | ||
this.someParam = someParam; | ||
} | ||
int myNumber() { | ||
return 5; | ||
} | ||
} | ||
""") | ||
|
||
|
||
def bean = ctx.getBean(ctx.getClassLoader().loadClass("tst.MyBean")) | ||
def beanDefinition = ctx.getBeanDefinition(ctx.getClassLoader().loadClass("tst.MyBean")) | ||
when: | ||
def av = beanDefinition.getRequiredMethod("doStuff", String.class).getArguments()[0].getAnnotationMetadata().getAnnotation(Value.class) | ||
then: | ||
(av as EvaluatedAnnotationValue).withArguments(bean, "MyValue").stringValue().get() == "7" | ||
when: | ||
buildContext('tst.MyBean', """ | ||
package tst; | ||
import io.micronaut.context.annotation.Executable; | ||
import io.micronaut.context.annotation.Value; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
@Singleton | ||
class MyBean { | ||
@Executable | ||
static void doStuffStatic(@Value("#{1 + 1 + this.myNumber()}") String someParam) { | ||
} | ||
int myNumber() { | ||
return 5; | ||
} | ||
} | ||
""") | ||
then: | ||
def e = thrown(Exception) | ||
e.message.contains("Cannot reference 'this'") | ||
when: | ||
buildContext('tst.MyBean2', """ | ||
package tst; | ||
import io.micronaut.context.annotation.Executable; | ||
import io.micronaut.context.annotation.Value; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
@Singleton | ||
class MyBean2 { | ||
String someParam; | ||
MyBean2(@Value("#{1 + 1 + this.myNumber()}") String someParam) { | ||
this.someParam = someParam; | ||
} | ||
} | ||
""") | ||
then: | ||
def ee = thrown(Exception) | ||
ee.message.contains("Cannot reference 'this'") | ||
|
||
cleanup: | ||
ctx.stop() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters