com.github.simondan.ojcms.beans.util.StorableUtil Maven / Gradle / Ivy
The newest version!
package com.github.simondan.ojcms.beans.util;
import com.github.simondan.ojcms.beans.definition.*;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.*;
import java.util.*;
/**
* Utility-Klasse für die StorableObject-Definition
*
* @author Simon Danner, 02.06.2016.
*/
public final class StorableUtil
{
private StorableUtil()
{
}
public static Map getDescriptionKeyMap(Class extends AbstractStorableObject> pClass)
{
List declaredFields = new ArrayList<>();
//Alle Felder (auch der Super-Klassen) ermitteln
Class current = pClass;
do
{
declaredFields.addAll(Arrays.asList(current.getDeclaredFields()));
}
while ((current = current.getSuperclass()) != null);
Map map = new LinkedHashMap<>();
for (Field field : declaredFields)
if (Modifier.isStatic(field.getModifiers()) && AbstractFieldDescription.class.isAssignableFrom(field.getType()))
try
{
map.put((AbstractFieldDescription) field.get(null), field.getName());
}
catch (IllegalAccessException pE)
{
throw new RuntimeException(pE);
}
return map;
}
@Nullable
public static AbstractFieldDescription getDescriptionFromKey(Map pDescKeyMap, String pKey)
{
for (AbstractFieldDescription description : pDescKeyMap.keySet())
if (pDescKeyMap.get(description).equals(pKey))
return description;
return null;
}
}