Black monkey house: Scala annotation parameters

1, notes Parameter Description

Java annotations can have a band name argument

@Test(timeout = 100, expected = classOf[IOException])

However, if the parameter value is named, the name can be omitted, directly to the
value parameter is "creds"

@Named("creds") 
var credentials: Credentials = _  

If the annotations without parameters, the parentheses may be omitted to

@Entity 
class Credentials

Most annotation parameters have default values. For example, JUnit annotation of @Test timeout parameter has a default value of 0 indicates no timeout. The expected default parameters have a fake class said they did not expect any exception. If you use the following code

@Test def testSomeFeature(){….}

This is equivalent to writing notes

@Test{timeout = 0,expected = classOf[org.junit.Test.None]}
@Test def testSomeFeature(){….}

2, Java annotations parameter type can only be

(1) numeric literals
(2) strings
(3) in the literal
(4) Java enumeration
(5) Other notes
(6) of an array of the type described above (but not an array of arrays)
Scala annotation may be any type, but only a few notes Scala advantage of this additional (extra) flexibility.
This module can be understood as

Reproduced in: https: //www.jianshu.com/p/9adcf3d33124

Guess you like

Origin blog.csdn.net/weixin_34259159/article/details/91182515