相关疑难解决方法(0)

ElementType.LOCAL_VARIABLE注释类型

我想创建自己的注释来注释一些局部变量.编写注释不是问题,问题是在运行时获取它们的信息.我只能从带注释的方法或方法参数中获取一些信息,但不能从局部变量中获取.有没有办法得到它?

我自己的注释是这样的:

public void m(int a)  
@MyOwnAnnotation(some information)  
int b = 5;  
}  
Run Code Online (Sandbox Code Playgroud)

或者,作为替代方案,有没有办法获取方法的代码,进一步解析它,最后得到注释值?

提前致谢.

java reflection annotations

14
推荐指数
3
解决办法
7582
查看次数

如何创建处理局部变量的注释处理器?

我正在尝试为局部变量创建注释.我知道我不能在生成的字节码中保留注释,但我应该能够在编译时通过执行以下操作来访问信息:

@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

思考和/或解决方法?

java annotations

8
推荐指数
2
解决办法
4174
查看次数

ModelMapper:确保该方法的参数为零,并且不返回void

我为模型映射器配置了以下配置,以将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)

java spring modelmapper

8
推荐指数
1
解决办法
3176
查看次数

标签 统计

java ×3

annotations ×2

modelmapper ×1

reflection ×1

spring ×1