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:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlElementRef() {
    Class<WithXmlElementRef> clazz = WithXmlElementRef.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  2s .c om*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlElementRef());
            Assert.assertEquals("Franz", fi.getElementRefName());
            Assert.assertNull(fi.getElementRefNamespace());
            Assert.assertNull(fi.getElementRefType());
        }
    }
}

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

@Test
public void testWithXmlAnyAttribute() {
    Class<WithXmlAnyAttribute> clazz = WithXmlAnyAttribute.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  . jav 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.hasXmlAnyAttribute());
        }
    }
}

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

@Test
public final void testWithXmlElements() {
    Class<WithXmlElements> clazz = WithXmlElements.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  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.hasXmlElements());
            //                List<XmlElement> xmlElements = fi.getXmlElements();
            //                Assert.assertNotNull(xmlElements);

        }
    }
    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 testWithXmlAttachmentRef() {
    Class<WithXmlAttachmentRef> clazz = WithXmlAttachmentRef.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  av a2s . c o  m*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlAttachmentRef());

        }
    }
}

From source file:edu.mayo.cts2.framework.webapp.rest.controller.AbstractController.java

@ExceptionHandler(ConversionNotSupportedException.class)
@ResponseBody/*from  www. j ava  2  s  .  c  om*/
public UnspecifiedCts2Exception handleException(HttpServletResponse response, HttpServletRequest request,
        ConversionNotSupportedException ex) {
    int status = HttpServletResponse.SC_BAD_REQUEST;
    response.setStatus(status);

    Class<?> requiredType = ex.getRequiredType();
    String typeName = requiredType.getSimpleName();
    String value = ex.getValue().toString();

    String possibleValues = "";

    if (requiredType.isEnum()) {
        StringBuilder sb = new StringBuilder();
        sb.append(" Possible values include: ");

        Object[] values = requiredType.getEnumConstants();

        sb.append(StringUtils.join(values, ", "));

        possibleValues = sb.toString();
    }

    return ExceptionFactory.createUnknownException(
            "Cannot convert value " + value + " to the type " + typeName + "." + possibleValues,
            getUrlString(request), getParameterString(request));
}

From source file:hu.bme.mit.sette.common.validator.reflection.ClassValidator.java

/**
 * Sets the required type for the Java class.
 *
 * @param type/*from  w  ww. j  a  va 2s.com*/
 *            the required type for the Java class.
 * @return this object
 */
public ClassValidator type(final ClassType type) {
    Validate.notNull(type, "The type must not be null");

    if (getSubject() != null) {
        Class<?> javaClass = getSubject();
        boolean isTypeValid = false;

        switch (type) {
        case CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray();
            break;

        case REGULAR_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && !javaClass.isMemberClass()
                    && !javaClass.isAnonymousClass() && !javaClass.isLocalClass();
            break;

        case MEMBER_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isMemberClass();
            break;

        case ANONYMOUS_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isAnonymousClass();
            break;

        case LOCAL_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isLocalClass();
            break;

        case INTERFACE:
            isTypeValid = javaClass.isInterface();
            break;

        case REGULAR_INTERFACE:
            isTypeValid = javaClass.isInterface() && !javaClass.isMemberClass();
            break;

        case MEMBER_INTERFACE:
            isTypeValid = javaClass.isInterface() && javaClass.isMemberClass();
            break;

        case ENUM:
            isTypeValid = javaClass.isEnum();
            break;

        case REGULAR_ENUM:
            isTypeValid = javaClass.isEnum() && !javaClass.isMemberClass();
            break;

        case MEMBER_ENUM:
            isTypeValid = javaClass.isEnum() && javaClass.isMemberClass();
            break;

        case ANNOTATION:
            isTypeValid = javaClass.isAnnotation();
            break;

        case REGULAR_ANNOTATION:
            isTypeValid = javaClass.isAnnotation() && !javaClass.isMemberClass();
            break;

        case MEMBER_ANNOTATION:
            isTypeValid = javaClass.isAnnotation() && javaClass.isMemberClass();
            break;

        case PRIMITIVE:
            isTypeValid = javaClass.isPrimitive();
            break;

        case ARRAY:
            isTypeValid = javaClass.isArray();
            break;

        default:
            throw new UnsupportedOperationException("Unknown class type: " + type);
        }

        if (!isTypeValid) {
            this.addException(
                    String.format("The Java class must have the specified type\n" + "(type: [%s])", type));
        }
    }

    return this;
}

