Search in sources :

Example 1 with CPluginCustomization

use of com.sun.tools.xjc.model.CPluginCustomization in project jaxb-ri by eclipse-ee4j.

the class PluginImpl method checkAndInject.

private static void checkAndInject(Collection<? extends CustomizableOutline> outlines) {
    for (CustomizableOutline co : outlines) {
        CPluginCustomization c = co.getTarget().getCustomizations().find(Const.NS, "code");
        if (c == null)
            // no customization --- nothing to inject here
            continue;
        c.markAsAcknowledged();
        // TODO: ideally you should validate this DOM element to make sure
        // that there's no typo/etc. JAXP 1.3 can do this very easily.
        String codeFragment = DOMUtils.getElementText(c.element);
        // inject the specified code fragment into the implementation class.
        co.getImplClass().direct(codeFragment);
    }
}
Also used : CustomizableOutline(com.sun.tools.xjc.outline.CustomizableOutline) CPluginCustomization(com.sun.tools.xjc.model.CPluginCustomization)

Example 2 with CPluginCustomization

use of com.sun.tools.xjc.model.CPluginCustomization in project jaxb-ri by eclipse-ee4j.

the class BindInfo method toCustomizationList.

/**
 * Gets the list of {@link CPluginCustomization}s from this.
 *
 * <p>
 * Note that calling this method marks all those plug-in customizations
 * as 'used'. So call it only when it's really necessary.
 */
public CCustomizations toCustomizationList() {
    CCustomizations r = null;
    for (BIDeclaration d : this) {
        if (d instanceof BIXPluginCustomization) {
            BIXPluginCustomization pc = (BIXPluginCustomization) d;
            pc.markAsAcknowledged();
            if (!Ring.get(Model.class).options.pluginURIs.contains(pc.getName().getNamespaceURI()))
                // this isn't a plugin customization
                continue;
            if (r == null)
                r = new CCustomizations();
            r.add(new CPluginCustomization(pc.element, pc.getLocation()));
        }
    }
    if (r == null)
        r = CCustomizations.EMPTY;
    return new CCustomizations(r);
}
Also used : CCustomizations(com.sun.tools.xjc.model.CCustomizations) CPluginCustomization(com.sun.tools.xjc.model.CPluginCustomization)

Example 3 with CPluginCustomization

use of com.sun.tools.xjc.model.CPluginCustomization in project jstuff by sebthom.

the class FieldInstantiatingPlugin method run.

