জাভা জেনেরিক্স ব্যবহার করে Class এবং Method সম্পর্কিত তথ্য এক্সট্রাক্ট করা বেশ কার্যকর, বিশেষ করে যখন Reflection API এর সাথে একত্রে ব্যবহৃত হয়। এটি জেনেরিক ক্লাস ও মেথডের টাইপ প্যারামিটার সম্পর্কিত তথ্য ডাইনামিকভাবে অ্যাক্সেস করতে সাহায্য করে।
Reflection এর সাহায্যে জেনেরিক তথ্য এক্সট্রাক্ট করা
জাভার java.lang.reflect প্যাকেজটি টাইম প্যারামিটার, টাইপ বাউন্ড, এবং জেনেরিক প্যারামিটার সম্পর্কিত তথ্য বের করার সুযোগ দেয়।
1. Generics সহ Class Information Extraction
উদাহরণ:
import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public class GenericClassInfo<T> {
public static void main(String[] args) {
// Anonymous subclass to retain generic type information
GenericClassInfo<List<String>> instance = new GenericClassInfo<List<String>>() {};
// Extract the superclass type
Type superClass = instance.getClass().getGenericSuperclass();
// Check if the type is parameterized
if (superClass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) superClass;
// Get actual type arguments
Type[] typeArguments = parameterizedType.getActualTypeArguments();
for (Type type : typeArguments) {
System.out.println("Type argument: " + type.getTypeName());
}
}
}
}
আউটপুট:
Type argument: java.util.List<java.lang.String>
2. Generics সহ Method Information Extraction
উদাহরণ:
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GenericMethodInfo {
public <T> void genericMethod(T param) {}
public static void main(String[] args) throws NoSuchMethodException {
// Get the method
Method method = GenericMethodInfo.class.getMethod("genericMethod", Object.class);
// Get the generic parameter types
Type[] parameterTypes = method.getGenericParameterTypes();
for (Type type : parameterTypes) {
System.out.println("Parameter type: " + type.getTypeName());
}
}
}
আউটপুট:
Parameter type: T
3. Extracting Return Type of Generic Methods
উদাহরণ:
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GenericReturnType {
public <T> T getGenericValue() {
return null;
}
public static void main(String[] args) throws NoSuchMethodException {
// Get the method
Method method = GenericReturnType.class.getMethod("getGenericValue");
// Extract the return type
Type returnType = method.getGenericReturnType();
System.out.println("Return type: " + returnType.getTypeName());
}
}
আউটপুট:
Return type: T
Generics এর মাধ্যমে Complex Class Structures Information Extraction
Generics সহ Nested Class এবং Multiple Type Extraction
উদাহরণ:
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
public class ComplexGenericClass<A, B> {
public static void main(String[] args) {
ComplexGenericClass<Map<String, Integer>, Double> instance = new ComplexGenericClass<>() {};
Type superClass = instance.getClass().getGenericSuperclass();
if (superClass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) superClass;
Type[] typeArguments = parameterizedType.getActualTypeArguments();
for (Type type : typeArguments) {
System.out.println("Type argument: " + type.getTypeName());
}
}
}
}
আউটপুট:
Type argument: java.util.Map<java.lang.String, java.lang.Integer>
Type argument: java.lang.Double
Annotation এর সাথে জেনেরিক্স এবং মেথড ইনফরমেশন Extraction
উদাহরণ:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface GenericAnnotation {}
public class GenericAnnotationExample {
@GenericAnnotation
public <T> T annotatedGenericMethod(T param) {
return param;
}
public static void main(String[] args) throws NoSuchMethodException {
Method method = GenericAnnotationExample.class.getMethod("annotatedGenericMethod", Object.class);
// Check if the method has the annotation
if (method.isAnnotationPresent(GenericAnnotation.class)) {
System.out.println("Method has GenericAnnotation");
// Extract the generic return type
System.out.println("Return type: " + method.getGenericReturnType());
}
}
}
আউটপুট:
Method has GenericAnnotation
Return type: T
প্রসঙ্গ ও সীমাবদ্ধতা
প্রসঙ্গ:
- Type Erasure: জেনেরিক্স টাইপ তথ্য Runtime-এ মুছে যায়, তবে Reflection ব্যবহার করে কিছু তথ্য পুনরুদ্ধার করা সম্ভব।
- Reusable Code: জেনেরিক ক্লাস বা মেথডের টাইপ প্যারামিটার ডাইনামিকভাবে বুঝতে পারলে, কোড আরও রিইউজেবল হয়।
সীমাবদ্ধতা:
- Type Erasure এর কারণে সীমিত ইনফরমেশন:
- Runtime-এ জেনেরিক টাইপ সম্পর্কে সীমিত তথ্য পাওয়া যায়।
- Complexity:
- জটিল জেনেরিক টাইপ বা ইনহেরিটেন্সের ক্ষেত্রে Reflective কোড আরও জটিল হয়ে যায়।
Generics এবং Reflection একত্রে ব্যবহার করে জেনেরিক ক্লাস ও মেথডের টাইপ প্যারামিটার সম্পর্কিত তথ্য ডাইনামিকভাবে বের করা সম্ভব। এটি অ্যাপ্লিকেশন ডেভেলপমেন্টে টাইপ সেফটি বজায় রেখে রিফ্লেকটিভ মেটাডেটা প্রসেসিং সহজ করে।
Read more