CodeBlanks Logo Icon CodeBlanks

Reflection ve annotation

Reflection ve annotation ile yapılandırma yazarsın.

@interface ile annotation

Bu derste özel bir annotation tanımlayacaksın. @interface ile bir işaretleme türü oluşturulur. String value() tek bir özellik tanımlar.

@interface Logged {
    String value();
}

Bir metodu @Logged("audit") ile işaretlersin. Değer daha sonra reflection ile okunabilir. Bu, koda meta bilgi eklemenin temiz bir yoludur.

reflection ile metot çağırma

reflection ile bir sınıfın metotlarını çalışma zamanında bulup çağırabilirsin. getMethod("describe") metodu ada göre arar; invoke örnek üzerinde çağırır.

Method m = EventService.class.getMethod("describe");
String result = (String) m.invoke(service);
Logged logged = m.getAnnotation(Logged.class);
System.out.println(logged.value());

Burada @Logged annotation değeri olan audit dizgisi okunur. Bu,annotation tabanlı işaretleme ve okuma akışını gösterir.

Uygulama Reflection ile describe ve @Logged değerini yazdır

EventService sınıfında @Logged("audit") ile işaretli describe metodu var. Method m = EventService.class.getMethod("describe"); ile metodu bul. m.invoke(service) ile çağır ve sonucu yazdır. m.getAnnotation(Logged.class) ile annotation'i al ve value()'yu yazdır.

Başlangıç kodu

Main.java
import java.util.ArrayList;

class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

@interface Logged {
    String value();
}

class EventService {
    @Logged("audit")
    public String describe() {
        return "event-service";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        // EventService.describe metodunu reflection ile çağır
        // @Logged annotation değerini yazdır
    }
}

Örnek çözüm

Main.java
import java.util.ArrayList;
import java.lang.reflect.*;

class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

@interface Logged {
    String value();
}

class EventService {
    @Logged("audit")
    public String describe() {
        return "event-service";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        EventService service = new EventService();
        Method m = EventService.class.getMethod("describe");
        String result = (String) m.invoke(service);
        Logged logged = m.getAnnotation(Logged.class);
        System.out.println(result);
        System.out.println(logged.value());
    }
}

Beklenen çıktı

event-service
audit

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 annotation tanımını tamamla

Soru
@interface Logged {
    String ____();
}
Cevap
@interface Logged {
    String value();
}

Açıklama: value() annotation'un tek özelliğidir.

2 getMethod("describe") ne döndürür?

  • Bir Method nesnesi Doğru cevap
  • Bir String
  • Bir int
  • Bir Class

Açıklama: getMethod bir Method nesnesi döndürür.

3 Reflection kavramlarını eşleştir

Kavram Anlamı
@interface Annotation tanımı
getMethod Metodu ada göre bulur
invoke Metodu çağırır
getAnnotation Annotation'u okur

İleri seviye bölümündeki tüm dersler