Java Code Examples for com.sun.tools.internal.xjc.generator.bean.field.Messages

Following code examples demonstrate how to use com.sun.tools.internal.xjc.generator.bean.field.Messagesfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use com.sun.tools.internal.xjc.generator.bean.field.Messagesand various code implementation of this class.

    public FieldOutline generate(ClassOutlineImpl context, CPropertyInfo propertyInfo) {
        final BeanGenerator generator = context.parent();
        final Model model = generator.getModel();
        final JCodeModel codeModel = generator.getCodeModel();
        final JClass optional = new JDirectClassEx(codeModel, "java.util.Optional");

        final JType implType = getType(context, propertyInfo, IMPLEMENTATION);
        final JType exposedType = getType(context, propertyInfo, EXPOSED);
        assert !exposedType.isPrimitive() && !implType.isPrimitive();

        final JFieldVar field = emitField(context, propertyInfo, implType, exposedType);
        final JFieldRef fieldRef = _this().ref(field);
        final boolean fieldIsRequired = isRequired(propertyInfo);

        final AugmentedMethodWriter writer = AugmentedMethodWriter.create(context, configuration(), fieldIsRequired, false);
        final NameConverter nc = model.getNameConverter();

        final JExpression defaultValue = null != propertyInfo.defaultValue ? propertyInfo.defaultValue.compute(generator) : null;

        JType getterType = (model.options.enableIntrospection || null == defaultValue) ? exposedType : exposedType.unboxify();
        if (!fieldIsRequired && configuration().useOptional() && null == defaultValue) {
            getterType = optional.narrow(getterType.boxify());
        }

        JMethod getter = writer.declareMethod(getterType, getGetterMethod(context, propertyInfo, implType));

        String javadoc = propertyInfo.javadoc;
        if (javadoc.isEmpty()) {
            javadoc = Messages.DEFAULT_GETTER_JAVADOC.format(nc.toVariableName(propertyInfo.getName(true)));
        }
        writer.javadoc().append(javadoc);

        if (null != defaultValue) {
            getter.body()._return(cond(_null().ne(fieldRef), fieldRef, defaultValue));
        } else if (!fieldIsRequired && configuration().useOptional()) {
            getter.body()._return(optional.staticInvoke("ofNullable").arg(fieldRef));
        } else {
            getter.body()._return(fieldRef);
        }

        final List<Object> possibleTypes = listPossibleTypes(generator, propertyInfo);

        final String documentation = XJCUtils.extractDocumentation(propertyInfo);
        if (documentation.isEmpty()) {
            if (2 < possibleTypes.size()) {
                writer.javadoc().addReturn().append("possible object is\n").append(possibleTypes);
            }
        } else {
            writer.javadoc().addReturn().append(documentation);
        }

        if (!configuration().finalizeFields() || !fieldIsRequired) {
            JMethod setter = writer.declareMethod(generator.getCodeModel().VOID, "set" + propertyInfo.getName(true));

            JType setterType = (model.options.enableIntrospection || null == defaultValue) ? exposedType : exposedType.unboxify();
            JVar value = writer.addParameter(setterType, "value");
            assert null != value;

            if (null != defaultValue && setterType.isPrimitive() && !fieldIsRequired) {
                setter.body().assign(fieldRef, cond(defaultValue.eq(value), _null(), setterType.boxify().staticInvoke("valueOf").arg(value)));
            } else {
                setter.body().assign(fieldRef, requireNotNull(codeModel, castToImplType(implType, exposedType, value), value.type().isReference() && fieldIsRequired));
            }

            writer.javadoc().append(Messages.DEFAULT_SETTER_JAVADOC.format(nc.toVariableName(propertyInfo.getName(true))));

            if (2 < possibleTypes.size()) {
                writer.javadoc().addParam(value).append("allowed object is\n").append(possibleTypes);
            }

            if (value.type().isReference() && fieldIsRequired) {
                writer.javadoc().addThrows(NullPointerException.class).append("if value is null");
            }
        }

        return new ModernizationFieldOutline(context, propertyInfo, field, exposedType);
    } 


Advertisement
Advertisement