List of usage examples for java.lang Class getEnumConstants
public T[] getEnumConstants()
From source file:org.force66.beantester.valuegens.ValueGeneratorFactory.java
public ValueGenerator<?> forClass(Class<?> targetClass) { Validate.notNull(targetClass, "Null class not allowed"); ValueGenerator<?> generator = registeredGeneratorMap.get(targetClass); if (generator == null) { for (ValueGenerator<?> gen : STOCK_GENERATORS) { if (gen.canGenerate(targetClass)) { registeredGeneratorMap.put(targetClass, gen); return gen; }// w w w . j av a 2 s. co m } } else { return generator; } if (targetClass.isInterface()) { InterfaceValueGenerator gen = new InterfaceValueGenerator(targetClass); this.registerGenerator(targetClass, gen); return gen; } else if (Modifier.isAbstract(targetClass.getModifiers())) { return null; // generator not possible on abstract classes } else if (targetClass.isEnum()) { return registerGenericGenerator(targetClass, targetClass.getEnumConstants()); } else if (targetClass.isArray()) { ArrayValueGenerator gen = new ArrayValueGenerator(targetClass, this); this.registerGenerator(targetClass, gen); return gen; } else if (Class.class.equals(targetClass)) { return registerGenericGenerator(targetClass, new Object[] { Object.class }); } else { return registerGenericGenerator(targetClass, new Object[] { InstantiationUtils.safeNewInstance(this, targetClass) }); } }
From source file:com.jkoolcloud.tnt4j.streams.utils.Utils.java
/** * Returns enumeration entry based on entry name value ignoring case. * * @param enumClass//w ww. j ava 2s. c o m * enumeration instance class * @param name * name of enumeration entry * @param <E> * type of enumeration * @return enumeration object having provided name * @throws IllegalArgumentException * if name is not a valid enumeration entry name or is {@code null} */ public static <E extends Enum<E>> E valueOfIgnoreCase(Class<E> enumClass, String name) throws IllegalArgumentException { if (StringUtils.isEmpty(name)) { throw new IllegalArgumentException( StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "Utils.name.empty")); } E[] enumConstants = enumClass.getEnumConstants(); for (E ec : enumConstants) { if (ec.name().equalsIgnoreCase(name)) { return ec; } } throw new IllegalArgumentException(StreamsResources.getStringFormatted( StreamsResources.RESOURCE_BUNDLE_NAME, "Utils.no.enum.constant", name, enumClass.getSimpleName())); }
From source file:com.jcwhatever.nucleus.internal.managed.commands.Arguments.java
@Override public <T extends Enum<T>> T getEnum(String parameterName, Class<T> enumClass) throws InvalidArgumentException { PreCon.notNullOrEmpty(parameterName); PreCon.notNull(enumClass);/*from ww w. jav a2s .co m*/ return getEnum(parameterName, enumClass, enumClass.getEnumConstants()); }
From source file:org.apache.syncope.client.console.panels.BeanPanel.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldPanel buildSinglePanel(final Serializable bean, final Class<?> type, final String fieldName, final String id) { FieldPanel result = null;// www . j a va 2 s .com PropertyModel model = new PropertyModel(bean, fieldName); if (ClassUtils.isAssignable(Boolean.class, type)) { result = new AjaxCheckBoxPanel(id, fieldName, model); } else if (ClassUtils.isAssignable(Number.class, type)) { result = new AjaxSpinnerFieldPanel.Builder<>().build(id, fieldName, (Class<Number>) ClassUtils.resolvePrimitiveIfNecessary(type), model); } else if (Date.class.equals(type)) { result = new AjaxDateTimeFieldPanel(id, fieldName, model, SyncopeConstants.DEFAULT_DATE_PATTERN); } else if (type.isEnum()) { result = new AjaxDropDownChoicePanel(id, fieldName, model) .setChoices(Arrays.asList(type.getEnumConstants())); } // treat as String if nothing matched above if (result == null) { result = new AjaxTextFieldPanel(id, fieldName, model); } result.hideLabel(); return result; }
From source file:org.eclipse.wb.core.editor.actions.assistant.AbstractAssistantPage.java
/** * Creates {@link Group} for choosing one of the enum values for property. *///from ww w. j a v a 2s . c o m protected final Group addEnumProperty(Composite parent, String property, String title, String enumClassName) { Object[][] titleAndValueArray; ClassLoader classLoader = GlobalState.getClassLoader(); try { Class<?> enumClass = classLoader.loadClass(enumClassName); if (enumClass.isEnum()) { Enum<?>[] constants = (Enum<?>[]) enumClass.getEnumConstants(); titleAndValueArray = new Object[constants.length][2]; for (int i = 0; i < constants.length; i++) { Enum<?> constantValue = constants[i]; titleAndValueArray[i][0] = constantValue.name(); titleAndValueArray[i][1] = constantValue; } } else { titleAndValueArray = new Object[][] { {} }; } return addChoiceProperty(parent, property, title, titleAndValueArray); } catch (ClassNotFoundException e) { DesignerPlugin.log(e); return new Group(parent, SWT.NONE); } }
From source file:org.rythmengine.RythmEngine.java
public static ShutdownService getShutdownService(boolean isGaeAvailable) { if (!isGaeAvailable) { return DefaultShutdownService.INSTANCE; }//from w ww . j ava 2 s. c o m try { String classname = "org.rythmengine.GaeShutdownService"; Class clazz = Class.forName(classname); Object[] oa = clazz.getEnumConstants(); ShutdownService result = (ShutdownService) oa[0]; return result; } catch (Throwable t) { // Nothing to do } return DefaultShutdownService.INSTANCE; }
From source file:org.apache.syncope.console.pages.ReportletConfModalPage.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldPanel buildSinglePanel(final Class<?> type, final String fieldName, final String id) { FieldPanel result = null;// ww w. j a v a 2 s . co m PropertyModel model = new PropertyModel(ReportletConfModalPage.this.reportletConf, fieldName); if (ClassUtils.isAssignable(Boolean.class, type)) { result = new AjaxCheckBoxPanel(id, fieldName, model); } else if (ClassUtils.isAssignable(Number.class, type)) { result = new SpinnerFieldPanel<Number>(id, fieldName, (Class<Number>) type, model, null, null); } else if (Date.class.equals(type)) { result = new DateTimeFieldPanel(id, fieldName, model, SyncopeConstants.DEFAULT_DATE_PATTERN); } else if (type.isEnum()) { result = new AjaxDropDownChoicePanel(id, fieldName, model) .setChoices(Arrays.asList(type.getEnumConstants())); } // treat as String if nothing matched above if (result == null) { result = new AjaxTextFieldPanel(id, fieldName, model); } return result; }
From source file:org.apache.syncope.client.console.pages.ReportletConfModalPage.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldPanel buildSinglePanel(final Class<?> type, final String fieldName, final String id) { FieldPanel result = null;/*from w ww. ja va 2 s.c o m*/ PropertyModel model = new PropertyModel(ReportletConfModalPage.this.reportletConf, fieldName); if (ClassUtils.isAssignable(Boolean.class, type)) { result = new AjaxCheckBoxPanel(id, fieldName, model); } else if (ClassUtils.isAssignable(Number.class, type)) { result = new SpinnerFieldPanel<Number>(id, fieldName, (Class<Number>) ClassUtils.resolvePrimitiveIfNecessary(type), model, null, null); } else if (Date.class.equals(type)) { result = new DateTimeFieldPanel(id, fieldName, model, SyncopeConstants.DEFAULT_DATE_PATTERN); } else if (type.isEnum()) { result = new AjaxDropDownChoicePanel(id, fieldName, model) .setChoices(Arrays.asList(type.getEnumConstants())); } // treat as String if nothing matched above if (result == null) { result = new AjaxTextFieldPanel(id, fieldName, model); } return result; }
From source file:com.jhkt.playgroundArena.db.nosql.mongodb.beans.AbstractDocument.java
private final static Object convertBacktoClassInstanceHelper(DBObject dBObject) { Object returnValue = null;//from www . j a v a 2 s. c om try { String className = dBObject.get(JPAConstants.CONVERTER_CLASS.CLASS.name()).toString(); Class<?> classCheck = Class.forName(className); if (AbstractDocument.class.isAssignableFrom(classCheck)) { Class<? extends AbstractDocument> classToConvertTo = classCheck.asSubclass(AbstractDocument.class); AbstractDocument dInstance = classToConvertTo.newInstance(); for (String key : dBObject.keySet()) { Object value = dBObject.get(key); char[] propertyChars = key.toCharArray(); String methodMain = String.valueOf(propertyChars[0]).toUpperCase() + key.substring(1); String methodName = "set" + methodMain; String getMethodName = "get" + methodMain; if (key.equals(JPAConstants.CONVERTER_CLASS.CLASS.name())) { continue; } if (value instanceof BasicDBObject) { value = convertBacktoClassInstanceHelper(BasicDBObject.class.cast(value)); } try { Method getMethod = classToConvertTo.getMethod(getMethodName); Class<?> getReturnType = getMethod.getReturnType(); Method method = classToConvertTo.getMethod(methodName, getReturnType); if (getMethod.isAnnotationPresent(IDocumentKeyValue.class)) { method.invoke(dInstance, value); } } catch (NoSuchMethodException nsMe) { _log.warn("Within convertBacktoClassInstance, following method was not found " + methodName, nsMe); } } returnValue = dInstance; } else if (Enum.class.isAssignableFrom(classCheck)) { List<?> constants = Arrays.asList(classCheck.getEnumConstants()); String name = String.class.cast(dBObject.get(JPAConstants.CONVERTER_CLASS.CONTENT.name())); for (Object constant : constants) { if (constant.toString().equals(name)) { returnValue = constant; } } } else if (Collection.class.isAssignableFrom(classCheck)) { @SuppressWarnings("unchecked") Class<? extends Collection<? super Object>> classToConvertTo = (Class<? extends Collection<? super Object>>) classCheck; Collection<? super Object> cInstance = classToConvertTo.newInstance(); BasicDBList bDBList = (BasicDBList) dBObject.get(JPAConstants.CONVERTER_CLASS.CONTENT.name()); cInstance.addAll(bDBList); returnValue = cInstance; } else if (Map.class.isAssignableFrom(classCheck)) { @SuppressWarnings("unchecked") Class<? extends Map<String, ? super Object>> classToConvertTo = (Class<? extends Map<String, ? super Object>>) classCheck; Map<String, ? super Object> mInstance = classToConvertTo.newInstance(); BasicDBObject mapObject = (BasicDBObject) dBObject.get(JPAConstants.CONVERTER_CLASS.CONTENT.name()); mInstance.putAll(mapObject); returnValue = mInstance; } } catch (Exception e) { throw new RuntimeException(e); } return returnValue; }
From source file:io.apiman.manager.api.es.EsMarshallingTest.java
/** * Fabricates a new instance of the given bean type. Uses reflection to figure * out all the fields and assign generated values for each. *//*w w w. j av a 2 s . c o m*/ private static <T> T createBean(Class<T> beanClass) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { T bean = beanClass.newInstance(); Map<String, String> beanProps = BeanUtils.describe(bean); for (String key : beanProps.keySet()) { try { Field declaredField = beanClass.getDeclaredField(key); Class<?> fieldType = declaredField.getType(); if (fieldType == String.class) { BeanUtils.setProperty(bean, key, StringUtils.upperCase(key)); } else if (fieldType == Boolean.class || fieldType == boolean.class) { BeanUtils.setProperty(bean, key, Boolean.TRUE); } else if (fieldType == Date.class) { BeanUtils.setProperty(bean, key, new Date(1)); } else if (fieldType == Long.class || fieldType == long.class) { BeanUtils.setProperty(bean, key, 17L); } else if (fieldType == Integer.class || fieldType == long.class) { BeanUtils.setProperty(bean, key, 11); } else if (fieldType == Set.class) { // Initialize to a linked hash set so that order is maintained. BeanUtils.setProperty(bean, key, new LinkedHashSet()); Type genericType = declaredField.getGenericType(); String typeName = genericType.getTypeName(); String typeClassName = typeName.substring(14, typeName.length() - 1); Class<?> typeClass = Class.forName(typeClassName); Set collection = (Set) BeanUtilsBean.getInstance().getPropertyUtils().getProperty(bean, key); populateSet(collection, typeClass); } else if (fieldType == Map.class) { Map<String, String> map = new LinkedHashMap<String, String>(); map.put("KEY-1", "VALUE-1"); map.put("KEY-2", "VALUE-2"); BeanUtils.setProperty(bean, key, map); } else if (fieldType.isEnum()) { BeanUtils.setProperty(bean, key, fieldType.getEnumConstants()[0]); } else if (fieldType.getPackage() != null && fieldType.getPackage().getName().startsWith("io.apiman.manager.api.beans")) { Object childBean = createBean(fieldType); BeanUtils.setProperty(bean, key, childBean); } else { throw new IllegalAccessException( "Failed to handle property named [" + key + "] type: " + fieldType.getSimpleName()); } // String capKey = StringUtils.capitalize(key); // System.out.println(key);; } catch (NoSuchFieldException e) { // Skip it - there is not really a bean property with this name! } } return bean; }