List of usage examples for java.lang Class getEnumConstants
public T[] getEnumConstants()
From source file:org.structr.common.ValidationHelper.java
/** * Checks whether the value for the given property key of the given node * is a valid enum value of the given type. In case of an error, the * type identifiery in typeString is used for the error message. * * @param typeString//from w w w. j a v a2 s. c o m * @param node the node * @param key the property key * @param enumType the enum type to check against * @param errorBuffer the error buffer * * @return true if there is an error checking the given node */ public static boolean checkStringInEnum(final String typeString, final GraphObject node, final PropertyKey<? extends Enum> key, Class<? extends Enum> enumType, final ErrorBuffer errorBuffer) { Enum value = node.getProperty(key); Enum[] values = enumType.getEnumConstants(); for (Enum v : values) { if (v.equals(value)) { return false; } } errorBuffer.add(new ValueToken(typeString, key, values)); return true; }
From source file:org.wildfly.swarm.microprofile.openapi.runtime.util.JandexUtil.java
/** * Reads a String property value from the given annotation instance. If no value is found * this will return null.//from ww w .ja v a 2 s . c o m * @param annotation * @param propertyName */ @SuppressWarnings("rawtypes") public static <T extends Enum> T enumValue(AnnotationInstance annotation, String propertyName, Class<T> clazz) { AnnotationValue value = annotation.value(propertyName); if (value == null) { return null; } String strVal = value.asString(); T[] constants = clazz.getEnumConstants(); for (T t : constants) { if (t.name().equals(strVal)) { return t; } } for (T t : constants) { if (t.name().equalsIgnoreCase(strVal)) { return t; } } return null; }
From source file:org.rhq.core.domain.server.PersistenceUtility.java
@SuppressWarnings("unchecked") // used in hibernate.jsp public static Object cast(String value, Type hibernateType) { if (hibernateType instanceof PrimitiveType) { Class<?> type = ((PrimitiveType) hibernateType).getPrimitiveClass(); if (type.equals(Byte.TYPE)) { return Byte.valueOf(value); } else if (type.equals(Short.TYPE)) { return Short.valueOf(value); } else if (type.equals(Integer.TYPE)) { return Integer.valueOf(value); } else if (type.equals(Long.TYPE)) { return Long.valueOf(value); } else if (type.equals(Float.TYPE)) { return Float.valueOf(value); } else if (type.equals(Double.TYPE)) { return Double.valueOf(value); } else if (type.equals(Boolean.TYPE)) { return Boolean.valueOf(value); }/*from ww w . j a va 2s . c om*/ } else if (hibernateType instanceof EntityType) { String entityName = ((EntityType) hibernateType).getAssociatedEntityName(); try { Class<?> entityClass = Class.forName(entityName); Object entity = entityClass.newInstance(); Field primaryKeyField = entityClass.getDeclaredField("id"); primaryKeyField.setAccessible(true); primaryKeyField.setInt(entity, Integer.valueOf(value)); return entity; } catch (Throwable t) { throw new IllegalArgumentException("Type[" + entityName + "] must have PK field named 'id'"); } } else if (hibernateType instanceof CustomType) { if (Enum.class.isAssignableFrom(hibernateType.getReturnedClass())) { Class<? extends Enum<?>> enumClass = hibernateType.getReturnedClass(); Enum<?>[] enumValues = enumClass.getEnumConstants(); try { int enumOrdinal = Integer.valueOf(value); try { return enumValues[enumOrdinal]; } catch (ArrayIndexOutOfBoundsException aioobe) { throw new IllegalArgumentException("There is no " + enumClass.getSimpleName() + " enum with ordinal '" + enumOrdinal + "'"); } } catch (NumberFormatException nfe) { String ucaseValue = value.toUpperCase(); for (Enum<?> nextEnum : enumValues) { if (nextEnum.name().toUpperCase().equals(ucaseValue)) { return nextEnum; } } throw new IllegalArgumentException( "There is no " + enumClass.getSimpleName() + " enum with name '" + value + "'"); } } } return value; }
From source file:sk.lazyman.gizmo.util.GizmoUtils.java
public static <T extends Enum> IModel<List<T>> createReadonlyModelFromEnum(final Class<T> type) { return new AbstractReadOnlyModel<List<T>>() { @Override//from w w w . ja v a 2s. c o m public List<T> getObject() { List<T> list = new ArrayList<T>(); Collections.addAll(list, type.getEnumConstants()); return list; } }; }
From source file:com.evolveum.midpoint.prism.xml.XmlTypeConverter.java
public static <T> T toXmlEnum(Class<T> expectedType, String stringValue) { if (stringValue == null) { return null; }/*from w w w . ja v a 2s . co m*/ for (T enumConstant : expectedType.getEnumConstants()) { Field field; try { field = expectedType.getField(((Enum) enumConstant).name()); } catch (SecurityException e) { throw new IllegalArgumentException( "Error getting field from '" + enumConstant + "' in " + expectedType, e); } catch (NoSuchFieldException e) { throw new IllegalArgumentException( "Error getting field from '" + enumConstant + "' in " + expectedType, e); } XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class); if (annotation.value().equals(stringValue)) { return enumConstant; } } throw new IllegalArgumentException("No enum value '" + stringValue + "' in " + expectedType); }
From source file:com.feilong.core.lang.EnumUtil.java
/** * <code>propertyName</code> <code>specifiedValue</code> . * /*w w w . j av a2 s .c o m*/ * <p> * <code>HttpMethodType</code> ?,?: * </p> * * <pre class="code"> * EnumUtil.getEnumByField(HttpMethodType.class, "method", "get") * </pre> * * <h3>:</h3> * * <blockquote> * <p> * ??,?<code>propertyName</code> <code>specifiedValue</code>,null * </p> * </blockquote> * * @param <E> * the element type * @param <T> * the generic type * @param enumClass * <code>HttpMethodType</code> * @param propertyName * ??, <code>HttpMethodType</code> method,javabean * @param specifiedValue * post * @param ignoreCase * ?? * @return the enum by property value * @throws NullPointerException * <code>enumClass</code> null, <code>propertyName</code> null * @throws IllegalArgumentException * <code>propertyName</code> blank * @throws BeanUtilException * <code>propertyName</code> , <code>HttpMethodType</code> <b>"method"</b> , <b>"method2222"</b> * @see com.feilong.core.bean.PropertyUtil#getProperty(Object, String) * @since 1.0.8 */ private static <E extends Enum<?>, T> E getEnumByPropertyValue(Class<E> enumClass, String propertyName, T specifiedValue, boolean ignoreCase) { Validate.notNull(enumClass, "enumClass can't be null!"); Validate.notBlank(propertyName, "propertyName can't be null/empty!"); //************************************************************************* // An enum is a kind of class // An annotation is a kind of interface // Class ?, null. E[] enumConstants = enumClass.getEnumConstants(); if (LOGGER.isTraceEnabled()) { LOGGER.trace("enumClass:[{}],enumConstants:{}", enumClass.getCanonicalName(), JsonUtil.format(enumConstants, 0, 0)); } //************************************************************************* for (E e : enumConstants) { Object propertyValue = PropertyUtil.getProperty(e, propertyName); if (isEquals(propertyValue, specifiedValue, ignoreCase)) { return e; } } //************************************************************************* if (LOGGER.isInfoEnabled()) { String messagePattern = "[{}],propertyName:[{}],value:[{}],ignoreCase:[{}],constants not found"; LOGGER.info(Slf4jUtil.format(messagePattern, enumClass, propertyName, specifiedValue, ignoreCase)); } return null; }
From source file:com.heliosphere.athena.base.resource.bundle.ResourceBundleManager.java
/** * Registers a resource bundle.// w w w . j a va 2 s .c o m * <p> * @param bundleClass Class of the resource bundle to register. * @param bundle Resource bundle to register. * @throws ResourceBundleException Thrown if an error occurred while trying * to register a resource bundle. */ @SuppressWarnings("nls") private static final void register(final Class<? extends IBundle> bundleClass, final ResourceBundle bundle) { ResourceBundle previous = null; try { if (!BUNDLES.containsKey(bundleClass)) { BUNDLES.put(bundleClass, bundle); NAMES.put(bundleClass, bundleClass.getEnumConstants()[0].getKey()); setInitialized(true); String filename = bundleClass.getEnumConstants()[0].getKey() + "_" + locale.toString() + ".properties"; int index = filename.indexOf("."); if (index > -1) { filename = filename.substring(index + 1, filename.length()); } log.debug(getMessage(BundleAthenaBase.ResourceBundleRegistered, bundleClass.getSimpleName(), locale.toString(), Integer.valueOf(bundle.keySet().size()), filename)); } else { /* * Case where the bundle class already exists. If the locale is * the same, do nothing but if the locale is different then * replace it with the new one. */ previous = BUNDLES.get(bundleClass); if (previous.getLocale().equals(bundle.getLocale())) { log.debug(getMessage(BundleAthenaBase.ResourceBundleAlreadyRegistered, bundleClass.getSimpleName(), locale.toString())); } else { BUNDLES.put(bundleClass, bundle); log.debug(getMessage(BundleAthenaBase.ResourceBundleReplaced, bundleClass.getSimpleName(), previous.getLocale().toString(), locale.toString(), Integer.valueOf(bundle.keySet().size()))); } } } catch (final Exception e) { log.error(e.getMessage(), e); throw (ResourceBundleException) e; } }
From source file:com.heliosphere.demeter.base.resource.bundle.ResourceBundleManager.java
/** * Registers a resource bundle./*from w w w . j ava2 s .co m*/ * <p> * @param bundleClass Class of the resource bundle to register. * @param bundle Resource bundle to register. * @throws ResourceBundleException Thrown if an error occurred while trying * to register a resource bundle. */ @SuppressWarnings("nls") private static final void register(final Class<? extends IBundle> bundleClass, final ResourceBundle bundle) { ResourceBundle previous = null; try { if (!BUNDLES.containsKey(bundleClass)) { BUNDLES.put(bundleClass, bundle); NAMES.put(bundleClass, bundleClass.getEnumConstants()[0].getKey()); setInitialized(true); String filename = bundleClass.getEnumConstants()[0].getKey() + "_" + locale.toString() + ".properties"; int index = filename.indexOf("."); if (index > -1) { filename = filename.substring(index + 1, filename.length()); } log.debug(getMessage(BundleDemeterBase.ResourceBundleRegistered, bundleClass.getSimpleName(), locale.toString(), Integer.valueOf(bundle.keySet().size()), filename)); } else { /* * Case where the bundle class already exists. If the locale is * the same, do nothing but if the locale is different then * replace it with the new one. */ previous = BUNDLES.get(bundleClass); if (previous.getLocale().equals(bundle.getLocale())) { log.debug(getMessage(BundleDemeterBase.ResourceBundleAlreadyRegistered, bundleClass.getSimpleName(), locale.toString())); } else { BUNDLES.put(bundleClass, bundle); log.debug(getMessage(BundleDemeterBase.ResourceBundleReplaced, bundleClass.getSimpleName(), previous.getLocale().toString(), locale.toString(), Integer.valueOf(bundle.keySet().size()))); } } } catch (final Exception e) { log.error(e.getMessage(), e); throw (ResourceBundleException) e; } }
From source file:com.discovery.darchrow.lang.EnumUtil.java
/** * fieldName value <br>/*from ww w. ja v a2 s . c o m*/ * * <pre> * * ?{@link HttpMethodType} ,?: * * {@code * EnumUtil.getEnumByField(HttpMethodType.class, "method", "get") * } * </pre> * * @param <E> * the element type * @param <T> * the generic type * @param enumClass * the enum class {@link HttpMethodType} * @param propertyName * ??, {@link HttpMethodType}method,javabean * @param value * post * @param ignoreCase * ?? * @return enum constant * @see com.baozun.nebulaplus.bean.BeanUtil#getProperty(Object, String) * @since 1.0.8 */ private static <E extends Enum<?>, T> E getEnumByPropertyValue(Class<E> enumClass, String propertyName, T value, boolean ignoreCase) { if (Validator.isNullOrEmpty(enumClass)) { throw new IllegalArgumentException("enumClass is null or empty!"); } if (Validator.isNullOrEmpty(propertyName)) { throw new IllegalArgumentException("the fieldName is null or empty!"); } // An enum is a kind of class // and an annotation is a kind of interface // Class ? null. E[] enumConstants = enumClass.getEnumConstants(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("enumClass:[{}],enumConstants:{}", enumClass.getCanonicalName(), JsonUtil.format(enumConstants)); } for (E e : enumConstants) { Object propertyValue = PropertyUtil.getProperty(e, propertyName); if (null == propertyValue && null == value) { return e; } if (null != propertyValue && null != value) { if (ignoreCase && propertyValue.toString().equalsIgnoreCase(value.toString())) { return e; } else if (propertyValue.equals(value)) { return e; } } } String messagePattern = "can not found the enum constants,enumClass:[{}],propertyName:[{}],value:[{}],ignoreCase:[{}]"; throw new BeanUtilException( Slf4jUtil.formatMessage(messagePattern, enumClass, propertyName, value, ignoreCase)); }
From source file:com.htmlhifive.pitalium.core.config.PtlTestConfig.java
/** * ?????????/*from w w w. j av a2 s .co m*/ * * @param type ??String, int(Integer), double(Double),Enum????? * @param value ?? * @return ?? * @throws IllegalArgumentException Enum??????? * @throws TestRuntimeException ?????????? */ static Object convertFromString(Class<?> type, String value) throws IllegalArgumentException, TestRuntimeException { if (type == String.class) { return value; } else if (type == int.class || type == Integer.class) { return Integer.parseInt(value); } else if (type == double.class || type == Double.class) { return Double.parseDouble(value); } else if (type.isEnum()) { for (Object o : type.getEnumConstants()) { if (((Enum) o).name().equals(value)) { return o; } } throw new IllegalArgumentException(); } else { throw new TestRuntimeException("Cannot convert type \"" + type.getName() + "\""); } }