具有名称常量
/** from * N o w J a v a . c o m - 时代Java **/ //package com.nowjava; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; public class Main { public static boolean hasConstantWithName(final Class<?> clazz, final String fieldName) { for (final Field field : retrieveConstantFields(clazz)) { if (field.getName().equals(fieldName)) { return true; }//from N o w J a v a . c o m } return false; } public static List<Field> retrieveConstantFields(final Class<?> clazz) { final List<Field> fields = new ArrayList<Field>(); for (final Field field : retrieveAllFields(clazz)) { if (!isFieldToBeProvided(field)) { fields.add(field); } } return fields; } public static List<Field> retrieveAllFields(final Class<?> clazz) { final List<Field> fields = new ArrayList<Field>(); Class<?> currentClass = clazz; while (!Object.class.equals(currentClass)) { for (final Field field : currentClass.getDeclaredFields()) { fields.add(field); } currentClass = currentClass.getSuperclass(); } return fields; }