Example usage for java.lang Class isEnum

List of usage examples for java.lang Class isEnum

Introduction

In this page you can find the example usage for java.lang Class isEnum.

Prototype

public boolean isEnum() 

Source Link

Document

Returns true if and only if this class was declared as an enum in the source code.

Usage

From source file:com.medigy.persist.model.data.EntitySeedDataPopulator.java

public void populateEntityCacheData() throws HibernateException {

    Iterator itr = null;/*from w w w .j  a v a  2 s  . c  o m*/
    if (!useEjb)
        itr = configuration.getClassMappings();
    else
        itr = ejb3Configuration.getClassMappings();

    while (itr.hasNext()) {
        Class entityClass = ((PersistentClass) itr.next()).getMappedClass(); //(Class) classMappings.next();
        log.warn(entityClass.getName());
        if (!Entity.class.isAssignableFrom(entityClass))
            continue;

        Class[] innerClasses = entityClass.getDeclaredClasses();
        for (Class innerClass : innerClasses) {
            // TODO: assume that this is the inner CACHE class !???!!! maybe make Cache extend an interface to indicate this??
            if (innerClass.isEnum() && !entityClass.equals(Party.class)) {
                try {
                    final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass);
                    final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
                    final Hashtable pdsByName = new Hashtable();
                    for (int i = 0; i < descriptors.length; i++) {
                        final PropertyDescriptor descriptor = descriptors[i];
                        if (descriptor.getWriteMethod() != null)
                            pdsByName.put(descriptor.getReadMethod().getName(), descriptor.getWriteMethod());
                    }

                    Object[] enumObjects = innerClass.getEnumConstants();
                    // now match the enum methods with the enclosing class' methods
                    for (Object enumObj : enumObjects) {
                        Object entityObj = entityClass.newInstance();
                        final Method[] enumMethods = enumObj.getClass().getMethods();
                        for (Method enumMethod : enumMethods) {
                            final Method writeMethod = (Method) pdsByName.get(enumMethod.getName());
                            if (writeMethod != null) {
                                writeMethod.invoke(entityObj, enumMethod.invoke(enumObj));
                            }
                        }
                        HibernateUtil.getSession().save(entityObj);
                    }
                } catch (IntrospectionException e) {
                    log.error(e);
                } catch (IllegalAccessException e) {
                    log.error(e);
                } catch (InstantiationException e) {
                    log.error(e);
                } catch (InvocationTargetException e) {
                    log.error(e);
                } catch (HibernateException e) {
                    log.error(e);
                }
            }
        }
    }
}

From source file:org.talend.mdm.repository.utils.Bean2EObjUtil.java

private void guessField(Class cls, EClass eCls) {
    Map<Object, Method[]> map = beanClassUtil.findFieldMap(cls);

    if (map != null) {
        EList<EStructuralFeature> features = eCls.getEAllStructuralFeatures();
        if (cls.isEnum()) {
            for (EStructuralFeature feature : features) {
                String featureName = feature.getName();
                if ("value".equals(featureName)) { //$NON-NLS-1$
                    fieldMap.put(cls, feature);
                    break;
                }//from  w w w .ja  va 2s .  co  m
            }
        } else {

            for (Object fieldObj : map.keySet()) {
                Field field = (Field) fieldObj;
                String fieldName = field.getName();
                boolean found = false;
                for (EStructuralFeature feature : features) {
                    String featureName = feature.getName();
                    if (fieldName.equalsIgnoreCase(featureName)) {
                        fieldMap.put(field, feature);
                        found = true;
                        //                        System.out.println("\t Found Field Map:" + fieldName + "\n\tfield=" + field + "\n\tfeature=" + feature); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        break;
                    }
                }
                if (!found) {
                    log.debug("\t Failed to Found Field Map:" + fieldName); //$NON-NLS-1$
                }
            }
        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public void testWithXmlMimeType() {
    Class<WithXmlMimeType> clazz = WithXmlMimeType.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*from w w w.  ja  va 2s . c  o  m*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlMimeType());
            Assert.assertEquals("text/xml", fi.getMimtType());
        }
    }
}

From source file:org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter.java

private JsonSchemaProperty getSchemaProperty(BeanPropertyDefinition definition, TypeInformation<?> type,
        ResourceDescription description) {

    String name = definition.getName();
    String title = resolveMessageWithDefault(new ResolvableProperty(definition));
    String resolvedDescription = resolveMessage(description);
    boolean required = definition.isRequired();
    Class<?> rawType = type.getType();

    if (!rawType.isEnum()) {
        return new JsonSchemaProperty(name, title, resolvedDescription, required).with(type);
    }//from   w ww. j av a2 s.c om

    String message = resolveMessage(new DefaultMessageSourceResolvable(description.getMessage()));

    return new EnumProperty(name, title, rawType,
            description.getDefaultMessage().equals(resolvedDescription) ? message : resolvedDescription,
            required);
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public void testWithXmlElement() {
    Class<WithXmlElement> clazz = WithXmlElement.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*from   ww w.j a  v a  2s .co m*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlElement());
            Assert.assertEquals("Franz", fi.getElementName());
            //                Assert.assertNull(fi.getElementNamespace());
            //                Assert.assertNull(fi.getElementDefaultValue());
            //                Assert.assertNull(fi.getElementType());
        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public void testWithXmlTransient() {
    Class<WithXmlTransient> clazz = WithXmlTransient.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*from w  w  w .j  a v  a 2  s .c o m*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlTransient());
        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public void testProcessAnnotations() {
    Class<NotASingleField> clazz = NotASingleField.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*from  w  ww . j a  v a 2  s  .  c  o  m*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
    }
    Method[] methods = clazz.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(method.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, method.getAnnotations());
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlAttribute() {
    Class<WithXmlAttribute> clazz = WithXmlAttribute.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];//from  ww w .ja  v a 2s. c  o m
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlAttribute());
            Assert.assertEquals("Franz", fi.getAttributeName());
            //                Assert.assertNull(fi.getAttributeNamespace());
        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlElementRefs() {
    Class<WithXmlElementRefs> clazz = WithXmlElementRefs.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*from w  ww  .j  a va  2s.c  o  m*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlElementRefs());
        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlAnyElement() {
    Class<WithXmlAnyElement> clazz = WithXmlAnyElement.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];//  www.  j a  va 2 s .c o  m
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlAnyElement());
            Assert.assertTrue(fi.getAnyElementLax());
            Assert.assertNull(fi.getAnyElementDomHandler());
        }
    }
}