जावा में कस्टम क्लास मैपर
इस लेख में, मैं समझाने जा रहा हूँ कि मैपस्ट्रक्चर जैसे तीसरे पक्ष के पुस्तकालयों का उपयोग किए बिना जावा में एक कस्टम मैपर उपयोग वर्ग को कैसे लागू किया जाए।
दोस्तों सबसे पहले आपको जावा प्रोग्रामिंग लैंग्वेज और जावा रिफ्लेक्शन एपीआई की बेसिक जानकारी होनी चाहिए।
चलो शुरू करो।
public static <O, Target> O map(Object instance, Class<Target> targetClass) {
if (instance.getClass().isPrimitive() || targetClass.isPrimitive()) {
throw new RuntimeException("Provide a non primitive class");
}
if (instance.getClass().isInterface() || targetClass.isInterface()) {
throw new RuntimeException("Provide a non interface class");
}
if (instance.getClass().isEnum() || targetClass.isEnum()) {
throw new RuntimeException("Provide a non enum class");
}
if (Modifier.isAbstract(instance.getClass().getModifiers()) || Modifier.isAbstract(targetClass.getModifiers())) {
throw new RuntimeException("Provide a non abstract class");
}
return doMap(instance, instance.getClass(), targetClass);
}
@SneakyThrows
private static <O, Source, Target> O doMap(Object sourceInstance, Class<Source> sourceClass, Class<Target> targetClass) {
Set<Field> declaredSourceFields = findAllFields(sourceClass);
Set<Field> declaredTargetFields = findAllFields(targetClass);
Object targetInstance = targetClass.getDeclaredConstructor().newInstance();
for (Field field : declaredTargetFields) {
Field sourceField = findSourceField(field, declaredSourceFields);
if (sourceField != null) {
field.setAccessible(true);
field.set(targetInstance, sourceField.get(sourceInstance));
}
}
return (O) targetInstance;
}
public static Set<Field> findAllFields(Class<?> inputClass) {
Set<Field> allFields = new HashSet<>();
if (inputClass == null) {
return allFields;
}
Field[] fields = inputClass.getDeclaredFields();
allFields.addAll(Set.of(fields));
allFields.addAll(findAllFields(inputClass.getSuperclass()));
return allFields;
}
private static Field findSourceField(Field targetField, Set<Field> declaredSourceFields) {
for (Field field : declaredSourceFields) {
field.setAccessible(true);
String sourceFieldName = field.getName();
if (sourceFieldName.equals(targetField.getName()) &&
field.getType().equals(targetField.getType())) {
return field;
}
}
return null;
}
उदाहरण:
@Setter
@Getter
public class SourceClass {
String surname;
Integer age;
private String name;
@Override
public String toString() {
return "SourceClass{" +
"surname='" + surname + '\'' +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
@Setter
@Getter
public class TargetClass {
Integer age;
private String name;
@Override
public String toString() {
return "TargetClass{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
public static void main(String[] args) {
SourceClass sourceInstance = new SourceClass();
sourceInstance.setAge(27);
sourceInstance.setName("Java");
sourceInstance.setSurname("Reflection");
TargetClass targetInstance = CustomMapper.map(sourceInstance, TargetClass.class);
System.out.println(targetInstance);
}
उपनाम आउटपुट में मौजूद नहीं है क्योंकि यह लक्षित वर्ग में उपलब्ध नहीं है। इसलिए मानचित्र विधि इस क्षेत्र की उपेक्षा करती है।
निष्कर्ष:
जावा में लिखी गई अधिकांश लाइब्रेरी, फ्रेमवर्क और आईडी जावा रिफ्लेक्शन एपीआई का लाभ उठाते हैं। लेकिन यह एपीआई थोड़ा धीमा है क्योंकि यह प्रत्येक ऑपरेशन के लिए सुरक्षा अनुमतियों की जांच करता है। बेहतर प्रदर्शन के लिए आप इस लिंक से जावा मेथड हैंडल की जांच कर सकते हैंhttps://docs.oracle.com/javase/8/docs/api/java/lang/invoke/MethodHandles.html.
पढ़ने के लिए आपका शुक्रिया।
साभार

![क्या एक लिंक्ड सूची है, वैसे भी? [भाग 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