From source file:org.atricore.idbus.kernel.main.databinding.JAXBUtils.java

/**
 * @param list/*from   www.  ja  va  2s  .  co m*/
 * @param pkg
 */
private static void checkClasses(List<Class> list, String pkg) {
    // The installed classfinder or directory search may inadvertently add too many
    // classes.  This rountine is a 'double check' to make sure that the classes
    // are acceptable.
    for (int i = 0; i < list.size();) {
        Class cls = list.get(i);
        if (!cls.isInterface()
                && (cls.isEnum() || getAnnotation(cls, XmlType.class) != null
                        || ClassUtils.getDefaultPublicConstructor(cls) != null)
                && !ClassUtils.isJAXWSClass(cls) && !isSkipClass(cls)
                && cls.getPackage().getName().equals(pkg)) {
            i++; // Acceptable class
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Removing class " + cls + " from consideration because it is not in package " + pkg
                        + " or is an interface or does not have a public constructor or is" + " a jaxws class");
            }
            list.remove(i);
        }
    }
}

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

@Test
public void testWithXmlElementWrapper() {
    Class<WithXmlElementWrapper> clazz = WithXmlElementWrapper.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*  w w  w .j a v a2  s. c  om*/
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlElementWrapper());
            Assert.assertEquals("theWrapper", fi.getElementWrapperName());
            Assert.assertNull(fi.getElementWrapperNamespace());
            Assert.assertFalse(fi.getElementWrapperNillable());
            Assert.assertFalse(fi.getElementWrapperRequired());
        }
    }
}

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

@Test
public void testWithXmlInlineBinaryData() {
    Class<WithXmlInlineBinaryData> clazz = WithXmlInlineBinaryData.class;
    Assert.assertFalse(clazz.isEnum());
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];// ww w.  jav  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.hasXmlInlineBinaryData());

        }
    }
}

From source file:edu.usu.sdl.openstorefront.doc.JaxrsProcessor.java

private static void mapComplexTypes(List<APITypeModel> typeModels, Field fields[], boolean onlyConsumeField) {
    //Should strip duplicate types
    Set<String> typesInList = new HashSet<>();
    typeModels.forEach(type -> {//from   www.j  a v  a 2  s  . com
        typesInList.add(type.getName());
    });

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    for (Field field : fields) {
        boolean capture = true;
        if (onlyConsumeField) {
            ConsumeField consumeField = (ConsumeField) field.getAnnotation(ConsumeField.class);
            if (consumeField == null) {
                capture = false;
            }
        }

        if (capture) {

            Class fieldClass = field.getType();
            DataType dataType = (DataType) field.getAnnotation(DataType.class);
            if (dataType != null) {
                fieldClass = dataType.value();
            }

            if (ReflectionUtil.isComplexClass(fieldClass)) {

                APITypeModel typeModel = new APITypeModel();
                typeModel.setName(fieldClass.getSimpleName());

                APIDescription aPIDescription = (APIDescription) fieldClass.getAnnotation(APIDescription.class);
                if (aPIDescription != null) {
                    typeModel.setDescription(aPIDescription.value());
                }

                Set<String> fieldList = mapValueField(typeModel.getFields(), fieldClass.getDeclaredFields(),
                        onlyConsumeField);
                if (fieldClass.isEnum()) {
                    typeModel.setObject(Arrays.toString(fieldClass.getEnumConstants()));
                } else {
                    if (fieldClass.isInterface() == false) {
                        try {
                            typeModel.setObject(objectMapper.writeValueAsString(fieldClass.newInstance()));

                            String cleanUpJson = StringProcessor.stripeFieldJSON(typeModel.getObject(),
                                    fieldList);
                            typeModel.setObject(cleanUpJson);

                        } catch (InstantiationException | IllegalAccessException | JsonProcessingException ex) {
                            log.log(Level.WARNING,
                                    "Unable to process/map complex field: " + fieldClass.getSimpleName(), ex);
                            typeModel.setObject("{ Unable to view }");
                        }
                        mapComplexTypes(typeModels, fieldClass.getDeclaredFields(), onlyConsumeField);
                    }
                }
                typeModels.add(typeModel);
                typesInList.add(typeModel.getName());
            }

        }
    }
}