@Override
public boolean run(final Outline outline, final Options options, final ErrorHandler errorHandler) throws SAXException {
    // collect all types defined in the XSD
    final List<JType> typeDefs = new ArrayList<>();
    for (final ClassOutline classDef : outline.getClasses()) {
        typeDefs.add(classDef.implClass);
    }
    // scan all XSD based classes for field references to other XSD based classes
    for (final ClassOutline classDef : outline.getClasses()) {
        for (final JFieldVar fieldDecl : classDef.implClass.fields().values()) {
            /*
             * @XmlElementRefs({
             *    @XmlElementRef(name = "bike", namespace = "my-config", type = JAXBElement.class, required = false),
             *    @XmlElementRef(name = "car", namespace = "my-config", type = JAXBElement.class, required = false)
             * })
             * private List<JAXBElement<?>> bikesAndCars;
             */
            final Field memberValueFields = Fields.find(JAnnotationUse.class, "memberValues");
            for (final JAnnotationUse a : fieldDecl.annotations()) {
                if (// 
                jakarta.xml.bind.annotation.XmlElementRefs.class.getName().equals(a.getAnnotationClass().binaryName()) || // 
                "javax.xml.bind.annotation.XmlElementRefs".equals(a.getAnnotationClass().binaryName())) {
                    for (final JAnnotationUse xmlElementRefAnno : ((JAnnotationArrayMember) a.getAnnotationMembers().get("value")).annotations()) {
                        final JAnnotationValue requiredAttribute = xmlElementRefAnno.getAnnotationMembers().get("required");
                        if (requiredAttribute != null) {
                            ((Map<?, ?>) Fields.read(xmlElementRefAnno, memberValueFields)).remove("required");
                        }
                    }
                }
            }
            if (!typeDefs.contains(fieldDecl.type())) {
                continue;
            }
            FieldOutline fieldDef = null;
            for (final FieldOutline f : classDef.getDeclaredFields()) {
                if (f.getPropertyInfo().getName(false).equals(fieldDecl.name())) {
                    fieldDef = f;
                }
            }
            if (fieldDef == null)
                throw new IllegalStateException("FieldOutline not found for " + fieldDecl.name());
            boolean doInstantiate = false;
            for (final CPluginCustomization pc : findCustomizations(fieldDef.getPropertyInfo().getCustomizations(), CUSTOMIZATION_ENABLED_TAG)) {
                pc.markAsAcknowledged();
                doInstantiate = true;
            }
            // initialize field
            if (doInstantiate) {
                LOG.info("%s#%s = new %s()", classDef.implClass.name(), fieldDecl.name(), fieldDecl.type().name());
                fieldDecl.init(JExpr._new(fieldDecl.type()));
            } else {
                LOG.info("Not instantiating %s#%s", classDef.implClass.name(), fieldDecl.name());
            }
        }
    }
    return true;
}
Also used : CPluginCustomization(com.sun.tools.xjc.model.CPluginCustomization) ArrayList(java.util.ArrayList) JAnnotationArrayMember(com.sun.codemodel.JAnnotationArrayMember) ClassOutline(com.sun.tools.xjc.outline.ClassOutline) Field(java.lang.reflect.Field) JFieldVar(com.sun.codemodel.JFieldVar) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JAnnotationValue(com.sun.codemodel.JAnnotationValue) Map(java.util.Map) JType(com.sun.codemodel.JType) FieldOutline(com.sun.tools.xjc.outline.FieldOutline)

Example 4 with CPluginCustomization

use of com.sun.tools.xjc.model.CPluginCustomization in project vcd-api-tools by vmware.

the class RestApiVersionsPlugin method addContentTypeAnnotation.

/**
 * This method adds {@link ContentType} annotation and a CONTENT TYPE
 * constant of type "public static final String" to the JAXB generated Java
 * and adds a corresponding Java annotation if not empty.
 * class. It reads the "meta:content-type" annotation element in the schema
 *
 * @param classOutline the element which will be annotated
 * @param customizations schema customizations for this element
 */
private void addContentTypeAnnotation(ClassOutline classOutline, CCustomizations customizations) {
    JDefinedClass implClass = classOutline.implClass;
    if (implClass == null) {
        return;
    }
    final CPluginCustomization customization = customizations.find(NAMESPACE_URI, ELEMENT_CONTENT_TYPE);
    if (customization == null) {
        return;
    }
    customization.markAsAcknowledged();
    final String contentType = customization.element.getTextContent();
    if (contentType == null) {
        return;
    }
    final JAnnotationUse annotation = implClass.annotate(ContentType.class);
    annotation.param("value", contentType.trim());
    final JCodeModel codeModel = implClass.owner();
    final JExpression contentTypeConst = JExpr.lit(contentType);
    final JFieldVar contentTypeField = implClass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, codeModel.ref(String.class), CONTENT_TYPE_CONST, contentTypeConst);
    MEDIATYPE_TYPE_MAP.put(contentTypeField, implClass);
    CONTENTTYPE_TYPES_MAP.add(contentType.toLowerCase(), classOutline);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) CPluginCustomization(com.sun.tools.xjc.model.CPluginCustomization) JFieldVar(com.sun.codemodel.JFieldVar) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JExpression(com.sun.codemodel.JExpression) JCodeModel(com.sun.codemodel.JCodeModel)

Example 5 with CPluginCustomization

use of com.sun.tools.xjc.model.CPluginCustomization in project vcd-api-tools by vmware.

the class RestApiVersionsPlugin method addSupportedAnnotation.

