List of usage examples for java.lang Class isEnum
public boolean isEnum()
From source file:org.trianacode.taskgraph.ser.ObjectMarshaller.java
private static Object deserSimple(String type, String value) { if (value == null) { return null; }/*from www. j av a 2s.c om*/ if (type.equals("java.lang.String")) { return value; } else { try { Class cls = ClassLoaders.forName(type); if (cls.isAssignableFrom(StringBuffer.class)) { return new StringBuffer(value); } else if (cls.isEnum()) { return Enum.valueOf(cls, value); } else { return getPrimitive(type, value); } } catch (ClassNotFoundException e) { return value; } } }
From source file:net.solarnetwork.node.util.ClassUtils.java
/** * Get a Map of non-null <em>simple</em> bean properties for an object. * /*from w ww . j av a 2 s .c o m*/ * @param o * the object to inspect * @param ignore * a set of property names to ignore (optional) * @return Map (never <em>null</em>) * @since 1.1 */ public static Map<String, Object> getSimpleBeanProperties(Object o, Set<String> ignore) { if (ignore == null) { ignore = DEFAULT_BEAN_PROP_NAME_IGNORE; } Map<String, Object> result = new LinkedHashMap<String, Object>(); BeanWrapper bean = new BeanWrapperImpl(o); PropertyDescriptor[] props = bean.getPropertyDescriptors(); for (PropertyDescriptor prop : props) { if (prop.getReadMethod() == null) { continue; } String propName = prop.getName(); if (ignore != null && ignore.contains(propName)) { continue; } Class<?> propType = bean.getPropertyType(propName); if (!(propType.isPrimitive() || propType.isEnum() || String.class.isAssignableFrom(propType) || Number.class.isAssignableFrom(propType) || Character.class.isAssignableFrom(propType) || Byte.class.isAssignableFrom(propType) || Date.class.isAssignableFrom(propType))) { continue; } Object propValue = bean.getPropertyValue(propName); if (propValue == null) { continue; } if (propType.isEnum()) { propValue = propValue.toString(); } else if (Date.class.isAssignableFrom(propType)) { propValue = ((Date) propValue).getTime(); } result.put(propName, propValue); } return result; }
From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java
private static IPredicateFactory<?> getPredicateFactoryForClass(Field field) { Class clazz = field.getType(); if (clazz.isEnum()) { return new EnumStringPredicateFactory(clazz); } else if (Persistable.class.isAssignableFrom(clazz)) { if (field.isAnnotationPresent(CurrentPrincipalIdPredicate.class)) { return currentPrincipalPredicateFactory; } else {/* w ww .j a v a 2 s. com*/ return anyToOnePredicateFactory; } } else { return factoryForClassMap.get(clazz); } }
From source file:management.limbr.test.util.PojoTester.java
@SuppressWarnings("unchecked") private static <T> T createRichObject(Class<T> type) { try {/*from w ww . j a v a 2 s .co m*/ if (type.equals(Set.class)) { return (T) new HashSet<>(); } else if (type.equals(List.class)) { return (T) new ArrayList<>(); } else if (type.isEnum()) { T[] values = type.getEnumConstants(); return values[random.nextInt(values.length)]; } else { return type.newInstance(); } } catch (IllegalAccessException | InstantiationException ex) { throw new IllegalStateException("Couldn't construct object of type " + type.getName() + ".", ex); } }
From source file:cop.raml.mocks.MockUtils.java
public static TypeElementMock createElement(@NotNull Class<?> cls) throws ClassNotFoundException { if (cls.isPrimitive()) return createPrimitiveElement(cls); if (cls.isEnum()) return createEnumElement(cls); if (cls.isArray()) return setAnnotations(createArrayElement(cls.getComponentType()), cls); if (Collection.class.isAssignableFrom(cls)) return setAnnotations(createCollectionElement(), cls); return createClassElement(cls); }
From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java
private static boolean isEnumType(Class<?> typeOfField) { return typeOfField.isEnum(); }
From source file:org.jnetstream.protocol.ProtocolRegistry.java
/** * @param protocol/* w w w .ja v a2s . co m*/ */ private static void fillInFromClassInfo(DefaultProtocolEntry entry, Protocol protocol) { Class<? extends Protocol> c = protocol.getClass(); if (c.isEnum() == false) { return; } Enum<?> constant = null; for (Enum<?> e : (Enum[]) c.getEnumConstants()) { if (e == protocol) { constant = e; } } Package pkg = c.getPackage(); String suite = c.getSimpleName(); String name = constant.name(); String headeri = pkg.getName() + "." + name; String headerc = pkg.getName() + "." + name + "Header"; String headercdc = pkg.getName() + "." + name + "Codec"; // System.out.printf("suite=%s,\n name=%s,\n headeri=%s,\n headerc=%s\n", // suite, name, headeri, headerc); entry.setSuite(suite); entry.setName(name); try { entry.setProtocolClass((Class<? extends Header>) Class.forName(headeri)); } catch (Exception e) { logger.warn("missing header: " + headeri); logger.debug(e); } try { entry.setCodec((Class<HeaderCodec<? extends Header>>) Class.forName(headercdc)); HeaderCodec<? extends Header> codec = entry.getCodecClass().newInstance(); entry.setCodec(codec); } catch (Exception e) { logger.warn("missing codec: " + headercdc); logger.debug(e); } }
From source file:com.netflix.simianarmy.aws.AbstractRecorder.java
/** * Value to enum. Converts a "name|type" string back to an enum. * * @param value//from ww w. j a v a2 s .com * the value * @return the enum */ @SuppressWarnings("unchecked") protected static Enum valueToEnum(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()) { final Class<? extends Enum> enumSubClass = enumClass.asSubclass(Enum.class); return Enum.valueOf(enumSubClass, parts[0]); } throw new RuntimeException("value " + value + " does not appear to be an enum type"); }
From source file:org.jaxygen.converters.properties.PropertiesToBeanConverter.java
private static Object parsePropertyToValue(Object valueObject, Class<?> propertyType) { Object value = null;//www . jav a 2s.c o m //TODO: add cache of enum converters boolean isEnum = propertyType.isEnum(); if (isEnum) { ConvertUtils.register(new EnumConverter(), propertyType); } if (valueObject != null && valueObject.getClass().equals(String.class)) { value = ConvertUtils.convert((String) valueObject, propertyType); } else { value = valueObject; } return value; }
From source file:com.medallia.spider.api.DynamicInputImpl.java
private static Object parseSingleValue(Class<?> rt, String v, AnnotatedElement anno, Map<Class<?>, InputArgParser<?>> inputArgParsers) { if (rt.isEnum()) { String vlow = v.toLowerCase(); for (Enum e : rt.asSubclass(Enum.class).getEnumConstants()) { if (e.name().toLowerCase().equals(vlow)) return e; }/*from ww w. j a va 2 s.c o m*/ throw new AssertionError("Enum constant not found: " + v); } else if (rt == Integer.class) { // map blank strings to null return Strings.hasContent(v) ? Integer.valueOf(v) : null; } else if (rt == Integer.TYPE) { // primitive int must have a value return Integer.valueOf(v); } else if (rt == Long.class) { // map blank strings to null return Strings.hasContent(v) ? Long.valueOf(v) : null; } else if (rt == Long.TYPE) { // primitive long must have a value return Long.valueOf(v); } else if (rt == Double.class) { // map blank strings to null return Strings.hasContent(v) ? Double.valueOf(v) : null; } else if (rt == Double.TYPE) { // primitive double must have a value return Double.valueOf(v); } else if (rt == String.class) { return v; } else if (rt.isArray()) { Input.List ann = anno.getAnnotation(Input.List.class); if (ann == null) throw new AssertionError("Array type but no annotation (see " + Input.class + "): " + anno); String separator = ann.separator(); String[] strVals = v.split(separator, -1); Class<?> arrayType = rt.getComponentType(); Object a = Array.newInstance(arrayType, strVals.length); for (int i = 0; i < strVals.length; i++) { Array.set(a, i, parseSingleValue(arrayType, strVals[i], anno, inputArgParsers)); } return a; } else if (inputArgParsers != null) { InputArgParser<?> argParser = inputArgParsers.get(rt); if (argParser != null) { return argParser.parse(v); } } throw new AssertionError("Unknown return type " + rt + " (val: " + v + ")"); }