我想创建自己的注释来注释一些局部变量.编写注释不是问题,问题是在运行时获取它们的信息.我只能从带注释的方法或方法参数中获取一些信息,但不能从局部变量中获取.有没有办法得到它?
我自己的注释是这样的:
public void m(int a)
@MyOwnAnnotation(some information)
int b = 5;
}
Run Code Online (Sandbox Code Playgroud)
或者,作为替代方案,有没有办法获取方法的代码,进一步解析它,最后得到注释值?
提前致谢.
我正在尝试为局部变量创建注释.我知道我不能在生成的字节码中保留注释,但我应该能够在编译时通过执行以下操作来访问信息:
@Target({ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface Junk {
String value();
}
Run Code Online (Sandbox Code Playgroud)
只有,当我在下面指定了支持类型中具有"垃圾"的ProcessorFactory时,这不会被apt或javac处理:
class JunkTester {
public static void main(String[] args) {
@Junk String tmp = "Hello World";
System.out.println(tmp);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我之前移动@Junk注释时它会起作用 public static
思考和/或解决方法?
我为模型映射器配置了以下配置,以将User类的实例转换为实例ExtendedGetUserDto.
public ExtendedGetUserDto convertToExtendedDto(User user) {
PropertyMap<User, ExtendedGetUserDto> userMap = new PropertyMap<User, ExtendedGetUserDto>() {
protected void configure() {
map().setDescription(source.getDescription());
map().setId(source.getId());
// map().setReceivedExpenses(
// source.getReceivedExpenses()
// .stream()
// .map(expense -> expenseDtoConverter.convertToDto(expense))
// .collect(Collectors.toSet())
// );
Set<GetInvitationDto> result = new HashSet<GetInvitationDto>();
for (Invitation inv: source.getReceivedInvitations()) {
System.out.println("HELLO");
//result.add(null);
}
//map().setReceivedInvitations(result);
}
};
modelMapper.addMappings(userMap);
return modelMapper.map(user, ExtendedGetUserDto.class);
}
Run Code Online (Sandbox Code Playgroud)
在评论之前setReceivedExpense我收到了这个错误:
org.modelmapper.ConfigurationException: ModelMapper configuration errors:
1) Invalid source method java.util.stream.Stream.map(). Ensure that method has zero parameters and does not …Run Code Online (Sandbox Code Playgroud)