/**
 * This method adds {@link Supported} annotation to the given element. It reads the "added-in"
 * and "removed-in" values from the customizations if present. Otherwise, it uses the default
 * {@link #version} for "added-in" and leaves "removed-in" empty.
 * <P>
 * If "removed-in" is present, @{@link Deprecated} annotation and the {@code @deprecated} doclet
 * are also added.
 *
 * @param annotatable
 *            the element which will be annotated
 * @param customizations
 *            schema customizations for this element
 */
private void addSupportedAnnotation(JAnnotatable annotatable, CCustomizations customizations) {
    if (annotatable == null) {
        return;
    }
    String addedIn = version;
    String removedIn = null;
    CPluginCustomization customization = customizations.find(NAMESPACE_URI, ELEMENT_VERSION);
    if (customization != null) {
        customization.markAsAcknowledged();
        if (customization.element.hasAttribute(X_ANNOTATION_ADDED_IN)) {
            addedIn = customization.element.getAttribute(X_ANNOTATION_ADDED_IN);
        }
        if (customization.element.hasAttribute(X_ANNOTATION_REMOVED_IN)) {
            removedIn = customization.element.getAttribute(X_ANNOTATION_REMOVED_IN);
        }
    }
    // Let's validate the API version strings first
    try {
        ApiVersion.fromValue(addedIn);
        if (removedIn != null) {
            ApiVersion.fromValue(removedIn);
        }
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("Double check API version string on " + customization.locator.getSystemId() + ":" + customization.locator.getLineNumber() + "", e);
    }
    JAnnotationUse annotation = annotatable.annotate(Supported.class);
    annotation.param(J_ANNOTATION_ADDED_IN, addedIn);
    if (removedIn == null) {
        return;
    }
    annotation.param(J_ANNOTATION_REMOVED_IN, removedIn);
    annotatable.annotate(Deprecated.class);
    final JDocComment javadoc;
    if (annotatable instanceof JMethod) {
        javadoc = ((JMethod) annotatable).javadoc();
    } else if (annotatable instanceof JFieldVar) {
        javadoc = ((JFieldVar) annotatable).javadoc();
    } else if (annotatable instanceof JDefinedClass) {
        javadoc = ((JDefinedClass) annotatable).javadoc();
    } else if (annotatable instanceof JEnumConstant) {
        javadoc = ((JMethod) annotatable).javadoc();
    } else {
        return;
    }
    final String deprecatedCommentMessage = "Removed since REST version " + removedIn;
    javadoc.addDeprecated().append(deprecatedCommentMessage);
}
Also used : JEnumConstant(com.sun.codemodel.JEnumConstant) CPluginCustomization(com.sun.tools.xjc.model.CPluginCustomization) JFieldVar(com.sun.codemodel.JFieldVar) JDefinedClass(com.sun.codemodel.JDefinedClass) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JMethod(com.sun.codemodel.JMethod) JDocComment(com.sun.codemodel.JDocComment)

Aggregations

CPluginCustomization (com.sun.tools.xjc.model.CPluginCustomization)5 JAnnotationUse (com.sun.codemodel.JAnnotationUse)3 JFieldVar (com.sun.codemodel.JFieldVar)3 JDefinedClass (com.sun.codemodel.JDefinedClass)2 JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)1 JAnnotationValue (com.sun.codemodel.JAnnotationValue)1 JCodeModel (com.sun.codemodel.JCodeModel)1 JDocComment (com.sun.codemodel.JDocComment)1 JEnumConstant (com.sun.codemodel.JEnumConstant)1 JExpression (com.sun.codemodel.JExpression)1 JMethod (com.sun.codemodel.JMethod)1 JType (com.sun.codemodel.JType)1 CCustomizations (com.sun.tools.xjc.model.CCustomizations)1 ClassOutline (com.sun.tools.xjc.outline.ClassOutline)1 CustomizableOutline (com.sun.tools.xjc.outline.CustomizableOutline)1 FieldOutline (com.sun.tools.xjc.outline.FieldOutline)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1