All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.TKnudsen.ComplexDataObject.model.tools.ReflectionTools Maven / Gradle / Ivy

Go to download

A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.

The newest version!
package com.github.TKnudsen.ComplexDataObject.model.tools;

import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 

* Description: little helpers when reflection is needed. *

* *

* Copyright: Copyright (c) 2017-2021 *

* * @author Juergen Bernard * @version 1.03 */ public class ReflectionTools { /** * @param fields * @param type * @param includeSuperClassFields * @return */ public static List getAllFields(List fields, Class type, boolean includeSuperClassFields) { if (fields == null) fields = new ArrayList<>(); try { for (Field field : type.getDeclaredFields()) fields.add(field); } catch (Exception e) { System.err.println("ReflectionTools: problems with getting the declared fields of a type:"); e.printStackTrace(); } if (includeSuperClassFields && type.getSuperclass() != null) fields = getAllFields(fields, type.getSuperclass(), includeSuperClassFields); return fields; } /** * * @param fields * @param instance * @param targetClass * @param includeSuperClassFields * @param rolloutCollectionsAndMaps * @return */ public static List getAllFieldsObjectsOfInstance(List fields, Object instance, Class targetClass, boolean includeSuperClassFields, boolean rolloutCollectionsAndMaps) { List targets = new ArrayList<>(); List allFields = getAllFields(null, instance.getClass(), includeSuperClassFields); for (Field field : allFields) { if (rolloutCollectionsAndMaps) { Collection collection = getObjectFromField(field, instance, Collection.class); if (collection != null) for (Object object : collection) if (object != null) targets.addAll(getAllFieldsObjectsOfInstance(null, object, targetClass, includeSuperClassFields, rolloutCollectionsAndMaps)); Map map = getObjectFromField(field, instance, Map.class); if (map != null) for (Object object : map.values()) targets.addAll(getAllFieldsObjectsOfInstance(null, object, targetClass, includeSuperClassFields, rolloutCollectionsAndMaps)); } if (targetClass.isAssignableFrom(field.getType())) { try { field.setAccessible(true); O target = (O) field.get(instance); targets.add(target); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } return targets; } /** * * @param field * @param instance * @param targetClass * @return */ public static O getObjectFromField(Field field, Object instance, Class targetClass) { if (!targetClass.isAssignableFrom(field.getType())) return null; try { field.setAccessible(true); O target = (O) field.get(instance); return target; } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } /** * retrieves the folder location where the class is living in. Advantage: this * is the folder of the classe's library, not the folder of the executing * project. * * @param c * @return */ public static String classLocation(Class c) { if (c == null) return null; URL url = c.getResource(c.getSimpleName() + ".class"); String folder = url.getFile(); return folder; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy