Created
April 12, 2017 17:25
-
-
Save InsanusMokrassar/7bb7cd29989b2d469df044f598d3dc52 to your computer and use it in GitHub Desktop.
Sheet of mamont. I think it will useless, but I will be able to be wrong
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FieldsExtractor { | |
protected static final Map<Class, List<Field>> classFieldsCache = new HashMap<>(); | |
public static List<Field> getPublicFields(Class targetClass) { | |
List<Field> result = new ArrayList<>(); | |
if (!classFieldsCache.containsKey(targetClass)) { | |
List<Field> fields = new ArrayList<>(); | |
Class currentValuesTypeClass = targetClass; | |
do { | |
fields.addAll(Arrays.asList(currentValuesTypeClass.getDeclaredFields())); | |
currentValuesTypeClass = currentValuesTypeClass.getSuperclass(); | |
} while (currentValuesTypeClass != null); | |
classFieldsCache.put(targetClass, fields); | |
} | |
result.addAll(classFieldsCache.get(targetClass)); | |
for (int i = 0; i < result.size(); i++) { | |
Field currentField = result.get(i); | |
int modifiers = currentField.getModifiers(); | |
if (Modifier.isStatic(modifiers) || | |
currentField.isSynthetic() || | |
!Modifier.isPublic(modifiers)) { | |
result.remove(i); | |
i--; | |
continue; | |
} | |
try { | |
currentField.getType().getConstructor(String.class); | |
} catch (NoSuchMethodException e) { | |
throw new IllegalStateException("Can't create isntance", e); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment