All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.tools.xjc.generator.bean.field.UntypedListField Maven / Gradle / Ivy

There is a newer version: 4.0.5
Show newest version
/*
 * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package com.sun.tools.xjc.generator.bean.field;

import java.util.ArrayList;
import java.util.List;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JVar;
import com.sun.tools.xjc.generator.bean.ClassOutlineImpl;
import com.sun.tools.xjc.generator.bean.MethodWriter;
import com.sun.tools.xjc.model.CPropertyInfo;
import com.sun.tools.xjc.outline.FieldAccessor;
import org.glassfish.jaxb.core.api.impl.NameConverter;

/**
 * Realizes a property as an untyped {@link List}.
 * 
 * 
 * List getXXX();
 * 
* *

Default value handling

*

* Since unmarshaller just adds new values into the storage, * we can't fill the storage by default values at the time of * instanciation. (or oherwise values found in the document will * be appended to default values, where it should overwrite them.) *

* Therefore, when the object is created, the storage will be empty. * When the getXXX method is called, we'll check if the storage is * modified in anyway. If it is modified, it must mean that the values * are found in the document, so we just return it. * * Otherwise we will fill in default values and return it to the user. * *

* When a list has default values, its dirty flag is set to true. * Marshaller will check this and treat it appropriately. * * * @author * Kohsuke Kawaguchi ([email protected]) */ public class UntypedListField extends AbstractListField { /** * A concrete class that implements the List interface. * An instance of this class will be used to store data * for this field. */ private final JClass coreList; /** List getFIELD() method. */ private JMethod $get; /** * @param coreList * A concrete class that implements the List interface. * An instance of this class will be used to store data * for this field. */ protected UntypedListField(ClassOutlineImpl context, CPropertyInfo prop, JClass coreList) { // the JAXB runtime picks ArrayList if the signature is List, // so don't do eager allocation if it's ArrayList. // otherwise we need to do eager allocation so that the collection type specified by the user // will be used. super(context, prop, !coreList.fullName().equals("java.util.ArrayList")); this.coreList = coreList.narrow(exposedType.boxify()); generate(); } @Override protected final JClass getCoreListType() { return coreList; } @Override public void generateAccessors() { final MethodWriter writer = outline.createMethodWriter(); final Accessor acc = (Accessor) create(JExpr._this()); // [RESULT] // List getXXX() { // return ; // } $get = writer.declareMethod(listT,"get"+prop.getName(true)); if (prop.javadoc != null && prop.javadoc.length() > 0) { writer.javadoc().append(prop.javadoc).append("\n\n"); } JBlock block = $get.body(); fixNullRef(block); // avoid using an internal getter block._return(acc.ref(true)); appendJavadoc(writer); } @Override public FieldAccessor create(JExpression targetObject) { return new Accessor(targetObject); } class Accessor extends AbstractListField.Accessor { protected Accessor( JExpression $target ) { super($target); } @Override public void toRawValue(JBlock block, JVar $var) { // [RESULT] // $.addAll(bean.getLIST()); // list.toArray( array ); block.assign($var,JExpr._new(codeModel.ref(ArrayList.class).narrow(exposedType.boxify())).arg( $target.invoke($get) )); } @Override public void fromRawValue(JBlock block, String uniqueName, JExpression $var) { // [RESULT] // bean.getLIST().addAll($); JVar $list = block.decl(listT,uniqueName+'l',$target.invoke($get)); block.invoke($list,"addAll").arg($var); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy