List of usage examples for java.lang Class isEnum
public boolean isEnum()
From source file:fr.exanpe.tapestry.tldgen.taglib.builder.StructureBuilder.java
/** * Builds the output taglib structure// w w w .ja v a 2 s .c om * * @param rootPackage the root package to look the components for * @param supportedPackages all sub packages to scan * @param urls the urls used to scan the packages * @return the structure containing the information on the taglib to generate * @throws MojoExecutionException if any unexpected error occurs */ public Taglib build(String rootPackage, String[] supportedPackages, URL[] urls) throws MojoExecutionException { Taglib taglib = new Taglib(); log.debug("Creating taglib object model..."); for (String subPackage : supportedPackages) { String pkgname = rootPackage + "." + subPackage; log.debug("Processing taglib for full package named : " + pkgname); Reflections reflections = new Reflections(new ConfigurationBuilder() .filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(pkgname))).setUrls(urls) .setScanners(new TypesScanner())); Store store = reflections.getStore(); // Return classes anaylised by TypeScanner Multimap<String, String> classes = store.getStoreMap().values().iterator().next(); log.debug(String.format("%s classes to analyse for %s package...", classes.keySet().size(), pkgname)); // Loop on found classes for (final String s : classes.keySet()) { Class<?> c; try { log.debug(String.format("Load class %s into classloader", s)); c = Thread.currentThread().getContextClassLoader().loadClass(s); } catch (ClassNotFoundException e) { // should not happen as it has just been parsed by Reflection... log.error(e); throw new MojoExecutionException("Class loader internal error for class :" + s, e); } if (!c.isAnnotation() && !c.isAnonymousClass() && !c.isEnum() && !c.isInterface() && !c.isLocalClass() && !c.isMemberClass() && !c.isSynthetic() && !Modifier.isAbstract(c.getModifiers())) { log.debug("Processing Tag : " + c.getName()); Tag tag = buildTagFromClass(rootPackage, c); taglib.getTags().add(tag); } } } log.debug("Taglib object model completed"); return taglib; }
From source file:wwutil.jsoda.DataUtil.java
/** Caller should handle custom valueType first before calling this. */ @SuppressWarnings("unchecked") static Object decodeAttrStrToValue(String attrStr, Class valueType) throws Exception { // Set null if input is null. if (attrStr == null) return null; if (valueType == String.class) return attrStr; // Return string type as it is. // non-String field having "" is treated as null. if (attrStr.equals("")) return null; if (valueType == Byte.class || valueType == byte.class) { return new Byte((byte) SimpleDBUtils.decodeZeroPaddingInt(attrStr)); } else if (valueType == Short.class || valueType == short.class) { return new Short((short) SimpleDBUtils.decodeZeroPaddingInt(attrStr)); } else if (valueType == Integer.class || valueType == int.class) { return new Integer(SimpleDBUtils.decodeZeroPaddingInt(attrStr)); } else if (valueType == Long.class || valueType == long.class) { return new Long(SimpleDBUtils.decodeZeroPaddingLong(attrStr)); } else if (valueType == Float.class || valueType == float.class) { return new Float(SimpleDBUtils.decodeZeroPaddingFloat(attrStr)); } else if (valueType == Double.class || valueType == double.class) { return new Double(attrStr); } else if (valueType == Boolean.class || valueType == boolean.class) { return new Boolean(attrStr); } else if (valueType == Character.class || valueType == char.class) { return attrStr.charAt(0); } else if (valueType == Date.class) { return SimpleDBUtils.decodeDate(attrStr); } else if (valueType.isEnum()) { return Enum.valueOf(valueType, attrStr); }// w ww .j a v a2 s .c o m // de-JSONify the rest. return fromJson(attrStr, valueType); }
From source file:org.bimserver.tools.generators.ProtocolBuffersGenerator.java
private String genInitializerCode(Class<?> type) { if (type.isPrimitive()) { return getDefaultLiteralCode(type); } else if (type == String.class) { return "\"\""; } else if (type.isEnum()) { return "null"; } else {// ww w . java2 s .c o m return "new " + type.getName() + "()"; } }
From source file:com.p5solutions.core.utils.ReflectionUtility.java
/** * Checks if is array./*from w ww.j a va 2 s . co m*/ * * @param clazz * the clazz * @return the boolean */ public static Boolean isEnum(Class<?> clazz) { return clazz.isEnum(); }
From source file:io.konik.utils.RandomInvoiceGenerator.java
private Object generatePrimitveValue(Class<?> type, String methodName) { if (methodName.equals("getClass")) return type; if (String.class.isAssignableFrom(type)) return generateStringBasedOnName(methodName); if (BigDecimal.class.isAssignableFrom(type)) return new BigDecimal(randomNumeric(3) + "." + randomNumeric(6)); if (isAssignable(type, Boolean.class, true)) return TRUE; if (isAssignable(type, Integer.class, true)) return Integer.valueOf(random.nextInt(100)); if (type.isEnum()) return getEnum(type); if (isAssignable(type, ZfDate.class, true)) return ZfDateFactory.create(supportedDateFormatts[random.nextInt(3)]); if (isAssignable(type, Date.class, true)) return new Date(); if (isAssignable(type, Long.class, true)) return Long.valueOf(random.nextInt(100)); if (Profile.class.equals(type)) return new Profile(ConformanceLevel.EXTENDED); throw new IllegalArgumentException("Type " + type + " was not found"); }
From source file:org.openmrs.reporting.ReportObjectXMLEncoder.java
@SuppressWarnings("unchecked") public String toXmlString() { ByteArrayOutputStream arr = new ByteArrayOutputStream(); EnumDelegate enumDelegate = new EnumDelegate(); XMLEncoder enc = new XMLEncoder(new BufferedOutputStream(arr)); enc.setPersistenceDelegate(User.class, new UserDelegate()); enc.setPersistenceDelegate(Location.class, new LocationDelegate()); enc.setPersistenceDelegate(Cohort.class, new CohortDelegate()); enc.setPersistenceDelegate(Concept.class, new ConceptDelegate()); enc.setPersistenceDelegate(Drug.class, new DrugDelegate()); enc.setPersistenceDelegate(Encounter.class, new EncounterDelegate()); enc.setPersistenceDelegate(Patient.class, new PatientDelegate()); enc.setPersistenceDelegate(Program.class, new ProgramDelegate()); enc.setPersistenceDelegate(ProgramWorkflow.class, new ProgramWorkflowDelegate()); enc.setPersistenceDelegate(ProgramWorkflowState.class, new ProgramWorkflowStateDelegate()); enc.setPersistenceDelegate(ConceptAnswer.class, new ConceptAnswerDelegate()); enc.setPersistenceDelegate(EncounterType.class, new EncounterTypeDelegate()); enc.setPersistenceDelegate(PersonAttributeType.class, new PersonAttributeTypeDelegate()); enc.setPersistenceDelegate(ConceptNumeric.class, new ConceptNumericDelegate()); Set<Class> alreadyAdded = new HashSet<Class>(); {/*from w ww. ja v a 2s . c o m*/ List<Class> enumClasses = new ArrayList<Class>(); enumClasses.add(PatientSetService.Modifier.class); enumClasses.add(PatientSetService.TimeModifier.class); enumClasses.add(PatientSetService.BooleanOperator.class); enumClasses.add(PatientSetService.GroupMethod.class); for (Class clz : enumClasses) { enc.setPersistenceDelegate(clz, enumDelegate); alreadyAdded.add(clz); } } // This original implementation won't handle enums that aren't direct properties of the bean, but I'm leaving it here anyway. for (Field f : this.objectToEncode.getClass().getDeclaredFields()) { Class clz = f.getType(); if (clz.isEnum() && !alreadyAdded.contains(clz)) { try { enc.setPersistenceDelegate(clz, enumDelegate); alreadyAdded.add(clz); } catch (Exception e) { log.error("ReportObjectXMLEncoder failed to write enumeration " + f.getName(), e); } } } log.debug("objectToEncode.type: " + objectToEncode.getClass()); enc.writeObject(this.objectToEncode); enc.close(); return arr.toString(); }
From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java
private byte type(Class c) { Byte type = typeCodes.get(c); if (type == null) { if (c.isEnum()) type = TYPE_ENUM;/*from ww w . j ava 2s . c om*/ else if (c.isArray()) type = TYPE_ARRAY; else if (Collection.class.isAssignableFrom(c)) type = TYPE_COLLECTION; else if (Map.class.isAssignableFrom(c)) type = TYPE_MAP; else type = TYPE_OBJECT; } return type; }
From source file:org.talend.mdm.repository.utils.Bean2EObjUtil.java
public EObject convertFromBean2EObj(Object bean, EObject eObj) { Class beanCls = bean.getClass(); EClass eCls = (EClass) classMap.get(beanCls); if (eObj == null) { eObj = MdmserverobjectFactory.eINSTANCE.create(eCls); }/*from ww w. ja v a2 s . c o m*/ if (eCls != null) { Map<Object, Method[]> beanFieldMap = beanClassUtil.findFieldMap(beanCls); if (beanFieldMap == null) { return null; } if (beanCls.isEnum()) { Method[] methods = beanFieldMap.get(beanCls); EStructuralFeature feature = fieldMap.get(beanCls); if (feature != null) { try { Object value = methods[0].invoke(bean); eObj.eSet(feature, value); } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); } catch (IllegalAccessException e) { log.error(e.getMessage(), e); } catch (InvocationTargetException e) { log.error(e.getMessage(), e); } } } else { for (Object fieldObj : beanFieldMap.keySet()) { Field field = (Field) fieldObj; try { EStructuralFeature feature = fieldMap.get(field); // System.out.println("Field>>\t" + field + "\n\tfeature>>" + feature); if (feature == null) { dumpMap(); } if (feature != null) { Method getMethod = beanFieldMap.get(field)[0]; Object value = getMethod.invoke(bean); if (beanClassUtil.isCollectionField(field)) { if (value != null) { ((EList) eObj.eGet(feature)).clear(); for (Object childObj : (Collection) value) { if (childObj != null) { Object eChildObj = null; if (beanClassUtil.isJavaClass(childObj.getClass())) { eChildObj = childObj; } else { eChildObj = convertFromBean2EObj(childObj, null); } if (eChildObj != null) { ((EList) eObj.eGet(feature)).add(eChildObj); } } } } } else if (beanClassUtil.isArrayField(field)) { if (value != null) { ((EList) eObj.eGet(feature)).clear(); for (Object childObj : (Object[]) value) { if (childObj != null) { Object eChildObj = null; if (beanClassUtil.isJavaClass(childObj.getClass())) { eChildObj = childObj; } else { eChildObj = convertFromBean2EObj(childObj, null); } if (eChildObj != null) { ((EList) eObj.eGet(feature)).add(eChildObj); } } } } } else { Object eValue = null; if (beanClassUtil.isJavaField(field)) { eValue = value; } else { // a object reference if (value != null) { eValue = convertFromBean2EObj(value, null); } else { eValue = null; } } eObj.eSet(feature, eValue); } } } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); } catch (IllegalAccessException e) { log.error(e.getMessage(), e); } catch (InvocationTargetException e) { log.error(e.getMessage(), e); } } } return eObj; } return null; }
From source file:nl.knaw.dans.common.ldap.repo.LdapMapper.java
@SuppressWarnings("unchecked") private Object getSingleValue(Class<?> type, Object o) throws NamingException { Object value = null;/*from w w w .j a v a2s . com*/ if (o != null) { if (type.isPrimitive()) { value = getPrimitive(type, (String) o); } else if (type.isEnum()) { value = Enum.valueOf(type.asSubclass(Enum.class), (String) o); } else { value = o; } } return value; }
From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java
@SuppressWarnings("unchecked") private Object toExactObject(Object value) { DBObject bson = (DBObject) value;/*from w w w. java 2 s.c om*/ if (bson.containsField(Constants.TYPE) && bson.containsField(Constants.VALUE)) { String t = (String) bson.get(Constants.TYPE); if (Constants.CUSTOM_BINARY.equals(t)) { Object result = deserializeObject(bson); return result; } else { try { @SuppressWarnings("rawtypes") Class type = Class.forName(t); if (type.isEnum()) return Enum.valueOf(type, (String) bson.get(Constants.VALUE)); else return fromSpecialType((DBObject) value); } catch (ClassNotFoundException e) { } } } return toDocument(bson); }