List of usage examples for java.lang Class isEnum
public boolean isEnum()
From source file:org.amplafi.flow.flowproperty.FixedFlowPropertyValueProvider.java
@SuppressWarnings("unchecked") public static <FPP extends FlowPropertyProvider> FixedFlowPropertyValueProvider newFixedFlowPropertyValueProvider( Class<?> defaultClass, FlowPropertyDefinitionImplementor flowPropertyDefinition, boolean testAndConfigFlowPropertyDefinition) { FixedFlowPropertyValueProvider fixedFlowPropertyValueProvider = null; ApplicationIllegalArgumentException.valid(!defaultClass.isAnnotation(), defaultClass, "is an annotation. Annotations cannot be instatiated."); if (defaultClass == int.class) { return newFixedFlowPropertyValueProvider(Integer.valueOf(0), flowPropertyDefinition, testAndConfigFlowPropertyDefinition); } else if (defaultClass == long.class) { return newFixedFlowPropertyValueProvider(Long.valueOf(0), flowPropertyDefinition, testAndConfigFlowPropertyDefinition); } else if (defaultClass == boolean.class) { return newFixedFlowPropertyValueProvider(Boolean.valueOf(false), flowPropertyDefinition, testAndConfigFlowPropertyDefinition); } else if (defaultClass.isEnum()) { // use the first enum as the default. return newFixedFlowPropertyValueProvider(((Class<Enum<?>>) defaultClass).getEnumConstants()[0], flowPropertyDefinition, testAndConfigFlowPropertyDefinition); }/* w ww .j av a 2 s . com*/ Class<?> classToCreate; if (defaultClass == Set.class) { classToCreate = LinkedHashSet.class; } else if (defaultClass == List.class) { classToCreate = ArrayList.class; } else if (defaultClass == Map.class) { classToCreate = HashMap.class; } else { classToCreate = defaultClass; } fixedFlowPropertyValueProvider = new FixedFlowPropertyValueProvider(classToCreate); fixedFlowPropertyValueProvider.convertable(flowPropertyDefinition); if (testAndConfigFlowPropertyDefinition) { DataClassDefinition dataClassDefinition = flowPropertyDefinition.getDataClassDefinition(); if (dataClassDefinition.isDataClassDefined()) { ApplicationIllegalArgumentException.valid(dataClassDefinition.isAssignableFrom(defaultClass), dataClassDefinition, " cannot be assigned "); } else { dataClassDefinition.setDataClass(defaultClass); } } return fixedFlowPropertyValueProvider; }
From source file:eu.squadd.testing.objectspopulator.RandomValuePopulator.java
public Object populateAllFields(final Class targetClass, Map exclusions) throws IllegalAccessException, InstantiationException { final Object target; try {//from w ww . ja va 2 s . c o m if (isMathNumberType(targetClass)) { target = getMathNumberType(targetClass); } else { target = ConstructorUtils.invokeConstructor(targetClass, null); } } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException ex) { System.err.println(ex.getMessage()); return null; } //final Object target = targetClass.newInstance(); //Get all fields present on the target class final Set<Field> allFields = getAllFields(targetClass, Predicates.<Field>alwaysTrue()); if (this.stopOnMxRecusionDepth) this.currentRecursionDepth++; //Iterate through fields if recursion depth is not reached if (!this.stopOnMxRecusionDepth || (this.stopOnMxRecusionDepth && this.currentRecursionDepth <= scannerFactory.getRecursionDepth())) { for (final Field field : allFields) { try { // check if the field is not on exclusion list if (exclusions != null && exclusions.containsValue(field.getName())) continue; //Set fields to be accessible even when private field.setAccessible(true); final Class<?> fieldType = field.getType(); if (fieldType.isEnum() && Enum.class.isAssignableFrom(fieldType)) { //handle any enums here if you have any } else if (isMathNumberType(fieldType)) { //System.out.println("*** Math number found, populating it: "+fieldType); field.set(target, getManufacturedPojo(fieldType)); } //Check if the field is a collection else if (Collection.class.isAssignableFrom(fieldType)) { //Get the generic type class of the collection final Class<?> genericClass = getGenericClass(field); //Check if the generic type of a list is abstract if (Modifier.isAbstract(genericClass.getModifiers())) { System.out.println("Abstract classes are not supported !!!"); // this stuff needs real class extending abstract one to work //final List<Object> list = new ArrayList(); //list.add(populateAllIn(ClassExtendingAbstract.class)); //field.set(target, list); } else { final List<Object> list = new ArrayList(); list.add(populateAllFields(genericClass, exclusions)); field.set(target, list); } } else if ((isSimpleType(fieldType) || isSimplePrimitiveWrapperType(fieldType)) && !fieldType.isEnum()) { field.set(target, getManufacturedPojo(fieldType)); } else if (!fieldType.isEnum()) { field.set(target, populateAllFields(fieldType, exclusions)); } } catch (IllegalAccessException | InstantiationException ex) { System.err.println(ex.getMessage()); } } } return target; }
From source file:se.vgregion.portal.innovatinosslussen.domain.TypesBeanTest.java
void doGetterSetterValuesMatch(Object o) throws IllegalAccessException, InstantiationException { BeanMap bm = new BeanMap(o); final String javaLangPackageName = String.class.getPackage().getName(); for (Object key : bm.keySet()) { String name = (String) key; if ("ideaContentPrivate".equals(name) || "ideaPerson".equals(name) || "ideaContentPublic".equals(name)) { continue; }/* w ww.j av a 2 s .c om*/ if (bm.getWriteMethod(name) != null) { if (bm.getType(name).equals(String.class)) { bm.put(name, name); Assert.assertTrue(name == bm.get(name)); } else { Class clazz = bm.getType(name); if (!clazz.getName().startsWith(javaLangPackageName) && !clazz.isEnum()) { Object value = defaultPrim.get(clazz); if (value == null) { value = clazz.newInstance(); } bm.put(name, value); Assert.assertTrue("1, " + o.getClass() + "." + key, value.equals(bm.get(name))); Assert.assertTrue("2, " + o.getClass() + "." + key, value.hashCode() == bm.get(name).hashCode()); } } } } }
From source file:com.vmware.photon.controller.deployer.helpers.TestHelper.java
public static Object[][] getValidStartStages(@Nullable Class<? extends Enum> subStages) { if (subStages == null) { ////from www . jav a 2 s.co m // N.B. Tasks without defined sub-stages must accept these default start stages. // return new Object[][] { { null }, { TaskState.TaskStage.CREATED }, { TaskState.TaskStage.STARTED }, { TaskState.TaskStage.FINISHED }, { TaskState.TaskStage.FAILED }, { TaskState.TaskStage.CANCELLED }, }; } if (!subStages.isEnum() || subStages.getEnumConstants().length == 0) { throw new IllegalStateException("Class " + subStages.getName() + " is not a valid enum"); } Enum[] enumConstants = subStages.getEnumConstants(); List<Object[]> validStartStages = new ArrayList<>(); validStartStages.add(new Object[] { null, null }); validStartStages.add(new Object[] { TaskState.TaskStage.CREATED, null }); for (int i = 0; i < enumConstants.length; i++) { validStartStages.add(new Object[] { TaskState.TaskStage.STARTED, enumConstants[i] }); } validStartStages.add(new Object[] { TaskState.TaskStage.FINISHED, null }); validStartStages.add(new Object[] { TaskState.TaskStage.FAILED, null }); validStartStages.add(new Object[] { TaskState.TaskStage.CANCELLED, null }); Object[][] returnValue = new Object[validStartStages.size()][2]; for (int i = 0; i < validStartStages.size(); i++) { returnValue[i][0] = validStartStages.get(i)[0]; returnValue[i][1] = validStartStages.get(i)[1]; } return returnValue; }
From source file:com.googlecode.jsonschema2pojo.integration.EnumIT.java
@Test @SuppressWarnings("unchecked") public void enumAtRootCreatesATopLevelType() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { ClassLoader resultsClassLoader = generateAndCompile("/schema/enum/enumAsRoot.json", "com.example"); Class<Enum> rootEnumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.enums.EnumAsRoot"); assertThat(rootEnumClass.isEnum(), is(true)); assertThat(isPublic(rootEnumClass.getModifiers()), is(true)); }
From source file:com.l2jfree.config.model.ConfigFieldInfo.java
public void print(PrintWriter out, PrintMode mode) { if (getBeginningGroup() != null && (mode != PrintMode.MODIFIED || getBeginningGroup().isModified())) { out.println("########################################"); out.println("## " + getConfigGroupBeginning().name()); if (!ArrayUtils.isEmpty(getConfigGroupBeginning().comment())) for (String line : getConfigGroupBeginning().comment()) out.println("# " + line); out.println();/*from ww w . j a va 2 s.c o m*/ } if (mode != PrintMode.MODIFIED || isModified()) { if (!ArrayUtils.isEmpty(getConfigField().comment())) for (String line : getConfigField().comment()) out.println("# " + line); out.println("# "); out.println("# Default: " + getDefaultValue()); if (getField().getType().isEnum()) out.println("# Available: " + StringUtils.join(getField().getType().getEnumConstants(), "|")); else if (getField().getType().isArray()) { final Class<?> fieldComponentType = getField().getType().getComponentType(); if (fieldComponentType.isEnum()) out.println("# Available: " + StringUtils.join(fieldComponentType.getEnumConstants(), ",")); } out.println("# "); out.println(getName() + " = " + (mode == null || mode == PrintMode.DEFAULT ? getDefaultValue() : getCurrentValue())); out.println(); } if (getEndingGroup() != null && (mode != PrintMode.MODIFIED || getEndingGroup().isModified())) { if (!ArrayUtils.isEmpty(getConfigGroupEnding().comment())) for (String line : getConfigGroupEnding().comment()) out.println("# " + line); out.println("## " + getConfigGroupEnding().name()); out.println("########################################"); out.println(); } }
From source file:org.xwiki.wysiwyg.server.internal.plugin.macro.XWikiMacroService.java
/** * NOTE: We can't send a {@link Type} instance to the client side because GWT can't serialize it so we have to * convert it to a {@link ParameterType} instance. * //from w w w. j a v a 2 s . c o m * @param type the type that defines the values a macro parameter can have * @return the parameter type associated with the given type */ private ParameterType createMacroParameterType(Type type) { ParameterType parameterType = new ParameterType(); if (type instanceof Class) { Class<?> parameterClass = (Class<?>) type; parameterType.setName(parameterClass.getName()); if (parameterClass.isEnum()) { Object[] parameterClassConstants = parameterClass.getEnumConstants(); Map<String, String> parameterTypeConstants = new LinkedHashMap<String, String>(); for (int i = 0; i < parameterClassConstants.length; i++) { String constant = String.valueOf(parameterClassConstants[i]); // We leave the constant unlocalized for now. parameterTypeConstants.put(constant, constant); } parameterType.setEnumConstants(parameterTypeConstants); } } return parameterType; }
From source file:org.hdiv.web.servlet.tags.form.OptionsTagHDIV.java
@Override protected int writeTagContent(TagWriter tagWriter) throws JspException { // make sure we are under a '<code>select</code>' tag before proceeding. assertUnderSelectTag();/*from www.ja v a 2s . c om*/ IDataComposer dataComposer = (IDataComposer) this.pageContext.getRequest() .getAttribute(org.hdiv.web.util.TagUtils.DATA_COMPOSER); SelectTagHDIV selectTag = (SelectTagHDIV) org.hdiv.web.util.TagUtils.getAncestorOfType(this, SelectTagHDIV.class); Object items = getItems(); Object itemsObject = null; if (items != null) { itemsObject = (items instanceof String ? evaluate("items", (String) items) : items); } else { Class<?> selectTagBoundType = ((SelectTagHDIV) findAncestorWithClass(this, SelectTagHDIV.class)) .getBindStatus().getValueType(); if (selectTagBoundType != null && selectTagBoundType.isEnum()) { itemsObject = selectTagBoundType.getEnumConstants(); } } if (itemsObject != null) { String itemValue = getItemValue(); String itemLabel = getItemLabel(); String valueProperty = (itemValue != null ? ObjectUtils.getDisplayString(evaluate("itemValue", itemValue)) : null); String labelProperty = (itemLabel != null ? ObjectUtils.getDisplayString(evaluate("itemLabel", itemLabel)) : null); OptionsWriter optionWriter = new OptionsWriter(dataComposer, selectTag.getName(), itemsObject, valueProperty, labelProperty); optionWriter.writeOptions(tagWriter); } return SKIP_BODY; }
From source file:com.yosanai.java.swing.editor.ObjectEditorTableModel.java
public boolean isPrimitive(Class<?> classObj) { boolean ret = false; ret = null != classObj && (classObj.isPrimitive() || classObj.isEnum() || classObj.equals(Integer.class) || classObj.equals(Float.class) || classObj.equals(Double.class) || classObj.equals(Date.class) || classObj.equals(String.class) || classObj.equals(Boolean.class) || Number.class.isAssignableFrom(classObj)); return ret;/*from ww w . ja v a2s . c o m*/ }
From source file:ro.pippo.csv.CsvEngine.java
public String objectToString(Object object) { if (object == null) { return null; }//from www . j av a 2s. com Class<?> objectClass = object.getClass(); if (objectClass.isEnum()) { return ((Enum<?>) object).name(); } else if (object instanceof java.sql.Date) { return object.toString(); } else if (object instanceof java.sql.Time) { return object.toString(); } else if (object instanceof java.sql.Timestamp) { return object.toString(); } else if (object instanceof Date) { return new SimpleDateFormat(datePattern).format((Date) object); } return object.toString(); }