2017-02-28 157 views
1

我想創建一個自定義的註釋跳過方法執行自定義Java註釋跳過方法執行

這是我的註釋代碼,以驗證類

@Target({ METHOD , FIELD , PARAMETER }) 
@Retention(RetentionPolicy.RUNTIME) 
@Constraint(validatedBy={MyValidator .class}) 
public @interface MyAnnotation { 

    String message() default "DEFAULT_FALSE"; 

    Class<?>[] groups() default{}; 

    Class<? extends Payload>[] payload() default{}; 

} 

我驗證嘗試過。這是我的驗證器看起來像

public class MyValidator implements ConstraintValidator<MyAnnotation, String >{ 

    @Override 
    public void initialize(MyAnnotation arg0) { 

    } 

    @Override 
    public boolean isValid(String arg0, ConstraintValidatorContext arg1) { 

     if(str=="msg"){ 
      return true; 
     } 
     return false; 
    } 

} 

這就是我想要如何使用 - 我想在方法級別使用註釋並跳過方法執行。

我不知道是否有可能..請幫助。

public class Test { 



    public static void main(String[] args) { 
    Test t = new Test(); 
     boolean valid=false; 

     valid=t.validate(); 
     System.out.println(valid); 

    } 

@MyAnnotation(message="msg") 
    public boolean validate(){ 

    // some code to return true or false 
    return true; 


    } 
} 
+3

您需要更改'如果(STR ==「味精」){'使用'等於()' –

+0

怎麼會是「跳躍」的事情將要發生? –

+0

嗨,那是我的問題,如果有可能使用這種驗證,我不知道,如果是可以做到的 –

回答

2

它其實很簡單,可以寫成最簡單的方面。 ;-)

關於您的示例代碼的醜陋之處在於它使用了幾個不顯示源代碼的類,所以我必須創建虛擬類/接口才能編譯代碼。你也不會展示驗證器是如何應用的,所以我必須推測。總之,這裏是一個完全自我一致的樣本類:

Helper類:

這僅僅是爲了使一切編譯腳手架。

package de.scrum_master.app; 

public interface Payload {} 
package de.scrum_master.app; 

public class ConstraintValidatorContext {} 
package de.scrum_master.app; 

public @interface Constraint { 
    Class<MyValidator>[] validatedBy(); 
} 
package de.scrum_master.app; 

import java.lang.annotation.Annotation; 

public interface ConstraintValidator<T1 extends Annotation, T2> { 
    void initialize(T1 annotation); 
    boolean isValid(T2 value, ConstraintValidatorContext validatorContext); 
} 
package de.scrum_master.app; 

public class MyValidator implements ConstraintValidator<MyAnnotation, String> { 
    @Override 
    public void initialize(MyAnnotation annotation) {} 

    @Override 
    public boolean isValid(String value, ConstraintValidatorContext validatorContext) { 
    if ("msg".equals(value)) 
     return true; 
    return false; 
    } 
} 
package de.scrum_master.app; 

import java.lang.annotation.Target; 
import static java.lang.annotation.ElementType.*; 

import java.lang.annotation.Retention; 
import static java.lang.annotation.RetentionPolicy.*; 

@Target({ METHOD, FIELD, PARAMETER }) 
@Retention(RUNTIME) 
@Constraint(validatedBy = { MyValidator.class }) 
public @interface MyAnnotation { 
    String message() default "DEFAULT_FALSE"; 
    Class<?>[] groups() default {}; 
    Class<? extends Payload>[] payload() default {}; 
} 

驅動程序:

如果你想測試的東西,你不只是需要一個積極的測試情況下,也一個負面的。因爲你沒有提供,所以用戶Sampisa的回答並不是你想要的。順便說一句,我認爲你應該能夠從中推導出解決方案。你甚至沒有嘗試。你沒有任何編程經驗?

package de.scrum_master.app; 

public class Application { 
    public static void main(String[] args) { 
    Application application = new Application(); 
    System.out.println(application.validate1()); 
    System.out.println(application.validate2()); 
    } 

    @MyAnnotation(message = "execute me") 
    public boolean validate1() { 
    return true; 
    } 

    @MyAnnotation(message = "msg") 
    public boolean validate2() { 
    return true; 
    } 
} 

看點:

爲什麼我除了Sampisa添加另一個示例方面的唯一原因是,他的解決方案是最理想的關於自己的倒影使用。它很醜,速度很慢。我認爲我的解決方案更加優雅。見自己:

package de.scrum_master.aspect; 

import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 

@Aspect 
public class SkipValidationAspect { 
    @Around("execution(@de.scrum_master.app.MyAnnotation(message=\"msg\") boolean *(..))") 
    public boolean skipValidation(ProceedingJoinPoint thisJoinPoint) throws Throwable { 
    return false; 
    } 
} 

很簡單,不是嗎?

控制檯日誌:

true 
false 

的Et瞧 - 我想這就是你要找的人。

+0

aye atain隊長...我看到的最好的答案之一stackoverflow.com –

+0

這工作正如我所料 –

+0

我只是編輯的兩個答案的方面點點,組合 –

3

您應該使用AOP了點。創建一個AspectJ項目,並嘗試這樣的事:

MyAnnotation.java:

package moo.aspecttest; 

import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME) 
@Target(value = { ElementType.METHOD }) 
public @interface MyAnnotation 
{ 
    public String value(); 
} 

MyAspectClass.java:

package moo.aspecttest; 

import java.lang.reflect.Method; 

import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.reflect.MethodSignature; 

@Aspect 
public class MyAspectClass 
{ 

    @Around("execution(* *(..))") 
    public Object aroundAdvice(ProceedingJoinPoint point) throws Throwable 
    { 
     Method method = MethodSignature.class.cast(point.getSignature()).getMethod(); 
     String name = method.getName(); 
     MyAnnotation puff = method.getAnnotation(MyAnnotation.class); 
     if (puff != null) { 
      System.out.println("Method " + name + " annotated with " + puff.value() + ": skipped"); 
      return null; 
     } else { 
      System.out.println("Method " + name + " non annotated: executing..."); 
      Object toret = point.proceed(); 
      System.out.println("Method " + name + " non annotated: executed"); 
      return toret; 
     } 
    } 
} 

MyTestClass.java:

package moo.aspecttest; 

public class MyTestClass 
{ 

    @MyAnnotation("doh") 
    public boolean validate(String s) { 
     System.out.println("Validating "+s); 
     return true; 
    } 

    public boolean validate2(String s) { 
     System.out.println("Validating2 "+s); 
     return true; 
    } 

    public static void main(String[] args) 
    { 
     MyTestClass mc = new MyTestClass(); 

     mc.validate("hello"); 
     mc.validate2("cheers"); 

     } 
    } 
} 

輸出,當你運行它產生:

Method main non annotated: executing... 
Method validate annotated with doh: skipped 
Method validate2 non annotated: executing... 
Validating2 cheers 
Method validate2 non annotated: executed 
Method main non annotated: executed 

我用了一個很普通的aroundAdvice, ,但你可以使用一個beforeAdvice,如果你想 。事實上,我認爲這一點很明確。

+0

後你好,這是一些我想做的,但我厭倦了這一點,執行流程甚至沒有進入方面課,你是否錯過了這裏的東西..? –

+0

我真正想要的是跳過方法執行的方法 - 驗證,你在示例中註釋 - 用@MyAnnotation(「doh」) –

+1

如果它不適合你,**你**錯過了一些東西,而不是@Sampisa。你可以學習如何以有意義的方式提出問題,這也表達了你的實際意圖? – kriegaex