List of usage examples for java.lang Enum valueOf
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)
From source file:org.jannocessor.adapter.JavaElementAdapter.java
@SuppressWarnings("unchecked") protected <T extends Enum<T>> T[] getModifierValues(Class<T> enumType, String... exceptValues) { Set<Modifier> modifiers = element.getModifiers(); T[] values = (T[]) Array.newInstance(enumType, modifiers.size()); int index = 0; for (Modifier modifier : modifiers) { if (!ArrayUtils.contains(exceptValues, modifier.name())) { values[index++] = Enum.valueOf(enumType, modifier.name()); }//from w w w .ja v a 2 s . com } if (index < values.length) { // if some modifier was filtered out, cut the array values = Arrays.copyOf(values, index); } return values; }
From source file:org.openmrs.module.web.taglib.ExtensionPointTag.java
public int doStartTag() { log.debug("Starting tag for extension point: " + pointId); // "zero out" the extension list and other variables extensions = null;//from w w w .j a v a 2s . co m parameterMap = OpenmrsUtil.parseParameterList(parameters); status = new HashMap<String, Object>(); List<Extension> extensionList = null; List<Extension> validExtensions = new ArrayList<Extension>(); if (type != null && type.length() > 0) { try { Extension.MEDIA_TYPE mediaType = Enum.valueOf(Extension.MEDIA_TYPE.class, type); log.debug("Getting extensions: " + pointId + " : " + mediaType); extensionList = ModuleFactory.getExtensions(pointId, mediaType); } catch (IllegalArgumentException e) { log.warn("extension point type: '" + type + "' is invalid. Must be enum of Extension.MEDIA_TYPE", e); } } else { log.debug("Getting extensions: " + pointId); extensionList = ModuleFactory.getExtensions(pointId); } if (extensionList != null) { log.debug("Found " + extensionList.size() + " extensions"); if (requiredClass == null) { validExtensions = extensionList; } else { try { Class<?> clazz = Class.forName(requiredClass); for (Extension ext : extensionList) { if (!clazz.isAssignableFrom(ext.getClass())) { log.warn("Extensions at this point (" + pointId + ") are " + "required to be of " + clazz + " or a subclass. " + ext.getClass() + " is not."); } else { validExtensions.add(ext); } } } catch (ClassNotFoundException ex) { throw new IllegalArgumentException(ex); } } extensions = validExtensions.iterator(); } if (extensions == null || !extensions.hasNext()) { extensions = null; return SKIP_BODY; } else { return EVAL_BODY_BUFFERED; } }
From source file:com.base.dao.sql.ReflectionUtils.java
public static Enum<?> enumValue(Class toClass, Object o) { Enum<?> result = null;// ww w . j a v a 2 s. c o m if (o == null) { result = null; } else if (o instanceof String[]) { result = Enum.valueOf(toClass, ((String[]) o)[0]); } else if (o instanceof String) { result = Enum.valueOf(toClass, (String) o); } return result; }
From source file:org.apache.ant.compress.taskdefs.SevenZ.java
private static SevenZMethod asMethod(String method) { return (SevenZMethod) Enum.valueOf(SevenZMethod.class, method.toUpperCase(Locale.US)); }
From source file:org.sonar.api.server.ws.Request.java
public <E extends Enum<E>> E mandatoryParamAsEnum(String key, Class<E> enumClass) { return Enum.valueOf(enumClass, mandatoryParam(key)); }
From source file:it.restrung.rest.marshalling.response.AbstractJSONResponse.java
/** * @see JSONResponse#fromJSON(org.json.JSONObject) *///from www . j a va 2s.c o m @Override @SuppressWarnings("unchecked") public void fromJSON(JSONObject jsonObject) throws JSONException { this.delegate = jsonObject; Method[] methods = getClass().getDeclaredMethods(); for (Method method : methods) { if (method.getParameterTypes().length == 1 && method.getName().startsWith("set") && method.getName().length() > 3) { Class argType = method.getParameterTypes()[0]; String propertyName = method.getName().substring(3); propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1); try { Field foundField = getClass().getDeclaredField(propertyName); if (foundField.isAnnotationPresent(JsonProperty.class)) { propertyName = foundField.getAnnotation(JsonProperty.class).value(); } } catch (NoSuchFieldException e) { //todo log errors when field names mismatch their setter } Object result = null; if (String.class.isAssignableFrom(argType)) { result = getString(propertyName); } else if (Boolean.class.isAssignableFrom(argType) || boolean.class.isAssignableFrom(argType)) { result = getBoolean(propertyName); } else if (Double.class.isAssignableFrom(argType) || double.class.isAssignableFrom(argType)) { result = getDouble(propertyName); } else if (Long.class.isAssignableFrom(argType) || long.class.isAssignableFrom(argType)) { result = getLong(propertyName); } else if (Integer.class.isAssignableFrom(argType) || int.class.isAssignableFrom(argType)) { result = getInt(propertyName); } else if (Date.class.isAssignableFrom(argType)) { result = getDate(propertyName); } else if (JSONResponse.class.isAssignableFrom(argType)) { result = getObject(propertyName, argType); } else if (Enum.class.isAssignableFrom(argType)) { String value = getString(propertyName); if (value != null) { result = Enum.valueOf((Class<Enum>) argType, getString(propertyName)); } } else if (List.class.isAssignableFrom(argType)) { Class typeArg = (Class) ((ParameterizedType) method.getGenericParameterTypes()[0]) .getActualTypeArguments()[0]; if (JSONResponse.class.isAssignableFrom(typeArg)) { result = getList(propertyName, typeArg); } else { result = getElementCollection(propertyName); } } else if (Map.class.isAssignableFrom(argType)) { Class typeArg = (Class) ((ParameterizedType) method.getGenericParameterTypes()[0]) .getActualTypeArguments()[0]; if (JSONResponse.class.isAssignableFrom(typeArg)) { result = getMap(propertyName, typeArg); } else { result = getElementMap(propertyName); } } else { throw new UnsupportedOperationException(String.format( "%s is of type: %s which is not yet supported by the AbstractJSONResponse serialization", propertyName, argType)); } try { method.invoke(this, result); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } } }
From source file:org.opensingular.form.STypeSimple.java
@SuppressWarnings("unchecked") @Override// w ww. j av a 2 s. com public <T> T convert(Object value, Class<T> resultClass) { if (value == null) { return null; } else if (resultClass.isAssignableFrom(valueClass)) { return resultClass.cast(convert(value)); } else if (resultClass.isInstance(value)) { return resultClass.cast(value); } else if (resultClass.isAssignableFrom(String.class)) { if (valueClass.isInstance(value)) { return resultClass.cast(toStringPersistence(valueClass.cast(value))); } return resultClass.cast(value.toString()); } else if (Enum.class.isAssignableFrom(resultClass)) { Class<? extends Enum> enumClass = resultClass.asSubclass(Enum.class); return (T) Enum.valueOf(enumClass, value.toString()); } else { Converter apacheConverter = ConvertUtils.lookup(value.getClass(), resultClass); if (apacheConverter != null) { return resultClass.cast(apacheConverter.convert(resultClass, value)); } } throw createConversionError(value, resultClass); }
From source file:nc.noumea.mairie.appock.core.services.impl.BootstrapServiceImpl.java
@SuppressWarnings("unchecked") void checkConcordanceEnum(Class<?> classeEnum, String nomTable) { String requete = "SELECT name FROM " + nomTable + " ORDER BY ID"; List<String> listeName = em.createNativeQuery(requete).getResultList(); if (listeName.size() != ((Class<Enum<?>>) classeEnum).getEnumConstants().length) { throw new RuntimeException("L'enumration " + classeEnum.getSimpleName() + " ne contient pas le mme nombre d'lments que dans la table " + nomTable); }//from ww w . ja v a2 s . co m Object[] enumConstants = classeEnum.getEnumConstants(); for (int i = 0; i < listeName.size(); i++) { String name = listeName.get(i); try { @SuppressWarnings("rawtypes") Enum enumValue = Enum.valueOf((Class<Enum>) classeEnum, name); if (enumValue != enumConstants[i]) { throw new RuntimeException("L'enumration " + classeEnum.getSimpleName() + " la position " + i + " ne vaut pas la valeur attendue " + name); } } catch (IllegalArgumentException e) { throw new RuntimeException("L'enumration " + classeEnum.getSimpleName() + " ne contient pas la valeur '" + name + "' prsente dans la table " + nomTable); } } }
From source file:org.carrot2.util.simplexml.SimpleXmlWrapperValue.java
/** * Unwraps the actual value represented by this wrapper. * // w w w . j a v a 2 s.com * @return the actual value or <code>null</code> if value cannot be unwrapped */ @SuppressWarnings({ "unchecked", "rawtypes" }) Object unwrap() { if (value != null) { if (StringUtils.isEmpty(type)) { type = String.class.getName(); } final Class<?> valueType = loadClassWrapAsRuntime(type); if (TO_STRING_VALUE_OF_TYPES.contains(valueType)) { Method valueOfMethod; try { valueOfMethod = valueType.getMethod("valueOf", String.class); return valueOfMethod.invoke(null, value); } catch (Exception e) { throw ExceptionUtils.wrapAsRuntimeException(e); } } else if (Character.class.getName().equals(type)) { return value.length() > 0 ? value.charAt(0) : null; } else if (String.class.getName().equals(type)) { return value; } else if (Class.class.getName().equals(type)) { return loadClassWrapAsRuntime(value); } else if (Enum.class.isAssignableFrom(valueType)) { return Enum.valueOf((Class<? extends Enum>) valueType, value); } } else if (wrapper != null) { if (wrapper instanceof ISimpleXmlWrapper) { return ((ISimpleXmlWrapper<?>) wrapper).getValue(); } else { return wrapper; } } return null; }
From source file:com.netflix.simianarmy.aws.SimpleDBRecorder.java
/** * Value to enum. Converts a "name|type" string back to an enum. * * @param value// www . j av a 2s .com * the value * @return the enum */ private static <T extends NamedType> T valueToEnum(Class<T> type, String value) { // parts = [enum value, enum class type] String[] parts = value.split("\\|", 2); if (parts.length < 2) { throw new RuntimeException("value " + value + " does not appear to be an internal enum format"); } Class<?> enumClass; try { enumClass = Class.forName(parts[1]); } catch (ClassNotFoundException e) { throw new RuntimeException("class for enum value " + value + " not found"); } if (!enumClass.isEnum()) { throw new RuntimeException("value " + value + " does not appear to be of an enum type"); } if (!type.isAssignableFrom(enumClass)) { throw new RuntimeException("value " + value + " cannot be assigned to a variable of this type: " + type.getCanonicalName()); } @SuppressWarnings("rawtypes") Class<? extends Enum> enumType = enumClass.asSubclass(Enum.class); @SuppressWarnings("unchecked") T enumValue = (T) Enum.valueOf(enumType, parts[0]); return enumValue; }