Java Code Examples for com.sun.codemodel.JDefinedClass#enumConstant()

The following examples show how to use com.sun.codemodel.JDefinedClass#enumConstant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JaxRsEnumRule.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
private void addEnumConstants(JsonNode node, JDefinedClass _enum, JsonNode customNames, JType type) {
    Collection<String> existingConstantNames = new ArrayList<String>();
    for (int i = 0; i < node.size(); i++) {
        JsonNode value = node.path(i);

        if (!value.isNull()) {
            String constantName = getConstantName(value.asText(), customNames.path(i).asText());
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);

            JEnumConstant constant = _enum.enumConstant(constantName);
            
            String typeName = type.unboxify().fullName(); 
            if(typeName.equals("int")){ // integer
                constant.arg(JExpr.lit(value.intValue()));   
            } else if(typeName.equals("long")){ // integer-as-long
                constant.arg(JExpr.lit(value.longValue()));
            } else if(typeName.equals("double")){ // number
                constant.arg(JExpr.lit(value.doubleValue()));
            } else if(typeName.equals("boolean")){ // boolean
                constant.arg(JExpr.lit(value.booleanValue()));    
            } else { // string, null, array, object?  
                // only string should really be valid here... TODO throw error?
                constant.arg(JExpr.lit(value.asText()));
            }
            ruleFactory.getAnnotator().enumConstant(_enum, constant, value.asText());
        }
    }
}