簡體   English   中英

像 Lombok 這樣的自定義注釋

[英]Custom Annotation like Lombok

我想實現一個自定義注釋,當其他類使用它時會向它們公開兩種方法。 如下所示:

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface BaseEntity{
     public String toString(){ return "Hello" ;}
     public String toCustomString() { return "my hello";}
}

現在我想要的是任何 class 使用上述注釋。 默認情況下,它會將這些方法暴露給它,就像我們使用 @Getter 時 Lombok 所做的那樣

@BaseEntity
public class Person{}
Person p = new Person();
p.toCustomString(); // this should work

您需要創建一個注解處理 class(它必須是javax.annotation.processing.AbstractProcessor的子類)才能將代碼實際添加到Person class 中。

參見: http://hannesdorfmann.com/annotation-processing/annotationprocessing101

雖然不完全符合您的要求,但我認為您可以使用利用注釋的 class 層次結構來實現您想要的:

@Retention(RetentionPolicy.RUNTIME)                                             
@Target(ElementType.TYPE)                                                       
public @interface BaseEntity {                                                  
    String string();                                                            
    String customString();                                                      
}                                                                               
                                                                                
public abstract class AbstractClass {                                           
    @Override                                                                   
    public String toString() {                                                  
        return getClass().getAnnotation(BaseEntity.class).string();             
    }                                                                           
                                                                                
    public String toCustomString() {                                            
        return getClass().getAnnotation(BaseEntity.class).customString();       
    }                                                                           
}

然后是一個具體的子類:

@BaseEntity(string = "Hello", customString = "my hello")                        
public class Example extends AbstractClass {                                    
    public static void main(String[] args) throws Exception {                   
        Example example = new Example();                                        
                                                                                
        System.out.println(example.toString());                                 
        System.out.println(example.toCustomString());                           
    }                                                                           
}

產量:

Hello                                                                           
my hello

針對您的評論,我的實際解決方案是使用默認方法定義注釋和接口(這使得toString()實現有問題):

@BaseEntity(customString = "my hello")                                          
public class Example implements BaseEntityAnnotated {                           
    public static void main(String[] args) throws Exception {                   
        Example example = new Example();                                        
                                                                                
        System.out.println(example.toCustomString());                           
    }                                                                           
}                                                                               
                                                                                
@Retention(RetentionPolicy.RUNTIME)                                             
@Target(ElementType.TYPE)                                                       
public @interface BaseEntity {                                                  
    String customString();                                                      
}                                                                               
                                                                                
public interface BaseEntityAnnotated {                                          
    default String toCustomString() {                                           
        return this.getClass().getAnnotation(BaseEntity.class).customString();  
    }                                                                           
}

然后我實現了一個Processor ,它強制使用BaseEntity注釋的實體也必須實現BaseEntityAnnotated

我在AntTaskAnnotatedAntTask.getAntTaskName()AntTaskProcessor有示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM