今天我想按照这个文档创建我的第一个注释界面,我得到了编译器错误
Run Code Online (Sandbox Code Playgroud)Invalid type for annotation member": public @interface MyAnnotation { Object myParameter; ^^^^^^ }
显然Object不能用作注释成员的类型.不幸的是,我找不到任何关于哪些类型可以使用的信息.
我发现这是使用反复试验:
String →有效int →有效Integer →无效(令人惊讶)String[] →有效(令人惊讶)Object →无效也许某人可以了解实际允许哪些类型以及原因.
如果我在Spring bean中的私有方法上有@Transactional -annotation,那么注释是否有效?
如果@Transactional注释是在公共方法上,则它可以工作并打开事务.
public class Bean {
public void doStuff() {
doPrivateStuff();
}
@Transactional
private void doPrivateStuff() {
}
}
...
Bean bean = (Bean)appContext.getBean("bean");
bean.doStuff();
Run Code Online (Sandbox Code Playgroud) 我知道这不是最重要的问题,但我只是意识到我可以在注释之前或之后放置javadoc注释块.我们希望采用什么作为编码标准?
/**
* This is a javadoc comment before the annotation
*/
@Component
public class MyClass {
@Autowired
/**
* This is a javadoc comment after the annotation
*/
private MyOtherClass other;
}
Run Code Online (Sandbox Code Playgroud) 以明确的方式有谁能解释之间的实际差别java.lang.annotation.RetentionPolicy常数SOURCE,CLASS和RUNTIME?
我也不完全确定"保留注释"这个短语是什么意思.
功能注释:PEP-3107
我跑过一段代码,展示了Python3的功能注释.这个概念很简单,但我想不出为什么这些在Python3中实现或者对它们有任何好用.也许SO可以启发我吗?
这个怎么运作:
def foo(a: 'x', b: 5 + 6, c: list) -> max(2, 9):
... function body ...
Run Code Online (Sandbox Code Playgroud)
参数后面后面的所有内容都是"注释",后面的信息->是函数返回值的注释.
foo.func_annotations将返回一个字典:
{'a': 'x',
'b': 11,
'c': list,
'return': 9}
Run Code Online (Sandbox Code Playgroud)
有这个有什么意义?
所以问题是能够结合多个警告抑制,以便每个项目不需要它自己的@SuppressWarnings注释.
例如:
public class Example
public Example() {
GO go = new GO(); // unused
....
List<String> list = ( List<String> ) go.getList(); // unchecked
}
...
// getters/setters/other methods
}
Run Code Online (Sandbox Code Playgroud)
现在@SuppressWarnings我没有两个警告,而是在课堂上有两个警告,所以这样:
@SuppressWarnings( "unused", "unchecked" )
public class Example
public Example() {
GO go = new GO(); // unused - suppressed
....
List<String> list = ( List<String> ) go.getList(); // unchecked - suppressed
}
...
// getters/setters/other methods
}
Run Code Online (Sandbox Code Playgroud)
但这不是一个有效的语法,有没有办法做到这一点?
我认为这在Java中是不可能的,因为注释及其参数在编译时被解析.我有一个如下界面,
public interface FieldValues {
String[] FIELD1 = new String[]{"value1", "value2"};
}
Run Code Online (Sandbox Code Playgroud)
和另一个班级,
@SomeAnnotation(locations = {"value1", "value2"})
public class MyClass {
....
}
Run Code Online (Sandbox Code Playgroud)
我用注释标记了很多类,我想知道我是否可以避免在每个注释中指定字符串而我更愿意使用
@SomeAnnotation(locations = FieldValues.FIELD1)
public class MyClass {
....
}
Run Code Online (Sandbox Code Playgroud)
但是这会产生编译错误,例如注释值应该是数组初始化器等.有人知道如何使用String常量或String []常量来为注释提供值吗?
我今天遇到了lombok.
我非常想知道它是如何工作的.
Java Geek文章提供了一些线索,但对我来说并不是很清楚:
Java 6删除了apt并使javac能够管理注释,简化流程以获得更简单的单步计算.这是龙目岛采取的路径.
也许使用Java 6编译过程将是:javac - > apt - > lombok apt process - >读取类文件并使用ASM添加set/get方法?
你能告诉我关于机制的更多细节吗?
有人可以解释Spring 3中的注释@RequestBody和@ResponseBody注释吗?它们适用于什么?任何例子都会很棒.
annotations ×10
java ×9
spring ×2
arrays ×1
coding-style ×1
eclipse ×1
function ×1
javadoc ×1
lombok ×1
python ×1
python-3.x ×1
spring-mvc ×1
transactions ×1